|
| 1 | +// src/user/user.service.ts |
| 2 | +import { Injectable } from '@nestjs/common'; |
| 3 | +import { DatabaseService } from '../database/database.service'; |
| 4 | +import { |
| 5 | + GetUserActivitiesDto, |
| 6 | + GetUserDataDto, |
| 7 | + UserDataResponseDto, |
| 8 | + UserActivitiesResponseDto, |
| 9 | + UserActionDto, |
| 10 | + GetUserVotesDto, |
| 11 | + UserVotesResponseDto, |
| 12 | +} from './user.dto'; |
| 13 | + |
| 14 | +type UserActionFilters = { |
| 15 | + userId: number; |
| 16 | + type?: 'CREATED' | 'VOTED'; |
| 17 | + poll?: { |
| 18 | + endDate?: { |
| 19 | + gte?: Date; |
| 20 | + lt?: Date; |
| 21 | + }; |
| 22 | + }; |
| 23 | +}; |
| 24 | + |
| 25 | +@Injectable() |
| 26 | +export class UserService { |
| 27 | + constructor(private readonly databaseService: DatabaseService) {} |
| 28 | + |
| 29 | + async getUserData(dto: GetUserDataDto): Promise<UserDataResponseDto> { |
| 30 | + const user = await this.databaseService.user.findUnique({ |
| 31 | + where: { worldID: dto.worldID }, |
| 32 | + select: { |
| 33 | + pollsCreatedCount: true, |
| 34 | + pollsParticipatedCount: true, |
| 35 | + worldID: true, |
| 36 | + }, |
| 37 | + }); |
| 38 | + if (!user) { |
| 39 | + throw new Error('User not found'); |
| 40 | + } |
| 41 | + return { |
| 42 | + pollsCreated: user.pollsCreatedCount, |
| 43 | + pollsParticipated: user.pollsParticipatedCount, |
| 44 | + worldID: user.worldID, |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + async getUserActivities( |
| 49 | + dto: GetUserActivitiesDto, |
| 50 | + ): Promise<UserActivitiesResponseDto> { |
| 51 | + const user = await this.databaseService.user.findUnique({ |
| 52 | + where: { worldID: dto.worldID }, |
| 53 | + select: { id: true }, |
| 54 | + }); |
| 55 | + if (!user) { |
| 56 | + throw new Error('User not found'); |
| 57 | + } |
| 58 | + const filters: UserActionFilters = { userId: user.id }; |
| 59 | + const now = new Date(); |
| 60 | + if (dto.filter === 'active') { |
| 61 | + filters.poll = { endDate: { gte: now } }; |
| 62 | + } else if (dto.filter === 'inactive') { |
| 63 | + filters.poll = { endDate: { lt: now } }; |
| 64 | + } else if (dto.filter === 'created') { |
| 65 | + filters.type = 'CREATED'; |
| 66 | + } else if (dto.filter === 'participated') { |
| 67 | + filters.type = 'VOTED'; |
| 68 | + } |
| 69 | + const userActions = await this.databaseService.userAction.findMany({ |
| 70 | + where: filters, |
| 71 | + orderBy: { poll: { endDate: 'desc' } }, |
| 72 | + select: { |
| 73 | + type: true, |
| 74 | + poll: { |
| 75 | + select: { |
| 76 | + pollId: true, |
| 77 | + title: true, |
| 78 | + description: true, |
| 79 | + endDate: true, |
| 80 | + participantCount: true, |
| 81 | + authorUserId: true, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }); |
| 86 | + const actions: UserActionDto[] = userActions.map((action) => ({ |
| 87 | + type: action.type.toLowerCase() as 'created' | 'voted', |
| 88 | + pollId: action.poll.pollId, |
| 89 | + pollTitle: action.poll.title, |
| 90 | + pollDescription: action.poll.description ?? '', |
| 91 | + endDate: action.poll.endDate, |
| 92 | + isActive: action.poll.endDate >= now, |
| 93 | + votersParticipated: action.poll.participantCount, |
| 94 | + authorUserId: action.poll.authorUserId, |
| 95 | + })); |
| 96 | + return { userActions: actions }; |
| 97 | + } |
| 98 | + |
| 99 | + async getUserVotes(dto: GetUserVotesDto): Promise<UserVotesResponseDto> { |
| 100 | + const user = await this.databaseService.user.findUnique({ |
| 101 | + where: { worldID: dto.worldID }, |
| 102 | + select: { id: true }, |
| 103 | + }); |
| 104 | + if (!user) { |
| 105 | + throw new Error('User not found'); |
| 106 | + } |
| 107 | + const poll = await this.databaseService.poll.findUnique({ |
| 108 | + where: { pollId: dto.pollID }, |
| 109 | + select: { endDate: true, options: true }, |
| 110 | + }); |
| 111 | + if (!poll || poll.endDate < new Date()) { |
| 112 | + throw new Error('Poll is not active or does not exist'); |
| 113 | + } |
| 114 | + const votes = await this.databaseService.vote.findMany({ |
| 115 | + where: { |
| 116 | + pollId: dto.pollID, |
| 117 | + userId: user.id, |
| 118 | + }, |
| 119 | + select: { votingPower: true, weightDistribution: true }, |
| 120 | + }); |
| 121 | + if (votes.length === 0) { |
| 122 | + throw new Error('Vote not found for the given poll and user'); |
| 123 | + } |
| 124 | + const vote = votes[0]; |
| 125 | + return { |
| 126 | + options: poll.options, |
| 127 | + votingPower: vote.votingPower, |
| 128 | + weightDistribution: vote.weightDistribution as Record<string, number>, |
| 129 | + }; |
| 130 | + } |
| 131 | +} |
0 commit comments