99 lines
3.3 KiB
TypeScript
99 lines
3.3 KiB
TypeScript
import { Injectable } from '@angular/core';
|
||
import { BehaviorSubject, Observable } from 'rxjs';
|
||
|
||
import { MessageSeverity } from '../enums/message-severity.enum';
|
||
import { Answer } from '../enums/answer.enum';
|
||
import { Choice } from '../models/choice.model';
|
||
import { Poll } from '../models/poll.model';
|
||
import { User } from '../models/user.model';
|
||
import { ApiService } from './api.service';
|
||
import { ToastService } from './toast.service';
|
||
|
||
@Injectable({
|
||
providedIn: 'root',
|
||
})
|
||
export class PollService {
|
||
private _poll: BehaviorSubject<Poll | undefined> = new BehaviorSubject<Poll | undefined>(undefined);
|
||
public readonly poll: Observable<Poll | undefined> = this._poll.asObservable();
|
||
|
||
constructor(private apiService: ApiService, private toastService: ToastService) {}
|
||
|
||
public updateCurrentPoll(poll: Poll): void {
|
||
this._poll.next(poll);
|
||
}
|
||
|
||
public async getPollBySlug(slug: string): Promise<void> {
|
||
const poll: Poll | undefined = await this.apiService.getPollBySlug(slug);
|
||
this.updateCurrentPoll(poll);
|
||
}
|
||
|
||
public async saveCurrentPoll(): Promise<void> {
|
||
const pollUrl: string = await this.apiService.createPoll(this._poll.getValue());
|
||
// TODO: Maybe handle the url to update currentPoll according to backend response
|
||
if (pollUrl) {
|
||
this.toastService.display('Le sondage a été enregistré.');
|
||
} else {
|
||
this.toastService.display('Le sondage n’a été correctement enregistré, veuillez ré-essayer.');
|
||
}
|
||
}
|
||
|
||
public saveParticipation(choice: Choice, user: User, response: Answer): void {
|
||
const currentPoll = this._poll.getValue();
|
||
currentPoll.choices.find((c) => c.label === choice.label)?.updateParticipation(user, response);
|
||
this.updateCurrentPoll(currentPoll);
|
||
this.apiService.createParticipation(currentPoll.slug, choice.label, user.pseudo, response);
|
||
this.toastService.display(MessageSeverity.SUCCESS, 'Votre participation au sondage a été enregistrée.');
|
||
}
|
||
|
||
public async deleteAllAnswers(): Promise<void> {
|
||
await this.apiService.deletePollAnswers(this._poll.getValue().slug);
|
||
this.toastService.display(
|
||
MessageSeverity.SUCCESS,
|
||
'Les participations des votants à ce sondage ont été supprimées.'
|
||
);
|
||
}
|
||
|
||
public async addComment(comment: string): Promise<void> {
|
||
await this.apiService.createComment(this._poll.getValue().slug, comment);
|
||
this.toastService.display('Votre commentaire a été enregistré.');
|
||
}
|
||
|
||
public async deleteComments(): Promise<void> {
|
||
await this.apiService.deletePollComments(this._poll.getValue().slug);
|
||
this.toastService.display('Les commentaires de ce sondage ont été supprimés.');
|
||
}
|
||
|
||
public buildAnswersByChoiceLabelByPseudo(poll: Poll): Map<string, Map<string, Answer>> {
|
||
const pseudos: Set<string> = new Set();
|
||
poll.choices.forEach((choice: Choice) => {
|
||
choice.participants.forEach((users: Set<User>) => {
|
||
users.forEach((user: User) => {
|
||
pseudos.add(user.pseudo);
|
||
});
|
||
});
|
||
});
|
||
|
||
const list = new Map<string, Map<string, Answer>>();
|
||
pseudos.forEach((pseudo: string) => {
|
||
list.set(
|
||
pseudo,
|
||
new Map<string, Answer>(
|
||
poll.choices.map((choice: Choice) => {
|
||
return [choice.label, undefined];
|
||
})
|
||
)
|
||
);
|
||
});
|
||
|
||
poll.choices.forEach((choice: Choice) => {
|
||
choice.participants.forEach((users: Set<User>, answer: Answer) => {
|
||
users.forEach((user: User) => {
|
||
list.get(user.pseudo).set(choice.label, answer);
|
||
});
|
||
});
|
||
});
|
||
|
||
return list;
|
||
}
|
||
}
|