|
| 1 | +import { type NextRequest } from 'next/server'; |
| 2 | +import { z } from 'zod'; |
| 3 | +import { handleError } from '@/lib/utils/errors.utils'; |
| 4 | +import { getSession } from '@/lib/utils/auth.utils'; |
| 5 | +import { assertRecruiterOrAbove } from '@/lib/utils/permissions.utils'; |
| 6 | +import { prisma } from '@/lib/prisma'; |
| 7 | +import sesConnector from '@/lib/connectors/ses.connector'; |
| 8 | +import { generateAssessmentInvitationHTML } from '@/lib/templates/assessment-invitation-email'; |
| 9 | +import { type Application } from '@/generated/prisma'; |
| 10 | + |
| 11 | +const sendAssessmentInvitationSchema = z.object({ |
| 12 | + candidateId: z.string().cuid(), |
| 13 | +}); |
| 14 | + |
| 15 | +export async function POST(request: NextRequest) { |
| 16 | + try { |
| 17 | + const session = await getSession(); |
| 18 | + await assertRecruiterOrAbove(request.headers); |
| 19 | + |
| 20 | + const body = await request.json(); |
| 21 | + const { candidateId } = sendAssessmentInvitationSchema.parse(body); |
| 22 | + |
| 23 | + const candidate = await prisma.candidate.findUnique({ |
| 24 | + where: { id: candidateId }, |
| 25 | + include: { |
| 26 | + applications: { |
| 27 | + include: { |
| 28 | + assessment: true, |
| 29 | + position: true, |
| 30 | + }, |
| 31 | + }, |
| 32 | + organization: true, |
| 33 | + }, |
| 34 | + }); |
| 35 | + |
| 36 | + //validation |
| 37 | + if (!candidate) { |
| 38 | + return Response.json({ error: 'Candidate not found' }, { status: 404 }); |
| 39 | + } |
| 40 | + |
| 41 | + if (candidate.orgId !== session.activeOrganizationId) { |
| 42 | + return Response.json({ error: 'Unauthorized' }, { status: 403 }); |
| 43 | + } |
| 44 | + |
| 45 | + const applicationWithAssessment = candidate.applications.find( |
| 46 | + (app: Application & { assessment: any }) => app.assessment !== null |
| 47 | + ); |
| 48 | + |
| 49 | + if (!applicationWithAssessment || !applicationWithAssessment.assessment) { |
| 50 | + return Response.json( |
| 51 | + { error: 'Candidate does not have an assigned assessment' }, |
| 52 | + { status: 400 } |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + const assessment = applicationWithAssessment.assessment; |
| 57 | + const position = applicationWithAssessment.position; |
| 58 | + const organization = candidate.organization; |
| 59 | + |
| 60 | + //create url |
| 61 | + const baseUrl = |
| 62 | + process.env.BETTER_AUTH_URL || |
| 63 | + process.env.NEXT_PUBLIC_APP_URL || |
| 64 | + 'http://localhost:3000'; |
| 65 | + const assessmentUrl = `${baseUrl}/assessment/${assessment.uniqueLink}`; |
| 66 | + const logoUrl = `${baseUrl}/Sarge_logo.svg`; |
| 67 | + |
| 68 | + //email html |
| 69 | + const htmlContent = generateAssessmentInvitationHTML({ |
| 70 | + candidateName: candidate.name, |
| 71 | + positionTitle: position.title, |
| 72 | + organizationName: organization.name, |
| 73 | + assessmentId: assessment.id, |
| 74 | + assessmentUrl, |
| 75 | + logoUrl, |
| 76 | + }); |
| 77 | + |
| 78 | + //send email |
| 79 | + const emailSent = await sesConnector.sendEmail( |
| 80 | + candidate.email, |
| 81 | + `${organization.name} Software Engineering Role: Online Assessment Invitation`, |
| 82 | + `Hello ${candidate.name}, you have been invited to complete an online assessment for the ${position.title} position at ${organization.name}. Visit ${assessmentUrl} to begin.`, |
| 83 | + { html: htmlContent } |
| 84 | + ); |
| 85 | + |
| 86 | + if (!emailSent) { |
| 87 | + return Response.json({ error: 'Failed to send invitation email' }, { status: 500 }); |
| 88 | + } |
| 89 | + |
| 90 | + return Response.json( |
| 91 | + { |
| 92 | + data: { |
| 93 | + success: true, |
| 94 | + message: `Assessment invitation sent to ${candidate.email}`, |
| 95 | + candidateName: candidate.name, |
| 96 | + positionTitle: position.title, |
| 97 | + assessmentId: assessment.id, |
| 98 | + }, |
| 99 | + }, |
| 100 | + { status: 200 } |
| 101 | + ); |
| 102 | + } catch (err) { |
| 103 | + return handleError(err); |
| 104 | + } |
| 105 | +} |
0 commit comments