|
| 1 | +import { TypedWorker } from './worker'; |
| 2 | +import { CandidateRejectedOpportunityMessage } from '@dailydotdev/schema'; |
| 3 | +import { User } from '../entity'; |
| 4 | +import { sendEmail, baseNotificationEmailData } from '../common'; |
| 5 | +import { isSubscribedToNotificationType } from './notifications/utils'; |
| 6 | +import { NotificationChannel, NotificationType } from '../notifications/common'; |
| 7 | + |
| 8 | +const worker: TypedWorker<'api.v1.recruiter-rejected-candidate-match'> = { |
| 9 | + subscription: 'api.recruiter-rejected-candidate-match-email', |
| 10 | + handler: async ({ data }, con, logger): Promise<void> => { |
| 11 | + const { userId, opportunityId } = data; |
| 12 | + |
| 13 | + try { |
| 14 | + const user = await con.getRepository(User).findOne({ |
| 15 | + where: { id: userId }, |
| 16 | + select: ['id', 'email', 'notificationFlags'], |
| 17 | + }); |
| 18 | + |
| 19 | + if (!user) { |
| 20 | + logger.warn( |
| 21 | + { userId, opportunityId }, |
| 22 | + 'User not found for recruiter rejected candidate email', |
| 23 | + ); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + if (!user.email) { |
| 28 | + logger.warn( |
| 29 | + { userId, opportunityId }, |
| 30 | + 'User has no email for recruiter rejected candidate email', |
| 31 | + ); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const shouldReceiveEmail = isSubscribedToNotificationType( |
| 36 | + user.notificationFlags, |
| 37 | + NotificationType.NewOpportunityMatch, |
| 38 | + NotificationChannel.Email, |
| 39 | + ); |
| 40 | + |
| 41 | + if (!shouldReceiveEmail) { |
| 42 | + logger.info( |
| 43 | + { userId, opportunityId }, |
| 44 | + 'User is not subscribed to recruiter rejected opportunity emails', |
| 45 | + ); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + await sendEmail({ |
| 50 | + ...baseNotificationEmailData, |
| 51 | + reply_to: 'ido@daily.dev', |
| 52 | + transactional_message_id: '85', |
| 53 | + message_data: { |
| 54 | + opportunity_id: opportunityId, |
| 55 | + }, |
| 56 | + identifiers: { |
| 57 | + id: user.id, |
| 58 | + }, |
| 59 | + to: user.email, |
| 60 | + }); |
| 61 | + } catch (_err) { |
| 62 | + const err = _err as Error; |
| 63 | + logger.error( |
| 64 | + { err, userId, opportunityId }, |
| 65 | + 'failed to send recruiter rejected candidate email', |
| 66 | + ); |
| 67 | + throw err; |
| 68 | + } |
| 69 | + }, |
| 70 | + parseMessage: (message) => |
| 71 | + CandidateRejectedOpportunityMessage.fromBinary(message.data), |
| 72 | +}; |
| 73 | + |
| 74 | +export default worker; |
0 commit comments