Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/poll/Poll.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum PollSortBy {
CREATION_DATE = 'creationDate',
END_DATE = 'endDate',
PARTICIPANT_COUNT = 'participantCount',
CLOSEST_END_DATE = 'closestEndDate',
}

export class CreatePollDto {
Expand Down
198 changes: 105 additions & 93 deletions src/poll/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,55 @@ export class PollService {
return draft
}

private async executeQueryPollWithDualSorting(
filters: Prisma.PollWhereInput,
userId: number,
limit: number,
skip: number,
activeFilters: Prisma.PollWhereInput,
secondaryFilters: Prisma.PollWhereInput,
activeOrderBy: Prisma.PollOrderByWithRelationInput,
secondaryOrderBy: Prisma.PollOrderByWithRelationInput,
) {
const [activePolls, secondaryPolls, total] =
await this.databaseService.$transaction([
this.databaseService.poll.findMany({
where: activeFilters,
include: {
author: true,
votes: {
where: { userId },
select: { voteID: true },
},
},
orderBy: activeOrderBy,
take: Number(limit) + skip,
}),
this.databaseService.poll.findMany({
where: secondaryFilters,
include: {
author: true,
votes: {
where: { userId },
select: { voteID: true },
},
},
orderBy: secondaryOrderBy,
take: Number(limit) + skip,
}),
this.databaseService.poll.count({ where: filters }),
])

const combinedPolls = [...activePolls, ...secondaryPolls]
const paginatedPolls = combinedPolls.slice(skip, skip + Number(limit))
const pollsWithVoteStatus = this.mapPollsWithVoteStatus(paginatedPolls)

return {
polls: pollsWithVoteStatus,
total,
}
}

async getPolls(query: GetPollsDto, worldID: string) {
const {
page = 1,
Expand Down Expand Up @@ -349,105 +398,68 @@ export class PollService {
OR: [{ startDate: { gt: now } }, { endDate: { lte: now } }],
}

const [activePolls, inactivePolls, total] =
await this.databaseService.$transaction([
this.databaseService.poll.findMany({
where: activeFilters,
include: {
author: true,
votes: {
where: { userId },
select: { voteID: true },
},
},
orderBy: { endDate: sortOrder },
take: Number(limit) + skip,
}),
this.databaseService.poll.findMany({
where: inactiveFilters,
include: {
author: true,
votes: {
where: { userId },
select: { voteID: true },
},
},
orderBy: { endDate: sortOrder },
take: Number(limit) + skip,
}),
this.databaseService.poll.count({ where: filters }),
])

const combinedPolls = [...activePolls, ...inactivePolls]

const paginatedPolls = combinedPolls.slice(skip, skip + Number(limit))

const pollsWithVoteStatus = this.mapPollsWithVoteStatus(paginatedPolls)
return await this.executeQueryPollWithDualSorting(
filters,
userId,
limit,
skip,
activeFilters,
inactiveFilters,
{ endDate: sortOrder },
{ endDate: sortOrder },
)
} else if (sortBy === PollSortBy.PARTICIPANT_COUNT) {
// For participantCount: show active polls first (by highest voter count), then ended polls
const activeFilters = {
...filters,
startDate: { lte: now },
endDate: { gt: now },
}

return {
polls: pollsWithVoteStatus,
total,
const endedFilters = {
...filters,
endDate: { lte: now },
}
} else if (sortBy) {
// creationDate or participantCount
if (sortBy === PollSortBy.PARTICIPANT_COUNT) {
// For participantCount: show active polls first (by highest voter count), then ended polls
const activeFilters = {
...filters,
startDate: { lte: now },
endDate: { gt: now },
}

const endedFilters = {
...filters,
endDate: { lte: now },
}
return await this.executeQueryPollWithDualSorting(
filters,
userId,
limit,
skip,
activeFilters,
endedFilters,
{ participantCount: sortOrder },
{ participantCount: sortOrder },
)
} else if (sortBy === PollSortBy.CLOSEST_END_DATE) {
// For closestEndDate: show active polls first (closest to ending), then ended polls
const activeFilters = {
...filters,
startDate: { lte: now },
endDate: { gt: now },
}

const [activePolls, endedPolls, total] =
await this.databaseService.$transaction([
this.databaseService.poll.findMany({
where: activeFilters,
include: {
author: true,
votes: {
where: { userId },
select: { voteID: true },
},
},
orderBy: { participantCount: sortOrder }, // Highest voter count first for active polls
take: Number(limit) + skip,
}),
this.databaseService.poll.findMany({
where: endedFilters,
include: {
author: true,
votes: {
where: { userId },
select: { voteID: true },
},
},
orderBy: { participantCount: sortOrder }, // Highest voter count first for ended polls too
take: Number(limit) + skip,
}),
this.databaseService.poll.count({ where: filters }),
])

const combinedPolls = [...activePolls, ...endedPolls]
const paginatedPolls = combinedPolls.slice(skip, skip + Number(limit))

const pollsWithVoteStatus = this.mapPollsWithVoteStatus(paginatedPolls)

return {
polls: pollsWithVoteStatus,
total,
}
} else {
// creationDate
orderBy.push({
[sortBy]: sortOrder,
})
const endedFilters = {
...filters,
endDate: { lte: now },
}

return await this.executeQueryPollWithDualSorting(
filters,
userId,
limit,
skip,
activeFilters,
endedFilters,
{ endDate: 'asc' }, // Closest to ending first for active polls
{ endDate: 'desc' }, // Most recently ended first for ended polls
)
} else if (sortBy) {
// creationDate or other sort types
orderBy.push({
[sortBy]: sortOrder,
})

const [polls, total] = await this.databaseService.$transaction([
this.databaseService.poll.findMany({
where: filters,
Expand Down
Loading