Skip to content

Commit

Permalink
Merge pull request #744 from bounswe/be/hot-fix-2
Browse files Browse the repository at this point in the history
Extend poll functionalities
  • Loading branch information
BatuhanIlhan authored Dec 24, 2023
2 parents 7acf2ef + 3b9850d commit 837079b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
16 changes: 16 additions & 0 deletions app/backend/src/poll/poll.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export class PollController {
@ApiQuery({ name: 'likedById', required: false })
@ApiQuery({ name: 'votedById', required: false })
@ApiQuery({ name: 'followedById', required: false })
@ApiQuery({ name: 'is_settled', required: false })
@ApiQuery({ name: 'sort', required: false })
@ApiQuery({ name: 'tags', required: false })
@ApiResponse({
Expand All @@ -176,6 +177,8 @@ export class PollController {
votedById?: string,
@Query('followedById', new ParseUUIDPipe({ optional: true }))
followedById?: string,
@Query('is_settled', new ParseIntPipe({ optional: true }))
is_settled?: string,
@Query('sort')
sortString?: string,
@Query('tags', new ParseArrayPipe({ optional: true }))
Expand All @@ -190,6 +193,7 @@ export class PollController {
followedById,
sortString,
tags,
is_settled,
userId,
});
}
Expand All @@ -199,6 +203,7 @@ export class PollController {
@ApiQuery({ name: 'creatorId', required: false })
@ApiQuery({ name: 'likedById', required: false })
@ApiQuery({ name: 'votedById', required: false })
@ApiQuery({ name: 'is_settled', required: false })
@ApiQuery({ name: 'followedById', required: false })
@ApiQuery({ name: 'sort', required: false })
@ApiQuery({ name: 'tags', required: false })
Expand Down Expand Up @@ -226,6 +231,8 @@ export class PollController {
votedById?: string,
@Query('followedById', new ParseUUIDPipe({ optional: true }))
followedById?: string,
@Query('is_settled', new ParseIntPipe({ optional: true }))
is_settled?: string,
@Query('sort')
sortString?: string,
@Query('tags', new ParseArrayPipe({ optional: true }))
Expand All @@ -240,6 +247,7 @@ export class PollController {
followedById,
sortString,
tags,
is_settled,
userId,
pageSize,
pageNum,
Expand Down Expand Up @@ -268,6 +276,7 @@ export class PollController {
followedById: null,
sortString: null,
tags: null,
is_settled: 0,
userId: creatorId,
});
}
Expand Down Expand Up @@ -301,6 +310,7 @@ export class PollController {
followedById: null,
sortString: null,
tags: null,
is_settled: 0,
userId: creatorId,
pageSize,
pageNum,
Expand Down Expand Up @@ -353,6 +363,7 @@ export class PollController {
votedById: null,
followedById: null,
sortString: null,
is_settled: 0,
tags: null,
userId,
});
Expand Down Expand Up @@ -387,6 +398,7 @@ export class PollController {
followedById: null,
sortString: null,
tags: null,
is_settled: 0,
userId,
pageSize,
pageNum,
Expand All @@ -413,6 +425,7 @@ export class PollController {
votedById: userId,
followedById: null,
sortString: null,
is_settled: 0,
tags: null,
userId,
});
Expand Down Expand Up @@ -447,6 +460,7 @@ export class PollController {
followedById: null,
sortString: null,
tags: null,
is_settled: 0,
userId,
pageSize,
pageNum,
Expand Down Expand Up @@ -517,6 +531,7 @@ export class PollController {
votedById: null,
followedById: userId,
sortString: null,
is_settled: 0,
tags: null,
userId,
});
Expand Down Expand Up @@ -551,6 +566,7 @@ export class PollController {
followedById: userId,
sortString: null,
tags: null,
is_settled: 0,
userId,
pageSize,
pageNum,
Expand Down
40 changes: 28 additions & 12 deletions app/backend/src/poll/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export class PollService {
followedById,
tags,
sortString,
is_settled,
userId,
}) {
const whereClause: any = {};
Expand All @@ -215,6 +216,10 @@ export class PollService {
whereClause.approveStatus = approveStatus;
}

if (is_settled != null) {
whereClause.is_settled = is_settled;
}

if (likedById) {
whereClause.likes = {
user: {
Expand Down Expand Up @@ -267,7 +272,7 @@ export class PollService {
tags.every((tag) => poll.tags.some((tagItem) => tagItem.name === tag)),
);
}

let extendedPolls = await Promise.all(
polls.map(async (poll) => {
return {
Expand All @@ -277,44 +282,49 @@ export class PollService {
voteCount: await this.voteService.getVoteCount(poll.id),
votedOption: null,
didLike: null,
voteDistribution: poll.is_settled === Settle.SETTLED ? await this.voteService.getVoteRate(poll.id) : null
voteDistribution:
poll.is_settled === Settle.SETTLED
? await this.voteService.getVoteRate(poll.id)
: null,
};
})
}),
);

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




return extendedPolls;
}

public async findPollsUserdidNotVote(voterId: string) {
const polls = await this.pollRepository.find({
where: [
{
approveStatus: true,
is_settled: 0,
votes: {
user: {
id: Not(voterId),
},
},
},
{
approveStatus: true,
is_settled: 0,
votes: {
user: {
id: IsNull(),
Expand Down Expand Up @@ -362,6 +372,7 @@ export class PollService {
followedById,
tags,
sortString,
is_settled,
userId,
pageSize,
pageNum,
Expand All @@ -378,6 +389,10 @@ export class PollService {
whereClause.approveStatus = approveStatus;
}

if (is_settled != null) {
whereClause.is_settled = is_settled;
}

if (likedById) {
whereClause.likes = {
user: {
Expand Down Expand Up @@ -541,8 +556,9 @@ export class PollService {
throw new NotFoundException('Poll not found');
}


const votedOption = userId ? (await this.voteService.findOne(poll.id, userId))?.option || null : null;

let voteDistribution = null;
if (votedOption) {
voteDistribution = await this.voteService.getVoteRate(pollId);
Expand Down

0 comments on commit 837079b

Please sign in to comment.