File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed
Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff 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 : {
You can’t perform that action at this time.
0 commit comments