Skip to content

Commit

Permalink
Merge pull request #694 from bounswe/be-686/add_vote_distrubution_to_…
Browse files Browse the repository at this point in the history
…votedByMe

be-686/ refactor findall in poll service
  • Loading branch information
Alputer authored Dec 23, 2023
2 parents ada951d + 530ce60 commit d81088d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
4 changes: 1 addition & 3 deletions app/backend/src/poll/poll.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ export class PollController {
approveStatus?: string,
@Query('likedById', new ParseUUIDPipe({ optional: true }))
likedById?: string,
@Query('votedById', new ParseUUIDPipe({ optional: true }))
votedById?: string,
@Query('followedById', new ParseUUIDPipe({ optional: true }))
followedById?: string,
@Query('sort')
Expand All @@ -188,7 +186,7 @@ export class PollController {
creatorId,
approveStatus,
likedById,
votedById,
votedById: null,
followedById,
sortString,
tags,
Expand Down
55 changes: 28 additions & 27 deletions app/backend/src/poll/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,6 @@ export class PollService {
'options',
'tags',
'creator',
'votes',
'votes.user',
'votes.option',
'likes',
'likes.user',
'comments',
Expand All @@ -270,36 +267,40 @@ export class PollService {
tags.every((tag) => poll.tags.some((tagItem) => tagItem.name === tag)),
);
}

let extendedPolls = [];
if (!userId) {
extendedPolls = polls.map((poll) => {

let extendedPolls = await Promise.all(
polls.map(async (poll) => {
return {
...poll,
votedOption: null,
didLike: false,
likeCount: poll.likes.length,
commentCount: poll.comments.length,
};
});
} else {
extendedPolls = polls.map((poll) => {
return {
...poll,
votedOption:
poll.votes
.filter((vote) => vote.user && vote.user.id == userId)
.map((vote) => vote.option.id)[0] || null,
didLike: poll.likes.some(
(like) => like.user && like.user.id == userId,
),
voteCount: poll.votes.length,
likeCount: poll.likes.length,
commentCount: poll.comments.length,
vote_count: await this.voteService.getVoteCount(poll.id),
votedOption: null,
didLike: null,
vote_distribution: poll.is_settled === Settle.SETTLED ? await this.voteService.getVoteRate(poll.id) : null
};
});
})
);

if(userId){
extendedPolls = await Promise.all(
extendedPolls.map(async (poll) => {
const votedOption = (await this.voteService.findOne(poll.id, userId))?.option ?? null;
if (!poll.vote_distribution && votedOption) {
poll.vote_distribution = await this.voteService.getVoteRate(poll.id);
}
return {
...poll,
votedOption:votedOption,
didLike: poll.likes.some((like) => like.user?.id === userId),
};
})
);
}




return extendedPolls;
}

Expand Down Expand Up @@ -369,7 +370,7 @@ export class PollService {
if (!poll) {
throw new NotFoundException('Poll not found');
}
const votedOption = await this.voteService.findOne(pollId,userId);
const votedOption = (await this.voteService.findOne(poll.id, userId))?.option || null;

let voteDistribution = null;
if(votedOption){
Expand Down
4 changes: 2 additions & 2 deletions app/backend/src/vote/vote.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export class VoteService {
}

async findOne(pollID: string,userID: string) {
return await this.voteReposityory.findOne({where:{poll:{id:pollID},user:{id:userID}}})
return await this.voteReposityory.findOne({where:{poll:{id:pollID},user:{id:userID}},relations:["option"]})
}

async getVoteCount(pollID: string) {
async getVoteCount(pollID: string): Promise<number> {
return await this.voteReposityory.createQueryBuilder("vote")
.where("vote.poll.id = :pollID", { pollID: pollID })
.getCount()
Expand Down

0 comments on commit d81088d

Please sign in to comment.