|
| 1 | +import { ANONYMOUS_USER_ID } from '@sim/auth/principal' |
| 2 | +import { createLogger } from '@sim/logger' |
| 3 | +import { getErrorMessage } from '@sim/utils/errors' |
| 4 | +import type { OrganizationSurfaceResponse } from '@/lib/api/contracts/organization' |
| 5 | +import type { OperationUseCase } from '@/lib/core/application/operation' |
| 6 | +import { |
| 7 | + authorizeOrganizationOperation, |
| 8 | + OrganizationMembershipNotFoundError, |
| 9 | +} from '@/lib/core/application/organization-authorization' |
| 10 | +import { PrincipalKindAuthorizationError } from '@/lib/core/application/workspace-authorization' |
| 11 | +import { organizationOperations } from '@/lib/organizations/application/operations' |
| 12 | +import { |
| 13 | + getOrganizationSurfaceContext, |
| 14 | + resolveOrganizationLanding, |
| 15 | +} from '@/lib/organizations/surface' |
| 16 | +import { resolveOrganizationSurfaceFlags } from '@/lib/organizations/surface-flags' |
| 17 | +import { canBrowsePeople } from '@/lib/people/feature-flag' |
| 18 | +import { isPlatformAdmin } from '@/lib/permissions/super-user' |
| 19 | +import { isSimLinksEnabled } from '@/lib/sim-links/feature-flag' |
| 20 | +import { organizationSurfaceSettingsNavigation } from '@/app/o/[organizationId]/settings/navigation' |
| 21 | + |
| 22 | +const logger = createLogger('OrganizationSurfaceRead') |
| 23 | + |
| 24 | +/** |
| 25 | + * The viewer's organization surface, resolved from their membership. A member belongs to one |
| 26 | + * organization, so the caller names none and session active-organization state is not consulted. |
| 27 | + * Having no membership reads the same as not being a member of a named organization. |
| 28 | + */ |
| 29 | +export const readOrganizationSurface: OperationUseCase< |
| 30 | + typeof organizationOperations.readSurface, |
| 31 | + Record<string, never>, |
| 32 | + OrganizationSurfaceResponse |
| 33 | +> = { |
| 34 | + operation: organizationOperations.readSurface, |
| 35 | + async execute({ principal }) { |
| 36 | + if (principal.kind !== 'session') { |
| 37 | + throw new PrincipalKindAuthorizationError( |
| 38 | + principal.kind, |
| 39 | + organizationOperations.readSurface.id |
| 40 | + ) |
| 41 | + } |
| 42 | + const { userId } = principal |
| 43 | + const organizationId = await resolveOrganizationLanding(userId, null) |
| 44 | + if (!organizationId) throw new OrganizationMembershipNotFoundError() |
| 45 | + await authorizeOrganizationOperation(principal, organizationOperations.readSurface, { |
| 46 | + organizationId, |
| 47 | + }) |
| 48 | + const context = await getOrganizationSurfaceContext(organizationId, userId) |
| 49 | + if (!context) throw new OrganizationMembershipNotFoundError() |
| 50 | + |
| 51 | + const [{ flags, benchmarkEnabled }, people, simLinks, platformAdmin] = await Promise.all([ |
| 52 | + resolveOrganizationSurfaceFlags(organizationId, userId), |
| 53 | + canBrowsePeople(organizationId, context.viewer.role), |
| 54 | + isSimLinksEnabled(organizationId), |
| 55 | + /** Decides which links are drawn; the pages behind them authorize for themselves. */ |
| 56 | + isPlatformAdmin(userId).catch((error: unknown) => { |
| 57 | + logger.warn('Could not resolve platform administration navigation', { |
| 58 | + error: getErrorMessage(error), |
| 59 | + }) |
| 60 | + return false |
| 61 | + }), |
| 62 | + ]) |
| 63 | + |
| 64 | + return { |
| 65 | + organization: context.organization, |
| 66 | + viewer: context.viewer, |
| 67 | + mothershipAvailable: context.mothershipAvailable, |
| 68 | + canBuild: context.canBuild, |
| 69 | + searchAccess: context.searchAccess, |
| 70 | + connectedAccountsAvailable: context.connectedAccountsAvailable, |
| 71 | + settingsFeatures: context.settingsFeatures, |
| 72 | + deployment: context.deployment, |
| 73 | + navFlags: { |
| 74 | + people, |
| 75 | + simLinks, |
| 76 | + benchmark: benchmarkEnabled, |
| 77 | + platformAdmin, |
| 78 | + anonymous: userId === ANONYMOUS_USER_ID, |
| 79 | + }, |
| 80 | + featureFlags: flags, |
| 81 | + settingsSections: organizationSurfaceSettingsNavigation( |
| 82 | + context.viewer.isAdmin, |
| 83 | + context.settingsFeatures, |
| 84 | + { |
| 85 | + connectedAccounts: context.connectedAccountsAvailable, |
| 86 | + search: context.searchAccess.memberScoped, |
| 87 | + knowledgeGraphs: flags['mothership-memory-spaces'], |
| 88 | + }, |
| 89 | + platformAdmin |
| 90 | + ).map((item) => item.id), |
| 91 | + } |
| 92 | + }, |
| 93 | +} |
0 commit comments