1- // src/user/user.service.ts
21import { Injectable } from '@nestjs/common' ;
32import { DatabaseService } from '../database/database.service' ;
43import {
@@ -9,7 +8,14 @@ import {
98 UserActionDto ,
109 GetUserVotesDto ,
1110 UserVotesResponseDto ,
11+ SetVoteDto ,
12+ SetVoteResponseDto ,
13+ EditVoteDto ,
14+ EditVoteResponseDto ,
1215} from './user.dto' ;
16+ import { ActionType } from '@prisma/client' ;
17+
18+ const votingPower = 100 ; // Set as a constant for now
1319
1420type UserActionFilters = {
1521 userId : number ;
@@ -70,7 +76,7 @@ export class UserService {
7076 where : filters ,
7177 orderBy : { poll : { endDate : 'desc' } } ,
7278 select : {
73- actionID : true ,
79+ id : true ,
7480 type : true ,
7581 poll : {
7682 select : {
@@ -85,7 +91,7 @@ export class UserService {
8591 } ,
8692 } ) ;
8793 const actions : UserActionDto [ ] = userActions . map ( ( action ) => ( {
88- actionID : action . actionID ,
94+ id : action . id ,
8995 type : action . type . toLowerCase ( ) as 'created' | 'voted' ,
9096 pollId : action . poll . pollId ,
9197 pollTitle : action . poll . title ,
@@ -130,4 +136,81 @@ export class UserService {
130136 weightDistribution : vote . weightDistribution as Record < string , number > ,
131137 } ;
132138 }
139+
140+ async setVote ( dto : SetVoteDto ) : Promise < SetVoteResponseDto > {
141+ const user = await this . databaseService . user . findUnique ( {
142+ where : { worldID : dto . worldID } ,
143+ select : { id : true } ,
144+ } ) ;
145+ if ( ! user ) {
146+ throw new Error ( 'User not found' ) ;
147+ }
148+
149+ const poll = await this . databaseService . poll . findUnique ( {
150+ where : { pollId : dto . pollId } ,
151+ select : { endDate : true , options : true } ,
152+ } ) ;
153+ if ( ! poll || poll . endDate < new Date ( ) ) {
154+ throw new Error ( 'Poll is not active or does not exist' ) ;
155+ }
156+
157+ const vote = await this . databaseService . vote . create ( {
158+ data : {
159+ userId : user . id ,
160+ pollId : dto . pollId ,
161+ votingPower,
162+ weightDistribution : dto . weightDistribution ,
163+ proof : '' , // TODO implement Bandada proof later
164+ } ,
165+ } ) ;
166+ const action = await this . databaseService . userAction . create ( {
167+ data : {
168+ userId : user . id ,
169+ pollId : dto . pollId ,
170+ type : ActionType . VOTED ,
171+ } ,
172+ } ) ;
173+ return {
174+ voteID : vote . voteID ,
175+ actionId : action . id ,
176+ } ;
177+ }
178+
179+ async editVote ( dto : EditVoteDto ) : Promise < EditVoteResponseDto > {
180+ const vote = await this . databaseService . vote . findUnique ( {
181+ where : { voteID : dto . voteID } ,
182+ select : {
183+ userId : true ,
184+ poll : {
185+ select : { endDate : true } ,
186+ } ,
187+ } ,
188+ } ) ;
189+ if ( ! vote ) {
190+ throw new Error ( 'Vote not found' ) ;
191+ }
192+ if ( vote . poll . endDate < new Date ( ) ) {
193+ throw new Error ( 'Cannot edit vote for an inactive poll' ) ;
194+ }
195+ const updatedVote = await this . databaseService . vote . update ( {
196+ where : { voteID : dto . voteID } ,
197+ data : {
198+ weightDistribution : dto . weightDistribution ,
199+ } ,
200+ } ) ;
201+ const userAction = await this . databaseService . userAction . findFirst ( {
202+ where : {
203+ userId : vote . userId ,
204+ pollId : updatedVote . pollId ,
205+ type : ActionType . VOTED ,
206+ } ,
207+ select : { id : true } ,
208+ } ) ;
209+ if ( ! userAction ) {
210+ throw new Error ( 'User action not found' ) ;
211+ }
212+ return {
213+ actionId : userAction . id ,
214+ } ;
215+ }
133216}
0 commit comments