All files / libs/tenants/users/profile/components/src/lib/roles/user-role-selector useRoleOptions.tsx

0% Statements 0/92
0% Branches 0/1
0% Functions 0/1
0% Lines 0/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                                                                                                                                                                                         
import { without } from 'lodash-es';
import { useMemo } from 'react';
import { useIntl } from 'react-intl';

import { type SelectOption } from '@allshares/studio-design-system';
import { assert } from '@amalia/ext/typescript';
import { type AdminScope } from '@amalia/kernel/auth/types';
import { useAdminScopes } from '@amalia/tenants/companies/admin-scopes/state';
import { useFeatureFlag } from '@amalia/tenants/companies/state';
import { CompanyFeatureFlags } from '@amalia/tenants/companies/types';
import { UserRole } from '@amalia/tenants/users/types';

import { UserRoleLabels } from '../user-role/roles.messages';
import { UserRoleBadge } from '../user-role/UserRoleBadge';

export type RoleOption = SelectOption<string>;

export const toRoleOptionValue = (role: UserRole, adminScopeId?: AdminScope['id'] | null): RoleOption['value'] => {
  if (role === UserRole.SCOPED_ADMIN) {
    assert(adminScopeId, 'adminScopeId must be defined when role is scoped admin');
  }

  return JSON.stringify({ role, adminScopeId: role === UserRole.SCOPED_ADMIN ? adminScopeId : null });
};

export const fromRoleOptionValue = (value: string): { role: UserRole; adminScopeId: AdminScope['id'] | null } =>
  JSON.parse(value) as { role: UserRole; adminScopeId: AdminScope['id'] | null };

export const useRoleOptions = (): RoleOption[] => {
  const { formatMessage } = useIntl();
  const { isFeatureEnabled: isFinanceUserRoleFeatureEnabled } = useFeatureFlag(CompanyFeatureFlags.FINANCE_USER_ROLE);

  const { isFeatureEnabled: isAdminScopesFeatureEnabled } = useFeatureFlag(CompanyFeatureFlags.ADMIN_SCOPES);

  const { data: adminScopes } = useAdminScopes({ enabled: !!isAdminScopesFeatureEnabled });

  return useMemo(
    () => [
      {
        label: isAdminScopesFeatureEnabled
          ? // If we have scopes activated, make the global admin explicit.
            formatMessage({ defaultMessage: 'Admin (full access)' })
          : formatMessage(UserRoleLabels[UserRole.ADMIN]),
        value: toRoleOptionValue(UserRole.ADMIN, null),
        valueLabel: (
          <UserRoleBadge
            adminScopeId={null}
            role={UserRole.ADMIN}
            size="small"
          />
        ),
      },
      ...(adminScopes ?? []).map((adminScope) => ({
        label: formatMessage({ defaultMessage: 'Admin ({scopeName})' }, { scopeName: adminScope.name }),
        value: toRoleOptionValue(UserRole.SCOPED_ADMIN, adminScope.id),
        valueLabel: (
          <UserRoleBadge
            adminScopeId={adminScope.id}
            role={UserRole.SCOPED_ADMIN}
            size="small"
          />
        ),
      })),
      ...without(
        Object.values(UserRole),
        // eslint-disable-next-line unicorn/no-useless-spread
        ...[
          // Admin global is above scopes.
          UserRole.ADMIN,
          // Scopes roles are not set like this, it has to come with a scope id.
          UserRole.SCOPED_ADMIN,
          UserRole.SCOPED_READ_ONLY_ADMIN,
          UserRole.READ_ONLY_EMPLOYEE,
          !isFinanceUserRoleFeatureEnabled && UserRole.FINANCE,
        ],
      )
        .filter(Boolean)
        .map((role) => ({
          label: formatMessage(UserRoleLabels[role]),
          value: toRoleOptionValue(role),
          valueLabel: (
            <UserRoleBadge
              adminScopeId={null}
              role={role}
              size="small"
            />
          ),
        })),
    ],
    [isFinanceUserRoleFeatureEnabled, adminScopes, formatMessage, isAdminScopesFeatureEnabled],
  );
};