From 3736f6854cae341af0e4d09fc11d31d9f423104b Mon Sep 17 00:00:00 2001 From: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Date: Tue, 29 Oct 2024 09:51:03 +1000 Subject: [PATCH] Add getUserId and getOptionalUserId rxjs functions (#11741) --- .../src/auth/services/account.service.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/libs/common/src/auth/services/account.service.ts b/libs/common/src/auth/services/account.service.ts index dceec2cbf1..04a0c62dd9 100644 --- a/libs/common/src/auth/services/account.service.ts +++ b/libs/common/src/auth/services/account.service.ts @@ -45,6 +45,24 @@ const LOGGED_OUT_INFO: AccountInfo = { name: undefined, }; +/** + * An rxjs map operator that extracts the UserId from an account, or throws if the account or UserId are null. + */ +export const getUserId = map<{ id: UserId | undefined }, UserId>((account) => { + if (account?.id == null) { + throw new Error("Null account or account ID"); + } + + return account.id; +}); + +/** + * An rxjs map operator that extracts the UserId from an account, or returns undefined if the account or UserId are null. + */ +export const getOptionalUserId = map<{ id: UserId | undefined }, UserId | undefined>( + (account) => account?.id ?? undefined, +); + export class AccountServiceImplementation implements InternalAccountService { private accountsState: GlobalState>; private activeAccountIdState: GlobalState;