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';
|
2020-04-19 14:22:10 +02:00
|
|
|
import { User } from '../models/user.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 {
|
2020-06-12 19:17:39 +02:00
|
|
|
public anonymous: User = new User('', '', [], UserRole.ANONYMOUS);
|
2020-05-05 18:17:12 +02:00
|
|
|
|
2020-05-01 19:10:17 +02:00
|
|
|
private _user: BehaviorSubject<User> = new BehaviorSubject<User>(this.anonymous);
|
|
|
|
public readonly user: Observable<User> = this._user.asObservable();
|
|
|
|
|
2020-06-16 18:40:48 +02:00
|
|
|
constructor(private apiService: ApiService) {}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
2020-04-21 17:26:25 +02:00
|
|
|
public updateUser(user: User): void {
|
|
|
|
this._user.next(user);
|
|
|
|
}
|
2020-05-01 19:10:17 +02:00
|
|
|
|
2020-06-25 22:42:26 +02:00
|
|
|
public getCurrentUser(): User {
|
|
|
|
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> {
|
|
|
|
const currentUser: User = this._user.getValue();
|
2020-05-12 19:16:23 +02:00
|
|
|
currentUser.polls = await this.apiService.getPollsUrlsByUserEmail(currentUser.email);
|
2020-05-01 19:10:17 +02:00
|
|
|
this.updateUser(currentUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async sendEmailToUserTheListOfItsPolls(): Promise<void> {
|
2020-05-12 19:16:23 +02:00
|
|
|
await this.apiService.sendEmailToUserOfItsPollsList(this._user.getValue().email);
|
2020-05-01 19:10:17 +02:00
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
}
|