feat: auto-create MemberLunchPick on attendee writes
Adds ensureLunchPickForAttendingMember helper called from confirm, adminConfirm, and editAttendees attendee-creation paths. No-ops when the program has no LunchEvent. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
||||
} from '../services/in-app-notification'
|
||||
import { sendFinalistConfirmationEmail } from '@/lib/email'
|
||||
import { verifyFinalistToken } from '@/lib/finalist-token'
|
||||
import { ensureLunchPickForAttendingMember } from '../services/lunch-pick-sync'
|
||||
|
||||
export const finalistRouter = router({
|
||||
/** List all per-category finalist slot quotas for a program. */
|
||||
@@ -351,6 +352,13 @@ export const finalistRouter = router({
|
||||
data: created.map((m) => ({ attendingMemberId: m.id, status: 'REQUESTED' })),
|
||||
})
|
||||
}
|
||||
const allMembers = await tx.attendingMember.findMany({
|
||||
where: { confirmationId: confirmation.id, userId: { in: input.attendingUserIds } },
|
||||
select: { id: true },
|
||||
})
|
||||
for (const m of allMembers) {
|
||||
await ensureLunchPickForAttendingMember(tx, m.id)
|
||||
}
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
@@ -505,6 +513,13 @@ export const finalistRouter = router({
|
||||
data: created.map((m) => ({ attendingMemberId: m.id, status: 'REQUESTED' })),
|
||||
})
|
||||
}
|
||||
const allMembers = await tx.attendingMember.findMany({
|
||||
where: { confirmationId: confirmation.id, userId: { in: input.attendingUserIds } },
|
||||
select: { id: true },
|
||||
})
|
||||
for (const m of allMembers) {
|
||||
await ensureLunchPickForAttendingMember(tx, m.id)
|
||||
}
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
@@ -1071,6 +1086,13 @@ export const finalistRouter = router({
|
||||
data: created.map((m) => ({ attendingMemberId: m.id, status: 'REQUESTED' })),
|
||||
})
|
||||
}
|
||||
const newMembers = await tx.attendingMember.findMany({
|
||||
where: { confirmationId: confirmation.id, userId: { in: toCreate } },
|
||||
select: { id: true },
|
||||
})
|
||||
for (const m of newMembers) {
|
||||
await ensureLunchPickForAttendingMember(tx, m.id)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
33
src/server/services/lunch-pick-sync.ts
Normal file
33
src/server/services/lunch-pick-sync.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import type { Prisma, PrismaClient } from '@prisma/client'
|
||||
|
||||
type PrismaLike = PrismaClient | Prisma.TransactionClient
|
||||
|
||||
/**
|
||||
* Ensure a MemberLunchPick row exists for the given AttendingMember.
|
||||
* No-ops when the parent program has no LunchEvent.
|
||||
* Idempotent — safe to call repeatedly.
|
||||
*/
|
||||
export async function ensureLunchPickForAttendingMember(
|
||||
prisma: PrismaLike,
|
||||
attendingMemberId: string,
|
||||
): Promise<void> {
|
||||
const member = await prisma.attendingMember.findUnique({
|
||||
where: { id: attendingMemberId },
|
||||
select: {
|
||||
id: true,
|
||||
confirmation: { select: { project: { select: { programId: true } } } },
|
||||
lunchPick: { select: { id: true } },
|
||||
},
|
||||
})
|
||||
if (!member) return
|
||||
if (member.lunchPick) return
|
||||
const programId = member.confirmation.project.programId
|
||||
const lunchEvent = await prisma.lunchEvent.findUnique({
|
||||
where: { programId },
|
||||
select: { id: true },
|
||||
})
|
||||
if (!lunchEvent) return
|
||||
await prisma.memberLunchPick.create({
|
||||
data: { attendingMemberId: member.id },
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user