Skip to content

Commit

Permalink
Merge pull request #765 from bounswe/be/add_not_voted_by_me_for_nonauth
Browse files Browse the repository at this point in the history
be/add no auth user to not vtoed boy me endpoint
  • Loading branch information
BatuhanIlhan authored Dec 25, 2023
2 parents 1984cd1 + d9f910b commit 2c89a9e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 47 deletions.
6 changes: 3 additions & 3 deletions app/backend/src/poll/poll.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ export class PollController {
});
}

@UseGuards(AuthGuard, VerificationGuard)

@ApiQuery({ name: 'is_settled', required: true })
@ApiResponse({
status: 200,
Expand All @@ -486,8 +486,8 @@ export class PollController {
@Query('is_settled', ParseIntPipe)
is_settled?: number,
): Promise<any> {
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)
Expand Down
1 change: 1 addition & 0 deletions app/backend/src/poll/poll.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
{ path: '/poll/pinecone/search', method: RequestMethod.GET },
);
}
Expand Down
113 changes: 69 additions & 44 deletions app/backend/src/poll/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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]),
};
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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]),
};
Expand Down

0 comments on commit 2c89a9e

Please sign in to comment.