2020-04-19 14:22:10 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
|
|
|
|
2020-05-05 18:17:12 +02:00
|
|
|
import { UserRole } from '../enums/user-role.enum';
|
2021-04-28 12:01:09 +02:00
|
|
|
import { Owner } from '../models/owner.model';
|
2020-05-01 19:10:17 +02:00
|
|
|
import { ApiService } from './api.service';
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
|
|
@Injectable({
|
2020-04-21 17:26:25 +02:00
|
|
|
providedIn: 'root',
|
2020-04-19 14:22:10 +02:00
|
|
|
})
|
|
|
|
export class UserService {
|
2021-04-28 12:01:09 +02:00
|
|
|
public anonymous: Owner = new Owner('', '', [], UserRole.ANONYMOUS);
|
2020-05-05 18:17:12 +02:00
|
|
|
|
2021-04-28 12:01:09 +02:00
|
|
|
private _user: BehaviorSubject<Owner> = new BehaviorSubject<Owner>(this.anonymous);
|
|
|
|
public readonly user: Observable<Owner> = this._user.asObservable();
|
2020-05-01 19:10:17 +02:00
|
|
|
|
2020-06-16 18:40:48 +02:00
|
|
|
constructor(private apiService: ApiService) {}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
2021-04-28 12:01:09 +02:00
|
|
|
public updateUser(user: Owner): void {
|
2020-04-21 17:26:25 +02:00
|
|
|
this._user.next(user);
|
|
|
|
}
|
2020-05-01 19:10:17 +02:00
|
|
|
|
2021-04-28 12:01:09 +02:00
|
|
|
public getCurrentUser(): Owner {
|
2020-06-25 22:42:26 +02:00
|
|
|
return this._user.getValue();
|
|
|
|
}
|
|
|
|
|
2020-05-05 18:17:12 +02:00
|
|
|
public isCurrentUserIdentifiable(): boolean {
|
2020-06-25 22:42:26 +02:00
|
|
|
return this._user.getValue()?.pseudo ? true : false;
|
2020-05-05 18:17:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 19:10:17 +02:00
|
|
|
public async getUserPolls(): Promise<void> {
|
2021-04-28 12:01:09 +02:00
|
|
|
const currentUser: Owner = this._user.getValue();
|
2021-05-21 13:10:04 +02:00
|
|
|
currentUser.polls = await this.apiService.findMyPollsByEmail(currentUser.email);
|
2020-05-01 19:10:17 +02:00
|
|
|
this.updateUser(currentUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async sendEmailToUserTheListOfItsPolls(): Promise<void> {
|
2021-05-21 13:10:04 +02:00
|
|
|
await this.apiService.findMyPollsByEmail(this._user.getValue().email);
|
2020-05-01 19:10:17 +02:00
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
}
|