From d9b6a42f9a045ec3eb593224a1dd50cfe4bffb09 Mon Sep 17 00:00:00 2001 From: Luke Vella Date: Sat, 4 Jan 2025 19:19:47 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20update=20participant=20vot?= =?UTF-8?q?es=20(#1481)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/trpc/routers/polls/participants.ts | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/apps/web/src/trpc/routers/polls/participants.ts b/apps/web/src/trpc/routers/polls/participants.ts index ed082687a..1c0ba04b0 100644 --- a/apps/web/src/trpc/routers/polls/participants.ts +++ b/apps/web/src/trpc/routers/polls/participants.ts @@ -205,27 +205,34 @@ export const participants = router({ }), ) .mutation(async ({ input: { pollId, participantId, votes } }) => { - const participant = await prisma.participant.update({ - where: { - id: participantId, - }, - data: { - votes: { - deleteMany: { - pollId: pollId, - }, - createMany: { - data: votes.map(({ optionId, type }) => ({ - optionId, - type, - pollId, - })), - }, + const participant = await prisma.$transaction(async (tx) => { + // Delete existing votes + await tx.vote.deleteMany({ + where: { + participantId, + pollId, }, - }, - include: { - votes: true, - }, + }); + + // Create new votes + await tx.vote.createMany({ + data: votes.map(({ optionId, type }) => ({ + optionId, + type, + pollId, + participantId, + })), + }); + + // Return updated participant with votes + return tx.participant.findUniqueOrThrow({ + where: { + id: participantId, + }, + include: { + votes: true, + }, + }); }); return participant;