feat(finalist): re-invite-safe confirmation reset helper

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Matt
2026-06-04 15:16:52 +02:00
parent ca9edcd038
commit dde8ea9345
2 changed files with 126 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
import type { CompetitionCategory, Prisma, PrismaClient } from '@prisma/client'
import { signFinalistToken } from '@/lib/finalist-token'
type TxClient = PrismaClient | Prisma.TransactionClient
/**
* Re-invite-safe variant of createPendingConfirmation. If a confirmation row
* already exists for the project (projectId is @unique), reset any
* non-CONFIRMED row back to a fresh PENDING with a new token/deadline and
* clear stale attendee rows; report CONFIRMED rows as a no-op so callers can
* skip them. Returns the row id + token + deadline for the email step.
*/
export async function resetOrCreatePendingConfirmation(
prisma: TxClient,
args: { projectId: string; category: CompetitionCategory; windowHours: number },
): Promise<{ id: string; token: string; deadline: Date; alreadyConfirmed: boolean }> {
const deadline = new Date(Date.now() + args.windowHours * 3_600_000)
const existing = await prisma.finalistConfirmation.findUnique({
where: { projectId: args.projectId },
select: { id: true, status: true },
})
if (existing?.status === 'CONFIRMED') {
return { id: existing.id, token: '', deadline, alreadyConfirmed: true }
}
if (existing) {
const token = signFinalistToken({
confirmationId: existing.id,
exp: Math.floor(deadline.getTime() / 1000),
})
// Clear any attendee rows from a prior cycle (cascade-deletes flight/visa/lunch).
await prisma.attendingMember.deleteMany({ where: { confirmationId: existing.id } })
await prisma.finalistConfirmation.update({
where: { id: existing.id },
data: {
category: args.category,
status: 'PENDING',
deadline,
token,
confirmedAt: null,
declinedAt: null,
declineReason: null,
expiredAt: null,
},
})
return { id: existing.id, token, deadline, alreadyConfirmed: false }
}
const id = `cmfc_${Math.random().toString(36).slice(2, 10)}_${Date.now().toString(36)}`
const token = signFinalistToken({
confirmationId: id,
exp: Math.floor(deadline.getTime() / 1000),
})
await prisma.finalistConfirmation.create({
data: {
id,
projectId: args.projectId,
category: args.category,
status: 'PENDING',
deadline,
token,
},
})
return { id, token, deadline, alreadyConfirmed: false }
}