Skip to content

Commit

Permalink
feat(backend): Implement "Show results after deadline" option
Browse files Browse the repository at this point in the history
  • Loading branch information
Clashsoft committed Apr 20, 2024
1 parent 8c5b915 commit db25aca
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions apps/backend/src/poll/poll/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,7 @@ export class PollService implements OnModuleInit {
token,
}).exec();

if (poll.adminToken === token || poll.settings.showResult === ShowResultOptions.IMMEDIATELY ||
poll.settings.showResult === ShowResultOptions.AFTER_PARTICIPATING && currentParticipant.length > 0) {
if (this.canViewResults(poll, token, currentParticipant.length > 0)) {
const participants = await this.participantModel.find({
poll: new Types.ObjectId(id),
token: {$ne: token},
Expand All @@ -270,6 +269,22 @@ export class PollService implements OnModuleInit {
return currentParticipant;
}

private canViewResults(poll: Poll, token: string, currentParticipant: boolean) {
if (poll.adminToken === token) {
return true;
}
switch (poll.settings.showResult) {
case ShowResultOptions.IMMEDIATELY:
return true;
case ShowResultOptions.AFTER_PARTICIPATING:
return currentParticipant;
case ShowResultOptions.AFTER_DEADLINE:
return !poll.settings.deadline || +poll.settings.deadline < Date.now();
case ShowResultOptions.NEVER:
return false;
}
}

async findAllParticipants(poll: Types.ObjectId): Promise<Participant[]> {
return this.participantModel.find({poll}).exec();
}
Expand Down

0 comments on commit db25aca

Please sign in to comment.