@open-mercato/core 0.6.4-develop.4236.1.9fa6806b34 → 0.6.4-develop.4254.1.7a123d970c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/helpers/integration/authFixtures.js +70 -1
  3. package/dist/helpers/integration/authFixtures.js.map +2 -2
  4. package/dist/helpers/integration/dbFixtures.js +98 -0
  5. package/dist/helpers/integration/dbFixtures.js.map +7 -0
  6. package/dist/modules/business_rules/api/execute/route.js +2 -1
  7. package/dist/modules/business_rules/api/execute/route.js.map +2 -2
  8. package/dist/modules/business_rules/api/rules/route.js +10 -0
  9. package/dist/modules/business_rules/api/rules/route.js.map +2 -2
  10. package/dist/modules/business_rules/backend/logs/[id]/page.js +24 -5
  11. package/dist/modules/business_rules/backend/logs/[id]/page.js.map +2 -2
  12. package/dist/modules/business_rules/cli.js +6 -0
  13. package/dist/modules/business_rules/cli.js.map +2 -2
  14. package/dist/modules/business_rules/lib/rule-engine.js +116 -9
  15. package/dist/modules/business_rules/lib/rule-engine.js.map +2 -2
  16. package/dist/modules/business_rules/subscribers/crud-rule-trigger.js +3 -2
  17. package/dist/modules/business_rules/subscribers/crud-rule-trigger.js.map +2 -2
  18. package/dist/modules/catalog/api/offers/route.js +15 -5
  19. package/dist/modules/catalog/api/offers/route.js.map +2 -2
  20. package/dist/modules/catalog/api/products/route.js +21 -4
  21. package/dist/modules/catalog/api/products/route.js.map +2 -2
  22. package/dist/modules/catalog/lib/pricing.js +6 -0
  23. package/dist/modules/catalog/lib/pricing.js.map +2 -2
  24. package/dist/modules/catalog/services/catalogPricingService.js +5 -1
  25. package/dist/modules/catalog/services/catalogPricingService.js.map +2 -2
  26. package/dist/modules/currencies/backend/currencies/[id]/page.js +19 -2
  27. package/dist/modules/currencies/backend/currencies/[id]/page.js.map +2 -2
  28. package/dist/modules/customer_accounts/backend/customer_accounts/roles/[id]/page.js +27 -7
  29. package/dist/modules/customer_accounts/backend/customer_accounts/roles/[id]/page.js.map +2 -2
  30. package/dist/modules/customer_accounts/backend/customer_accounts/users/[id]/page.js +27 -7
  31. package/dist/modules/customer_accounts/backend/customer_accounts/users/[id]/page.js.map +2 -2
  32. package/dist/modules/customers/api/activities/route.js +15 -2
  33. package/dist/modules/customers/api/activities/route.js.map +2 -2
  34. package/dist/modules/customers/api/comments/route.js +15 -3
  35. package/dist/modules/customers/api/comments/route.js.map +2 -2
  36. package/dist/modules/customers/api/companies/[id]/people/route.js +2 -4
  37. package/dist/modules/customers/api/companies/[id]/people/route.js.map +2 -2
  38. package/dist/modules/customers/api/companies/[id]/route.js +2 -4
  39. package/dist/modules/customers/api/companies/[id]/route.js.map +2 -2
  40. package/dist/modules/customers/api/deals/[id]/companies/route.js +2 -4
  41. package/dist/modules/customers/api/deals/[id]/companies/route.js.map +2 -2
  42. package/dist/modules/customers/api/deals/[id]/people/route.js +2 -4
  43. package/dist/modules/customers/api/deals/[id]/people/route.js.map +2 -2
  44. package/dist/modules/customers/api/deals/[id]/route.js +2 -9
  45. package/dist/modules/customers/api/deals/[id]/route.js.map +2 -2
  46. package/dist/modules/customers/api/deals/[id]/stats/route.js +2 -9
  47. package/dist/modules/customers/api/deals/[id]/stats/route.js.map +2 -2
  48. package/dist/modules/customers/api/entity-roles-factory.js +2 -8
  49. package/dist/modules/customers/api/entity-roles-factory.js.map +2 -2
  50. package/dist/modules/customers/api/people/[id]/companies/context.js +2 -4
  51. package/dist/modules/customers/api/people/[id]/companies/context.js.map +2 -2
  52. package/dist/modules/customers/api/people/[id]/companies/enriched/route.js +2 -4
  53. package/dist/modules/customers/api/people/[id]/companies/enriched/route.js.map +2 -2
  54. package/dist/modules/customers/api/people/[id]/route.js +2 -4
  55. package/dist/modules/customers/api/people/[id]/route.js.map +2 -2
  56. package/dist/modules/customers/backend/customers/people/[id]/page.js +29 -8
  57. package/dist/modules/customers/backend/customers/people/[id]/page.js.map +2 -2
  58. package/dist/modules/directory/utils/organizationScopeGuard.js +22 -0
  59. package/dist/modules/directory/utils/organizationScopeGuard.js.map +7 -0
  60. package/dist/modules/progress/acl.js +8 -4
  61. package/dist/modules/progress/acl.js.map +2 -2
  62. package/dist/modules/workflows/backend/events/[id]/page.js +24 -6
  63. package/dist/modules/workflows/backend/events/[id]/page.js.map +2 -2
  64. package/dist/modules/workflows/backend/instances/[id]/page.js +27 -5
  65. package/dist/modules/workflows/backend/instances/[id]/page.js.map +2 -2
  66. package/dist/modules/workflows/backend/tasks/[id]/page.js +25 -6
  67. package/dist/modules/workflows/backend/tasks/[id]/page.js.map +2 -2
  68. package/dist/modules/workflows/cli.js +8 -0
  69. package/dist/modules/workflows/cli.js.map +2 -2
  70. package/dist/modules/workflows/lib/seeds.js +8 -4
  71. package/dist/modules/workflows/lib/seeds.js.map +2 -2
  72. package/dist/modules/workflows/setup.js +3 -1
  73. package/dist/modules/workflows/setup.js.map +2 -2
  74. package/package.json +7 -7
  75. package/src/helpers/integration/authFixtures.ts +98 -0
  76. package/src/helpers/integration/dbFixtures.ts +144 -0
  77. package/src/modules/business_rules/api/execute/route.ts +2 -1
  78. package/src/modules/business_rules/api/rules/route.ts +10 -0
  79. package/src/modules/business_rules/backend/logs/[id]/page.tsx +32 -7
  80. package/src/modules/business_rules/cli.ts +6 -0
  81. package/src/modules/business_rules/lib/rule-engine.ts +163 -9
  82. package/src/modules/business_rules/subscribers/crud-rule-trigger.ts +3 -2
  83. package/src/modules/catalog/api/offers/route.ts +20 -5
  84. package/src/modules/catalog/api/products/route.ts +23 -4
  85. package/src/modules/catalog/lib/pricing.ts +9 -0
  86. package/src/modules/catalog/services/catalogPricingService.ts +6 -0
  87. package/src/modules/currencies/backend/currencies/[id]/page.tsx +21 -2
  88. package/src/modules/currencies/i18n/de.json +1 -0
  89. package/src/modules/currencies/i18n/en.json +1 -0
  90. package/src/modules/currencies/i18n/es.json +1 -0
  91. package/src/modules/currencies/i18n/pl.json +1 -0
  92. package/src/modules/customer_accounts/backend/customer_accounts/roles/[id]/page.tsx +34 -11
  93. package/src/modules/customer_accounts/backend/customer_accounts/users/[id]/page.tsx +34 -11
  94. package/src/modules/customers/api/activities/route.ts +16 -5
  95. package/src/modules/customers/api/comments/route.ts +15 -5
  96. package/src/modules/customers/api/companies/[id]/people/route.ts +2 -4
  97. package/src/modules/customers/api/companies/[id]/route.ts +2 -5
  98. package/src/modules/customers/api/deals/[id]/companies/route.ts +2 -4
  99. package/src/modules/customers/api/deals/[id]/people/route.ts +2 -4
  100. package/src/modules/customers/api/deals/[id]/route.ts +2 -9
  101. package/src/modules/customers/api/deals/[id]/stats/route.ts +2 -9
  102. package/src/modules/customers/api/entity-roles-factory.ts +2 -12
  103. package/src/modules/customers/api/people/[id]/companies/context.ts +2 -5
  104. package/src/modules/customers/api/people/[id]/companies/enriched/route.ts +2 -5
  105. package/src/modules/customers/api/people/[id]/route.ts +2 -5
  106. package/src/modules/customers/backend/customers/people/[id]/page.tsx +35 -11
  107. package/src/modules/directory/utils/organizationScopeGuard.ts +39 -0
  108. package/src/modules/progress/acl.ts +4 -0
  109. package/src/modules/workflows/backend/events/[id]/page.tsx +32 -10
  110. package/src/modules/workflows/backend/instances/[id]/page.tsx +33 -9
  111. package/src/modules/workflows/backend/tasks/[id]/page.tsx +33 -10
  112. package/src/modules/workflows/cli.ts +8 -0
  113. package/src/modules/workflows/i18n/de.json +1 -0
  114. package/src/modules/workflows/i18n/en.json +1 -0
  115. package/src/modules/workflows/i18n/es.json +1 -0
  116. package/src/modules/workflows/i18n/pl.json +1 -0
  117. package/src/modules/workflows/lib/seeds.ts +13 -3
  118. package/src/modules/workflows/setup.ts +3 -1
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/customers/api/entity-roles-factory.ts"],
4
- "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { CommandBus } from '@open-mercato/shared/lib/commands'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport { readJsonSafe } from '@open-mercato/shared/lib/http/readJsonSafe'\nimport { validateCrudMutationGuard, runCrudMutationGuardAfterSuccess } from '@open-mercato/shared/lib/crud/mutation-guard'\nimport { CrudHttpError, isCrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { User } from '@open-mercato/core/modules/auth/data/entities'\nimport type { RbacService } from '@open-mercato/core/modules/auth/services/rbacService'\nimport { CustomerEntity, CustomerEntityRole } from '../data/entities'\nimport { entityRoleCreateSchema, entityRoleUpdateSchema, entityRoleDeleteSchema, type EntityRoleCreateInput, type EntityRoleUpdateInput, type EntityRoleDeleteInput } from '../data/validators'\nimport { withScopedPayload } from './utils'\nimport { resolveCustomersRequestContext, resolveAuthActorId } from '../lib/interactionRequestContext'\nimport { deriveDisplayNameFromEmail } from '../lib/displayName'\nimport { withOperationMetadata } from '../lib/operationMetadata'\n\nconst paramsSchema = z.object({ id: z.string().uuid() })\nconst roleIdQuerySchema = z.object({ roleId: z.string().uuid() })\n\nconst createBodySchema = z.object({\n roleType: z.string().trim().min(1).max(100),\n userId: z.string().uuid(),\n})\nconst updateBodySchema = z.object({\n userId: z.string().uuid(),\n})\n\nconst listItemSchema = z.object({\n id: z.string().uuid(),\n entityType: z.enum(['company', 'person']),\n entityId: z.string().uuid(),\n userId: z.string().uuid(),\n userName: z.string().nullable().optional(),\n userEmail: z.string().nullable().optional(),\n userPhone: z.string().nullable().optional(),\n roleType: z.string(),\n createdAt: z.string(),\n updatedAt: z.string(),\n})\n\nconst listResponseSchema = z.object({ items: z.array(listItemSchema) })\nconst okResponseSchema = z.object({ ok: z.boolean() })\nconst createResponseSchema = z.object({ id: z.string().uuid() })\nconst errorSchema = z.object({ error: z.string() })\n\ntype EntityType = 'company' | 'person'\n\ntype Translator = Awaited<ReturnType<typeof resolveTranslations>>['translate']\n\nfunction getRoleContext(entityType: EntityType, entityId: string) {\n const resourceKind = entityType === 'company' ? 'customers.company' : 'customers.person'\n return { entityType, entityId, resourceKind, resourceId: entityId }\n}\n\nfunction buildValidationErrorResponse(error: z.ZodError, translate: Translator) {\n return NextResponse.json(\n { error: translate('customers.errors.validationFailed', 'Validation failed'), fieldErrors: error.flatten().fieldErrors },\n { status: 400 },\n )\n}\n\nasync function buildContext(request: Request) {\n const context = await resolveCustomersRequestContext(request)\n return {\n ...context,\n ctx: context.commandContext,\n }\n}\n\nfunction collectAllowedOrganizationIds(\n scope: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['scope'],\n auth: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['auth'],\n) {\n const allowedOrgIds = new Set<string>()\n if (scope?.filterIds?.length) scope.filterIds.forEach((id) => allowedOrgIds.add(id))\n else if (auth.orgId) allowedOrgIds.add(auth.orgId)\n return allowedOrgIds\n}\n\nfunction ensureRouteOrganizationAccess(\n organizationId: string,\n scope: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['scope'],\n auth: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['auth'],\n translate: Translator,\n) {\n const allowedOrgIds = collectAllowedOrganizationIds(scope, auth)\n if (allowedOrgIds.size > 0 && !allowedOrgIds.has(organizationId)) {\n throw new CrudHttpError(403, { error: translate('customers.errors.access_denied', 'Access denied') })\n }\n}\n\nasync function ensureFeatureOnOrganization(\n container: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['container'],\n auth: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['auth'],\n feature: string,\n organizationId: string,\n translate: Translator,\n) {\n const actorId = resolveAuthActorId(auth)\n if (!actorId) {\n throw new CrudHttpError(401, { error: translate('customers.errors.unauthorized', 'Unauthorized') })\n }\n let rbac: RbacService | undefined\n try {\n rbac = container.resolve('rbacService') as RbacService | undefined\n } catch (err) {\n console.error('[customers.entity-roles-factory] rbacService resolve failed', err)\n rbac = undefined\n }\n if (!rbac) {\n throw new CrudHttpError(500, { error: translate('customers.errors.internal', 'Internal error') })\n }\n const hasFeature = await rbac.userHasAllFeatures(actorId, [feature], {\n tenantId: auth.tenantId,\n organizationId,\n })\n if (!hasFeature) {\n throw new CrudHttpError(403, { error: translate('customers.errors.access_denied', 'Access denied') })\n }\n}\n\nasync function resolveEntityRouteScope(\n em: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['em'],\n auth: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['auth'],\n scope: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['scope'],\n entityType: EntityType,\n entityId: string,\n translate: Translator,\n) {\n const entity = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: entityId, kind: entityType, tenantId: auth.tenantId, deletedAt: null },\n undefined,\n { tenantId: auth.tenantId, organizationId: scope?.selectedId ?? auth.orgId ?? null },\n )\n if (!entity || entity.tenantId !== auth.tenantId) {\n throw new CrudHttpError(404, { error: translate('customers.errors.customer_not_found', 'Customer not found') })\n }\n ensureRouteOrganizationAccess(entity.organizationId, scope, auth, translate)\n return {\n entity,\n organizationId: entity.organizationId,\n tenantId: entity.tenantId,\n }\n}\n\nasync function resolveRoleRouteScope(\n em: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['em'],\n auth: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['auth'],\n scope: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['scope'],\n entityType: EntityType,\n entityId: string,\n roleId: string,\n translate: Translator,\n) {\n const role = await findOneWithDecryption(\n em,\n CustomerEntityRole,\n { id: roleId, tenantId: auth.tenantId, entityType, entityId, deletedAt: null },\n undefined,\n { tenantId: auth.tenantId, organizationId: scope?.selectedId ?? auth.orgId ?? null },\n )\n if (\n !role ||\n role.tenantId !== auth.tenantId ||\n role.entityType !== entityType ||\n role.entityId !== entityId\n ) {\n throw new CrudHttpError(404, { error: translate('customers.errors.role_not_found', 'Role not found') })\n }\n ensureRouteOrganizationAccess(role.organizationId, scope, auth, translate)\n return {\n role,\n organizationId: role.organizationId,\n tenantId: role.tenantId,\n }\n}\n\nfunction createScopedCommandContext(\n ctx: Awaited<ReturnType<typeof buildContext>>['ctx'],\n organizationId: string,\n) {\n return {\n ...ctx,\n selectedOrganizationId: organizationId,\n organizationIds: [organizationId],\n }\n}\n\nexport const entityRolesMetadata = {\n GET: { requireAuth: true, requireFeatures: ['customers.roles.view'] },\n POST: { requireAuth: true, requireFeatures: ['customers.roles.manage'] },\n PUT: { requireAuth: true, requireFeatures: ['customers.roles.manage'] },\n DELETE: { requireAuth: true, requireFeatures: ['customers.roles.manage'] },\n}\n\nexport function buildEntityRolesOpenApi(entityType: EntityType): OpenApiRouteDoc {\n const label = entityType === 'company' ? 'company' : 'person'\n return {\n tag: 'Customers',\n summary: `${label.charAt(0).toUpperCase() + label.slice(1)} role assignments`,\n pathParams: paramsSchema,\n methods: {\n GET: {\n summary: `List roles for a ${label}`,\n responses: [{ status: 200, description: 'Role assignments', schema: listResponseSchema }],\n errors: [\n { status: 400, description: 'Invalid request', schema: errorSchema },\n { status: 401, description: 'Unauthorized', schema: errorSchema },\n ],\n },\n POST: {\n summary: `Assign a role to a ${label}`,\n requestBody: { contentType: 'application/json', schema: createBodySchema },\n responses: [{ status: 201, description: 'Role created', schema: createResponseSchema }],\n errors: [\n { status: 400, description: 'Invalid request', schema: errorSchema },\n { status: 401, description: 'Unauthorized', schema: errorSchema },\n { status: 409, description: 'Role already assigned', schema: errorSchema },\n ],\n },\n PUT: {\n summary: `Update a ${label} role assignment`,\n query: roleIdQuerySchema,\n requestBody: { contentType: 'application/json', schema: updateBodySchema },\n responses: [{ status: 200, description: 'Role updated', schema: okResponseSchema }],\n errors: [\n { status: 400, description: 'Invalid request', schema: errorSchema },\n { status: 401, description: 'Unauthorized', schema: errorSchema },\n { status: 404, description: 'Role not found', schema: errorSchema },\n ],\n },\n DELETE: {\n summary: `Remove a ${label} role assignment`,\n query: roleIdQuerySchema,\n responses: [{ status: 200, description: 'Role deleted', schema: okResponseSchema }],\n errors: [\n { status: 400, description: 'Invalid request', schema: errorSchema },\n { status: 401, description: 'Unauthorized', schema: errorSchema },\n { status: 404, description: 'Role not found', schema: errorSchema },\n ],\n },\n },\n }\n}\n\nexport function createEntityRolesHandlers(entityType: EntityType) {\n const resourceKind = entityType === 'company' ? 'customers.company' : 'customers.person'\n const logPrefix = entityType === 'company' ? 'customers.company.roles' : 'customers.person.roles'\n\n async function GET(request: Request, { params }: { params: { id: string } }) {\n const { translate } = await resolveTranslations()\n try {\n const { id: entityId } = paramsSchema.parse(params)\n const { container, em, auth, scope } = await buildContext(request)\n const targetScope = await resolveEntityRouteScope(em, auth, scope, entityType, entityId, translate)\n await ensureFeatureOnOrganization(container, auth, 'customers.roles.view', targetScope.organizationId, translate)\n\n const roles = await findWithDecryption(\n em,\n CustomerEntityRole,\n {\n entityType,\n entityId,\n organizationId: targetScope.organizationId,\n tenantId: targetScope.tenantId,\n deletedAt: null,\n },\n { orderBy: { roleType: 'asc' } },\n {\n tenantId: targetScope.tenantId,\n organizationId: targetScope.organizationId,\n },\n )\n\n const userIds = Array.from(new Set(roles.map((role) => role.userId).filter((value): value is string => typeof value === 'string' && value.length > 0)))\n const users = userIds.length\n ? await findWithDecryption(\n em,\n User,\n {\n id: { $in: userIds },\n deletedAt: null,\n ...(targetScope.tenantId ? { tenantId: targetScope.tenantId } : {}),\n },\n undefined,\n {\n tenantId: targetScope.tenantId,\n organizationId: targetScope.organizationId,\n },\n )\n : []\n const userMap = new Map(users.map((user) => [user.id, {\n name: user.name ?? deriveDisplayNameFromEmail(user.email) ?? null,\n email: user.email ?? null,\n phone: null,\n }]))\n\n return NextResponse.json({\n items: roles.map((role) => ({\n ...(userMap.has(role.userId)\n ? {\n userName: userMap.get(role.userId)?.name ?? null,\n userEmail: userMap.get(role.userId)?.email ?? null,\n userPhone: userMap.get(role.userId)?.phone ?? null,\n }\n : {}),\n id: role.id,\n entityType: role.entityType,\n entityId: role.entityId,\n userId: role.userId,\n roleType: role.roleType,\n createdAt: role.createdAt.toISOString(),\n updatedAt: role.updatedAt.toISOString(),\n })),\n })\n } catch (err) {\n if (isCrudHttpError(err)) return NextResponse.json(err.body, { status: err.status })\n if (err instanceof z.ZodError) return buildValidationErrorResponse(err, translate)\n console.error(`${logPrefix}.get failed`, err)\n return NextResponse.json({ error: translate('customers.errors.failed_to_load_roles', 'Failed to load roles') }, { status: 500 })\n }\n }\n\n async function POST(request: Request, { params }: { params: { id: string } }) {\n const { translate } = await resolveTranslations()\n try {\n const { id: entityId } = paramsSchema.parse(params)\n const { container, em, auth, scope, ctx } = await buildContext(request)\n const targetScope = await resolveEntityRouteScope(em, auth, scope, entityType, entityId, translate)\n await ensureFeatureOnOrganization(container, auth, 'customers.roles.manage', targetScope.organizationId, translate)\n const commandCtx = createScopedCommandContext(ctx, targetScope.organizationId)\n\n const rawBody = await readJsonSafe<Record<string, unknown>>(request, {})\n const scoped = withScopedPayload(\n {\n ...rawBody,\n organizationId: targetScope.organizationId,\n tenantId: targetScope.tenantId,\n ...getRoleContext(entityType, entityId),\n },\n commandCtx,\n translate,\n )\n const parsed = entityRoleCreateSchema.parse(scoped)\n\n const guardUserId = resolveAuthActorId(auth)\n const guardResult = await validateCrudMutationGuard(container, {\n tenantId: targetScope.tenantId, organizationId: targetScope.organizationId, userId: guardUserId,\n resourceKind, resourceId: entityId, operation: 'custom',\n requestMethod: request.method, requestHeaders: request.headers, mutationPayload: rawBody,\n })\n if (guardResult && !guardResult.ok) return NextResponse.json(guardResult.body, { status: guardResult.status })\n\n const commandBus = container.resolve('commandBus') as CommandBus\n const { result, logEntry } = await commandBus.execute<EntityRoleCreateInput, { roleId: string }>(\n 'customers.entityRoles.create',\n { input: parsed, ctx: commandCtx },\n )\n\n if (guardResult?.ok && guardResult.shouldRunAfterSuccess) {\n await runCrudMutationGuardAfterSuccess(container, {\n tenantId: targetScope.tenantId, organizationId: targetScope.organizationId, userId: guardUserId,\n resourceKind, resourceId: entityId, operation: 'custom',\n requestMethod: request.method, requestHeaders: request.headers, metadata: guardResult.metadata ?? null,\n })\n }\n\n return withOperationMetadata(\n NextResponse.json({ id: result?.roleId ?? null }, { status: 201 }),\n logEntry,\n { resourceKind, resourceId: entityId },\n )\n } catch (err) {\n if (isCrudHttpError(err)) return NextResponse.json(err.body, { status: err.status })\n if (err instanceof z.ZodError) return buildValidationErrorResponse(err, translate)\n console.error(`${logPrefix}.post failed`, err)\n return NextResponse.json({ error: translate('customers.errors.failed_to_assign_role', 'Failed to assign role') }, { status: 500 })\n }\n }\n\n async function PUT(request: Request, { params }: { params: { id: string } }) {\n const { translate } = await resolveTranslations()\n try {\n const { id: entityId } = paramsSchema.parse(params)\n const { roleId } = roleIdQuerySchema.parse(Object.fromEntries(new URL(request.url).searchParams))\n const { container, em, auth, scope, ctx } = await buildContext(request)\n const targetScope = await resolveRoleRouteScope(em, auth, scope, entityType, entityId, roleId, translate)\n await ensureFeatureOnOrganization(container, auth, 'customers.roles.manage', targetScope.organizationId, translate)\n const commandCtx = createScopedCommandContext(ctx, targetScope.organizationId)\n\n const rawBody = await readJsonSafe<Record<string, unknown>>(request, {})\n const scoped = withScopedPayload(\n {\n ...rawBody,\n id: roleId,\n organizationId: targetScope.organizationId,\n tenantId: targetScope.tenantId,\n },\n commandCtx,\n translate,\n )\n const parsed = entityRoleUpdateSchema.parse(scoped)\n\n const guardUserId = resolveAuthActorId(auth)\n const guardResult = await validateCrudMutationGuard(container, {\n tenantId: targetScope.tenantId, organizationId: targetScope.organizationId, userId: guardUserId,\n resourceKind, resourceId: entityId, operation: 'custom',\n requestMethod: request.method, requestHeaders: request.headers, mutationPayload: { roleId, ...rawBody },\n })\n if (guardResult && !guardResult.ok) return NextResponse.json(guardResult.body, { status: guardResult.status })\n\n const commandBus = container.resolve('commandBus') as CommandBus\n const { logEntry } = await commandBus.execute<EntityRoleUpdateInput, { roleId: string }>(\n 'customers.entityRoles.update',\n { input: parsed, ctx: commandCtx },\n )\n\n if (guardResult?.ok && guardResult.shouldRunAfterSuccess) {\n await runCrudMutationGuardAfterSuccess(container, {\n tenantId: targetScope.tenantId, organizationId: targetScope.organizationId, userId: guardUserId,\n resourceKind, resourceId: entityId, operation: 'custom',\n requestMethod: request.method, requestHeaders: request.headers, metadata: guardResult.metadata ?? null,\n })\n }\n\n return withOperationMetadata(\n NextResponse.json({ ok: true }),\n logEntry,\n { resourceKind, resourceId: entityId },\n )\n } catch (err) {\n if (isCrudHttpError(err)) return NextResponse.json(err.body, { status: err.status })\n if (err instanceof z.ZodError) return buildValidationErrorResponse(err, translate)\n console.error(`${logPrefix}.put failed`, err)\n return NextResponse.json({ error: translate('customers.errors.failed_to_update_role', 'Failed to update role') }, { status: 500 })\n }\n }\n\n async function DELETE(request: Request, { params }: { params: { id: string } }) {\n const { translate } = await resolveTranslations()\n try {\n const { id: entityId } = paramsSchema.parse(params)\n const { roleId } = roleIdQuerySchema.parse(Object.fromEntries(new URL(request.url).searchParams))\n const { container, em, auth, scope, ctx } = await buildContext(request)\n const targetScope = await resolveRoleRouteScope(em, auth, scope, entityType, entityId, roleId, translate)\n await ensureFeatureOnOrganization(container, auth, 'customers.roles.manage', targetScope.organizationId, translate)\n const commandCtx = createScopedCommandContext(ctx, targetScope.organizationId)\n\n const parsed = entityRoleDeleteSchema.parse(\n withScopedPayload(\n {\n id: roleId,\n organizationId: targetScope.organizationId,\n tenantId: targetScope.tenantId,\n },\n commandCtx,\n translate,\n ),\n )\n const guardUserId = resolveAuthActorId(auth)\n const guardResult = await validateCrudMutationGuard(container, {\n tenantId: targetScope.tenantId, organizationId: targetScope.organizationId, userId: guardUserId,\n resourceKind, resourceId: entityId, operation: 'custom',\n requestMethod: request.method, requestHeaders: request.headers, mutationPayload: { roleId },\n })\n if (guardResult && !guardResult.ok) return NextResponse.json(guardResult.body, { status: guardResult.status })\n\n const commandBus = container.resolve('commandBus') as CommandBus\n const { logEntry } = await commandBus.execute<EntityRoleDeleteInput, { roleId: string }>(\n 'customers.entityRoles.delete',\n { input: parsed, ctx: commandCtx },\n )\n\n if (guardResult?.ok && guardResult.shouldRunAfterSuccess) {\n await runCrudMutationGuardAfterSuccess(container, {\n tenantId: targetScope.tenantId, organizationId: targetScope.organizationId, userId: guardUserId,\n resourceKind, resourceId: entityId, operation: 'custom',\n requestMethod: request.method, requestHeaders: request.headers, metadata: guardResult.metadata ?? null,\n })\n }\n\n return withOperationMetadata(\n NextResponse.json({ ok: true }),\n logEntry,\n { resourceKind, resourceId: entityId },\n )\n } catch (err) {\n if (isCrudHttpError(err)) return NextResponse.json(err.body, { status: err.status })\n if (err instanceof z.ZodError) return buildValidationErrorResponse(err, translate)\n console.error(`${logPrefix}.delete failed`, err)\n return NextResponse.json({ error: translate('customers.errors.failed_to_delete_role', 'Failed to delete role') }, { status: 500 })\n }\n }\n\n return { GET, POST, PUT, DELETE }\n}\n"],
5
- "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAGlB,SAAS,oBAAoB;AAC7B,SAAS,2BAA2B,wCAAwC;AAC5E,SAAS,eAAe,uBAAuB;AAC/C,SAAS,2BAA2B;AACpC,SAAS,uBAAuB,0BAA0B;AAC1D,SAAS,YAAY;AAErB,SAAS,gBAAgB,0BAA0B;AACnD,SAAS,wBAAwB,wBAAwB,8BAAkH;AAC3K,SAAS,yBAAyB;AAClC,SAAS,gCAAgC,0BAA0B;AACnE,SAAS,kCAAkC;AAC3C,SAAS,6BAA6B;AAEtC,MAAM,eAAe,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AACvD,MAAM,oBAAoB,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AAEhE,MAAM,mBAAmB,EAAE,OAAO;AAAA,EAChC,UAAU,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG;AAAA,EAC1C,QAAQ,EAAE,OAAO,EAAE,KAAK;AAC1B,CAAC;AACD,MAAM,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AI,KAAK,CAAC;AAAA,QAC9B;AAAA,QACA,EAAE,cAAc,YAAY,SAAS;AAAA,MACvC;AAAA,IACF,SAAS,KAAK;AACZ,UAAI,gBAAgB,GAAG,EAAG,QAAO,aAAa,KAAK,IAAI,MAAM,EAAE,QAAQ,IAAI,OAAO,CAAC;AACnF,UAAI,eAAe,EAAE,SAAU,QAAO,6BAA6B,KAAK,SAAS;AACjF,cAAQ,MAAM,GAAG,SAAS,kBAAkB,GAAG;AAC/C,aAAO,aAAa,KAAK,EAAE,OAAO,UAAU,0CAA0C,uBAAuB,EAAE,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,IACnI;AAAA,EACF;AAEA,SAAO,EAAE,KAAK,MAAM,KAAK,OAAO;AAClC;",
4
+ "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { CommandBus } from '@open-mercato/shared/lib/commands'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport { readJsonSafe } from '@open-mercato/shared/lib/http/readJsonSafe'\nimport { validateCrudMutationGuard, runCrudMutationGuardAfterSuccess } from '@open-mercato/shared/lib/crud/mutation-guard'\nimport { CrudHttpError, isCrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { isOrganizationReadAccessAllowed } from '@open-mercato/core/modules/directory/utils/organizationScopeGuard'\nimport { User } from '@open-mercato/core/modules/auth/data/entities'\nimport type { RbacService } from '@open-mercato/core/modules/auth/services/rbacService'\nimport { CustomerEntity, CustomerEntityRole } from '../data/entities'\nimport { entityRoleCreateSchema, entityRoleUpdateSchema, entityRoleDeleteSchema, type EntityRoleCreateInput, type EntityRoleUpdateInput, type EntityRoleDeleteInput } from '../data/validators'\nimport { withScopedPayload } from './utils'\nimport { resolveCustomersRequestContext, resolveAuthActorId } from '../lib/interactionRequestContext'\nimport { deriveDisplayNameFromEmail } from '../lib/displayName'\nimport { withOperationMetadata } from '../lib/operationMetadata'\n\nconst paramsSchema = z.object({ id: z.string().uuid() })\nconst roleIdQuerySchema = z.object({ roleId: z.string().uuid() })\n\nconst createBodySchema = z.object({\n roleType: z.string().trim().min(1).max(100),\n userId: z.string().uuid(),\n})\nconst updateBodySchema = z.object({\n userId: z.string().uuid(),\n})\n\nconst listItemSchema = z.object({\n id: z.string().uuid(),\n entityType: z.enum(['company', 'person']),\n entityId: z.string().uuid(),\n userId: z.string().uuid(),\n userName: z.string().nullable().optional(),\n userEmail: z.string().nullable().optional(),\n userPhone: z.string().nullable().optional(),\n roleType: z.string(),\n createdAt: z.string(),\n updatedAt: z.string(),\n})\n\nconst listResponseSchema = z.object({ items: z.array(listItemSchema) })\nconst okResponseSchema = z.object({ ok: z.boolean() })\nconst createResponseSchema = z.object({ id: z.string().uuid() })\nconst errorSchema = z.object({ error: z.string() })\n\ntype EntityType = 'company' | 'person'\n\ntype Translator = Awaited<ReturnType<typeof resolveTranslations>>['translate']\n\nfunction getRoleContext(entityType: EntityType, entityId: string) {\n const resourceKind = entityType === 'company' ? 'customers.company' : 'customers.person'\n return { entityType, entityId, resourceKind, resourceId: entityId }\n}\n\nfunction buildValidationErrorResponse(error: z.ZodError, translate: Translator) {\n return NextResponse.json(\n { error: translate('customers.errors.validationFailed', 'Validation failed'), fieldErrors: error.flatten().fieldErrors },\n { status: 400 },\n )\n}\n\nasync function buildContext(request: Request) {\n const context = await resolveCustomersRequestContext(request)\n return {\n ...context,\n ctx: context.commandContext,\n }\n}\n\nfunction ensureRouteOrganizationAccess(\n organizationId: string,\n scope: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['scope'],\n auth: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['auth'],\n translate: Translator,\n) {\n if (!isOrganizationReadAccessAllowed({ scope, auth, organizationId })) {\n throw new CrudHttpError(403, { error: translate('customers.errors.access_denied', 'Access denied') })\n }\n}\n\nasync function ensureFeatureOnOrganization(\n container: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['container'],\n auth: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['auth'],\n feature: string,\n organizationId: string,\n translate: Translator,\n) {\n const actorId = resolveAuthActorId(auth)\n if (!actorId) {\n throw new CrudHttpError(401, { error: translate('customers.errors.unauthorized', 'Unauthorized') })\n }\n let rbac: RbacService | undefined\n try {\n rbac = container.resolve('rbacService') as RbacService | undefined\n } catch (err) {\n console.error('[customers.entity-roles-factory] rbacService resolve failed', err)\n rbac = undefined\n }\n if (!rbac) {\n throw new CrudHttpError(500, { error: translate('customers.errors.internal', 'Internal error') })\n }\n const hasFeature = await rbac.userHasAllFeatures(actorId, [feature], {\n tenantId: auth.tenantId,\n organizationId,\n })\n if (!hasFeature) {\n throw new CrudHttpError(403, { error: translate('customers.errors.access_denied', 'Access denied') })\n }\n}\n\nasync function resolveEntityRouteScope(\n em: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['em'],\n auth: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['auth'],\n scope: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['scope'],\n entityType: EntityType,\n entityId: string,\n translate: Translator,\n) {\n const entity = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: entityId, kind: entityType, tenantId: auth.tenantId, deletedAt: null },\n undefined,\n { tenantId: auth.tenantId, organizationId: scope?.selectedId ?? auth.orgId ?? null },\n )\n if (!entity || entity.tenantId !== auth.tenantId) {\n throw new CrudHttpError(404, { error: translate('customers.errors.customer_not_found', 'Customer not found') })\n }\n ensureRouteOrganizationAccess(entity.organizationId, scope, auth, translate)\n return {\n entity,\n organizationId: entity.organizationId,\n tenantId: entity.tenantId,\n }\n}\n\nasync function resolveRoleRouteScope(\n em: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['em'],\n auth: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['auth'],\n scope: Awaited<ReturnType<typeof resolveCustomersRequestContext>>['scope'],\n entityType: EntityType,\n entityId: string,\n roleId: string,\n translate: Translator,\n) {\n const role = await findOneWithDecryption(\n em,\n CustomerEntityRole,\n { id: roleId, tenantId: auth.tenantId, entityType, entityId, deletedAt: null },\n undefined,\n { tenantId: auth.tenantId, organizationId: scope?.selectedId ?? auth.orgId ?? null },\n )\n if (\n !role ||\n role.tenantId !== auth.tenantId ||\n role.entityType !== entityType ||\n role.entityId !== entityId\n ) {\n throw new CrudHttpError(404, { error: translate('customers.errors.role_not_found', 'Role not found') })\n }\n ensureRouteOrganizationAccess(role.organizationId, scope, auth, translate)\n return {\n role,\n organizationId: role.organizationId,\n tenantId: role.tenantId,\n }\n}\n\nfunction createScopedCommandContext(\n ctx: Awaited<ReturnType<typeof buildContext>>['ctx'],\n organizationId: string,\n) {\n return {\n ...ctx,\n selectedOrganizationId: organizationId,\n organizationIds: [organizationId],\n }\n}\n\nexport const entityRolesMetadata = {\n GET: { requireAuth: true, requireFeatures: ['customers.roles.view'] },\n POST: { requireAuth: true, requireFeatures: ['customers.roles.manage'] },\n PUT: { requireAuth: true, requireFeatures: ['customers.roles.manage'] },\n DELETE: { requireAuth: true, requireFeatures: ['customers.roles.manage'] },\n}\n\nexport function buildEntityRolesOpenApi(entityType: EntityType): OpenApiRouteDoc {\n const label = entityType === 'company' ? 'company' : 'person'\n return {\n tag: 'Customers',\n summary: `${label.charAt(0).toUpperCase() + label.slice(1)} role assignments`,\n pathParams: paramsSchema,\n methods: {\n GET: {\n summary: `List roles for a ${label}`,\n responses: [{ status: 200, description: 'Role assignments', schema: listResponseSchema }],\n errors: [\n { status: 400, description: 'Invalid request', schema: errorSchema },\n { status: 401, description: 'Unauthorized', schema: errorSchema },\n ],\n },\n POST: {\n summary: `Assign a role to a ${label}`,\n requestBody: { contentType: 'application/json', schema: createBodySchema },\n responses: [{ status: 201, description: 'Role created', schema: createResponseSchema }],\n errors: [\n { status: 400, description: 'Invalid request', schema: errorSchema },\n { status: 401, description: 'Unauthorized', schema: errorSchema },\n { status: 409, description: 'Role already assigned', schema: errorSchema },\n ],\n },\n PUT: {\n summary: `Update a ${label} role assignment`,\n query: roleIdQuerySchema,\n requestBody: { contentType: 'application/json', schema: updateBodySchema },\n responses: [{ status: 200, description: 'Role updated', schema: okResponseSchema }],\n errors: [\n { status: 400, description: 'Invalid request', schema: errorSchema },\n { status: 401, description: 'Unauthorized', schema: errorSchema },\n { status: 404, description: 'Role not found', schema: errorSchema },\n ],\n },\n DELETE: {\n summary: `Remove a ${label} role assignment`,\n query: roleIdQuerySchema,\n responses: [{ status: 200, description: 'Role deleted', schema: okResponseSchema }],\n errors: [\n { status: 400, description: 'Invalid request', schema: errorSchema },\n { status: 401, description: 'Unauthorized', schema: errorSchema },\n { status: 404, description: 'Role not found', schema: errorSchema },\n ],\n },\n },\n }\n}\n\nexport function createEntityRolesHandlers(entityType: EntityType) {\n const resourceKind = entityType === 'company' ? 'customers.company' : 'customers.person'\n const logPrefix = entityType === 'company' ? 'customers.company.roles' : 'customers.person.roles'\n\n async function GET(request: Request, { params }: { params: { id: string } }) {\n const { translate } = await resolveTranslations()\n try {\n const { id: entityId } = paramsSchema.parse(params)\n const { container, em, auth, scope } = await buildContext(request)\n const targetScope = await resolveEntityRouteScope(em, auth, scope, entityType, entityId, translate)\n await ensureFeatureOnOrganization(container, auth, 'customers.roles.view', targetScope.organizationId, translate)\n\n const roles = await findWithDecryption(\n em,\n CustomerEntityRole,\n {\n entityType,\n entityId,\n organizationId: targetScope.organizationId,\n tenantId: targetScope.tenantId,\n deletedAt: null,\n },\n { orderBy: { roleType: 'asc' } },\n {\n tenantId: targetScope.tenantId,\n organizationId: targetScope.organizationId,\n },\n )\n\n const userIds = Array.from(new Set(roles.map((role) => role.userId).filter((value): value is string => typeof value === 'string' && value.length > 0)))\n const users = userIds.length\n ? await findWithDecryption(\n em,\n User,\n {\n id: { $in: userIds },\n deletedAt: null,\n ...(targetScope.tenantId ? { tenantId: targetScope.tenantId } : {}),\n },\n undefined,\n {\n tenantId: targetScope.tenantId,\n organizationId: targetScope.organizationId,\n },\n )\n : []\n const userMap = new Map(users.map((user) => [user.id, {\n name: user.name ?? deriveDisplayNameFromEmail(user.email) ?? null,\n email: user.email ?? null,\n phone: null,\n }]))\n\n return NextResponse.json({\n items: roles.map((role) => ({\n ...(userMap.has(role.userId)\n ? {\n userName: userMap.get(role.userId)?.name ?? null,\n userEmail: userMap.get(role.userId)?.email ?? null,\n userPhone: userMap.get(role.userId)?.phone ?? null,\n }\n : {}),\n id: role.id,\n entityType: role.entityType,\n entityId: role.entityId,\n userId: role.userId,\n roleType: role.roleType,\n createdAt: role.createdAt.toISOString(),\n updatedAt: role.updatedAt.toISOString(),\n })),\n })\n } catch (err) {\n if (isCrudHttpError(err)) return NextResponse.json(err.body, { status: err.status })\n if (err instanceof z.ZodError) return buildValidationErrorResponse(err, translate)\n console.error(`${logPrefix}.get failed`, err)\n return NextResponse.json({ error: translate('customers.errors.failed_to_load_roles', 'Failed to load roles') }, { status: 500 })\n }\n }\n\n async function POST(request: Request, { params }: { params: { id: string } }) {\n const { translate } = await resolveTranslations()\n try {\n const { id: entityId } = paramsSchema.parse(params)\n const { container, em, auth, scope, ctx } = await buildContext(request)\n const targetScope = await resolveEntityRouteScope(em, auth, scope, entityType, entityId, translate)\n await ensureFeatureOnOrganization(container, auth, 'customers.roles.manage', targetScope.organizationId, translate)\n const commandCtx = createScopedCommandContext(ctx, targetScope.organizationId)\n\n const rawBody = await readJsonSafe<Record<string, unknown>>(request, {})\n const scoped = withScopedPayload(\n {\n ...rawBody,\n organizationId: targetScope.organizationId,\n tenantId: targetScope.tenantId,\n ...getRoleContext(entityType, entityId),\n },\n commandCtx,\n translate,\n )\n const parsed = entityRoleCreateSchema.parse(scoped)\n\n const guardUserId = resolveAuthActorId(auth)\n const guardResult = await validateCrudMutationGuard(container, {\n tenantId: targetScope.tenantId, organizationId: targetScope.organizationId, userId: guardUserId,\n resourceKind, resourceId: entityId, operation: 'custom',\n requestMethod: request.method, requestHeaders: request.headers, mutationPayload: rawBody,\n })\n if (guardResult && !guardResult.ok) return NextResponse.json(guardResult.body, { status: guardResult.status })\n\n const commandBus = container.resolve('commandBus') as CommandBus\n const { result, logEntry } = await commandBus.execute<EntityRoleCreateInput, { roleId: string }>(\n 'customers.entityRoles.create',\n { input: parsed, ctx: commandCtx },\n )\n\n if (guardResult?.ok && guardResult.shouldRunAfterSuccess) {\n await runCrudMutationGuardAfterSuccess(container, {\n tenantId: targetScope.tenantId, organizationId: targetScope.organizationId, userId: guardUserId,\n resourceKind, resourceId: entityId, operation: 'custom',\n requestMethod: request.method, requestHeaders: request.headers, metadata: guardResult.metadata ?? null,\n })\n }\n\n return withOperationMetadata(\n NextResponse.json({ id: result?.roleId ?? null }, { status: 201 }),\n logEntry,\n { resourceKind, resourceId: entityId },\n )\n } catch (err) {\n if (isCrudHttpError(err)) return NextResponse.json(err.body, { status: err.status })\n if (err instanceof z.ZodError) return buildValidationErrorResponse(err, translate)\n console.error(`${logPrefix}.post failed`, err)\n return NextResponse.json({ error: translate('customers.errors.failed_to_assign_role', 'Failed to assign role') }, { status: 500 })\n }\n }\n\n async function PUT(request: Request, { params }: { params: { id: string } }) {\n const { translate } = await resolveTranslations()\n try {\n const { id: entityId } = paramsSchema.parse(params)\n const { roleId } = roleIdQuerySchema.parse(Object.fromEntries(new URL(request.url).searchParams))\n const { container, em, auth, scope, ctx } = await buildContext(request)\n const targetScope = await resolveRoleRouteScope(em, auth, scope, entityType, entityId, roleId, translate)\n await ensureFeatureOnOrganization(container, auth, 'customers.roles.manage', targetScope.organizationId, translate)\n const commandCtx = createScopedCommandContext(ctx, targetScope.organizationId)\n\n const rawBody = await readJsonSafe<Record<string, unknown>>(request, {})\n const scoped = withScopedPayload(\n {\n ...rawBody,\n id: roleId,\n organizationId: targetScope.organizationId,\n tenantId: targetScope.tenantId,\n },\n commandCtx,\n translate,\n )\n const parsed = entityRoleUpdateSchema.parse(scoped)\n\n const guardUserId = resolveAuthActorId(auth)\n const guardResult = await validateCrudMutationGuard(container, {\n tenantId: targetScope.tenantId, organizationId: targetScope.organizationId, userId: guardUserId,\n resourceKind, resourceId: entityId, operation: 'custom',\n requestMethod: request.method, requestHeaders: request.headers, mutationPayload: { roleId, ...rawBody },\n })\n if (guardResult && !guardResult.ok) return NextResponse.json(guardResult.body, { status: guardResult.status })\n\n const commandBus = container.resolve('commandBus') as CommandBus\n const { logEntry } = await commandBus.execute<EntityRoleUpdateInput, { roleId: string }>(\n 'customers.entityRoles.update',\n { input: parsed, ctx: commandCtx },\n )\n\n if (guardResult?.ok && guardResult.shouldRunAfterSuccess) {\n await runCrudMutationGuardAfterSuccess(container, {\n tenantId: targetScope.tenantId, organizationId: targetScope.organizationId, userId: guardUserId,\n resourceKind, resourceId: entityId, operation: 'custom',\n requestMethod: request.method, requestHeaders: request.headers, metadata: guardResult.metadata ?? null,\n })\n }\n\n return withOperationMetadata(\n NextResponse.json({ ok: true }),\n logEntry,\n { resourceKind, resourceId: entityId },\n )\n } catch (err) {\n if (isCrudHttpError(err)) return NextResponse.json(err.body, { status: err.status })\n if (err instanceof z.ZodError) return buildValidationErrorResponse(err, translate)\n console.error(`${logPrefix}.put failed`, err)\n return NextResponse.json({ error: translate('customers.errors.failed_to_update_role', 'Failed to update role') }, { status: 500 })\n }\n }\n\n async function DELETE(request: Request, { params }: { params: { id: string } }) {\n const { translate } = await resolveTranslations()\n try {\n const { id: entityId } = paramsSchema.parse(params)\n const { roleId } = roleIdQuerySchema.parse(Object.fromEntries(new URL(request.url).searchParams))\n const { container, em, auth, scope, ctx } = await buildContext(request)\n const targetScope = await resolveRoleRouteScope(em, auth, scope, entityType, entityId, roleId, translate)\n await ensureFeatureOnOrganization(container, auth, 'customers.roles.manage', targetScope.organizationId, translate)\n const commandCtx = createScopedCommandContext(ctx, targetScope.organizationId)\n\n const parsed = entityRoleDeleteSchema.parse(\n withScopedPayload(\n {\n id: roleId,\n organizationId: targetScope.organizationId,\n tenantId: targetScope.tenantId,\n },\n commandCtx,\n translate,\n ),\n )\n const guardUserId = resolveAuthActorId(auth)\n const guardResult = await validateCrudMutationGuard(container, {\n tenantId: targetScope.tenantId, organizationId: targetScope.organizationId, userId: guardUserId,\n resourceKind, resourceId: entityId, operation: 'custom',\n requestMethod: request.method, requestHeaders: request.headers, mutationPayload: { roleId },\n })\n if (guardResult && !guardResult.ok) return NextResponse.json(guardResult.body, { status: guardResult.status })\n\n const commandBus = container.resolve('commandBus') as CommandBus\n const { logEntry } = await commandBus.execute<EntityRoleDeleteInput, { roleId: string }>(\n 'customers.entityRoles.delete',\n { input: parsed, ctx: commandCtx },\n )\n\n if (guardResult?.ok && guardResult.shouldRunAfterSuccess) {\n await runCrudMutationGuardAfterSuccess(container, {\n tenantId: targetScope.tenantId, organizationId: targetScope.organizationId, userId: guardUserId,\n resourceKind, resourceId: entityId, operation: 'custom',\n requestMethod: request.method, requestHeaders: request.headers, metadata: guardResult.metadata ?? null,\n })\n }\n\n return withOperationMetadata(\n NextResponse.json({ ok: true }),\n logEntry,\n { resourceKind, resourceId: entityId },\n )\n } catch (err) {\n if (isCrudHttpError(err)) return NextResponse.json(err.body, { status: err.status })\n if (err instanceof z.ZodError) return buildValidationErrorResponse(err, translate)\n console.error(`${logPrefix}.delete failed`, err)\n return NextResponse.json({ error: translate('customers.errors.failed_to_delete_role', 'Failed to delete role') }, { status: 500 })\n }\n }\n\n return { GET, POST, PUT, DELETE }\n}\n"],
5
+ "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAGlB,SAAS,oBAAoB;AAC7B,SAAS,2BAA2B,wCAAwC;AAC5E,SAAS,eAAe,uBAAuB;AAC/C,SAAS,2BAA2B;AACpC,SAAS,uBAAuB,0BAA0B;AAC1D,SAAS,uCAAuC;AAChD,SAAS,YAAY;AAErB,SAAS,gBAAgB,0BAA0B;AACnD,SAAS,wBAAwB,wBAAwB,8BAAkH;AAC3K,SAAS,yBAAyB;AAClC,SAAS,gCAAgC,0BAA0B;AACnE,SAAS,kCAAkC;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,QAAQ;AAAA,UAAS,UAAU,YAAY,YAAY;AAAA,QACpG,CAAC;AAAA,MACH;AAEA,aAAO;AAAA,QACL,aAAa,KAAK,EAAE,IAAI,KAAK,CAAC;AAAA,QAC9B;AAAA,QACA,EAAE,cAAc,YAAY,SAAS;AAAA,MACvC;AAAA,IACF,SAAS,KAAK;AACZ,UAAI,gBAAgB,GAAG,EAAG,QAAO,aAAa,KAAK,IAAI,MAAM,EAAE,QAAQ,IAAI,OAAO,CAAC;AACnF,UAAI,eAAe,EAAE,SAAU,QAAO,6BAA6B,KAAK,SAAS;AACjF,cAAQ,MAAM,GAAG,SAAS,kBAAkB,GAAG;AAC/C,aAAO,aAAa,KAAK,EAAE,OAAO,UAAU,0CAA0C,uBAAuB,EAAE,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,IACnI;AAAA,EACF;AAEA,SAAO,EAAE,KAAK,MAAM,KAAK,OAAO;AAClC;",
6
6
  "names": []
7
7
  }
@@ -6,6 +6,7 @@ import {
6
6
  CustomerPersonProfile
7
7
  } from "@open-mercato/core/modules/customers/data/entities";
8
8
  import { resolveOrganizationScopeForRequest } from "@open-mercato/core/modules/directory/utils/organizationScope";
9
+ import { isOrganizationReadAccessAllowed } from "@open-mercato/core/modules/directory/utils/organizationScopeGuard";
9
10
  import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
10
11
  import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
11
12
  async function loadPersonContext(req, personId) {
@@ -32,10 +33,7 @@ async function loadPersonContext(req, personId) {
32
33
  if (!person) {
33
34
  throw new CrudHttpError(404, { error: translate("customers.errors.person_not_found", "Person not found") });
34
35
  }
35
- const allowedOrgIds = /* @__PURE__ */ new Set();
36
- if (scope?.filterIds?.length) scope.filterIds.forEach((entry) => allowedOrgIds.add(entry));
37
- else if (authenticatedAuth.orgId) allowedOrgIds.add(authenticatedAuth.orgId);
38
- if (allowedOrgIds.size > 0 && !allowedOrgIds.has(person.organizationId)) {
36
+ if (!isOrganizationReadAccessAllowed({ scope, auth: authenticatedAuth, organizationId: person.organizationId })) {
39
37
  throw new CrudHttpError(403, { error: translate("customers.errors.access_denied", "Access denied") });
40
38
  }
41
39
  const profile = await findOneWithDecryption(
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../../../src/modules/customers/api/people/%5Bid%5D/companies/context.ts"],
4
- "sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'\nimport {\n CustomerEntity,\n CustomerPersonProfile,\n} from '@open-mercato/core/modules/customers/data/entities'\nimport { resolveOrganizationScopeForRequest } from '@open-mercato/core/modules/directory/utils/organizationScope'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\n\nexport async function loadPersonContext(req: Request, personId: string) {\n const { translate } = await resolveTranslations()\n const auth = await getAuthFromRequest(req)\n if (!auth?.tenantId) {\n throw new CrudHttpError(401, { error: translate('customers.errors.unauthorized', 'Unauthorized') })\n }\n const authenticatedAuth = auth as typeof auth & { tenantId: string }\n\n const container = await createRequestContainer()\n const scope = await resolveOrganizationScopeForRequest({ container, auth: authenticatedAuth, request: req })\n const em = (container.resolve('em') as EntityManager).fork()\n const decryptionScope = {\n tenantId: authenticatedAuth.tenantId,\n organizationId: scope?.selectedId ?? authenticatedAuth.orgId ?? null,\n }\n const person = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: personId, kind: 'person', tenantId: authenticatedAuth.tenantId, deletedAt: null },\n {},\n decryptionScope,\n )\n\n if (!person) {\n throw new CrudHttpError(404, { error: translate('customers.errors.person_not_found', 'Person not found') })\n }\n\n const allowedOrgIds = new Set<string>()\n if (scope?.filterIds?.length) scope.filterIds.forEach((entry) => allowedOrgIds.add(entry))\n else if (authenticatedAuth.orgId) allowedOrgIds.add(authenticatedAuth.orgId)\n\n if (allowedOrgIds.size > 0 && !allowedOrgIds.has(person.organizationId)) {\n throw new CrudHttpError(403, { error: translate('customers.errors.access_denied', 'Access denied') })\n }\n\n const profile = await findOneWithDecryption(\n em,\n CustomerPersonProfile,\n { entity: person, tenantId: person.tenantId, organizationId: person.organizationId },\n { populate: ['company'] },\n {\n tenantId: person.tenantId,\n organizationId: person.organizationId,\n },\n )\n if (!profile) {\n throw new CrudHttpError(404, { error: translate('customers.errors.person_profile_not_found', 'Person profile not found') })\n }\n\n return {\n container,\n auth: authenticatedAuth,\n selectedOrganizationId: scope?.selectedId ?? authenticatedAuth.orgId ?? null,\n em,\n person,\n profile,\n }\n}\n"],
5
- "mappings": "AACA,SAAS,qBAAqB;AAC9B,SAAS,8BAA8B;AACvC,SAAS,0BAA0B;AACnC;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP,SAAS,0CAA0C;AACnD,SAAS,2BAA2B;AACpC,SAAS,6BAA6B;AAEtC,eAAsB,kBAAkB,KAAc,UAAkB;AACtE,QAAM,EAAE,UAAU,IAAI,MAAM,oBAAoB;AAChD,QAAM,OAAO,MAAM,mBAAmB,GAAG;AACzC,MAAI,CAAC,MAAM,UAAU;AACnB,UAAM,IAAI,cAAc,KAAK,EAAE,OAAO,UAAU,iCAAiC,cAAc,EAAE,CAAC;AAAA,EACpG;AACA,QAAM,oBAAoB;AAE1B,QAAM,YAAY,MAAM,uBAAuB;AAC/C,QAAM,QAAQ,MAAM,mCAAmC,EAAE,WAAW,MAAM,mBAAmB,SAAS,IAAI,CAAC;AAC3G,QAAM,KAAM,UAAU,QAAQ,IAAI,EAAoB,KAAK;AAC3D,QAAM,kBAAkB;AAAA,IACtB,UAAU,kBAAkB;AAAA,IAC5B,gBAAgB,OAAO,cAAc,kBAAkB,SAAS;AAAA,EAClE;AACA,QAAM,SAAS,MAAM;AAAA,IACnB;AAAA,IACA;AAAA,IACA,EAAE,IAAI,UAAU,MAAM,UAAU,UAAU,kBAAkB,UAAU,WAAW,KAAK;AAAA,IACtF,CAAC;AAAA,IACD;AAAA,EACF;AAEA,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,cAAc,KAAK,EAAE,OAAO,UAAU,qCAAqC,kBAAkB,EAAE,CAAC;AAAA,EAC5G;AAEA,QAAM,gBAAgB,oBAAI,IAAY;AACtC,MAAI,OAAO,WAAW,OAAQ,OAAM,UAAU,QAAQ,CAAC,UAAU,cAAc,IAAI,KAAK,CAAC;AAAA,WAChF,kBAAkB,MAAO,eAAc,IAAI,kBAAkB,KAAK;AAE3E,MAAI,cAAc,OAAO,KAAK,CAAC,cAAc,IAAI,OAAO,cAAc,GAAG;AACvE,UAAM,IAAI,cAAc,KAAK,EAAE,OAAO,UAAU,kCAAkC,eAAe,EAAE,CAAC;AAAA,EACtG;AAEA,QAAM,UAAU,MAAM;AAAA,IACpB;AAAA,IACA;AAAA,IACA,EAAE,QAAQ,QAAQ,UAAU,OAAO,UAAU,gBAAgB,OAAO,eAAe;AAAA,IACnF,EAAE,UAAU,CAAC,SAAS,EAAE;AAAA,IACxB;AAAA,MACE,UAAU,OAAO;AAAA,MACjB,gBAAgB,OAAO;AAAA,IACzB;AAAA,EACF;AACA,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,cAAc,KAAK,EAAE,OAAO,UAAU,6CAA6C,0BAA0B,EAAE,CAAC;AAAA,EAC5H;AAEA,SAAO;AAAA,IACL;AAAA,IACA,MAAM;AAAA,IACN,wBAAwB,OAAO,cAAc,kBAAkB,SAAS;AAAA,IACxE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'\nimport {\n CustomerEntity,\n CustomerPersonProfile,\n} from '@open-mercato/core/modules/customers/data/entities'\nimport { resolveOrganizationScopeForRequest } from '@open-mercato/core/modules/directory/utils/organizationScope'\nimport { isOrganizationReadAccessAllowed } from '@open-mercato/core/modules/directory/utils/organizationScopeGuard'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\n\nexport async function loadPersonContext(req: Request, personId: string) {\n const { translate } = await resolveTranslations()\n const auth = await getAuthFromRequest(req)\n if (!auth?.tenantId) {\n throw new CrudHttpError(401, { error: translate('customers.errors.unauthorized', 'Unauthorized') })\n }\n const authenticatedAuth = auth as typeof auth & { tenantId: string }\n\n const container = await createRequestContainer()\n const scope = await resolveOrganizationScopeForRequest({ container, auth: authenticatedAuth, request: req })\n const em = (container.resolve('em') as EntityManager).fork()\n const decryptionScope = {\n tenantId: authenticatedAuth.tenantId,\n organizationId: scope?.selectedId ?? authenticatedAuth.orgId ?? null,\n }\n const person = await findOneWithDecryption(\n em,\n CustomerEntity,\n { id: personId, kind: 'person', tenantId: authenticatedAuth.tenantId, deletedAt: null },\n {},\n decryptionScope,\n )\n\n if (!person) {\n throw new CrudHttpError(404, { error: translate('customers.errors.person_not_found', 'Person not found') })\n }\n\n if (!isOrganizationReadAccessAllowed({ scope, auth: authenticatedAuth, organizationId: person.organizationId })) {\n throw new CrudHttpError(403, { error: translate('customers.errors.access_denied', 'Access denied') })\n }\n\n const profile = await findOneWithDecryption(\n em,\n CustomerPersonProfile,\n { entity: person, tenantId: person.tenantId, organizationId: person.organizationId },\n { populate: ['company'] },\n {\n tenantId: person.tenantId,\n organizationId: person.organizationId,\n },\n )\n if (!profile) {\n throw new CrudHttpError(404, { error: translate('customers.errors.person_profile_not_found', 'Person profile not found') })\n }\n\n return {\n container,\n auth: authenticatedAuth,\n selectedOrganizationId: scope?.selectedId ?? authenticatedAuth.orgId ?? null,\n em,\n person,\n profile,\n }\n}\n"],
5
+ "mappings": "AACA,SAAS,qBAA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AU,gBAAgB,OAAO,eAAe;AAAA,IACnF,EAAE,UAAU,CAAC,SAAS,EAAE;AAAA,IACxB;AAAA,MACE,UAAU,OAAO;AAAA,MACjB,gBAAgB,OAAO;AAAA,IACzB;AAAA,EACF;AACA,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,cAAc,KAAK,EAAE,OAAO,UAAU,6CAA6C,0BAA0B,EAAE,CAAC;AAAA,EAC5H;AAEA,SAAO;AAAA,IACL;AAAA,IACA,MAAM;AAAA,IACN,wBAAwB,OAAO,cAAc,kBAAkB,SAAS;AAAA,IACxE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
@@ -6,6 +6,7 @@ import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
6
6
  import { resolveOrganizationScopeForRequest } from "@open-mercato/core/modules/directory/utils/organizationScope";
7
7
  import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
8
8
  import { findWithDecryption, findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
9
+ import { isOrganizationReadAccessAllowed } from "@open-mercato/core/modules/directory/utils/organizationScopeGuard";
9
10
  import {
10
11
  CustomerEntity,
11
12
  CustomerCompanyProfile,
@@ -148,10 +149,7 @@ async function GET(req, ctx) {
148
149
  if (!person) {
149
150
  throw new CrudHttpError(404, { error: translate("customers.errors.person_not_found", "Person not found") });
150
151
  }
151
- const allowedOrgIds = /* @__PURE__ */ new Set();
152
- if (scope?.filterIds?.length) scope.filterIds.forEach((entry) => allowedOrgIds.add(entry));
153
- else if (auth.orgId) allowedOrgIds.add(auth.orgId);
154
- if (allowedOrgIds.size > 0 && !allowedOrgIds.has(person.organizationId)) {
152
+ if (!isOrganizationReadAccessAllowed({ scope, auth, organizationId: person.organizationId })) {
155
153
  throw new CrudHttpError(403, { error: translate("customers.errors.access_denied", "Access denied") });
156
154
  }
157
155
  const entityScope = { tenantId: auth.tenantId, organizationId: person.organizationId };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../../../../src/modules/customers/api/people/%5Bid%5D/companies/enriched/route.ts"],
4
- "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { CrudHttpError, isCrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'\nimport { resolveOrganizationScopeForRequest } from '@open-mercato/core/modules/directory/utils/organizationScope'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport { findWithDecryption, findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport {\n CustomerEntity,\n CustomerCompanyProfile,\n CustomerPersonCompanyLink,\n CustomerAddress,\n CustomerCompanyBilling,\n CustomerPersonCompanyRole,\n CustomerTagAssignment,\n CustomerDealCompanyLink,\n CustomerDeal,\n CustomerInteraction,\n} from '../../../../../data/entities'\nimport {\n filterActivePersonCompanyLinks,\n withActiveCustomerPersonCompanyLinkFilter,\n} from '../../../../../lib/personCompanyLinkTable'\n\nconst paramsSchema = z.object({\n id: z.string().uuid(),\n})\n\nconst querySchema = z.object({\n page: z.coerce.number().min(1).default(1),\n pageSize: z.coerce.number().min(1).max(100).default(20),\n search: z.string().optional(),\n sort: z.enum(['name-asc', 'name-desc', 'recent']).default('name-asc'),\n})\n\nexport const metadata = {\n GET: { requireAuth: true, requireFeatures: ['customers.people.view'] },\n}\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Customers',\n methods: {\n GET: {\n summary: 'Get enriched company data for a person\\'s linked companies',\n query: querySchema,\n responses: [\n {\n status: 200,\n description: 'Enriched company rows with profile, billing, tags, deals and more',\n schema: z.object({\n items: z.array(\n z.object({\n linkId: z.string().uuid(),\n companyId: z.string().uuid(),\n displayName: z.string(),\n isPrimary: z.boolean(),\n subtitle: z.string().nullable(),\n profile: z\n .object({\n industry: z.string().nullable(),\n sizeBucket: z.string().nullable(),\n legalName: z.string().nullable(),\n domain: z.string().nullable(),\n websiteUrl: z.string().nullable(),\n })\n .nullable(),\n billing: z\n .object({\n bankName: z.string().nullable(),\n bankAccountMasked: z.string().nullable(),\n paymentTerms: z.string().nullable(),\n preferredCurrency: z.string().nullable(),\n })\n .nullable(),\n primaryAddress: z.object({ formatted: z.string() }).nullable(),\n tags: z.array(\n z.object({\n id: z.string().uuid(),\n label: z.string(),\n color: z.string().nullable(),\n }),\n ),\n roles: z.array(\n z.object({\n id: z.string().uuid(),\n roleValue: z.string(),\n }),\n ),\n activeDeal: z\n .object({\n title: z.string(),\n valueAmount: z.string().nullable(),\n valueCurrency: z.string().nullable(),\n })\n .nullable(),\n lastContactAt: z.string().nullable(),\n clv: z.object({ amount: z.number(), currency: z.string() }).nullable(),\n status: z.string().nullable(),\n lifecycleStage: z.string().nullable(),\n temperature: z.string().nullable(),\n renewalQuarter: z.string().nullable(),\n }),\n ),\n total: z.number().int().nonnegative(),\n page: z.number().int().min(1),\n pageSize: z.number().int().min(1),\n totalPages: z.number().int().min(1),\n }),\n },\n ],\n },\n },\n}\n\nfunction formatAddress(address: CustomerAddress): string {\n return [address.addressLine1, address.city, address.region, address.postalCode]\n .filter(Boolean)\n .join(', ')\n}\n\nfunction buildSubtitle(industry: string | null | undefined, address: CustomerAddress | null): string | null {\n const parts: string[] = []\n if (industry) parts.push(industry)\n if (address) {\n const locationParts = [address.city, address.region].filter(Boolean)\n if (locationParts.length > 0) parts.push(locationParts.join(', '))\n }\n return parts.length > 0 ? parts.join(' \u00B7 ') : null\n}\n\nfunction matchesSearch(item: Record<string, unknown>, query: string): boolean {\n const normalized = query.trim().toLowerCase()\n if (!normalized.length) return true\n return [\n typeof item.displayName === 'string' ? item.displayName : null,\n typeof item.subtitle === 'string' ? item.subtitle : null,\n typeof item.status === 'string' ? item.status : null,\n typeof item.lifecycleStage === 'string' ? item.lifecycleStage : null,\n typeof item.temperature === 'string' ? item.temperature : null,\n typeof item.renewalQuarter === 'string' ? item.renewalQuarter : null,\n ]\n .filter((value): value is string => typeof value === 'string' && value.length > 0)\n .some((value) => value.toLowerCase().includes(normalized))\n}\n\nexport async function GET(req: Request, ctx: { params?: { id?: string } }) {\n const { translate } = await resolveTranslations()\n try {\n const { id } = paramsSchema.parse({ id: ctx.params?.id })\n\n const auth = await getAuthFromRequest(req)\n if (!auth?.tenantId) {\n throw new CrudHttpError(401, { error: translate('customers.errors.unauthorized', 'Unauthorized') })\n }\n const url = new URL(req.url)\n const query = querySchema.parse({\n page: url.searchParams.get('page') ?? undefined,\n pageSize: url.searchParams.get('pageSize') ?? undefined,\n search: url.searchParams.get('search') ?? undefined,\n sort: url.searchParams.get('sort') ?? undefined,\n })\n\n const container = await createRequestContainer()\n const scope = await resolveOrganizationScopeForRequest({ container, auth, request: req })\n const em = (container.resolve('em') as EntityManager).fork()\n\n const decryptionScope = { tenantId: auth.tenantId, organizationId: auth.orgId ?? null }\n const person = await findOneWithDecryption(em, CustomerEntity, { id, kind: 'person', tenantId: auth.tenantId, deletedAt: null }, {}, decryptionScope)\n if (!person) {\n throw new CrudHttpError(404, { error: translate('customers.errors.person_not_found', 'Person not found') })\n }\n\n const allowedOrgIds = new Set<string>()\n if (scope?.filterIds?.length) scope.filterIds.forEach((entry) => allowedOrgIds.add(entry))\n else if (auth.orgId) allowedOrgIds.add(auth.orgId)\n\n if (allowedOrgIds.size > 0 && !allowedOrgIds.has(person.organizationId)) {\n throw new CrudHttpError(403, { error: translate('customers.errors.access_denied', 'Access denied') })\n }\n\n const entityScope = { tenantId: auth.tenantId, organizationId: person.organizationId }\n const linkWhere = await withActiveCustomerPersonCompanyLinkFilter(\n em,\n {\n person,\n tenantId: auth.tenantId,\n },\n 'customers.people.companiesEnriched.GET',\n )\n const links = filterActivePersonCompanyLinks(\n await findWithDecryption(\n em,\n CustomerPersonCompanyLink,\n linkWhere,\n { populate: ['company'] },\n entityScope,\n ),\n )\n\n const companyIds = links.map((link) => (link.company as CustomerEntity).id)\n\n if (companyIds.length === 0) {\n return NextResponse.json({\n items: [],\n total: 0,\n page: 1,\n pageSize: query.pageSize,\n totalPages: 1,\n })\n }\n\n const tenantScope = { tenantId: auth.tenantId, organizationId: person.organizationId }\n const [allProfiles, allAddresses, allBillings, allTagAssignments, allRoles, allDealLinks, allInteractions] =\n await Promise.all([\n findWithDecryption(em, CustomerCompanyProfile, { entity: { $in: companyIds }, ...tenantScope }, {}, entityScope),\n findWithDecryption(em, CustomerAddress, { entity: { $in: companyIds }, isPrimary: true, ...tenantScope }, {}, entityScope),\n findWithDecryption(em, CustomerCompanyBilling, { entity: { $in: companyIds }, ...tenantScope }, {}, entityScope),\n findWithDecryption(em, CustomerTagAssignment, { entity: { $in: companyIds }, ...tenantScope }, { populate: ['tag'] }, entityScope),\n findWithDecryption(em, CustomerPersonCompanyRole, { personEntity: person, companyEntity: { $in: companyIds }, ...tenantScope }, {}, entityScope),\n // CustomerDealCompanyLink is a pure junction table without tenantId/organizationId columns;\n // scoping flows transitively through the already-tenant-scoped `company` filter.\n findWithDecryption(em, CustomerDealCompanyLink, { company: { $in: companyIds } }, { populate: ['deal'] }, entityScope),\n findWithDecryption(em, CustomerInteraction, {\n entity: { $in: companyIds },\n occurredAt: { $ne: null },\n deletedAt: null,\n ...tenantScope,\n }, { orderBy: { occurredAt: 'DESC' } }, entityScope),\n ])\n\n const profileByCompany = new Map(allProfiles.map((p) => [(p.entity as { id: string }).id, p]))\n const addressByCompany = new Map(allAddresses.map((a) => [(a.entity as { id: string }).id, a]))\n const billingByCompany = new Map(allBillings.map((b) => [(b.entity as { id: string }).id, b]))\n\n const tagsByCompany = new Map<string, typeof allTagAssignments>()\n for (const ta of allTagAssignments) {\n const entityId = (ta.entity as { id: string }).id\n const existing = tagsByCompany.get(entityId) ?? []\n existing.push(ta)\n tagsByCompany.set(entityId, existing)\n }\n\n const rolesByCompany = new Map<string, typeof allRoles>()\n for (const role of allRoles) {\n const entityId = (role.companyEntity as { id: string }).id\n const existing = rolesByCompany.get(entityId) ?? []\n existing.push(role)\n rolesByCompany.set(entityId, existing)\n }\n\n const dealLinksByCompany = new Map<string, typeof allDealLinks>()\n for (const dcl of allDealLinks) {\n const entityId = (dcl.company as { id: string }).id\n const existing = dealLinksByCompany.get(entityId) ?? []\n existing.push(dcl)\n dealLinksByCompany.set(entityId, existing)\n }\n\n const lastInteractionByCompany = new Map<string, CustomerInteraction>()\n for (const interaction of allInteractions) {\n const entityId = (interaction.entity as { id: string }).id\n if (!lastInteractionByCompany.has(entityId)) {\n lastInteractionByCompany.set(entityId, interaction)\n }\n }\n\n const items = links.map((link) => {\n const company = link.company as CustomerEntity\n const companyId = company.id\n const profile = profileByCompany.get(companyId) ?? null\n const primaryAddress = addressByCompany.get(companyId) ?? null\n const billing = billingByCompany.get(companyId) ?? null\n const tagAssignments = tagsByCompany.get(companyId) ?? []\n const roles = rolesByCompany.get(companyId) ?? []\n const companyDealLinks = dealLinksByCompany.get(companyId) ?? []\n const lastInteraction = lastInteractionByCompany.get(companyId) ?? null\n\n const activeDeals = companyDealLinks\n .map((dcl) => dcl.deal as CustomerDeal)\n .filter((deal) => deal.status !== 'win' && deal.status !== 'loose' && !deal.deletedAt)\n .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())\n const activeDeal = activeDeals.length > 0 ? activeDeals[0] : null\n\n const wonDeals = companyDealLinks\n .map((dcl) => dcl.deal as CustomerDeal)\n .filter((deal) => deal.status === 'win' && !deal.deletedAt)\n let clv: { amount: number; currency: string } | null = null\n if (wonDeals.length > 0) {\n const currencies = new Map<string, number>()\n for (const deal of wonDeals) {\n if (deal.valueAmount) {\n const currency = deal.valueCurrency ?? 'USD'\n currencies.set(currency, (currencies.get(currency) ?? 0) + parseFloat(deal.valueAmount))\n }\n }\n if (currencies.size > 0) {\n const [currency, amount] = currencies.entries().next().value!\n clv = { amount, currency }\n }\n }\n\n return {\n linkId: link.id,\n companyId,\n displayName: company.displayName,\n isPrimary: Boolean(link.isPrimary),\n subtitle: buildSubtitle(profile?.industry, primaryAddress),\n profile: profile\n ? {\n industry: profile.industry ?? null,\n sizeBucket: profile.sizeBucket ?? null,\n legalName: profile.legalName ?? null,\n domain: profile.domain ?? null,\n websiteUrl: profile.websiteUrl ?? null,\n }\n : null,\n billing: billing\n ? {\n bankName: billing.bankName ?? null,\n bankAccountMasked: billing.bankAccountMasked ?? null,\n paymentTerms: billing.paymentTerms ?? null,\n preferredCurrency: billing.preferredCurrency ?? null,\n }\n : null,\n primaryAddress: primaryAddress ? { formatted: formatAddress(primaryAddress) } : null,\n tags: tagAssignments.map((ta) => {\n const tag = ta.tag as { id: string; label: string; color?: string | null }\n return {\n id: tag.id,\n label: tag.label,\n color: tag.color ?? null,\n }\n }),\n roles: roles.map((r) => ({ id: r.id, roleValue: r.roleValue })),\n activeDeal: activeDeal\n ? {\n title: activeDeal.title,\n valueAmount: activeDeal.valueAmount ?? null,\n valueCurrency: activeDeal.valueCurrency ?? null,\n }\n : null,\n lastContactAt: lastInteraction?.occurredAt?.toISOString() ?? null,\n clv,\n status: company.status ?? null,\n lifecycleStage: company.lifecycleStage ?? null,\n temperature: company.temperature ?? null,\n renewalQuarter: company.renewalQuarter ?? null,\n }\n })\n\n const filteredItems = query.search?.trim().length\n ? items.filter((item) => matchesSearch(item as Record<string, unknown>, query.search ?? ''))\n : items\n const sortedItems = [...filteredItems].sort((left, right) => {\n if (query.sort === 'recent') {\n const leftTimestamp = left.lastContactAt ? new Date(left.lastContactAt).getTime() : 0\n const rightTimestamp = right.lastContactAt ? new Date(right.lastContactAt).getTime() : 0\n if (leftTimestamp === rightTimestamp) {\n return left.displayName.localeCompare(right.displayName, undefined, { sensitivity: 'base' })\n }\n return rightTimestamp - leftTimestamp\n }\n const compare = left.displayName.localeCompare(right.displayName, undefined, { sensitivity: 'base' })\n return query.sort === 'name-asc' ? compare : -compare\n })\n\n const total = sortedItems.length\n const totalPages = Math.max(1, Math.ceil(total / query.pageSize))\n const page = Math.min(query.page, totalPages)\n const start = (page - 1) * query.pageSize\n\n return NextResponse.json({\n items: sortedItems.slice(start, start + query.pageSize),\n total,\n page,\n pageSize: query.pageSize,\n totalPages,\n })\n } catch (err) {\n if (isCrudHttpError(err)) {\n return NextResponse.json(err.body, { status: err.status })\n }\n console.error('[customers/people/[id]/companies/enriched] GET failed', err)\n return NextResponse.json({ error: translate('customers.errors.internal', 'Internal server error') }, { status: 500 })\n }\n}\n"],
5
- "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAElB,SAAS,eAAe,uBAAuB;AAC/C,SAAS,8BAA8B;AACvC,SAAS,0BAA0B;AACnC,SAAS,0CAA0C;AACnD,SAAS,2BAA2B;AAEpC,SAAS,oBAAoB,6BAA6B;AAC1D;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAEP,MAAM,eAAe,EAAE,OAAO;AAAA,EAC5B,IAAI,EAAE,OAAO,EAAE,KAAK;AACtB,CAAC;AAED,MAAM,cAAc,EAAE,OAAO;AAAA,EAC3B,MAAM,EAAE,OAAO,OAAO,EAAE,IAAI,CAAC,EAAE,QAAQ,CAAC;AAAA,EACxC,UAAU,EAAE,OAAO,OAAO,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,EAAE,QAAQ,EAAE;AAAA,EACtD,QAAQ,EAAE,OAAO,EAAE,SAAS;AAAA,EAC5B,MAAM,EAAE,KAAK,CAAC,YAAY,aAAa,QAAQ,CAAC,EAAE,QAAQ,UAAU;AACtE,CAAC;AAEM,MAAM,WAAW;AAAA,EACtB,KAAK,EAAE,aAAa,MAAM,iBAAiB,CAAC,uBAAuB,EAAE;AACvE;AAEO,MAAM,UAA2B;AAAA,EACtC,KAAK;AAAA,EACL,SAAS;AAAA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uBAAuB,EAAE,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,EACtH;AACF;",
4
+ "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { CrudHttpError, isCrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'\nimport { resolveOrganizationScopeForRequest } from '@open-mercato/core/modules/directory/utils/organizationScope'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport { findWithDecryption, findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { isOrganizationReadAccessAllowed } from '@open-mercato/core/modules/directory/utils/organizationScopeGuard'\nimport {\n CustomerEntity,\n CustomerCompanyProfile,\n CustomerPersonCompanyLink,\n CustomerAddress,\n CustomerCompanyBilling,\n CustomerPersonCompanyRole,\n CustomerTagAssignment,\n CustomerDealCompanyLink,\n CustomerDeal,\n CustomerInteraction,\n} from '../../../../../data/entities'\nimport {\n filterActivePersonCompanyLinks,\n withActiveCustomerPersonCompanyLinkFilter,\n} from '../../../../../lib/personCompanyLinkTable'\n\nconst paramsSchema = z.object({\n id: z.string().uuid(),\n})\n\nconst querySchema = z.object({\n page: z.coerce.number().min(1).default(1),\n pageSize: z.coerce.number().min(1).max(100).default(20),\n search: z.string().optional(),\n sort: z.enum(['name-asc', 'name-desc', 'recent']).default('name-asc'),\n})\n\nexport const metadata = {\n GET: { requireAuth: true, requireFeatures: ['customers.people.view'] },\n}\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Customers',\n methods: {\n GET: {\n summary: 'Get enriched company data for a person\\'s linked companies',\n query: querySchema,\n responses: [\n {\n status: 200,\n description: 'Enriched company rows with profile, billing, tags, deals and more',\n schema: z.object({\n items: z.array(\n z.object({\n linkId: z.string().uuid(),\n companyId: z.string().uuid(),\n displayName: z.string(),\n isPrimary: z.boolean(),\n subtitle: z.string().nullable(),\n profile: z\n .object({\n industry: z.string().nullable(),\n sizeBucket: z.string().nullable(),\n legalName: z.string().nullable(),\n domain: z.string().nullable(),\n websiteUrl: z.string().nullable(),\n })\n .nullable(),\n billing: z\n .object({\n bankName: z.string().nullable(),\n bankAccountMasked: z.string().nullable(),\n paymentTerms: z.string().nullable(),\n preferredCurrency: z.string().nullable(),\n })\n .nullable(),\n primaryAddress: z.object({ formatted: z.string() }).nullable(),\n tags: z.array(\n z.object({\n id: z.string().uuid(),\n label: z.string(),\n color: z.string().nullable(),\n }),\n ),\n roles: z.array(\n z.object({\n id: z.string().uuid(),\n roleValue: z.string(),\n }),\n ),\n activeDeal: z\n .object({\n title: z.string(),\n valueAmount: z.string().nullable(),\n valueCurrency: z.string().nullable(),\n })\n .nullable(),\n lastContactAt: z.string().nullable(),\n clv: z.object({ amount: z.number(), currency: z.string() }).nullable(),\n status: z.string().nullable(),\n lifecycleStage: z.string().nullable(),\n temperature: z.string().nullable(),\n renewalQuarter: z.string().nullable(),\n }),\n ),\n total: z.number().int().nonnegative(),\n page: z.number().int().min(1),\n pageSize: z.number().int().min(1),\n totalPages: z.number().int().min(1),\n }),\n },\n ],\n },\n },\n}\n\nfunction formatAddress(address: CustomerAddress): string {\n return [address.addressLine1, address.city, address.region, address.postalCode]\n .filter(Boolean)\n .join(', ')\n}\n\nfunction buildSubtitle(industry: string | null | undefined, address: CustomerAddress | null): string | null {\n const parts: string[] = []\n if (industry) parts.push(industry)\n if (address) {\n const locationParts = [address.city, address.region].filter(Boolean)\n if (locationParts.length > 0) parts.push(locationParts.join(', '))\n }\n return parts.length > 0 ? parts.join(' \u00B7 ') : null\n}\n\nfunction matchesSearch(item: Record<string, unknown>, query: string): boolean {\n const normalized = query.trim().toLowerCase()\n if (!normalized.length) return true\n return [\n typeof item.displayName === 'string' ? item.displayName : null,\n typeof item.subtitle === 'string' ? item.subtitle : null,\n typeof item.status === 'string' ? item.status : null,\n typeof item.lifecycleStage === 'string' ? item.lifecycleStage : null,\n typeof item.temperature === 'string' ? item.temperature : null,\n typeof item.renewalQuarter === 'string' ? item.renewalQuarter : null,\n ]\n .filter((value): value is string => typeof value === 'string' && value.length > 0)\n .some((value) => value.toLowerCase().includes(normalized))\n}\n\nexport async function GET(req: Request, ctx: { params?: { id?: string } }) {\n const { translate } = await resolveTranslations()\n try {\n const { id } = paramsSchema.parse({ id: ctx.params?.id })\n\n const auth = await getAuthFromRequest(req)\n if (!auth?.tenantId) {\n throw new CrudHttpError(401, { error: translate('customers.errors.unauthorized', 'Unauthorized') })\n }\n const url = new URL(req.url)\n const query = querySchema.parse({\n page: url.searchParams.get('page') ?? undefined,\n pageSize: url.searchParams.get('pageSize') ?? undefined,\n search: url.searchParams.get('search') ?? undefined,\n sort: url.searchParams.get('sort') ?? undefined,\n })\n\n const container = await createRequestContainer()\n const scope = await resolveOrganizationScopeForRequest({ container, auth, request: req })\n const em = (container.resolve('em') as EntityManager).fork()\n\n const decryptionScope = { tenantId: auth.tenantId, organizationId: auth.orgId ?? null }\n const person = await findOneWithDecryption(em, CustomerEntity, { id, kind: 'person', tenantId: auth.tenantId, deletedAt: null }, {}, decryptionScope)\n if (!person) {\n throw new CrudHttpError(404, { error: translate('customers.errors.person_not_found', 'Person not found') })\n }\n\n if (!isOrganizationReadAccessAllowed({ scope, auth, organizationId: person.organizationId })) {\n throw new CrudHttpError(403, { error: translate('customers.errors.access_denied', 'Access denied') })\n }\n\n const entityScope = { tenantId: auth.tenantId, organizationId: person.organizationId }\n const linkWhere = await withActiveCustomerPersonCompanyLinkFilter(\n em,\n {\n person,\n tenantId: auth.tenantId,\n },\n 'customers.people.companiesEnriched.GET',\n )\n const links = filterActivePersonCompanyLinks(\n await findWithDecryption(\n em,\n CustomerPersonCompanyLink,\n linkWhere,\n { populate: ['company'] },\n entityScope,\n ),\n )\n\n const companyIds = links.map((link) => (link.company as CustomerEntity).id)\n\n if (companyIds.length === 0) {\n return NextResponse.json({\n items: [],\n total: 0,\n page: 1,\n pageSize: query.pageSize,\n totalPages: 1,\n })\n }\n\n const tenantScope = { tenantId: auth.tenantId, organizationId: person.organizationId }\n const [allProfiles, allAddresses, allBillings, allTagAssignments, allRoles, allDealLinks, allInteractions] =\n await Promise.all([\n findWithDecryption(em, CustomerCompanyProfile, { entity: { $in: companyIds }, ...tenantScope }, {}, entityScope),\n findWithDecryption(em, CustomerAddress, { entity: { $in: companyIds }, isPrimary: true, ...tenantScope }, {}, entityScope),\n findWithDecryption(em, CustomerCompanyBilling, { entity: { $in: companyIds }, ...tenantScope }, {}, entityScope),\n findWithDecryption(em, CustomerTagAssignment, { entity: { $in: companyIds }, ...tenantScope }, { populate: ['tag'] }, entityScope),\n findWithDecryption(em, CustomerPersonCompanyRole, { personEntity: person, companyEntity: { $in: companyIds }, ...tenantScope }, {}, entityScope),\n // CustomerDealCompanyLink is a pure junction table without tenantId/organizationId columns;\n // scoping flows transitively through the already-tenant-scoped `company` filter.\n findWithDecryption(em, CustomerDealCompanyLink, { company: { $in: companyIds } }, { populate: ['deal'] }, entityScope),\n findWithDecryption(em, CustomerInteraction, {\n entity: { $in: companyIds },\n occurredAt: { $ne: null },\n deletedAt: null,\n ...tenantScope,\n }, { orderBy: { occurredAt: 'DESC' } }, entityScope),\n ])\n\n const profileByCompany = new Map(allProfiles.map((p) => [(p.entity as { id: string }).id, p]))\n const addressByCompany = new Map(allAddresses.map((a) => [(a.entity as { id: string }).id, a]))\n const billingByCompany = new Map(allBillings.map((b) => [(b.entity as { id: string }).id, b]))\n\n const tagsByCompany = new Map<string, typeof allTagAssignments>()\n for (const ta of allTagAssignments) {\n const entityId = (ta.entity as { id: string }).id\n const existing = tagsByCompany.get(entityId) ?? []\n existing.push(ta)\n tagsByCompany.set(entityId, existing)\n }\n\n const rolesByCompany = new Map<string, typeof allRoles>()\n for (const role of allRoles) {\n const entityId = (role.companyEntity as { id: string }).id\n const existing = rolesByCompany.get(entityId) ?? []\n existing.push(role)\n rolesByCompany.set(entityId, existing)\n }\n\n const dealLinksByCompany = new Map<string, typeof allDealLinks>()\n for (const dcl of allDealLinks) {\n const entityId = (dcl.company as { id: string }).id\n const existing = dealLinksByCompany.get(entityId) ?? []\n existing.push(dcl)\n dealLinksByCompany.set(entityId, existing)\n }\n\n const lastInteractionByCompany = new Map<string, CustomerInteraction>()\n for (const interaction of allInteractions) {\n const entityId = (interaction.entity as { id: string }).id\n if (!lastInteractionByCompany.has(entityId)) {\n lastInteractionByCompany.set(entityId, interaction)\n }\n }\n\n const items = links.map((link) => {\n const company = link.company as CustomerEntity\n const companyId = company.id\n const profile = profileByCompany.get(companyId) ?? null\n const primaryAddress = addressByCompany.get(companyId) ?? null\n const billing = billingByCompany.get(companyId) ?? null\n const tagAssignments = tagsByCompany.get(companyId) ?? []\n const roles = rolesByCompany.get(companyId) ?? []\n const companyDealLinks = dealLinksByCompany.get(companyId) ?? []\n const lastInteraction = lastInteractionByCompany.get(companyId) ?? null\n\n const activeDeals = companyDealLinks\n .map((dcl) => dcl.deal as CustomerDeal)\n .filter((deal) => deal.status !== 'win' && deal.status !== 'loose' && !deal.deletedAt)\n .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())\n const activeDeal = activeDeals.length > 0 ? activeDeals[0] : null\n\n const wonDeals = companyDealLinks\n .map((dcl) => dcl.deal as CustomerDeal)\n .filter((deal) => deal.status === 'win' && !deal.deletedAt)\n let clv: { amount: number; currency: string } | null = null\n if (wonDeals.length > 0) {\n const currencies = new Map<string, number>()\n for (const deal of wonDeals) {\n if (deal.valueAmount) {\n const currency = deal.valueCurrency ?? 'USD'\n currencies.set(currency, (currencies.get(currency) ?? 0) + parseFloat(deal.valueAmount))\n }\n }\n if (currencies.size > 0) {\n const [currency, amount] = currencies.entries().next().value!\n clv = { amount, currency }\n }\n }\n\n return {\n linkId: link.id,\n companyId,\n displayName: company.displayName,\n isPrimary: Boolean(link.isPrimary),\n subtitle: buildSubtitle(profile?.industry, primaryAddress),\n profile: profile\n ? {\n industry: profile.industry ?? null,\n sizeBucket: profile.sizeBucket ?? null,\n legalName: profile.legalName ?? null,\n domain: profile.domain ?? null,\n websiteUrl: profile.websiteUrl ?? null,\n }\n : null,\n billing: billing\n ? {\n bankName: billing.bankName ?? null,\n bankAccountMasked: billing.bankAccountMasked ?? null,\n paymentTerms: billing.paymentTerms ?? null,\n preferredCurrency: billing.preferredCurrency ?? null,\n }\n : null,\n primaryAddress: primaryAddress ? { formatted: formatAddress(primaryAddress) } : null,\n tags: tagAssignments.map((ta) => {\n const tag = ta.tag as { id: string; label: string; color?: string | null }\n return {\n id: tag.id,\n label: tag.label,\n color: tag.color ?? null,\n }\n }),\n roles: roles.map((r) => ({ id: r.id, roleValue: r.roleValue })),\n activeDeal: activeDeal\n ? {\n title: activeDeal.title,\n valueAmount: activeDeal.valueAmount ?? null,\n valueCurrency: activeDeal.valueCurrency ?? null,\n }\n : null,\n lastContactAt: lastInteraction?.occurredAt?.toISOString() ?? null,\n clv,\n status: company.status ?? null,\n lifecycleStage: company.lifecycleStage ?? null,\n temperature: company.temperature ?? null,\n renewalQuarter: company.renewalQuarter ?? null,\n }\n })\n\n const filteredItems = query.search?.trim().length\n ? items.filter((item) => matchesSearch(item as Record<string, unknown>, query.search ?? ''))\n : items\n const sortedItems = [...filteredItems].sort((left, right) => {\n if (query.sort === 'recent') {\n const leftTimestamp = left.lastContactAt ? new Date(left.lastContactAt).getTime() : 0\n const rightTimestamp = right.lastContactAt ? new Date(right.lastContactAt).getTime() : 0\n if (leftTimestamp === rightTimestamp) {\n return left.displayName.localeCompare(right.displayName, undefined, { sensitivity: 'base' })\n }\n return rightTimestamp - leftTimestamp\n }\n const compare = left.displayName.localeCompare(right.displayName, undefined, { sensitivity: 'base' })\n return query.sort === 'name-asc' ? compare : -compare\n })\n\n const total = sortedItems.length\n const totalPages = Math.max(1, Math.ceil(total / query.pageSize))\n const page = Math.min(query.page, totalPages)\n const start = (page - 1) * query.pageSize\n\n return NextResponse.json({\n items: sortedItems.slice(start, start + query.pageSize),\n total,\n page,\n pageSize: query.pageSize,\n totalPages,\n })\n } catch (err) {\n if (isCrudHttpError(err)) {\n return NextResponse.json(err.body, { status: err.status })\n }\n console.error('[customers/people/[id]/companies/enriched] GET failed', err)\n return NextResponse.json({ error: translate('customers.errors.internal', 'Internal server error') }, { status: 500 })\n }\n}\n"],
5
+ "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAElB,SAAS,eAAe,uBAAuB;AAC/C,SAAS,8BAA8B;AACvC,SAAS,0BAA0B;AACnC,SAAS,0CAA0C;AACnD,SAAS,2BAA2B;AAEpC,SAAS,oBAAoB,6BAA6B;AAC1D,SAAS,uCAAuC;AAChD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAEP,MAAM,eAAe,EAAE,OAAO;AAAA,EAC5B,IAAI,EAAE,OAAO,EAAE,KAAK;AACtB,CAAC;AAED,MAAM,cAAc,EAAE,OAAO;AAAA,EAC3B,MAAM,EAAE,OAAO,OAAO,EAAE,IAAI,CAAC,EAAE,QAAQ,CAAC;AAAA,EACxC,UAAU,EAAE,OAAO,OAAO,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,EAAE,QAAQ,EAAE;AAAA,EACtD,QAAQ,EAAE,OAAO,EAAE,SAAS;AAAA,EAC5B,MAAM,EAAE,KAAK,CAAC,YAAY,aAAa,QAAQ,CAAC,EAAE,QAAQ,UAAU;AACtE,CAAC;AAEM,MAAM,WAAW;AAAA,EACtB,KAAK,EAAE,aAAa,MAAM,iBAAiB,CAAC,uBAAuB,EAAE;AACvE;AAEO,MAAM,UAA2B;AAAA,EACtC,KAAK;AAAA,EACL,SAAS;AAAA,IACP,KAAK;AAAA,MACH,SAAS;AAAA,MACT,OAAO;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UAAI,MAAM,SAAS,UAAU;AAC3B,cAAM,gBAAgB,KAAK,gBAAgB,IAAI,KAAK,KAAK,aAAa,EAAE,QAAQ,IAAI;AACpF,cAAM,iBAAiB,MAAM,gBAAgB,IAAI,KAAK,MAAM,aAAa,EAAE,QAAQ,IAAI;AACvF,YAAI,kBAAkB,gBAAgB;AACpC,iBAAO,KAAK,YAAY,cAAc,MAAM,aAAa,QAAW,EAAE,aAAa,OAAO,CAAC;AAAA,QAC7F;AACA,eAAO,iBAAiB;AAAA,MAC1B;AACA,YAAM,UAAU,KAAK,YAAY,cAAc,MAAM,aAAa,QAAW,EAAE,aAAa,OAAO,CAAC;AACpG,aAAO,MAAM,SAAS,aAAa,UAAU,CAAC;AAAA,IAChD,CAAC;AAED,UAAM,QAAQ,YAAY;AAC1B,UAAM,aAAa,KAAK,IAAI,GAAG,KAAK,KAAK,QAAQ,MAAM,QAAQ,CAAC;AAChE,UAAM,OAAO,KAAK,IAAI,MAAM,MAAM,UAAU;AAC5C,UAAM,SAAS,OAAO,KAAK,MAAM;AAEjC,WAAO,aAAa,KAAK;AAAA,MACvB,OAAO,YAAY,MAAM,OAAO,QAAQ,MAAM,QAAQ;AAAA,MACtD;AAAA,MACA;AAAA,MACA,UAAU,MAAM;AAAA,MAChB;AAAA,IACF,CAAC;AAAA,EACH,SAAS,KAAK;AACZ,QAAI,gBAAgB,GAAG,GAAG;AACxB,aAAO,aAAa,KAAK,IAAI,MAAM,EAAE,QAAQ,IAAI,OAAO,CAAC;AAAA,IAC3D;AACA,YAAQ,MAAM,yDAAyD,GAAG;AAC1E,WAAO,aAAa,KAAK,EAAE,OAAO,UAAU,6BAA6B,uBAAuB,EAAE,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,EACtH;AACF;",
6
6
  "names": []
7
7
  }
@@ -30,6 +30,7 @@ import { resolveCustomerInteractionFeatureFlags } from "../../../lib/interaction
30
30
  import { hydrateCanonicalInteractions } from "../../../lib/interactionReadModel.js";
31
31
  import { findWithDecryption, findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
32
32
  import { parseBooleanFromUnknown, parseBooleanToken } from "@open-mercato/shared/lib/boolean";
33
+ import { isOrganizationReadAccessAllowed } from "@open-mercato/core/modules/directory/utils/organizationScopeGuard";
33
34
  import { loadPersonCompanyLinks, summarizePersonCompanies } from "../../../lib/personCompanies.js";
34
35
  import { normalizeCustomerDetailCustomFields } from "../../detailCustomFields.js";
35
36
  const metadata = {
@@ -401,10 +402,7 @@ async function GET(_req, ctx) {
401
402
  profileMeta = { reason: "person_tenant_mismatch" };
402
403
  return notFound("Person not found");
403
404
  }
404
- const allowedOrgIds = /* @__PURE__ */ new Set();
405
- if (scope?.filterIds?.length) scope.filterIds.forEach((id) => allowedOrgIds.add(id));
406
- else if (auth.orgId) allowedOrgIds.add(auth.orgId);
407
- if (allowedOrgIds.size && person.organizationId && !allowedOrgIds.has(person.organizationId)) {
405
+ if (!isOrganizationReadAccessAllowed({ scope, auth, organizationId: person.organizationId })) {
408
406
  statusCode = 403;
409
407
  profileMeta = { reason: "organization_forbidden" };
410
408
  return forbidden("Access denied");