feat: auto-create/sync VisaApplication on attendee writes
confirm and adminConfirm now create REQUESTED VisaApplication rows for every attendee with needsVisa=true, in the same Prisma transaction as the AttendingMember inserts. editAttendees was extended into a fully diff-aware sync: existing attendees whose needsVisa flips on get a new VisaApp; flipping off deletes it; staying true preserves the row (and its status / notes / dates). Removed attendees cascade automatically via the FK. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -327,19 +327,31 @@ export const finalistRouter = router({
|
||||
}
|
||||
}
|
||||
|
||||
await ctx.prisma.$transaction([
|
||||
ctx.prisma.finalistConfirmation.update({
|
||||
await ctx.prisma.$transaction(async (tx) => {
|
||||
await tx.finalistConfirmation.update({
|
||||
where: { id: confirmation.id },
|
||||
data: { status: 'CONFIRMED', confirmedAt: new Date() },
|
||||
}),
|
||||
ctx.prisma.attendingMember.createMany({
|
||||
})
|
||||
await tx.attendingMember.createMany({
|
||||
data: input.attendingUserIds.map((userId) => ({
|
||||
confirmationId: confirmation.id,
|
||||
userId,
|
||||
needsVisa: input.visaFlags[userId] ?? false,
|
||||
})),
|
||||
}),
|
||||
])
|
||||
})
|
||||
const visaUsers = input.attendingUserIds.filter(
|
||||
(uid) => input.visaFlags[uid] === true,
|
||||
)
|
||||
if (visaUsers.length > 0) {
|
||||
const created = await tx.attendingMember.findMany({
|
||||
where: { confirmationId: confirmation.id, userId: { in: visaUsers } },
|
||||
select: { id: true },
|
||||
})
|
||||
await tx.visaApplication.createMany({
|
||||
data: created.map((m) => ({ attendingMemberId: m.id, status: 'REQUESTED' })),
|
||||
})
|
||||
}
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
action: 'FINALIST_CONFIRMED',
|
||||
@@ -469,19 +481,31 @@ export const finalistRouter = router({
|
||||
}
|
||||
}
|
||||
|
||||
await ctx.prisma.$transaction([
|
||||
ctx.prisma.finalistConfirmation.update({
|
||||
await ctx.prisma.$transaction(async (tx) => {
|
||||
await tx.finalistConfirmation.update({
|
||||
where: { id: confirmation.id },
|
||||
data: { status: 'CONFIRMED', confirmedAt: new Date() },
|
||||
}),
|
||||
ctx.prisma.attendingMember.createMany({
|
||||
})
|
||||
await tx.attendingMember.createMany({
|
||||
data: input.attendingUserIds.map((userId) => ({
|
||||
confirmationId: confirmation.id,
|
||||
userId,
|
||||
needsVisa: input.visaFlags[userId] ?? false,
|
||||
})),
|
||||
}),
|
||||
])
|
||||
})
|
||||
const visaUsers = input.attendingUserIds.filter(
|
||||
(uid) => input.visaFlags[uid] === true,
|
||||
)
|
||||
if (visaUsers.length > 0) {
|
||||
const created = await tx.attendingMember.findMany({
|
||||
where: { confirmationId: confirmation.id, userId: { in: visaUsers } },
|
||||
select: { id: true },
|
||||
})
|
||||
await tx.visaApplication.createMany({
|
||||
data: created.map((m) => ({ attendingMemberId: m.id, status: 'REQUESTED' })),
|
||||
})
|
||||
}
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
@@ -916,7 +940,14 @@ export const finalistRouter = router({
|
||||
teamMembers: { select: { userId: true, role: true } },
|
||||
},
|
||||
},
|
||||
attendingMembers: { select: { id: true, userId: true } },
|
||||
attendingMembers: {
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
needsVisa: true,
|
||||
visaApplication: { select: { id: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -993,32 +1024,55 @@ export const finalistRouter = router({
|
||||
const toCreate = input.attendingUserIds.filter((id) => !existingByUser.has(id))
|
||||
const toUpdate = input.attendingUserIds.filter((id) => existingByUser.has(id))
|
||||
|
||||
await ctx.prisma.$transaction([
|
||||
...(toDelete.length > 0
|
||||
? [
|
||||
ctx.prisma.attendingMember.deleteMany({
|
||||
where: { id: { in: toDelete.map((m) => m.id) } },
|
||||
}),
|
||||
]
|
||||
: []),
|
||||
...toUpdate.map((userId) =>
|
||||
ctx.prisma.attendingMember.update({
|
||||
where: { id: existingByUser.get(userId)!.id },
|
||||
data: { needsVisa: input.visaFlags[userId] ?? false },
|
||||
}),
|
||||
),
|
||||
...(toCreate.length > 0
|
||||
? [
|
||||
ctx.prisma.attendingMember.createMany({
|
||||
data: toCreate.map((userId) => ({
|
||||
confirmationId: confirmation.id,
|
||||
userId,
|
||||
needsVisa: input.visaFlags[userId] ?? false,
|
||||
})),
|
||||
}),
|
||||
]
|
||||
: []),
|
||||
])
|
||||
await ctx.prisma.$transaction(async (tx) => {
|
||||
if (toDelete.length > 0) {
|
||||
// FK cascade removes any VisaApplication rows tied to deleted attendees
|
||||
await tx.attendingMember.deleteMany({
|
||||
where: { id: { in: toDelete.map((m) => m.id) } },
|
||||
})
|
||||
}
|
||||
|
||||
// Diff visa flips for users that stay
|
||||
const visaToDelete: string[] = []
|
||||
for (const userId of toUpdate) {
|
||||
const existing = existingByUser.get(userId)!
|
||||
const wantsVisa = input.visaFlags[userId] === true
|
||||
await tx.attendingMember.update({
|
||||
where: { id: existing.id },
|
||||
data: { needsVisa: wantsVisa },
|
||||
})
|
||||
if (existing.visaApplication && !wantsVisa) {
|
||||
visaToDelete.push(existing.visaApplication.id)
|
||||
} else if (!existing.visaApplication && wantsVisa) {
|
||||
await tx.visaApplication.create({
|
||||
data: { attendingMemberId: existing.id, status: 'REQUESTED' },
|
||||
})
|
||||
}
|
||||
}
|
||||
if (visaToDelete.length > 0) {
|
||||
await tx.visaApplication.deleteMany({ where: { id: { in: visaToDelete } } })
|
||||
}
|
||||
|
||||
if (toCreate.length > 0) {
|
||||
await tx.attendingMember.createMany({
|
||||
data: toCreate.map((userId) => ({
|
||||
confirmationId: confirmation.id,
|
||||
userId,
|
||||
needsVisa: input.visaFlags[userId] ?? false,
|
||||
})),
|
||||
})
|
||||
const newVisaUsers = toCreate.filter((id) => input.visaFlags[id] === true)
|
||||
if (newVisaUsers.length > 0) {
|
||||
const created = await tx.attendingMember.findMany({
|
||||
where: { confirmationId: confirmation.id, userId: { in: newVisaUsers } },
|
||||
select: { id: true },
|
||||
})
|
||||
await tx.visaApplication.createMany({
|
||||
data: created.map((m) => ({ attendingMemberId: m.id, status: 'REQUESTED' })),
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
|
||||
Reference in New Issue
Block a user