mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-01 15:39:03 +02:00
🐛 Fix update participant votes (#1481)
This commit is contained in:
parent
2b9000499d
commit
d9b6a42f9a
1 changed files with 27 additions and 20 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue