import { Injectable } from '@angular/core'; import { Actions, createEffect, ofType } from '@ngrx/effects'; import { filter, switchMap, map, catchError, mergeMap } from 'rxjs/operators'; import * as companyActions from './company.actions'; import * as userActions from './user.actions'; import { of, Observable } from 'rxjs'; import { MwCompanyApiService } from '../services/company-api.service'; import { CompanySettingsApiModel } from '../models/company.model'; import { TypedAction } from '@ngrx/store/src/models'; @Injectable() export class CompanyEffects { requestCompanySettings$ = createEffect(() => this.actions$.pipe( ofType(companyActions.requestCompanySettings), filter((action) => action.companyId > 0), switchMap((action) => this.getCompanySettings(action.companyId)) ) ); requestFeatureFlag$ = createEffect(() => this.actions$.pipe( ofType(companyActions.requestFeatureFlagState), mergeMap((action) => this.getFeatureFlagState(action.key, action.companyId) ) ) ); requestPreferences$ = createEffect(() => this.actions$.pipe( ofType(companyActions.requestPreference), switchMap((action) => this.companyApiService.getPreferences(action.companyId).pipe( map((data) => companyActions.preferenceReceived({ data })), catchError((err) => of(companyActions.preferenceReceiveFailed({ err })) ) ) ) ) ); setActiveCompany$ = createEffect(() => this.actions$.pipe( ofType(userActions.setActiveCompany), switchMap((action) => { const companyId = action.company?.companyId || 0; return [ companyActions.requestCompanySettings({ companyId, }), companyActions.requestPreference({ companyId, }), ]; }) ) ); clearPreference$ = createEffect(() => this.actions$.pipe( ofType(companyActions.clearPreference), switchMap(() => this.companyApiService.clearAllPreference().pipe( map(() => companyActions.preferenceCleared()), catchError((err) => of(companyActions.preferenceClearFailed({ err }))) ) ) ) ); savePreference$ = createEffect(() => this.actions$.pipe( ofType(companyActions.savePreference), switchMap((action) => this.companyApiService.savePreference(action.data).pipe( map(() => companyActions.preferenceSaved({ data: action.data })), catchError((err) => of(companyActions.preferenceSaveFailed({ err }))) ) ) ) ); constructor( private actions$: Actions, private companyApiService: MwCompanyApiService ) {} private getCompanySettings( companyId: number ): Observable< | ({ settings: CompanySettingsApiModel } & TypedAction) | ({ err: any } & TypedAction) > { return this.companyApiService.getSettings(companyId).pipe( map((result) => companyActions.companySettingsReceived({ settings: result }) ), catchError((err) => of(companyActions.companySettingsReceiveFailed({ err })) ) ); } private getFeatureFlagState( key: string, companyId: number ): Observable< | ({ key: string; isEnabled: boolean } & TypedAction) | ({ err: any } & TypedAction) > { return this.companyApiService.hasFeatureFlag(key, companyId).pipe( map((result) => companyActions.featureFlagReceived({ key, isEnabled: !!result, }) ), catchError((err) => of(companyActions.featureFlagReceiveFailed({ err }))) ); } }