@@ -328,6 +328,40 @@ async function sendStatusUpdateEmail(req, res, next) {
328328 }
329329}
330330
331+ /**
332+ * Sends a preset email to a user if a status change occured with email params.
333+ * @param {{body: {status?: string}, params: {email: string}} } req
334+ * @param {* } res
335+ * @param {(err?:*)=>void } next
336+ */
337+ async function completeStatusUpdateEmail ( req , res , next ) {
338+ //skip if the status doesn't exist
339+ if ( ! req . body . hacker . status ) {
340+ return next ( ) ;
341+ } else {
342+ // send it to the hacker that is being updated.
343+ const hacker = await Services . Hacker . findById ( req . body . hacker . _id ) ;
344+ const account = await Services . Account . findById ( hacker . accountId ) ;
345+ if ( ! hacker ) {
346+ return next ( {
347+ status : 404 ,
348+ message : Constants . Error . HACKER_404_MESSAGE
349+ } ) ;
350+ } else if ( ! account ) {
351+ return next ( {
352+ status : 500 ,
353+ message : Constants . Error . GENERIC_500_MESSAGE
354+ } ) ;
355+ }
356+ Services . Email . sendStatusUpdate (
357+ account . firstName ,
358+ account . email ,
359+ req . body . hacker . status ,
360+ next
361+ ) ;
362+ }
363+ }
364+
331365/**
332366 * Sends an email telling the user that they have applied. This is used exclusively when we POST a hacker.
333367 * @param {{body: {hacker: {accountId: string}}} } req
@@ -523,6 +557,40 @@ async function updateHacker(req, res, next) {
523557 }
524558}
525559
560+ /**
561+ * Updates a hacker that is specified by req.body.hacker._id, and then sets req.email
562+ * to the email of the hacker, found in Account.
563+ * @param {{params:{_id: string}, body: *} } req
564+ * @param {* } res
565+ * @param {* } next
566+ */
567+ async function obtainEmailByHackerId ( req , res , next ) {
568+ const hacker = await Services . Hacker . findById ( req . body . hacker . _id ) ;
569+ if ( hacker ) {
570+ const acct = await Services . Account . findById ( hacker . accountId ) ;
571+ if ( ! acct ) {
572+ return next ( {
573+ status : 500 ,
574+ message : Constants . Error . HACKER_UPDATE_500_MESSAGE ,
575+ data : {
576+ hackerId : hacker . id ,
577+ accountId : hacker . accountId
578+ }
579+ } ) ;
580+ }
581+ req . email = acct . email ;
582+ return next ( ) ;
583+ } else {
584+ return next ( {
585+ status : 404 ,
586+ message : Constants . Error . HACKER_404_MESSAGE ,
587+ data : {
588+ id : req . params . id
589+ }
590+ } ) ;
591+ }
592+ }
593+
526594/**
527595 * Sets req.body.status to Accepted for next middleware.
528596 * @param {{params:{id: string}, body: *} } req
@@ -531,6 +599,19 @@ async function updateHacker(req, res, next) {
531599 */
532600function parseAccept ( req , res , next ) {
533601 req . body . status = Constants . General . HACKER_STATUS_ACCEPTED ;
602+ req . hackerId = req . params . id ;
603+ next ( ) ;
604+ }
605+
606+ /**
607+ * Sets req.body.hacker.status to Accepted for next middleware.
608+ * @param {{params:{email: string}, body: *} } req
609+ * @param {* } res
610+ * @param {* } next
611+ */
612+ function parseAcceptEmail ( req , res , next ) {
613+ req . body . hacker . status = Constants . General . HACKER_STATUS_ACCEPTED ;
614+ req . hackerId = req . body . hacker . _id ;
534615 next ( ) ;
535616}
536617
@@ -652,7 +733,9 @@ module.exports = {
652733 sendAppliedStatusEmail
653734 ) ,
654735 updateHacker : Middleware . Util . asyncMiddleware ( updateHacker ) ,
736+ obtainEmailByHackerId : Middleware . Util . asyncMiddleware ( obtainEmailByHackerId ) ,
655737 parseAccept : parseAccept ,
738+ parseAcceptEmail : parseAcceptEmail ,
656739 validateConfirmedStatusFromAccountId : Middleware . Util . asyncMiddleware (
657740 validateConfirmedStatusFromAccountId
658741 ) ,
@@ -662,6 +745,9 @@ module.exports = {
662745 validateConfirmedStatusFromObject : Middleware . Util . asyncMiddleware (
663746 validateConfirmedStatusFromObject
664747 ) ,
748+ completeStatusUpdateEmail : Middleware . Util . asyncMiddleware (
749+ completeStatusUpdateEmail
750+ ) ,
665751 checkDuplicateAccountLinks : Middleware . Util . asyncMiddleware (
666752 checkDuplicateAccountLinks
667753 ) ,
0 commit comments