From 9b054661a1ab294ff389eea6396715d894ffc592 Mon Sep 17 00:00:00 2001 From: BatuhanIlhan Date: Mon, 25 Dec 2023 13:55:56 +0300 Subject: [PATCH 1/2] be/add no auth user to not vtoed boy me endpoint --- app/backend/src/poll/poll.controller.ts | 6 +- app/backend/src/poll/poll.service.ts | 113 +++++++++++++++--------- 2 files changed, 72 insertions(+), 47 deletions(-) diff --git a/app/backend/src/poll/poll.controller.ts b/app/backend/src/poll/poll.controller.ts index e3a442b9..e411025f 100644 --- a/app/backend/src/poll/poll.controller.ts +++ b/app/backend/src/poll/poll.controller.ts @@ -467,7 +467,7 @@ export class PollController { }); } - @UseGuards(AuthGuard, VerificationGuard) + @ApiQuery({ name: 'is_settled', required: true }) @ApiResponse({ status: 200, @@ -484,8 +484,8 @@ export class PollController { @Query('is_settled', ParseIntPipe) is_settled?: number, ): Promise { - const userId = req.user.id; - return await this.pollService.findPollsUserdidNotVote(userId, is_settled); + const userId = req.user?.sub; // Realize that it is not id instead sub. I do not know why but middleware gives this field. + return await this.pollService.findPollsUserdidNotVote(is_settled,userId); } @UseGuards(AuthGuard, VerificationGuard) diff --git a/app/backend/src/poll/poll.service.ts b/app/backend/src/poll/poll.service.ts index 629a72ec..c62ac9e3 100644 --- a/app/backend/src/poll/poll.service.ts +++ b/app/backend/src/poll/poll.service.ts @@ -120,7 +120,7 @@ export class PollService { where: { id: id, creator: { id: user.id } }, relations: ['options'], }); - console.log(poll); + if (!poll) { throw new NotFoundException('Poll not found.'); } @@ -232,7 +232,7 @@ export class PollService { const followings = await this.userService.getUsersFollowedById(followedById); const followingIds = followings.map((obj) => obj.id); - console.log(followingIds); + whereClause.creator = { id: In([...followingIds]), }; @@ -310,57 +310,82 @@ export class PollService { return extendedPolls; } - public async findPollsUserdidNotVote(voterId: string, is_settled: number) { - console.log(is_settled); - const polls = await this.pollRepository.find({ - where: [ - { - approveStatus: true, - is_settled: is_settled, - votes: { - user: { - id: Not(voterId), + public async findPollsUserdidNotVote(is_settled: number,voterId?: string,) { + let polls : Poll[]; + if(voterId){ + polls = await this.pollRepository.find({ + where: [ + { + approveStatus: true, + is_settled: is_settled, + votes: { + user: { + id: Not(voterId), + }, }, }, - }, - { - approveStatus: true, - is_settled: is_settled, - votes: { - user: { - id: IsNull(), + { + approveStatus: true, + is_settled: is_settled, + votes: { + user: { + id: IsNull(), + }, }, }, - }, - ], - relations: [ - 'options', - 'tags', - 'creator', - 'votes', - 'votes.user', - 'votes.option', - 'likes', - 'likes.user', - 'comments', - ], - }); + ], + relations: [ + 'options', + 'tags', + 'creator', + 'likes', + 'likes.user', + 'comments', + ], + }); + }else{ + polls = await this.pollRepository.find({ + where: { + approveStatus: true, + is_settled: is_settled, + }, + relations: [ + 'options', + 'tags', + 'creator', + 'likes', + 'likes.user', + 'comments', + ], + }); + } - const extendedPolls = polls.map((poll) => { + + let extendedPolls = await Promise.all( + polls.map(async (poll) => { return { ...poll, - votedOption: - poll.votes - .filter((vote) => vote.user && vote.user.id == voterId) - .map((vote) => vote.option.id)[0] || null, - didLike: poll.likes.some( - (like) => like.user && like.user.id == voterId, - ), - voteCount: poll.votes.length, + didLike: null, + voteCount: await this.voteService.getVoteCount(poll.id), likeCount: poll.likes.length, commentCount: poll.comments.length, + votedOption:null, + votedDistribution: is_settled ? await this.voteService.getVoteRate(poll.id): null, }; - }); + })); + + + if(voterId){ + extendedPolls = extendedPolls.map((poll) => { + return { + ...poll, + didLike: poll.likes.some( + (like) => like.user && like.user.id == voterId, + ), + }; + }); + } + return extendedPolls.sort((a, b) => b.voteCount - a.voteCount); } @@ -406,7 +431,7 @@ export class PollService { const followings = await this.userService.getUsersFollowedById(followedById); const followingIds = followings.map((obj) => obj.id); - console.log(followingIds); + whereClause.creator = { id: In([...followingIds]), }; From 85c0046f2c1ba7e279d8dc5455037f73c8b80d32 Mon Sep 17 00:00:00 2001 From: BatuhanIlhan Date: Mon, 25 Dec 2023 14:20:34 +0300 Subject: [PATCH 2/2] be/add_module_route --- app/backend/src/poll/poll.module.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/app/backend/src/poll/poll.module.ts b/app/backend/src/poll/poll.module.ts index 12c306e9..51a6abd8 100644 --- a/app/backend/src/poll/poll.module.ts +++ b/app/backend/src/poll/poll.module.ts @@ -71,6 +71,7 @@ export class PollModule implements NestModule { .forRoutes( { path: '/poll', method: RequestMethod.GET }, { path: '/poll/:param', method: RequestMethod.GET }, + { path: '/poll/not-voted-by-me', method: RequestMethod.GET }, ); } }