🐛 Fix update participant votes (#1481)

This commit is contained in:
Luke Vella 2025-01-04 19:19:47 +00:00 committed by GitHub
parent 2b9000499d
commit d9b6a42f9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -205,28 +205,35 @@ export const participants = router({
}), }),
) )
.mutation(async ({ input: { pollId, participantId, votes } }) => { .mutation(async ({ input: { pollId, participantId, votes } }) => {
const participant = await prisma.participant.update({ const participant = await prisma.$transaction(async (tx) => {
// Delete existing votes
await tx.vote.deleteMany({
where: { where: {
id: participantId, participantId,
pollId,
}, },
data: { });
votes: {
deleteMany: { // Create new votes
pollId: pollId, await tx.vote.createMany({
},
createMany: {
data: votes.map(({ optionId, type }) => ({ data: votes.map(({ optionId, type }) => ({
optionId, optionId,
type, type,
pollId, pollId,
participantId,
})), })),
}, });
},
// Return updated participant with votes
return tx.participant.findUniqueOrThrow({
where: {
id: participantId,
}, },
include: { include: {
votes: true, votes: true,
}, },
}); });
});
return participant; return participant;
}), }),