Skip to content

Commit

Permalink
Add getUserId and getOptionalUserId rxjs functions (#11741)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliykat authored Oct 28, 2024
1 parent 11f18c4 commit 3736f68
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions libs/common/src/auth/services/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<UserId, AccountInfo>>;
private activeAccountIdState: GlobalState<UserId | undefined>;
Expand Down

0 comments on commit 3736f68

Please sign in to comment.