From 04dcaab58fee5033bd495ee8c7e442b916ac4405 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Tue, 27 Feb 2024 13:17:22 +0100 Subject: [PATCH] fix(admin-ui): Fix alerts service registration This got broken when refactoring to use the new `PermissionsService` due to a race condition. --- .../src/providers/alerts/alerts.service.ts | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/admin-ui/src/lib/core/src/providers/alerts/alerts.service.ts b/packages/admin-ui/src/lib/core/src/providers/alerts/alerts.service.ts index d090f2ea5c..8778494319 100644 --- a/packages/admin-ui/src/lib/core/src/providers/alerts/alerts.service.ts +++ b/packages/admin-ui/src/lib/core/src/providers/alerts/alerts.service.ts @@ -1,7 +1,17 @@ import { Injectable } from '@angular/core'; import { notNullOrUndefined } from '@vendure/common/lib/shared-utils'; -import { BehaviorSubject, combineLatest, interval, isObservable, Observable, Subject, switchMap } from 'rxjs'; -import { map, startWith, take } from 'rxjs/operators'; +import { + BehaviorSubject, + combineLatest, + first, + interval, + isObservable, + Observable, + of, + Subject, + switchMap, +} from 'rxjs'; +import { filter, map, startWith, take } from 'rxjs/operators'; import { Permission } from '../../common/generated-types'; import { PermissionsService } from '../permissions/permissions.service'; @@ -94,17 +104,24 @@ export class AlertsService { } configureAlert(config: AlertConfig) { - if (this.hasSufficientPermissions(config.requiredPermissions)) { - this.alertsMap.set(config.id, new Alert(config)); - this.configUpdated.next(); - } + this.hasSufficientPermissions(config.requiredPermissions) + .pipe(first()) + .subscribe(hasPermissions => { + if (hasPermissions) { + this.alertsMap.set(config.id, new Alert(config)); + this.configUpdated.next(); + } + }); } hasSufficientPermissions(permissions?: Permission[]) { if (!permissions || permissions.length === 0) { - return true; + return of(true); } - return this.permissionsService.userHasPermissions(permissions); + return this.permissionsService.currentUserPermissions$.pipe( + filter(permissions => permissions.length > 0), + map(() => this.permissionsService.userHasPermissions(permissions)), + ); } refresh(id?: string) {