Skip to content

Commit

Permalink
Merge pull request #152 from Morphclue/feat/show-results-after-deadline
Browse files Browse the repository at this point in the history
Show results after deadline
  • Loading branch information
Morphclue authored Apr 20, 2024
2 parents 53859ca + db25aca commit 15f5a9d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 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 @@ -259,8 +259,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 @@ -271,6 +270,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
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ export class ChooseEventsComponent implements OnInit {
case ShowResultOptions.AFTER_PARTICIPATING:
this.hiddenReason = this.isAdmin || this.userVoted() ? undefined : 'This is a blind poll. You can\'t see results or other user\'s votes until you participate yourself.';
break;
case ShowResultOptions.AFTER_DEADLINE: {
const deadline = this.poll.settings.deadline;
if (this.isAdmin || !deadline || new Date(deadline) < new Date()) {
this.hiddenReason = undefined;
} else {
this.hiddenReason = 'The results of this poll are hidden until the deadline is over. You can only see your own votes.';
}
break;
}
default:
this.hiddenReason = undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ <h3>{{ preset.title }}</h3>
</label>
<input class="form-check-input" type="checkbox" id="anonymous" formControlName="anonymous">
</div>
<div class="form-group form-check" formGroupName="showResultGroup">
<div class="mt-3" formGroupName="showResultGroup">
<label>Show results to participants</label>
<div class="form-check">
<input class="form-check-input"
Expand All @@ -184,6 +184,17 @@ <h3>{{ preset.title }}</h3>
ngbTooltip="Show results to participants after submiting their vote."></i>
</label>
</div>
<div class="form-check">
<input class="form-check-input"
type="radio" id="showAfterDeadline"
[value]="ShowResultOptions.AFTER_DEADLINE"
formControlName="showResult">
<label class="form-check-label" for="showAfterDeadline">
After deadline
<i class="bi-question-circle text-muted" placement="top"
ngbTooltip="Show results to participants after the deadline is over."></i>
</label>
</div>
<div class="form-check">
<input class="form-check-input"
type="radio"
Expand Down
1 change: 1 addition & 0 deletions libs/types/src/lib/schema/show-result-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum ShowResultOptions {
IMMEDIATELY = 'immediately',
AFTER_PARTICIPATING = 'after_participating',
AFTER_DEADLINE = 'after_deadline',
NEVER = 'never',
}

0 comments on commit 15f5a9d

Please sign in to comment.