#328767 Add notification for when user unregisters from event. More refactoring and bugfixing in cloud code.
This commit is contained in:
Родитель
993bab748c
Коммит
64aca45718
|
@ -1,11 +1,11 @@
|
|||
function makePostReq (data) {
|
||||
function sendNotification (notifData) {
|
||||
var rsvp = require('rsvp');
|
||||
return new rsvp.Promise(function(resolve, reject) {
|
||||
var headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'MasterKey ' + Everlive.Parameters.masterKey
|
||||
};
|
||||
Everlive.Http.post(Everlive.Parameters.apiBaseUrlSecure + '/v1/' + Everlive.Parameters.apiKey + '/Functions/notifyUsers', { headers: headers, body: data }, function(err, resp) {
|
||||
Everlive.Http.post(Everlive.Parameters.apiBaseUrlSecure + '/v1/' + Everlive.Parameters.apiKey + '/Functions/notifyUsers', { headers: headers, body: notifData }, function(err, resp) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
|
@ -17,79 +17,38 @@ function makePostReq (data) {
|
|||
|
||||
Everlive.Events.afterCreate(function(request, response, context, done) {
|
||||
if (response.result) {
|
||||
var el = Everlive.Sdk.withMasterKey();
|
||||
var reqData = {
|
||||
var notifData = {
|
||||
alertType: 'UserRegisteredForEvent',
|
||||
userId: request.data.UserId,
|
||||
eventId: request.data.EventId
|
||||
};
|
||||
makePostReq(reqData)
|
||||
.then(done)
|
||||
.catch(function(err) {
|
||||
console.log('err: ' + JSON.stringify(err));
|
||||
done();
|
||||
});
|
||||
} else {
|
||||
done();
|
||||
sendNotification(notifData);
|
||||
// .then(done)
|
||||
// .catch(function(err) {
|
||||
// cant log it cause we dont wait for response
|
||||
// console.log('err: ' + JSON.stringify(err));
|
||||
// done();
|
||||
// });
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
Everlive.Events.afterDelete(function(request, response, context, done) {
|
||||
// this is set up to notify only when request is from app
|
||||
// (deletion is by filter with both UserId and EventId)
|
||||
var userId = request.filterExpression && request.filterExpression.UserId;
|
||||
var eventId = request.filterExpression && request.filterExpression.EventId;
|
||||
var isAppDelete = userId && eventId;
|
||||
|
||||
/*
|
||||
Everlive.Events.afterCreate(function(request, response, context, done) {
|
||||
if (response.result) {
|
||||
var eventId = request.data.EventId;
|
||||
var userId = request.data.UserId;
|
||||
|
||||
if (userId && eventId) {
|
||||
|
||||
var el = Everlive.Sdk.withMasterKey();
|
||||
var eventsDB = el.data('Events');
|
||||
var groupMembersDB = el.data('GroupMembers');
|
||||
var usersDB = el.data('Users');
|
||||
|
||||
var event;
|
||||
var user;
|
||||
var members;
|
||||
|
||||
eventsDB.getById(eventId)
|
||||
.then(function(eventResult) {
|
||||
event = eventResult.result;
|
||||
return usersDB.getById(userId);
|
||||
})
|
||||
.then(function(userResult) {
|
||||
user = userResult.result;
|
||||
var headers = {
|
||||
'x-everlive-single-field': 'UserId',
|
||||
'x-everlive-expand': '{ "UserId": { "TargetTypeName": "Users", "SingleField": "Email" } }'
|
||||
};
|
||||
return groupMembersDB.withHeaders(headers).get({ GroupId: event.GroupId, UserId: { $ne: userId } });
|
||||
})
|
||||
.then(function(getMemberEmailsResult) {
|
||||
var recipients = getMemberEmailsResult.result;
|
||||
var emailContext = {
|
||||
UserName: user.DisplayName,
|
||||
EventName: event.Name
|
||||
};
|
||||
Everlive.Email.sendEmailFromTemplate('UserRegisteredForEvent', ['dimo.mitev@progress.com'], emailContext,
|
||||
function(res) {
|
||||
done();
|
||||
}
|
||||
);
|
||||
})
|
||||
.catch(onError(response, done));
|
||||
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
if (isAppDelete) {
|
||||
var notifData = {
|
||||
alertType: 'UserUnregisteredFromEvent',
|
||||
userId: userId,
|
||||
eventId: eventId
|
||||
};
|
||||
sendNotification(notifData);
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
function onError(response, done) {
|
||||
return function(error) {
|
||||
console.log('error: ' + error.message);
|
||||
response.result = error;
|
||||
done();
|
||||
};
|
||||
}
|
||||
*/
|
|
@ -24,17 +24,19 @@ Everlive.Events.beforeDelete(function(request, context, done) {
|
|||
});
|
||||
|
||||
Everlive.Events.afterDelete(function(request, response, context, done) {
|
||||
if (!request.itemId) {
|
||||
return done();
|
||||
}
|
||||
|
||||
var deletedEventId = request.itemId;
|
||||
if (!deletedEventId) {
|
||||
var idsFilter = request.filterExpression && request.filterExpression._id && request.filterExpression._id.$in; // like from portal
|
||||
var idsToDelete = [];
|
||||
if (!deletedEventId && !idsFilter) {
|
||||
return done();
|
||||
} else if (deletedEventId) {
|
||||
idsToDelete = [deletedEventId];
|
||||
} else {
|
||||
idsToDelete = idsFilter;
|
||||
}
|
||||
|
||||
var el = Everlive.Sdk.withMasterKey();
|
||||
el.data('EventRegistrations').destroy({ EventId: deletedEventId })
|
||||
el.data('EventRegistrations').destroy({ EventId: { $in: idsToDelete } })
|
||||
.then(function(resp) {
|
||||
done();
|
||||
})
|
||||
|
@ -68,7 +70,6 @@ Everlive.Events.beforeUpdate(function(request, context, done) {
|
|||
}
|
||||
});
|
||||
|
||||
|
||||
Everlive.Events.afterUpdate(function(request, response, context, done) {
|
||||
if (response.result && request.itemId) {
|
||||
var el = Everlive.Sdk.withMasterKey();
|
||||
|
@ -168,4 +169,4 @@ function onError(response, done) {
|
|||
response.result = error;
|
||||
done();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
var rsvp = require('rsvp');
|
||||
var Promise = rsvp.Promise;
|
||||
|
||||
Everlive.CloudFunction.onRequest(function(request, response, done) {
|
||||
getData(request.data)
|
||||
|
@ -119,9 +120,8 @@ function getDataForEventRelated (templateName, context) {
|
|||
var members = groupMembersRes.result;
|
||||
var userData = filterForNotification(members);
|
||||
var emailContext = {
|
||||
Event: event.Name,
|
||||
Group: group.Name,
|
||||
EventDate: event.EventDate ? event.EventDate.toString() : '(TBD)'
|
||||
Event: event,
|
||||
GroupName: group.Name
|
||||
};
|
||||
|
||||
return {
|
||||
|
@ -183,15 +183,60 @@ function getDataForUserAskedToJoinGroup (context) {
|
|||
user = userRes.result;
|
||||
var emailContext = {
|
||||
UserName: user.DisplayName,
|
||||
UserEmail: user.Email, // or Username - it is actually email
|
||||
GroupName: group.Name
|
||||
};
|
||||
return {
|
||||
groupName: group.Name,
|
||||
userName: user.DisplayName,
|
||||
userEmail: user.Email,
|
||||
push: { userIds: [owner.Id] },
|
||||
email: {
|
||||
templateName: 'UserAskedToJoinGroup',
|
||||
recipients: [owner.Email], // or Username - it is actually email,
|
||||
recipients: [owner.Email], // or Username - it is actually email
|
||||
context: emailContext
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function getDataForUserUnregisteredFromEvent (context) {
|
||||
var event, unregisteredUser, organizer;
|
||||
var promises = [
|
||||
getEvent(context.eventId),
|
||||
getUser(context.userId)
|
||||
];
|
||||
|
||||
return Promise.all(promises)
|
||||
.then(function(result) {
|
||||
event = result[0].result;
|
||||
unregisteredUser = result[1].result;
|
||||
if (!event || !unregisteredUser) {
|
||||
return Promise.reject({ message: 'Could not find event or user' });
|
||||
}
|
||||
return getUser(event.OrganizerId);
|
||||
})
|
||||
.then(function(resp) {
|
||||
organizer = resp.result;
|
||||
if (!organizer) {
|
||||
return Promise.reject({ message: 'Could not find organizer' });
|
||||
}
|
||||
})
|
||||
.then(function() {
|
||||
var userData = filterForNotification([organizer]);
|
||||
var emailContext = {
|
||||
User: unregisteredUser.DisplayName,
|
||||
Event: event.Name
|
||||
};
|
||||
|
||||
// TODO: extract this part, lots of refactoring in this file :/
|
||||
return {
|
||||
eventName: event.Name,
|
||||
userName: unregisteredUser.DisplayName,
|
||||
push: { userIds: userData.userIds },
|
||||
email: {
|
||||
templateName: 'UserUnregisteredFromEvent',
|
||||
recipients: userData.recipients,
|
||||
context: emailContext
|
||||
}
|
||||
};
|
||||
|
@ -208,18 +253,37 @@ var getterByAlertType = {
|
|||
getDataForEventRegistrationClosed: getDataForEventRelated.bind(null, 'EventRegistrationClosed'),
|
||||
getDataForEventDatesUpdated: getDataForEventRelated.bind(null, 'EventDatesUpdated'),
|
||||
getDataForUserJoinedGroup: getDataForUserJoinedGroup,
|
||||
getDataForUserAskedToJoinGroup: getDataForUserAskedToJoinGroup
|
||||
getDataForUserAskedToJoinGroup: getDataForUserAskedToJoinGroup,
|
||||
getDataForUserUnregisteredFromEvent: getDataForUserUnregisteredFromEvent
|
||||
};
|
||||
|
||||
// === EMAIL ======================================================
|
||||
|
||||
function sendEmail (data) {
|
||||
Everlive.Email.sendEmailFromTemplate(data.templateName, data.recipients, data.context, done);
|
||||
if (!data.recipients || !data.recipients.length) {
|
||||
console.log('not sending email - no recipients');
|
||||
return rsvp.Promise.resolve();
|
||||
}
|
||||
|
||||
return new rsvp.Promise(function(resolve, reject) {
|
||||
Everlive.Email.sendEmailFromTemplate(data.templateName, data.recipients, data.context, function(err) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// === PUSH ======================================================
|
||||
|
||||
function sendPush (alertType, context) {
|
||||
if (!context.push || !context.push.userIds.length) {
|
||||
console.log('not sending push - no recipients');
|
||||
return rsvp.Promise.resolve();
|
||||
}
|
||||
|
||||
var el = Everlive.Sdk.withMasterKey();
|
||||
var notifObject = getNotificationObject(alertType, context);
|
||||
return el.push.send(notifObject);
|
||||
|
@ -254,6 +318,10 @@ function getNotificationObject (alertType, context) {
|
|||
title = 'New request to join ' + context.groupName;
|
||||
message = context.userName + ' asked to join ' + context.groupName;
|
||||
break;
|
||||
case 'UserUnregisteredFromEvent':
|
||||
title = context.userName + ' decided not to come';
|
||||
message = context.userName + ' is not coming to ' + context.eventName;
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
|
|
Загрузка…
Ссылка в новой задаче