import { Injectable } from '@angular/core' ; import { EMPTY , Observable } from 'rxjs' ; import { catchError , take , throttleTime } from 'rxjs/operators' ; import { ApiResponse , ApiService } from '../../../../common/api/api.service' ; import { FormToastService } from '../../../../common/services/form-toast.service' ; import { EmailNotificationGetResponse , PushNotificationGetResponse , PushNotificationGroup , } from './notifications-settings-v3.type' ; /**
 * Service handling the changes of email and push notification settings.
 */ @ Injectable ( { providedIn : 'root' } ) export class NotificationsSettingsV2Service { constructor ( private api , private toast ) { } returns { Observable < PushNotificationGetResponse > } get pushSettings$ ( ) : Observable < PushNotificationGetResponse > { return this . api . get ( 'api/v3/notifications/push/settings' ) . pipe ( take ( 1 ) , catchError ( e => this . handleError ( e ) ) , throttleTime ( 1000 ) ) ; } returns { Observable < EmailNotificationGetResponse > } get emailSettings$ ( ) : Observable < EmailNotificationGetResponse > { return this . api . get ( 'api/v2/settings/emails' ) . pipe ( take ( 1 ) , catchError ( e => this . handleError ( e ) ) , throttleTime ( 1000 ) ) ; } param { PushNotificationGroup } notificationGroup param { boolean } enabled returns { Observable < ApiResponse > } public togglePush ( notificationGroup , enabled = true ) { return this . api . post ( `api/v3/notifications/push/settings/${ notificationGroup }` , { enabled : enabled , } ) . pipe ( take ( 1 ) , catchError ( e => this . handleError ( e ) ) , throttleTime ( 1000 ) ) ; } param payload returns { Observable < ApiResponse > } public toggleEmail ( payload ) { return this . api . post ( 'api/v2/settings/emails' , payload ) . pipe ( take ( 1 ) , catchError ( e => this . handleError ( e ) ) , throttleTime ( 1000 ) ) ; } param e returns { Observable < null > } private handleError ( e ) { console . error ( e ) ; this . toast . error ( e . message ?? 'An error occurred retrieving your settings' ) ; return EMPTY ; } }