|
| 1 | +import { prisma } from '@/lib/prisma'; |
| 2 | +import sesConnector from '@/lib/connectors/ses.connector'; |
| 3 | +import { generateAssessmentInvitationHTML } from '@/lib/templates/invitation'; |
| 4 | + |
| 5 | +interface SendAssessmentInvitationResult { |
| 6 | + success: boolean; |
| 7 | + message: string; |
| 8 | + candidateName: string; |
| 9 | + positionTitle: string; |
| 10 | + assessmentId: string; |
| 11 | +} |
| 12 | + |
| 13 | +export async function sendAssessmentInvitationEmail( |
| 14 | + candidateId: string, |
| 15 | + activeOrganizationId: string |
| 16 | +): Promise<SendAssessmentInvitationResult> { |
| 17 | + const candidate = await prisma.candidate.findUnique({ |
| 18 | + where: { id: candidateId }, |
| 19 | + include: { |
| 20 | + applications: { |
| 21 | + include: { |
| 22 | + assessment: true, |
| 23 | + position: true, |
| 24 | + }, |
| 25 | + }, |
| 26 | + organization: true, |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + if (!candidate) { |
| 31 | + throw new Error('Candidate not found'); |
| 32 | + } |
| 33 | + |
| 34 | + if (candidate.orgId !== activeOrganizationId) { |
| 35 | + throw new Error('Unauthorized'); |
| 36 | + } |
| 37 | + |
| 38 | + const applicationWithAssessment = candidate.applications.find((app) => app.assessment !== null); |
| 39 | + |
| 40 | + if (!applicationWithAssessment?.assessment) { |
| 41 | + throw new Error('Candidate does not have an assigned assessment'); |
| 42 | + } |
| 43 | + |
| 44 | + const assessment = applicationWithAssessment.assessment; |
| 45 | + const position = applicationWithAssessment.position; |
| 46 | + const organization = candidate.organization; |
| 47 | + |
| 48 | + const baseUrl = |
| 49 | + process.env.BETTER_AUTH_URL ?? process.env.NEXT_PUBLIC_APP_URL ?? 'http://localhost:3000'; |
| 50 | + const assessmentUrl = `${baseUrl}/assessment/${assessment.uniqueLink}`; |
| 51 | + const logoUrl = `${baseUrl}/Sarge_logo.svg`; |
| 52 | + |
| 53 | + //placeholder duration and expiration |
| 54 | + const durationMinutes = 120; |
| 55 | + const expirationDate = 'March 16, 2026 11:59PM EST'; |
| 56 | + const htmlContent = generateAssessmentInvitationHTML({ |
| 57 | + candidateName: candidate.name, |
| 58 | + positionTitle: position.title, |
| 59 | + organizationName: organization.name, |
| 60 | + assessmentId: assessment.id, |
| 61 | + assessmentUrl, |
| 62 | + logoUrl, |
| 63 | + durationMinutes, |
| 64 | + expirationDate, |
| 65 | + }); |
| 66 | + |
| 67 | + // Send email |
| 68 | + const emailSent = await sesConnector.sendEmail( |
| 69 | + candidate.email, |
| 70 | + `${organization.name} Software Engineering Role: Online Assessment Invitation`, |
| 71 | + `Hello ${candidate.name}, you have been invited to complete an online assessment for the ${position.title} position at ${organization.name}. Visit ${assessmentUrl} to begin.`, |
| 72 | + { html: htmlContent } |
| 73 | + ); |
| 74 | + |
| 75 | + if (!emailSent) { |
| 76 | + throw new Error('Failed to send invitation email'); |
| 77 | + } |
| 78 | + |
| 79 | + return { |
| 80 | + success: true, |
| 81 | + message: `Assessment invitation sent to ${candidate.email}`, |
| 82 | + candidateName: candidate.name, |
| 83 | + positionTitle: position.title, |
| 84 | + assessmentId: assessment.id, |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +const emailService = { |
| 89 | + sendAssessmentInvitationEmail, |
| 90 | +}; |
| 91 | + |
| 92 | +export default emailService; |
0 commit comments