Skip to content

Commit 972e118

Browse files
committed
Moved sendInterviewInvitationNotifications from services/NotificationsSchedulerService to eventHandles/InterviewEventHandler
1 parent c003256 commit 972e118

File tree

4 files changed

+64
-42
lines changed

4 files changed

+64
-42
lines changed

app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ app.use(cors({
2626
exposedHeaders: ['X-Page', 'X-Per-Page', 'X-Total', 'X-Total-Pages', 'X-Prev-Page', 'X-Next-Page']
2727
}))
2828
app.use((...args) => {
29-
const [req, res, next] = args;
29+
const [req, res, next] = args
3030
// For test nylas webhook, we need raw buffer
3131
// Here i sCustom Middleware to compute rawBody. Unfortunately using
3232
// JSON.stringify(req.body) will remove spaces and newlines, so verification
3333
// will fail. We must add this middleware to ensure we're computing the correct
3434
// signature
35-
if(req.path === `${config.BASE_PATH}/taas/nylas-webhooks`) {
35+
if (req.path === `${config.BASE_PATH}/taas/nylas-webhooks`) {
3636
req.rawBody = ''
3737
req.on('data', (chunk) => (req.rawBody += chunk))
3838
req.on('error', () => res.status(500).send('Error parsing body'))
39-
39+
4040
req.on('end', () => {
4141
// because the stream has been consumed, other parsers like bodyParser.json
4242
// cannot stream the request data and will time out so we must explicitly parse the body

src/eventHandlers/InterviewEventHandler.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,62 @@ const models = require('../models')
99
const logger = require('../common/logger')
1010
const helper = require('../common/helper')
1111
const Constants = require('../../app-constants')
12+
const notificationsSchedulerService = require('../services/NotificationsSchedulerService')
13+
14+
/**
15+
* Send interview invitaion notifications
16+
* @param {*} interview the requested interview
17+
* @returns
18+
*/
19+
async function sendInterviewInvitationNotifications (interview) {
20+
logger.debug({
21+
component: 'InterviewEventHandler',
22+
context: 'sendInterviewInvitationNotifications',
23+
message: `send to interview ${interview.id}`
24+
})
25+
26+
try {
27+
const template = 'taas.notification.interview-invitation'
28+
29+
// const jobCandidate = await models.JobCandidate.findById(interview.jobCandidateId)
30+
// const { email, firstName, lastName } = await helper.getUserDetailsByUserUUID(interview.hostUserId)
31+
32+
// send host email
33+
const data = await notificationsSchedulerService.getDataForInterview(interview)
34+
if (!data) { return }
35+
36+
if (!_.isEmpty(data.guestEmail)) {
37+
// send guest emails
38+
await notificationsSchedulerService.sendNotification({}, {
39+
template,
40+
recipients: [{ email: data.guestEmail }],
41+
data: {
42+
...data,
43+
subject: `${data.duration} minutes tech interview with ${data.guestFullName} for ${data.jobTitle} is requested by the Customer`,
44+
nylasPageSlug: interview.nylasPageSlug
45+
}
46+
})
47+
} else {
48+
logger.error({
49+
component: 'InterviewEventHandler',
50+
context: 'sendInterviewInvitationNotifications',
51+
message: `Interview id: ${interview.id} guest emails not present`
52+
})
53+
}
54+
} catch (e) {
55+
logger.error({
56+
component: 'InterviewEventHandler',
57+
context: 'sendInterviewInvitationNotifications',
58+
message: `Send email to interview ${interview.id}: ${e}`
59+
})
60+
}
61+
62+
logger.debug({
63+
component: 'InterviewEventHandler',
64+
context: 'sendInterviewInvitationNotifications',
65+
message: `Sent notifications for interview ${interview.id}`
66+
})
67+
}
1268

1369
/**
1470
* Check if there is overlapping interview, if there is overlapping, then send notifications.
@@ -152,5 +208,6 @@ async function processUpdate (payload) {
152208

153209
module.exports = {
154210
processRequest,
155-
processUpdate
211+
processUpdate,
212+
sendInterviewInvitationNotifications
156213
}

src/services/InterviewService.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const {
3232
patchSchedulingPage
3333
} = require('./NylasService')
3434
const { createUserMeetingSettingsIfNotExisting } = require('./UserMeetingSettingsService')
35-
const notificationsSchedulerService = require('./NotificationsSchedulerService')
35+
const interviewEventHandler = require('../eventHandlers/InterviewEventHandler')
3636
/**
3737
* Ensures user is permitted for the operation.
3838
*
@@ -325,7 +325,7 @@ async function requestInterview (currentUser, jobCandidateId, interview) {
325325
// if reaches here, it's not one of the common errors handled in `handleSequelizeError`
326326
throw err
327327
}
328-
await notificationsSchedulerService.sendInterviewInvitationNotifications(interview)
328+
await interviewEventHandler.sendInterviewInvitationNotifications(interview)
329329
await helper.postEvent(config.TAAS_INTERVIEW_REQUEST_TOPIC, entity)
330330
await helper.postEvent(config.TAAS_JOB_CANDIDATE_UPDATE_TOPIC, jobCandidateEntity)
331331
// return created interview

src/services/NotificationsSchedulerService.js

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -740,41 +740,6 @@ async function sendInterviewExpiredNotifications () {
740740
localLogger.debug(`[sendInterviewExpiredNotifications]: Sent notifications for ${interviews.length} interviews which are expired.`)
741741
}
742742

743-
// Think it as only once scheduler when create an interview record
744-
async function sendInterviewInvitationNotifications (interview) {
745-
localLogger.debug(`[sendInterviewInvitationNotifications]: send to interview ${interview.id}`)
746-
747-
try {
748-
const template = 'taas.notification.interview-invitation'
749-
750-
// const jobCandidate = await models.JobCandidate.findById(interview.jobCandidateId)
751-
// const { email, firstName, lastName } = await helper.getUserDetailsByUserUUID(interview.hostUserId)
752-
753-
// send host email
754-
const data = await getDataForInterview(interview)
755-
if (!data) { return }
756-
757-
if (!_.isEmpty(data.guestEmail)) {
758-
// send guest emails
759-
await sendNotification({}, {
760-
template,
761-
recipients: [{ email: data.guestEmail }],
762-
data: {
763-
...data,
764-
subject: `${data.duration} minutes tech interview with ${data.guestFullName} for ${data.jobTitle} is requested by the Customer`,
765-
nylasPageSlug: interview.nylasPageSlug
766-
}
767-
})
768-
} else {
769-
localLogger.error(`Interview id: ${interview.id} guest emails not present`, 'sendInterviewExpiredNotifications')
770-
}
771-
} catch (e) {
772-
localLogger.error(`Send email to interview ${interview.id}: ${e}`)
773-
}
774-
775-
localLogger.debug(`[sendInterviewInvitationNotifications]: Sent notifications for interview ${interview.id}`)
776-
}
777-
778743
/**
779744
* For preventing app crashing by scheduler function, use this function to wrapper target handler.
780745
* @param {*} callback : function handler
@@ -797,7 +762,7 @@ module.exports = {
797762
sendInterviewExpiredNotifications: errorCatchWrapper(sendInterviewExpiredNotifications, 'sendInterviewExpiredNotifications'),
798763
sendInterviewScheduleReminderNotifications: errorCatchWrapper(sendInterviewScheduleReminderNotifications, 'sendInterviewScheduleReminderNotifications'),
799764
updateInterviewStatus,
800-
sendInterviewInvitationNotifications,
765+
getDataForInterview,
801766
sendPostInterviewActionNotifications: errorCatchWrapper(sendPostInterviewActionNotifications, 'sendPostInterviewActionNotifications'),
802767
sendResourceBookingExpirationNotifications: errorCatchWrapper(sendResourceBookingExpirationNotifications, 'sendResourceBookingExpirationNotifications')
803768
}

0 commit comments

Comments
 (0)