feat: public confirm/decline procedures with waitlist auto-promotion
- finalist.getByToken: public lookup of a confirmation by signed token, with all the data the public page needs (project, team members, current state). Throws on expired/tampered tokens. - finalist.confirm: validates team membership of every selected user, checks against program.defaultAttendeeCap, atomically writes status=CONFIRMED + AttendingMember rows in a transaction. - finalist.decline: captures optional reason, then promotes the next WAITING waitlist entry in the same category (no-op if waitlist empty). Resolves the new windowHours from the LIVE_FINAL round configJson. - promoteNextWaitlistEntry service: encapsulates the cascade (mark PROMOTED, create fresh PENDING confirmation, send email).
This commit is contained in:
@@ -187,3 +187,366 @@ describe('finalist.selectFinalists', () => {
|
||||
).rejects.toThrow(/category mismatch/i)
|
||||
})
|
||||
})
|
||||
|
||||
describe('finalist.confirm and decline (public)', () => {
|
||||
const programIds: string[] = []
|
||||
const userIds: string[] = []
|
||||
|
||||
afterAll(async () => {
|
||||
for (const programId of programIds) {
|
||||
await prisma.attendingMember.deleteMany({
|
||||
where: { confirmation: { project: { programId } } },
|
||||
})
|
||||
await prisma.finalistConfirmation.deleteMany({ where: { project: { programId } } })
|
||||
await prisma.waitlistEntry.deleteMany({ where: { programId } })
|
||||
await cleanupTestData(programId, [])
|
||||
}
|
||||
if (userIds.length > 0) {
|
||||
await prisma.user.deleteMany({ where: { id: { in: userIds } } })
|
||||
}
|
||||
})
|
||||
|
||||
async function setupPendingConfirmation(programName: string) {
|
||||
const program = await createTestProgram({ name: programName })
|
||||
programIds.push(program.id)
|
||||
const lead = await prisma.user.create({
|
||||
data: {
|
||||
id: uid('user'),
|
||||
email: `lead_${uid()}@test.local`,
|
||||
name: 'Team Lead',
|
||||
role: 'APPLICANT',
|
||||
roles: ['APPLICANT'],
|
||||
status: 'ACTIVE',
|
||||
},
|
||||
})
|
||||
userIds.push(lead.id)
|
||||
const teammate = await prisma.user.create({
|
||||
data: {
|
||||
id: uid('user'),
|
||||
email: `mate_${uid()}@test.local`,
|
||||
name: 'Teammate',
|
||||
role: 'APPLICANT',
|
||||
roles: ['APPLICANT'],
|
||||
status: 'ACTIVE',
|
||||
},
|
||||
})
|
||||
userIds.push(teammate.id)
|
||||
const project = await createTestProject(program.id, {
|
||||
title: 'Confirmable Project',
|
||||
competitionCategory: 'STARTUP',
|
||||
})
|
||||
await prisma.teamMember.createMany({
|
||||
data: [
|
||||
{ projectId: project.id, userId: lead.id, role: 'LEAD' },
|
||||
{ projectId: project.id, userId: teammate.id, role: 'MEMBER' },
|
||||
],
|
||||
})
|
||||
const competition = await createTestCompetition(program.id, { status: 'ACTIVE' })
|
||||
const round = await createTestRound(competition.id, {
|
||||
roundType: 'LIVE_FINAL',
|
||||
configJson: { confirmationWindowHours: 24 },
|
||||
})
|
||||
return { program, lead, teammate, project, round }
|
||||
}
|
||||
|
||||
it('confirm with valid token + valid attendees succeeds', async () => {
|
||||
const { program, lead, teammate, project } = await setupPendingConfirmation(
|
||||
`confirm-ok-${uid()}`,
|
||||
)
|
||||
const admin = await createTestUser('SUPER_ADMIN')
|
||||
userIds.push(admin.id)
|
||||
const adminCaller = createCaller(finalistRouter, {
|
||||
id: admin.id,
|
||||
email: admin.email,
|
||||
role: 'SUPER_ADMIN',
|
||||
})
|
||||
const round = await prisma.round.findFirstOrThrow({
|
||||
where: { competition: { programId: program.id } },
|
||||
})
|
||||
await adminCaller.selectFinalists({
|
||||
programId: program.id,
|
||||
category: 'STARTUP',
|
||||
projectIds: [project.id],
|
||||
roundId: round.id,
|
||||
})
|
||||
const confirmation = await prisma.finalistConfirmation.findUniqueOrThrow({
|
||||
where: { projectId: project.id },
|
||||
})
|
||||
const publicCaller = finalistRouter.createCaller({
|
||||
session: null,
|
||||
prisma,
|
||||
ip: '127.0.0.1',
|
||||
userAgent: 'vitest',
|
||||
} as never)
|
||||
await publicCaller.confirm({
|
||||
token: confirmation.token,
|
||||
attendingUserIds: [lead.id, teammate.id],
|
||||
visaFlags: { [teammate.id]: true },
|
||||
})
|
||||
const updated = await prisma.finalistConfirmation.findUniqueOrThrow({
|
||||
where: { id: confirmation.id },
|
||||
include: { attendingMembers: true },
|
||||
})
|
||||
expect(updated.status).toBe('CONFIRMED')
|
||||
expect(updated.attendingMembers).toHaveLength(2)
|
||||
const visaForTeammate = updated.attendingMembers.find((a) => a.userId === teammate.id)
|
||||
expect(visaForTeammate?.needsVisa).toBe(true)
|
||||
const visaForLead = updated.attendingMembers.find((a) => a.userId === lead.id)
|
||||
expect(visaForLead?.needsVisa).toBe(false)
|
||||
})
|
||||
|
||||
it('confirm rejects userIds not in the project team', async () => {
|
||||
const { program, project } = await setupPendingConfirmation(`confirm-bad-${uid()}`)
|
||||
const outsider = await prisma.user.create({
|
||||
data: {
|
||||
id: uid('user'),
|
||||
email: `outsider_${uid()}@test.local`,
|
||||
name: 'Outsider',
|
||||
role: 'APPLICANT',
|
||||
roles: ['APPLICANT'],
|
||||
status: 'ACTIVE',
|
||||
},
|
||||
})
|
||||
userIds.push(outsider.id)
|
||||
const admin = await createTestUser('SUPER_ADMIN')
|
||||
userIds.push(admin.id)
|
||||
const adminCaller = createCaller(finalistRouter, {
|
||||
id: admin.id,
|
||||
email: admin.email,
|
||||
role: 'SUPER_ADMIN',
|
||||
})
|
||||
const round = await prisma.round.findFirstOrThrow({
|
||||
where: { competition: { programId: program.id } },
|
||||
})
|
||||
await adminCaller.selectFinalists({
|
||||
programId: program.id,
|
||||
category: 'STARTUP',
|
||||
projectIds: [project.id],
|
||||
roundId: round.id,
|
||||
})
|
||||
const confirmation = await prisma.finalistConfirmation.findUniqueOrThrow({
|
||||
where: { projectId: project.id },
|
||||
})
|
||||
const publicCaller = finalistRouter.createCaller({
|
||||
session: null,
|
||||
prisma,
|
||||
ip: '127.0.0.1',
|
||||
userAgent: 'vitest',
|
||||
} as never)
|
||||
await expect(
|
||||
publicCaller.confirm({
|
||||
token: confirmation.token,
|
||||
attendingUserIds: [outsider.id],
|
||||
visaFlags: {},
|
||||
}),
|
||||
).rejects.toThrow(/not a team member/i)
|
||||
})
|
||||
|
||||
it('confirm rejects when attendee count > program.defaultAttendeeCap', async () => {
|
||||
const { program, lead, teammate, project } = await setupPendingConfirmation(
|
||||
`confirm-cap-${uid()}`,
|
||||
)
|
||||
await prisma.program.update({ where: { id: program.id }, data: { defaultAttendeeCap: 1 } })
|
||||
const admin = await createTestUser('SUPER_ADMIN')
|
||||
userIds.push(admin.id)
|
||||
const adminCaller = createCaller(finalistRouter, {
|
||||
id: admin.id,
|
||||
email: admin.email,
|
||||
role: 'SUPER_ADMIN',
|
||||
})
|
||||
const round = await prisma.round.findFirstOrThrow({
|
||||
where: { competition: { programId: program.id } },
|
||||
})
|
||||
await adminCaller.selectFinalists({
|
||||
programId: program.id,
|
||||
category: 'STARTUP',
|
||||
projectIds: [project.id],
|
||||
roundId: round.id,
|
||||
})
|
||||
const confirmation = await prisma.finalistConfirmation.findUniqueOrThrow({
|
||||
where: { projectId: project.id },
|
||||
})
|
||||
const publicCaller = finalistRouter.createCaller({
|
||||
session: null,
|
||||
prisma,
|
||||
ip: '127.0.0.1',
|
||||
userAgent: 'vitest',
|
||||
} as never)
|
||||
await expect(
|
||||
publicCaller.confirm({
|
||||
token: confirmation.token,
|
||||
attendingUserIds: [lead.id, teammate.id],
|
||||
visaFlags: {},
|
||||
}),
|
||||
).rejects.toThrow(/attendee cap/i)
|
||||
})
|
||||
|
||||
it('decline marks the confirmation DECLINED with optional reason', async () => {
|
||||
const { program, project } = await setupPendingConfirmation(`decline-${uid()}`)
|
||||
const admin = await createTestUser('SUPER_ADMIN')
|
||||
userIds.push(admin.id)
|
||||
const adminCaller = createCaller(finalistRouter, {
|
||||
id: admin.id,
|
||||
email: admin.email,
|
||||
role: 'SUPER_ADMIN',
|
||||
})
|
||||
const round = await prisma.round.findFirstOrThrow({
|
||||
where: { competition: { programId: program.id } },
|
||||
})
|
||||
await adminCaller.selectFinalists({
|
||||
programId: program.id,
|
||||
category: 'STARTUP',
|
||||
projectIds: [project.id],
|
||||
roundId: round.id,
|
||||
})
|
||||
const confirmation = await prisma.finalistConfirmation.findUniqueOrThrow({
|
||||
where: { projectId: project.id },
|
||||
})
|
||||
const publicCaller = finalistRouter.createCaller({
|
||||
session: null,
|
||||
prisma,
|
||||
ip: '127.0.0.1',
|
||||
userAgent: 'vitest',
|
||||
} as never)
|
||||
await publicCaller.decline({ token: confirmation.token, reason: 'team disbanded' })
|
||||
const updated = await prisma.finalistConfirmation.findUniqueOrThrow({
|
||||
where: { id: confirmation.id },
|
||||
})
|
||||
expect(updated.status).toBe('DECLINED')
|
||||
expect(updated.declineReason).toBe('team disbanded')
|
||||
expect(updated.declinedAt).not.toBeNull()
|
||||
})
|
||||
|
||||
it('decline triggers next waitlist entry promotion', async () => {
|
||||
const { program, project } = await setupPendingConfirmation(`decline-cascade-${uid()}`)
|
||||
// Create a waitlist entry for a different project in the same category
|
||||
const backupProject = await createTestProject(program.id, {
|
||||
title: 'Backup',
|
||||
competitionCategory: 'STARTUP',
|
||||
})
|
||||
const backupLead = await prisma.user.create({
|
||||
data: {
|
||||
id: uid('user'),
|
||||
email: `backup_${uid()}@test.local`,
|
||||
name: 'Backup Lead',
|
||||
role: 'APPLICANT',
|
||||
roles: ['APPLICANT'],
|
||||
status: 'ACTIVE',
|
||||
},
|
||||
})
|
||||
userIds.push(backupLead.id)
|
||||
await prisma.teamMember.create({
|
||||
data: { projectId: backupProject.id, userId: backupLead.id, role: 'LEAD' },
|
||||
})
|
||||
const waitlistEntry = await prisma.waitlistEntry.create({
|
||||
data: {
|
||||
programId: program.id,
|
||||
projectId: backupProject.id,
|
||||
category: 'STARTUP',
|
||||
rank: 1,
|
||||
status: 'WAITING',
|
||||
},
|
||||
})
|
||||
|
||||
const admin = await createTestUser('SUPER_ADMIN')
|
||||
userIds.push(admin.id)
|
||||
const adminCaller = createCaller(finalistRouter, {
|
||||
id: admin.id,
|
||||
email: admin.email,
|
||||
role: 'SUPER_ADMIN',
|
||||
})
|
||||
const round = await prisma.round.findFirstOrThrow({
|
||||
where: { competition: { programId: program.id } },
|
||||
})
|
||||
await adminCaller.selectFinalists({
|
||||
programId: program.id,
|
||||
category: 'STARTUP',
|
||||
projectIds: [project.id],
|
||||
roundId: round.id,
|
||||
})
|
||||
const original = await prisma.finalistConfirmation.findUniqueOrThrow({
|
||||
where: { projectId: project.id },
|
||||
})
|
||||
const publicCaller = finalistRouter.createCaller({
|
||||
session: null,
|
||||
prisma,
|
||||
ip: '127.0.0.1',
|
||||
userAgent: 'vitest',
|
||||
} as never)
|
||||
await publicCaller.decline({ token: original.token })
|
||||
|
||||
// Backup project should now have a PENDING confirmation
|
||||
const promoted = await prisma.finalistConfirmation.findUnique({
|
||||
where: { projectId: backupProject.id },
|
||||
})
|
||||
expect(promoted).not.toBeNull()
|
||||
expect(promoted?.status).toBe('PENDING')
|
||||
expect(promoted?.promotedFromWaitlistEntryId).toBe(waitlistEntry.id)
|
||||
|
||||
const updatedEntry = await prisma.waitlistEntry.findUniqueOrThrow({
|
||||
where: { id: waitlistEntry.id },
|
||||
})
|
||||
expect(updatedEntry.status).toBe('PROMOTED')
|
||||
})
|
||||
|
||||
it('decline succeeds even when waitlist is empty', async () => {
|
||||
const { program, project } = await setupPendingConfirmation(`decline-empty-${uid()}`)
|
||||
const admin = await createTestUser('SUPER_ADMIN')
|
||||
userIds.push(admin.id)
|
||||
const adminCaller = createCaller(finalistRouter, {
|
||||
id: admin.id,
|
||||
email: admin.email,
|
||||
role: 'SUPER_ADMIN',
|
||||
})
|
||||
const round = await prisma.round.findFirstOrThrow({
|
||||
where: { competition: { programId: program.id } },
|
||||
})
|
||||
await adminCaller.selectFinalists({
|
||||
programId: program.id,
|
||||
category: 'STARTUP',
|
||||
projectIds: [project.id],
|
||||
roundId: round.id,
|
||||
})
|
||||
const confirmation = await prisma.finalistConfirmation.findUniqueOrThrow({
|
||||
where: { projectId: project.id },
|
||||
})
|
||||
const publicCaller = finalistRouter.createCaller({
|
||||
session: null,
|
||||
prisma,
|
||||
ip: '127.0.0.1',
|
||||
userAgent: 'vitest',
|
||||
} as never)
|
||||
await expect(publicCaller.decline({ token: confirmation.token })).resolves.toEqual({
|
||||
ok: true,
|
||||
})
|
||||
})
|
||||
|
||||
it('getByToken rejects expired tokens', async () => {
|
||||
const { program, project } = await setupPendingConfirmation(`confirm-expired-${uid()}`)
|
||||
// Manually create a confirmation with a past deadline + signed-expired token
|
||||
const { signFinalistToken } = await import('../../src/lib/finalist-token')
|
||||
const id = `cmfc_expired_${uid()}`
|
||||
const expiredExp = Math.floor(Date.now() / 1000) - 60
|
||||
const token = signFinalistToken({ confirmationId: id, exp: expiredExp })
|
||||
await prisma.finalistConfirmation.create({
|
||||
data: {
|
||||
id,
|
||||
projectId: project.id,
|
||||
category: 'STARTUP',
|
||||
status: 'PENDING',
|
||||
deadline: new Date(Date.now() - 60_000),
|
||||
token,
|
||||
},
|
||||
})
|
||||
const publicCaller = finalistRouter.createCaller({
|
||||
session: null,
|
||||
prisma,
|
||||
ip: '127.0.0.1',
|
||||
userAgent: 'vitest',
|
||||
} as never)
|
||||
await expect(publicCaller.getByToken({ token })).rejects.toThrow(/expired/i)
|
||||
// Cleanup
|
||||
await prisma.finalistConfirmation.delete({ where: { id } })
|
||||
void program
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user