feat(finalist): withdrawal notification to team on decline/unconfirm/unenroll

Notifies the team LEAD with FINALIST_WITHDRAWN (in-app + email) when an admin
withdraws a grand-finale slot via adminDecline, unconfirm, or unenroll.
For unenroll, only notifies when a CONFIRMED confirmation existed before deletion.
Adds finalist-withdrawal.test.ts (4 tests) covering all three paths.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matt
2026-06-04 16:14:26 +02:00
parent b2826d595f
commit d501624c56
2 changed files with 364 additions and 4 deletions

View File

@@ -559,7 +559,19 @@ export const finalistRouter = router({
.mutation(async ({ ctx, input }) => {
const confirmation = await ctx.prisma.finalistConfirmation.findUniqueOrThrow({
where: { id: input.confirmationId },
include: { project: { select: { programId: true, title: true } } },
include: {
project: {
select: {
programId: true,
title: true,
teamMembers: {
where: { role: 'LEAD' },
take: 1,
select: { userId: true },
},
},
},
},
})
if (confirmation.status !== 'PENDING') {
throw new TRPCError({
@@ -603,6 +615,25 @@ export const finalistRouter = router({
console.error('[finalist.adminDecline] failed to send admin notification:', err)
}
// Withdrawal notification to team lead — best-effort, never throws
try {
const lead = confirmation.project.teamMembers[0]
if (lead) {
const projectTitle = confirmation.project.title
const reason = input.reason
await createNotification({
userId: lead.userId,
type: NotificationTypes.FINALIST_WITHDRAWN,
title: 'Grand finale slot withdrawn',
message: `Your team "${projectTitle}" is no longer a confirmed finalist.${reason ? ' Reason: ' + reason : ''}`,
linkUrl: '/applicant',
metadata: { projectTitle, reason },
})
}
} catch (err) {
console.error('[finalist.adminDecline] failed to send withdrawal notification to team:', err)
}
const round = await ctx.prisma.round.findFirst({
where: {
competition: { programId: confirmation.project.programId },
@@ -810,6 +841,11 @@ export const finalistRouter = router({
mentorId: true,
},
},
teamMembers: {
where: { role: 'LEAD' },
take: 1,
select: { userId: true },
},
},
},
},
@@ -868,6 +904,25 @@ export const finalistRouter = router({
cascadedAssignmentCount: activeAssignments.length,
},
})
// Withdrawal notification to team lead — best-effort, never throws
try {
const lead = confirmation.project.teamMembers[0]
if (lead) {
const projectTitle = confirmation.project.title
await createNotification({
userId: lead.userId,
type: NotificationTypes.FINALIST_WITHDRAWN,
title: 'Grand finale slot withdrawn',
message: `Your team "${projectTitle}" is no longer a confirmed finalist.`,
linkUrl: '/applicant',
metadata: { projectTitle },
})
}
} catch (err) {
console.error('[finalist.unconfirm] failed to send withdrawal notification to team:', err)
}
return { ok: true, cascadedMentorAssignment }
}),
@@ -1538,19 +1593,38 @@ export const finalistRouter = router({
})
}
// Step 1: Delete the FinalistConfirmation (cascade removes AttendingMember
// Step 1: Capture the CONFIRMED confirmation (if any) BEFORE deleting,
// so we can notify the team lead. We only notify when the team had
// already confirmed (CONFIRMED status) — not for PENDING or absent rows.
const confirmedConfirmation = await ctx.prisma.finalistConfirmation.findFirst({
where: { projectId: input.projectId, status: 'CONFIRMED' },
select: {
project: {
select: {
title: true,
teamMembers: {
where: { role: 'LEAD' },
take: 1,
select: { userId: true },
},
},
},
},
})
// Step 2: Delete the FinalistConfirmation (cascade removes AttendingMember
// / FlightDetail / VisaApplication / MemberLunchPick).
// deleteMany is no-op-safe when no row exists.
await ctx.prisma.finalistConfirmation.deleteMany({
where: { projectId: input.projectId },
})
// Step 2: Delete the LIVE_FINAL ProjectRoundState.
// Step 3: Delete the LIVE_FINAL ProjectRoundState.
await ctx.prisma.projectRoundState.deleteMany({
where: { projectId: input.projectId, roundId: input.roundId },
})
// Step 3: Audit log
// Step 4: Audit log
await logAudit({
prisma: ctx.prisma,
userId: ctx.user.id,
@@ -1563,6 +1637,27 @@ export const finalistRouter = router({
},
})
// Step 5: Withdrawal notification — only when a CONFIRMED row existed.
// Best-effort, never throws.
if (confirmedConfirmation) {
try {
const lead = confirmedConfirmation.project.teamMembers[0]
if (lead) {
const projectTitle = confirmedConfirmation.project.title
await createNotification({
userId: lead.userId,
type: NotificationTypes.FINALIST_WITHDRAWN,
title: 'Grand finale slot withdrawn',
message: `Your team "${projectTitle}" is no longer a confirmed finalist.`,
linkUrl: '/applicant',
metadata: { projectTitle },
})
}
} catch (err) {
console.error('[finalist.unenroll] failed to send withdrawal notification to team:', err)
}
}
return { ok: true }
}),
})