diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index 614319b..bb5d869 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -32,8 +32,9 @@ export class UserController { @Get('getUserActivities') async getUserActivities( @Query() query: GetUserActivitiesDto, + @User('worldID') worldID: string, ): Promise { - return await this.userService.getUserActivities(query) + return await this.userService.getUserActivities(query, worldID) } @Get('getUserVotes') diff --git a/src/user/user.dto.ts b/src/user/user.dto.ts index f1f5982..066baad 100644 --- a/src/user/user.dto.ts +++ b/src/user/user.dto.ts @@ -109,6 +109,9 @@ export class UserActionDto { @IsDateString() createdAt: string + + @IsBoolean() + hasVoted: boolean } export class UserActivitiesResponseDto { diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 82b7339..53aa309 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -115,6 +115,7 @@ export class UserService { async getUserActivities( dto: GetUserActivitiesDto, + requestingUserWorldID: string, ): Promise { const user = await this.databaseService.user.findUnique({ where: { worldID: dto.worldID }, @@ -125,6 +126,16 @@ export class UserService { throw new UserNotFoundException() } + // Get the requesting user's ID for hasVoted check + const requestingUser = await this.databaseService.user.findUnique({ + where: { worldID: requestingUserWorldID }, + select: { id: true }, + }) + + if (!requestingUser) { + throw new UserNotFoundException() + } + const filters: Prisma.UserActionWhereInput = { userId: user.id, poll: { status: PollStatus.PUBLISHED }, @@ -202,6 +213,21 @@ export class UserService { }, }) + // Get all poll IDs to check for votes by the requesting user + const pollIdsToCheck = userActions.map(action => action.poll.pollId) + + // Get votes by the requesting user for these polls + const requestingUserVotes = await this.databaseService.vote.findMany({ + where: { + userId: requestingUser.id, + pollId: { in: pollIdsToCheck }, + }, + select: { pollId: true }, + }) + + // Create a set of poll IDs that the requesting user has voted on + const votedPollIds = new Set(requestingUserVotes.map(vote => vote.pollId)) + const actions: UserActionDto[] = await Promise.all( // TODO: it's a temporary work around, should add authorWorldId to UserAction later userActions.map(async action => { @@ -223,6 +249,7 @@ export class UserService { authorName: author?.name || '', authorProfilePic: author?.profilePicture || null, createdAt: action.createdAt.toISOString(), + hasVoted: votedPollIds.has(action.poll.pollId), } }), )