🐛 Filter deleted options from votes (#1619)

This commit is contained in:
Luke Vella 2025-03-07 09:40:53 +00:00 committed by GitHub
parent 8f7aadb685
commit ebe265ddc3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -173,8 +173,23 @@ export const participants = router({
},
});
const options = await prisma.option.findMany({
where: {
pollId,
},
select: {
id: true,
},
});
const existingOptionIds = new Set(options.map((option) => option.id));
const validVotes = votes.filter(({ optionId }) =>
existingOptionIds.has(optionId),
);
await prisma.vote.createMany({
data: votes.map(({ optionId, type }) => ({
data: validVotes.map(({ optionId, type }) => ({
optionId,
type,
pollId,
@ -252,9 +267,24 @@ export const participants = router({
},
});
const options = await prisma.option.findMany({
where: {
pollId,
},
select: {
id: true,
},
});
const existingOptionIds = new Set(options.map((option) => option.id));
const validVotes = votes.filter(({ optionId }) =>
existingOptionIds.has(optionId),
);
// Create new votes
await tx.vote.createMany({
data: votes.map(({ optionId, type }) => ({
data: validVotes.map(({ optionId, type }) => ({
optionId,
type,
pollId,