import { Injectable } from '@angular/core'; import { Store, select } from '@ngrx/store'; import { filter, map, switchMap, take } from 'rxjs/operators'; import { Observable, of } from 'rxjs'; import { NamingTypeModel } from '../models/naming-type.model'; import * as companyActions from '../@store/company.actions'; import * as companyReducers from '../@store/company.reducer'; import * as userReducers from '../@store/user.reducer'; import { MwAuthService } from './auth.service'; @Injectable() export class MwCompanyService { constructor(private store: Store, private authService: MwAuthService) {} get featureFlagsState$(): Observable<{ [key: string]: boolean; }> { return this.store.pipe( select(companyReducers.areFeatureFlagsReady), filter((areReady) => areReady), switchMap(() => this.store.pipe(select(companyReducers.getFeatureFlags))) ); } get activeCompanyId$(): Observable { return this.authService.activeCompany$.pipe( map((company) => company?.companyId || 0) ); } get instantActiveCompanyId(): number { let companyId = 0; this.activeCompanyId$ .pipe(take(1)) .subscribe((localCompanyId) => (companyId = localCompanyId)); return companyId; } get hasManyCompanies$(): Observable { return this.store.pipe(select(userReducers.hasManyCompanies)); } get namingTypes$(): Observable { return this.store.pipe(select(companyReducers.namingType)); } clearPreference(): void { this.store.dispatch(companyActions.clearPreference()); } requestFeatureFlagState(key: string, companyId: number): void { this.store.dispatch( companyActions.requestFeatureFlagState({ key, companyId, }) ); } getFeatureFlagState(key: string): Observable { return this.store.select(companyReducers.getFeatureFlagState(key)); } }