funky-framadate-front/src/app/core/services/user.service.ts

41 lines
1.2 KiB
TypeScript
Raw Normal View History

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';
import { Owner } from '../models/owner.model';
2020-05-01 19:10:17 +02:00
import { ApiService } from './api.service';
@Injectable({
providedIn: 'root',
})
export class UserService {
public anonymous: Owner = new Owner('', '', [], UserRole.ANONYMOUS);
2020-05-05 18:17:12 +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
constructor(private apiService: ApiService) {}
public updateUser(user: Owner): void {
this._user.next(user);
}
2020-05-01 19:10:17 +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> {
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
}
}