diff --git a/libs/common/src/platform/abstractions/environment.service.ts b/libs/common/src/platform/abstractions/environment.service.ts index 0293d68903..8d32fc4231 100644 --- a/libs/common/src/platform/abstractions/environment.service.ts +++ b/libs/common/src/platform/abstractions/environment.service.ts @@ -128,5 +128,10 @@ export abstract class EnvironmentService { /** * Get the environment from state. Useful if you need to get the environment for another user. */ + abstract getEnvironment$(userId?: string): Observable; + + /** + * @deprecated Use {@link getEnvironment$} instead. + */ abstract getEnvironment(userId?: string): Promise; } diff --git a/libs/common/src/platform/services/default-environment.service.spec.ts b/libs/common/src/platform/services/default-environment.service.spec.ts index 7d266e93fc..870f887c16 100644 --- a/libs/common/src/platform/services/default-environment.service.spec.ts +++ b/libs/common/src/platform/services/default-environment.service.spec.ts @@ -314,7 +314,7 @@ describe("EnvironmentService", () => { await switchUser(testUser); - const env = await sut.getEnvironment(); + const env = await firstValueFrom(sut.getEnvironment$()); expect(env.getHostname()).toBe(expectedHost); }); @@ -325,7 +325,7 @@ describe("EnvironmentService", () => { setGlobalData(region, new EnvironmentUrls()); setUserData(Region.US, new EnvironmentUrls()); - const env = await sut.getEnvironment(); + const env = await firstValueFrom(sut.getEnvironment$()); expect(env.getHostname()).toBe(expectedHost); }); @@ -338,7 +338,7 @@ describe("EnvironmentService", () => { setGlobalData(region, new EnvironmentUrls()); setUserData(Region.US, new EnvironmentUrls()); - const env = await sut.getEnvironment(testUser); + const env = await firstValueFrom(sut.getEnvironment$(testUser)); expect(env.getHostname()).toBe(expectedHost); }, ); @@ -355,7 +355,7 @@ describe("EnvironmentService", () => { await switchUser(testUser); - const env = await sut.getEnvironment(alternateTestUser); + const env = await firstValueFrom(sut.getEnvironment$(alternateTestUser)); expect(env.getHostname()).toBe(expectedHost); }, ); @@ -366,7 +366,7 @@ describe("EnvironmentService", () => { setGlobalData(Region.SelfHosted, globalSelfHostUrls); setUserData(Region.EU, new EnvironmentUrls()); - const env = await sut.getEnvironment(); + const env = await firstValueFrom(sut.getEnvironment$()); expect(env.getHostname()).toBe("base.example.com"); }); @@ -377,7 +377,7 @@ describe("EnvironmentService", () => { setGlobalData(Region.SelfHosted, globalSelfHostUrls); setUserData(Region.EU, new EnvironmentUrls()); - const env = await sut.getEnvironment(); + const env = await firstValueFrom(sut.getEnvironment$()); expect(env.getHostname()).toBe("vault.example.com"); }); @@ -391,7 +391,7 @@ describe("EnvironmentService", () => { await switchUser(testUser); - const env = await sut.getEnvironment(alternateTestUser); + const env = await firstValueFrom(sut.getEnvironment$(alternateTestUser)); expect(env.getHostname()).toBe("base.example.com"); }); }); diff --git a/libs/common/src/platform/services/default-environment.service.ts b/libs/common/src/platform/services/default-environment.service.ts index 69d990ce69..ac3e39b2bb 100644 --- a/libs/common/src/platform/services/default-environment.service.ts +++ b/libs/common/src/platform/services/default-environment.service.ts @@ -271,23 +271,30 @@ export class DefaultEnvironmentService implements EnvironmentService { } } - async getEnvironment(userId?: UserId): Promise { + getEnvironment$(userId?: UserId): Observable { if (userId == null) { - return await firstValueFrom(this.environment$); + return this.environment$; } - const state = await this.getEnvironmentState(userId); - return this.buildEnvironment(state.region, state.urls); + return this.activeAccountId$.pipe( + switchMap((activeUserId) => { + // Previous rules dictated that we only get from user scoped state if there is an active user. + if (activeUserId == null) { + return this.globalState.state$; + } + return this.stateProvider.getUser(userId ?? activeUserId, USER_ENVIRONMENT_KEY).state$; + }), + map((state) => { + return this.buildEnvironment(state?.region, state?.urls); + }), + ); } - private async getEnvironmentState(userId: UserId | null) { - // Previous rules dictated that we only get from user scoped state if there is an active user. - const activeUserId = await firstValueFrom(this.activeAccountId$); - return activeUserId == null - ? await firstValueFrom(this.globalState.state$) - : await firstValueFrom( - this.stateProvider.getUser(userId ?? activeUserId, USER_ENVIRONMENT_KEY).state$, - ); + /** + * @deprecated Use getEnvironment$ instead. + */ + async getEnvironment(userId?: UserId): Promise { + return firstValueFrom(this.getEnvironment$(userId)); } async seedUserEnvironment(userId: UserId) { diff --git a/libs/common/src/platform/services/sdk/default-sdk.service.spec.ts b/libs/common/src/platform/services/sdk/default-sdk.service.spec.ts index 6593095b32..c5917e0230 100644 --- a/libs/common/src/platform/services/sdk/default-sdk.service.spec.ts +++ b/libs/common/src/platform/services/sdk/default-sdk.service.spec.ts @@ -56,6 +56,9 @@ describe("DefaultSdkService", () => { const userId = "user-id" as UserId; beforeEach(() => { + environmentService.getEnvironment$ + .calledWith(userId) + .mockReturnValue(new BehaviorSubject(mock())); accountService.accounts$ = of({ [userId]: { email: "email", emailVerified: true, name: "name" } as AccountInfo, }); diff --git a/libs/common/src/platform/services/sdk/default-sdk.service.ts b/libs/common/src/platform/services/sdk/default-sdk.service.ts index bf593aecf7..e9cecbb15d 100644 --- a/libs/common/src/platform/services/sdk/default-sdk.service.ts +++ b/libs/common/src/platform/services/sdk/default-sdk.service.ts @@ -78,7 +78,7 @@ export class DefaultSdkService implements SdkService { ); const client$ = combineLatest([ - this.environmentService.environment$, + this.environmentService.getEnvironment$(userId), account$, kdfParams$, privateKey$,