diff --git a/app/backend/src/comment/comment.module.ts b/app/backend/src/comment/comment.module.ts index 2658e90d..7158c621 100644 --- a/app/backend/src/comment/comment.module.ts +++ b/app/backend/src/comment/comment.module.ts @@ -21,6 +21,8 @@ import { GoogleGenerativeAIEmbeddings } from '@langchain/google-genai'; import { RankingService } from '../ranking/ranking.service'; import { Ranking } from '../ranking/entities/ranking.entity'; import { Vote } from '../vote/entities/vote.entity'; +import { VoteService } from '../vote/vote.service'; +import { OptionService } from '../option/option.service'; @Module({ imports: [ @@ -41,7 +43,9 @@ import { Vote } from '../vote/entities/vote.entity'; BadgeService, Pinecone, GoogleGenerativeAIEmbeddings, - RankingService + RankingService, + VoteService, + OptionService ], }) export class CommentModule {} diff --git a/app/backend/src/like/like.module.ts b/app/backend/src/like/like.module.ts index 3ab08946..e20bbde3 100644 --- a/app/backend/src/like/like.module.ts +++ b/app/backend/src/like/like.module.ts @@ -20,6 +20,8 @@ import { GoogleGenerativeAIEmbeddings } from '@langchain/google-genai'; import { RankingService } from '../ranking/ranking.service'; import { Ranking } from '../ranking/entities/ranking.entity'; import { Vote } from '../vote/entities/vote.entity'; +import { VoteService } from '../vote/vote.service'; +import { OptionService } from '../option/option.service'; @Module({ imports: [ @@ -46,7 +48,9 @@ import { Vote } from '../vote/entities/vote.entity'; BadgeService, Pinecone, GoogleGenerativeAIEmbeddings, - RankingService + RankingService, + VoteService, + OptionService ], }) export class LikeModule {} diff --git a/app/backend/src/poll/poll.module.ts b/app/backend/src/poll/poll.module.ts index c29ca202..12c306e9 100644 --- a/app/backend/src/poll/poll.module.ts +++ b/app/backend/src/poll/poll.module.ts @@ -28,6 +28,8 @@ import { GoogleGenerativeAIEmbeddings } from '@langchain/google-genai'; import { RankingService } from '../ranking/ranking.service'; import { Ranking } from '../ranking/entities/ranking.entity'; import { Vote } from '../vote/entities/vote.entity'; +import { VoteService } from '../vote/vote.service'; +import { OptionService } from '../option/option.service'; @Module({ imports: [ @@ -57,6 +59,8 @@ import { Vote } from '../vote/entities/vote.entity'; Pinecone, GoogleGenerativeAIEmbeddings, RankingService, + VoteService, + OptionService ], exports: [PollService], }) diff --git a/app/backend/src/poll/poll.service.ts b/app/backend/src/poll/poll.service.ts index d791f210..623cb2e6 100644 --- a/app/backend/src/poll/poll.service.ts +++ b/app/backend/src/poll/poll.service.ts @@ -24,6 +24,7 @@ import { GoogleGenerativeAIEmbeddings } from '@langchain/google-genai'; import { TaskType } from '@google/generative-ai'; import { RankingService } from '../ranking/ranking.service'; import { UpdateTagsDto } from './dto/update-tags.dto'; +import { VoteService } from '../vote/vote.service'; @Injectable() export class PollService { @@ -43,6 +44,7 @@ export class PollService { private readonly tagService: TagService, private readonly pinecone: Pinecone, private readonly rankingService: RankingService, + private readonly voteService: VoteService, ) { this.embeddings = new GoogleGenerativeAIEmbeddings({ modelName: 'embedding-001', // 768 dimensions @@ -358,9 +360,6 @@ export class PollService { 'options', 'tags', 'creator', - 'votes', - 'votes.user', - 'votes.option', 'likes', 'likes.user', 'comments', @@ -370,15 +369,21 @@ export class PollService { if (!poll) { throw new NotFoundException('Poll not found'); } + const votedOption = await this.voteService.findOne(pollId,userId); + + let voteDistribution = null; + if(votedOption){ + voteDistribution = await this.voteService.getVoteRate(pollId) + } + + const pollCount = await this.voteService.getVoteCount(pollId); + 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, + votedOption: votedOption, + voteDistribution: voteDistribution, + voteCount: pollCount, likeCount: poll.likes.length, commentCount: poll.comments.length, }; diff --git a/app/backend/src/vote/vote.service.ts b/app/backend/src/vote/vote.service.ts index 44b35e71..fc131e9c 100644 --- a/app/backend/src/vote/vote.service.ts +++ b/app/backend/src/vote/vote.service.ts @@ -57,8 +57,14 @@ export class VoteService { } - findOne(id: number) { - return `This action returns a #${id} vote`; + async findOne(pollID: string,userID: string) { + return await this.voteReposityory.findOne({where:{poll:{id:pollID},user:{id:userID}}}) + } + + async getVoteCount(pollID: string) { + return await this.voteReposityory.createQueryBuilder("vote") + .where("vote.poll.id = :pollID", { pollID: pollID }) + .getCount() }