Skip to content

Commit c20345a

Browse files
authored
Merge pull request #45 from GeneralMagicio/dont-allow-setVote-when-already-voted
don't allow setVote when already voted
2 parents 135d739 + 792b9d2 commit c20345a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/user/user.service.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,22 @@ export class UserService {
145145
if (!user) {
146146
throw new Error('User not found');
147147
}
148-
149148
const poll = await this.databaseService.poll.findUnique({
150149
where: { pollId: dto.pollId },
151150
select: { endDate: true, options: true },
152151
});
153152
if (!poll || poll.endDate < new Date()) {
154153
throw new Error('Poll is not active or does not exist');
155154
}
156-
155+
const existingVote = await this.databaseService.vote.findFirst({
156+
where: {
157+
pollId: dto.pollId,
158+
userId: user.id,
159+
},
160+
});
161+
if (existingVote) {
162+
throw new Error('User has already voted in this poll');
163+
}
157164
const vote = await this.databaseService.vote.create({
158165
data: {
159166
userId: user.id,

0 commit comments

Comments
 (0)