feat(finalist): admin alerts on confirm/decline/expire/promote

Wire notifyAdmins() into finalist lifecycle events so admins receive
in-app (+ email when enabled) notifications on each key transition:
- finalist.confirm → FINALIST_CONFIRMED
- finalist.decline / adminDecline → FINALIST_DECLINED
- expirePendingPastDeadline (per row) → FINALIST_EXPIRED
- promoteNextWaitlistEntry + manualPromote → FINALIST_WAITLIST_PROMOTED

All calls are wrapped in try/catch — comms never throw inside a mutation
or cron (CLAUDE.md: "round notifications never throw"). Added project
title to the project select where it was missing.

Tests: tests/unit/finalist-comms.test.ts — 3 tests passing (TDD).
Regression: finalist-confirmation, finalist-admin-confirm,
finalist-enrollment — 30 tests all passing.

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

View File

@@ -2,6 +2,7 @@ import type { CompetitionCategory, PrismaClient } from '@prisma/client'
import { signFinalistToken } from '@/lib/finalist-token'
import { sendFinalistConfirmationEmail } from '@/lib/email'
import { logAudit } from '@/server/utils/audit'
import { notifyAdmins, NotificationTypes } from './in-app-notification'
type AnyPrisma = Pick<PrismaClient, 'finalistConfirmation' | 'waitlistEntry' | 'project'>
@@ -104,6 +105,27 @@ export async function promoteNextWaitlistEntry(
}
}
// Admin alert — best-effort, never throws
try {
const projectTitle = project?.title ?? entry.projectId
await notifyAdmins({
type: NotificationTypes.FINALIST_WAITLIST_PROMOTED,
title: 'Waitlist entry promoted',
message: `"${projectTitle}" (${args.category}) was promoted from the waitlist.`,
linkUrl: '/admin/logistics',
metadata: {
projectId: entry.projectId,
projectTitle,
category: args.category,
},
})
} catch (err) {
console.error(
`[promoteNextWaitlistEntry] failed to send admin notification for project ${entry.projectId}:`,
err,
)
}
return { promoted: true, entryId: entry.id, confirmationId }
}
@@ -131,6 +153,31 @@ export async function expirePendingPastDeadline(
entityId: c.id,
detailsJson: { projectId: c.projectId, category: c.category },
})
// Admin alert — best-effort, never throws
try {
// Resolve project title for a meaningful notification message
const proj = await prisma.project.findUnique({
where: { id: c.projectId },
select: { title: true },
})
const projectTitle = proj?.title ?? c.projectId
await notifyAdmins({
type: NotificationTypes.FINALIST_EXPIRED,
title: 'Finalist confirmation expired',
message: `"${projectTitle}" (${c.category}) did not confirm in time — confirmation expired.`,
linkUrl: '/admin/logistics',
metadata: {
projectId: c.projectId,
projectTitle,
category: c.category,
},
})
} catch (err) {
console.error(
`[expirePendingPastDeadline] failed to send admin notification for project ${c.projectId}:`,
err,
)
}
// Resolve windowHours for this program's grand-finale round
const round = await prisma.round.findFirst({
where: {