-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
function 'sending email' is done && delete useless console.log in bac…
…kEnd
- Loading branch information
Showing
8 changed files
with
175 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ module.exports = { | |
'secret': 'asdawldjal', | ||
databaseUrl: 'mongodb://teamdivider:[email protected]:37101/teamdivider', | ||
authenticationMiddleware: passport.authenticate('jwt', { session: false }), | ||
SENDGRID_API_KEY: "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", | ||
}; |
159 changes: 159 additions & 0 deletions
159
server/routes/activities/controllers/emailParticipantsForCertainActivityController.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
const HttpStatus = require("http-status-codes"); | ||
const createErrorHandler = require("../../utils").createErrorHandler; | ||
const ObjectIdIsValid = require("mongoose").Types.ObjectId.isValid; | ||
|
||
const Activity = require("../../../models").Activity; | ||
|
||
const sendgridAPIKey = require("../../../config/main").SENDGRID_API_KEY; | ||
const sendgrid = require("sendgrid")(sendgridAPIKey); | ||
const mailHelper = require('sendgrid').mail; | ||
|
||
|
||
function groupParticipantsByGroupNumber (pars) { | ||
let groupList = []; | ||
pars.forEach(function (par) { | ||
// iterate all the group in groupList, if there exits that group number, add par to it, and return | ||
for (let i=0; i<groupList.length; i++){ | ||
const group = groupList[i]; | ||
if (group.groupNumber === par.groupNumber){ | ||
group.groupMembers.push(par); | ||
return ; | ||
} | ||
} | ||
// didn't find the group in groupList, create a group and add to groupList | ||
groupList.push({ | ||
groupNumber: par.groupNumber, | ||
groupMembers: [par], | ||
}); | ||
}); | ||
|
||
return groupList; | ||
} | ||
|
||
|
||
function generateHTMLText (participant, group, activity) { | ||
const participantName = participant.name; | ||
let partnerNameList = []; | ||
group.groupMembers.forEach(function (member) { | ||
if (member._id !== participant._id) { | ||
partnerNameList.push(member.name); | ||
} | ||
}) | ||
let text = "<b>Dear " + participantName + ",<br>" | ||
+ 'Congratulations! You are now in the group number ' + participant.groupNumber + ".<br>"; | ||
|
||
if (partnerNameList.length === 0){ | ||
text += 'Currently, you are the only one in this group. We would add more people to your group!<br><br>'; | ||
} | ||
else if (partnerNameList.length === 1){ | ||
text += 'Your partners is ' + partnerNameList[0] + "!<br><br>"; | ||
} | ||
else{ | ||
text += 'Your partners are '; | ||
for (let i = 0; i < partnerNameList.length - 1; i++) { | ||
text += partnerNameList[i] + ", "; | ||
} | ||
text += partnerNameList[partnerNameList.length -1] + ".<br><br>"; | ||
} | ||
return text + "Thank you for attending '" + activity.title + "'!</b>"; | ||
} | ||
|
||
|
||
function validateInput(req) { | ||
return validateParameters(req.params) ; | ||
} | ||
|
||
function validateParameters(prm) { | ||
return prm.hasOwnProperty('activityId') && typeof prm.activityId === 'string' | ||
&& ObjectIdIsValid(prm.activityId); | ||
} | ||
|
||
|
||
module.exports = function (req, res, next) { | ||
if (!validateInput(req)) { | ||
const errorMessage = 'please give the correct activityID in URL'; | ||
createErrorHandler(res, HttpStatus.BAD_REQUEST)(errorMessage); | ||
return; | ||
} | ||
|
||
Activity.findOne({_id: req.params.activityId, _creator: req.user._id, isDeleted: false}) | ||
.populate({ | ||
path: 'participants', | ||
select: 'name email groupNumber', | ||
match: {isDeleted: false, groupNumber: {'$nin': [-1, -2] } }, | ||
}) | ||
.exec() | ||
.then(function (activity) { | ||
if (activity === null) { | ||
const errorMessage = "Cannot find activity: " + req.params.activityId; | ||
return createErrorHandler(res, HttpStatus.NOT_FOUND)(errorMessage); | ||
} | ||
|
||
const groupList = groupParticipantsByGroupNumber(activity.participants); | ||
|
||
groupList.forEach(function (group) { | ||
group.groupMembers.forEach(function (member) { | ||
|
||
// send mail | ||
const from_email = new mailHelper.Email('[email protected]'); | ||
const to_email = new mailHelper.Email(member.email); | ||
const subject = 'Activity Notification: Group Information'; | ||
const content = new mailHelper.Content('text/html', generateHTMLText(member, group, activity)); | ||
const email = new mailHelper.Mail(from_email, subject, to_email, content); | ||
|
||
const emailRequest = sendgrid.emptyRequest({ | ||
method: 'POST', | ||
path: '/v3/mail/send', | ||
body: email.toJSON() | ||
}); | ||
|
||
sendgrid.API(emailRequest, function (error, response) { | ||
if (error !== null){ | ||
console.log(error); | ||
return; | ||
} | ||
// console.log(response.statusCode) | ||
// console.log(response.body) | ||
// console.log(response.headers) | ||
console.log("---success in sending email---"); | ||
}) | ||
|
||
}) | ||
|
||
}); | ||
|
||
|
||
return res.status(HttpStatus.OK).json({ | ||
groupList: groupList, | ||
}) | ||
}) | ||
.catch(createErrorHandler(res, HttpStatus.INTERNAL_SERVER_ERROR)); | ||
}; | ||
|
||
// function generateEmailText (participant, group, activity) { | ||
// const participantName = participant.name; | ||
// let partnerNameList = []; | ||
// group.groupMembers.forEach(function (member) { | ||
// if (member._id !== participant._id) { | ||
// partnerNameList.push(member.name); | ||
// } | ||
// }) | ||
// let text = "Dear " + participantName + ",\n" | ||
// + 'Congratulations! You are now in the group number ' + participant.groupNumber + ".\n"; | ||
// | ||
// if (partnerNameList.length === 0){ | ||
// text += 'Currently, you are the only one in this group. We would add more people to your group!\n\n'; | ||
// } | ||
// else if (partnerNameList.length === 1){ | ||
// text += 'Your partners is ' + partnerNameList[0] + "!\n\n"; | ||
// } | ||
// else{ | ||
// text += 'Your partners are '; | ||
// for (let i = 0; i < partnerNameList.length - 1; i++) { | ||
// text += partnerNameList[i] + ", "; | ||
// } | ||
// text += partnerNameList[partnerNameList.length -1] + ".\n\n"; | ||
// } | ||
// return text + "Thank you for attending '" + activity.title + "'!"; | ||
// } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters