Skip to content

Commit 1e7ff46

Browse files
authored
Merge pull request #63 from GeneralMagicio/add-options-check-to-editVote
add options check to editVote
2 parents e836c53 + 0ee2a79 commit 1e7ff46

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/user/user.service.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export class UserService {
231231
select: {
232232
userId: true,
233233
poll: {
234-
select: { endDate: true },
234+
select: { endDate: true, options: true },
235235
},
236236
},
237237
});
@@ -244,6 +244,24 @@ export class UserService {
244244
if (vote.userId !== dto.userId) {
245245
throw new Error('You are not authorized to edit this vote');
246246
}
247+
const dtoWeightKeys = Object.keys(dto.weightDistribution);
248+
const areWeightKeysMatching =
249+
dtoWeightKeys.length === vote.poll.options.length &&
250+
dtoWeightKeys.every((key) => vote.poll.options.includes(key));
251+
if (!areWeightKeysMatching) {
252+
throw new Error(
253+
'Weight distribution keys do not match poll options exactly',
254+
);
255+
}
256+
const totalWeight = Object.values(dto.weightDistribution).reduce(
257+
(acc, weight) => acc + weight,
258+
0,
259+
);
260+
if (totalWeight > votingPower) {
261+
throw new Error(
262+
`Total weight distribution must be equal or lower than the voting power of ${votingPower}`,
263+
);
264+
}
247265
const updatedVote = await this.databaseService.vote.update({
248266
where: { voteID: dto.voteID },
249267
data: {

0 commit comments

Comments
 (0)