@open-mercato/core 0.10.1-develop.7726.1.a447cc5525 → 0.10.1-develop.7727.1.0b89c4f800

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.
@@ -1,4 +1,4 @@
1
- [build:core] found 6146 entry points
1
+ [build:core] found 6147 entry points
2
2
  [build:core] built successfully
3
3
  [build:core:generated] found 271 entry points
4
4
  [build:core:generated] built successfully
@@ -3,6 +3,7 @@ import { CrudHttpError } from "@open-mercato/shared/lib/crud/errors";
3
3
  import { Organization, Tenant } from "@open-mercato/core/modules/directory/data/entities";
4
4
  import { organizationCreateSchema, organizationUpdateSchema } from "@open-mercato/core/modules/directory/data/validators";
5
5
  import {
6
+ findOrganizationParentCycle,
6
7
  lockOrganizationHierarchyForTenant,
7
8
  rebuildHierarchyForTenant
8
9
  } from "@open-mercato/core/modules/directory/lib/hierarchy";
@@ -28,6 +29,7 @@ import {
28
29
  requireId,
29
30
  buildChanges
30
31
  } from "@open-mercato/shared/lib/commands/helpers";
32
+ import { findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
31
33
  import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
32
34
  import { toOptionalString } from "@open-mercato/shared/lib/string/coerce";
33
35
  import { slugify } from "@open-mercato/shared/lib/slugify";
@@ -206,6 +208,26 @@ async function restoreChildParents(em, tenantId, snapshots) {
206
208
  }
207
209
  if (toPersist.length) await em.persist(toPersist).flush();
208
210
  }
211
+ const REPLAY_CYCLE_ERROR_KEY = "directory.organizations.errors.replayCycle";
212
+ async function assertReplayKeepsHierarchyAcyclic(em, tenantId, organizationIds) {
213
+ const filter = {
214
+ tenant: tenantId,
215
+ deletedAt: null
216
+ };
217
+ const organizations = await findWithDecryption(em, Organization, filter, void 0, {
218
+ tenantId,
219
+ organizationId: null
220
+ });
221
+ if (!findOrganizationParentCycle(organizations, organizationIds)) return;
222
+ const { translate } = await resolveTranslations();
223
+ throw new CrudHttpError(409, {
224
+ error: translate(
225
+ REPLAY_CYCLE_ERROR_KEY,
226
+ "The organization hierarchy changed since this action, so it can't be reverted without making an organization its own ancestor."
227
+ ),
228
+ code: REPLAY_CYCLE_ERROR_KEY
229
+ });
230
+ }
209
231
  function normalizeChildIds(ids, exclude) {
210
232
  const excludeSet = new Set(exclude);
211
233
  return Array.from(new Set(ids)).filter((id) => !excludeSet.has(id));
@@ -403,6 +425,11 @@ const createOrganizationCommand = {
403
425
  organization.parentId = null;
404
426
  await em.flush();
405
427
  }
428
+ await assertReplayKeepsHierarchyAcyclic(
429
+ em,
430
+ tenantId,
431
+ childrenBefore.map((entry) => entry.childId).filter((childId) => childId !== after.id)
432
+ );
406
433
  await rebuildHierarchyForTenant(em, tenantId);
407
434
  }
408
435
  ], { transaction: true });
@@ -473,6 +500,10 @@ const createOrganizationCommand = {
473
500
  }
474
501
  }
475
502
  await assignChildren(em, tenantId, after.id, after.childParents.map((entry) => entry.childId));
503
+ await assertReplayKeepsHierarchyAcyclic(em, tenantId, [
504
+ after.id,
505
+ ...after.childParents.map((entry) => entry.childId)
506
+ ]);
476
507
  await rebuildHierarchyForTenant(em, tenantId);
477
508
  }
478
509
  ], { transaction: true });
@@ -691,6 +722,10 @@ const updateOrganizationCommand = {
691
722
  }
692
723
  const childSnapshots = before.childParents;
693
724
  await restoreChildParents(em, tenantId, childSnapshots);
725
+ await assertReplayKeepsHierarchyAcyclic(em, tenantId, [
726
+ before.id,
727
+ ...(childSnapshots ?? []).map((entry) => entry.childId)
728
+ ]);
694
729
  await rebuildHierarchyForTenant(em, tenantId);
695
730
  }
696
731
  ], { transaction: true });
@@ -853,6 +888,10 @@ const deleteOrganizationCommand = {
853
888
  }
854
889
  }
855
890
  await restoreChildParents(em, tenantId, before.childParents);
891
+ await assertReplayKeepsHierarchyAcyclic(em, tenantId, [
892
+ before.id,
893
+ ...(before.childParents ?? []).map((entry) => entry.childId)
894
+ ]);
856
895
  await rebuildHierarchyForTenant(em, tenantId);
857
896
  }
858
897
  ], { transaction: true });
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/directory/commands/organizations.ts"],
4
- "sourcesContent": ["import type { CommandHandler, CommandRuntimeContext } from '@open-mercato/shared/lib/commands'\nimport { registerCommand } from '@open-mercato/shared/lib/commands'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport type { EntityManager, FilterQuery } from '@mikro-orm/postgresql'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport { Organization, Tenant } from '@open-mercato/core/modules/directory/data/entities'\nimport { organizationCreateSchema, organizationUpdateSchema } from '@open-mercato/core/modules/directory/data/validators'\nimport {\n lockOrganizationHierarchyForTenant,\n rebuildHierarchyForTenant,\n} from '@open-mercato/core/modules/directory/lib/hierarchy'\nimport {\n enforceTenantSelection,\n normalizeTenantId,\n resolveIsSuperAdmin,\n} from '@open-mercato/core/modules/auth/lib/tenantAccess'\nimport { E } from '#generated/entities.ids.generated'\nimport type { CrudEmitContext, CrudEventsConfig, CrudIndexerConfig } from '@open-mercato/shared/lib/crud/types'\nimport {\n loadCustomFieldSnapshot,\n buildCustomFieldResetMap,\n diffCustomFieldChanges,\n} from '@open-mercato/shared/lib/commands/customFieldSnapshots'\nimport { extractUndoPayload, reviveSnapshotDates } from '@open-mercato/shared/lib/commands/undo'\nimport { resolveRedoSnapshot } from '@open-mercato/shared/lib/commands/redo'\nimport { withAtomicFlush } from '@open-mercato/shared/lib/commands/flush'\nimport {\n parseWithCustomFields,\n setCustomFieldsIfAny,\n emitCrudSideEffects,\n emitCrudUndoSideEffects,\n requireId,\n buildChanges,\n} from '@open-mercato/shared/lib/commands/helpers'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { toOptionalString } from '@open-mercato/shared/lib/string/coerce'\nimport { slugify } from '@open-mercato/shared/lib/slugify'\n\nexport const organizationCrudEvents: CrudEventsConfig = {\n module: 'directory',\n entity: 'organization',\n persistent: true,\n buildPayload: (ctx) => {\n const orgCtx = ctx as CrudEmitContext<Organization>\n return {\n id: orgCtx.identifiers.id,\n tenantId: tenantIdFromContext(orgCtx),\n organizationId: orgCtx.identifiers.id,\n }\n },\n}\n\nexport const organizationCrudIndexer: CrudIndexerConfig = {\n entityType: E.directory.organization,\n buildUpsertPayload: (ctx) => {\n const orgCtx = ctx as CrudEmitContext<Organization>\n return {\n entityType: E.directory.organization,\n recordId: orgCtx.identifiers.id,\n organizationId: orgCtx.identifiers.id,\n tenantId: tenantIdFromContext(orgCtx),\n }\n },\n buildDeletePayload: (ctx) => {\n const orgCtx = ctx as CrudEmitContext<Organization>\n return {\n entityType: E.directory.organization,\n recordId: orgCtx.identifiers.id,\n organizationId: orgCtx.identifiers.id,\n tenantId: tenantIdFromContext(orgCtx),\n }\n },\n}\n\ntype OrganizationTenantShape = {\n __tenantId?: unknown\n tenant?: string | { id?: unknown; getEntity?: () => { id?: unknown } }\n tenantId?: unknown\n tenant_id?: unknown\n}\n\ntype SerializedOrganization = ReturnType<typeof serializeOrganization>\n\ntype ChildParentSnapshot = {\n childId: string\n parentId: string | null\n}\n\ntype OrganizationUndoSnapshot = {\n id: string\n tenantId: string | null\n createdAt?: string | null\n name: string\n slug?: string | null\n logoUrl?: string | null\n logoPreserveAspectRatio?: boolean\n isActive: boolean\n parentId: string | null\n childParents: ChildParentSnapshot[]\n custom?: Record<string, unknown>\n}\n\ntype OrganizationSnapshots = {\n view: SerializedOrganization\n undo: OrganizationUndoSnapshot\n}\n\ntype OrganizationActorScope = {\n isUnrestricted: boolean\n tenantId: string | null\n}\n\ntype AuthorizedOrganizationTarget = {\n organization: Organization\n tenantId: string\n}\n\nexport function resolveTenantIdFromEntity(entity: Organization): string | null {\n const shape = entity as unknown as OrganizationTenantShape\n const cached = toOptionalString(shape.__tenantId)\n if (cached) return cached\n const tenantRef = shape.tenant\n if (typeof tenantRef === 'string') return tenantRef\n if (tenantRef && typeof tenantRef === 'object') {\n const direct = toOptionalString(tenantRef.id)\n if (direct) return direct\n if (typeof tenantRef.getEntity === 'function') {\n const nested = tenantRef.getEntity()\n const nestedId = nested ? toOptionalString(nested.id) : null\n if (nestedId) return nestedId\n }\n }\n const fallback = toOptionalString(shape.tenantId) || toOptionalString(shape.tenant_id)\n return fallback\n}\n\nasync function resolveOrganizationActorScope(ctx: CommandRuntimeContext): Promise<OrganizationActorScope> {\n if (ctx.systemActor === true) return { isUnrestricted: true, tenantId: null }\n const tenantId = normalizeTenantId(ctx.auth?.tenantId ?? null) ?? null\n const isSuperAdmin = await resolveIsSuperAdmin(ctx)\n return { isUnrestricted: isSuperAdmin, tenantId }\n}\n\nfunction buildOrganizationTargetFilter(\n id: string,\n scope: OrganizationActorScope,\n): FilterQuery<Organization> | null {\n if (!scope.isUnrestricted && !scope.tenantId) return null\n return {\n id,\n deletedAt: null,\n ...(scope.isUnrestricted ? {} : { tenant: scope.tenantId }),\n } as FilterQuery<Organization>\n}\n\nasync function loadAuthorizedOrganizationTarget(\n em: EntityManager,\n id: string,\n ctx: CommandRuntimeContext,\n): Promise<AuthorizedOrganizationTarget> {\n const scope = await resolveOrganizationActorScope(ctx)\n const filter = buildOrganizationTargetFilter(id, scope)\n if (!filter) throw new CrudHttpError(404, { error: 'Not found' })\n const organization = await em.findOne(Organization, filter)\n if (!organization) throw new CrudHttpError(404, { error: 'Not found' })\n const tenantId = resolveTenantIdFromEntity(organization)\n if (!tenantId || (!scope.isUnrestricted && tenantId !== scope.tenantId)) {\n throw new CrudHttpError(404, { error: 'Not found' })\n }\n return { organization, tenantId }\n}\n\nfunction buildOrganizationMutationFilter(\n id: string,\n tenantId: string,\n): FilterQuery<Organization> {\n return {\n id,\n deletedAt: null,\n tenant: tenantId,\n } as FilterQuery<Organization>\n}\n\nasync function assertOrganizationUndoTenantAccess(\n ctx: CommandRuntimeContext,\n targetTenantId: string,\n): Promise<void> {\n const scope = await resolveOrganizationActorScope(ctx)\n if (!scope.isUnrestricted && scope.tenantId !== targetTenantId) {\n throw new CrudHttpError(404, { error: 'Not found' })\n }\n}\n\nfunction buildOrganizationUndoTargetFilter(\n id: string,\n tenantId: string,\n): FilterQuery<Organization> {\n return { id, tenant: tenantId } as FilterQuery<Organization>\n}\n\nfunction serializeOrganization(entity: Organization, custom?: Record<string, unknown> | null) {\n return {\n id: String(entity.id),\n tenantId: resolveTenantIdFromEntity(entity),\n name: entity.name,\n slug: entity.slug ?? null,\n logoUrl: entity.logoUrl ?? null,\n logoPreserveAspectRatio: !!entity.logoPreserveAspectRatio,\n isActive: !!entity.isActive,\n parentId: entity.parentId ?? null,\n ancestorIds: Array.isArray(entity.ancestorIds) ? [...entity.ancestorIds] : [],\n childIds: Array.isArray(entity.childIds) ? [...entity.childIds] : [],\n descendantIds: Array.isArray(entity.descendantIds) ? [...entity.descendantIds] : [],\n createdAt: entity.createdAt ? entity.createdAt.toISOString() : null,\n updatedAt: entity.updatedAt ? entity.updatedAt.toISOString() : null,\n ...(custom && Object.keys(custom).length ? { custom } : {}),\n }\n}\n\nfunction captureOrganizationSnapshots(\n entity: Organization,\n childParents: ChildParentSnapshot[],\n custom?: Record<string, unknown> | null\n): OrganizationSnapshots {\n const tenantId = resolveTenantIdFromEntity(entity)\n return {\n view: serializeOrganization(entity, custom),\n undo: {\n id: String(entity.id),\n tenantId,\n createdAt: entity.createdAt ? entity.createdAt.toISOString() : null,\n name: entity.name,\n slug: entity.slug ?? null,\n logoUrl: entity.logoUrl ?? null,\n logoPreserveAspectRatio: !!entity.logoPreserveAspectRatio,\n isActive: !!entity.isActive,\n parentId: entity.parentId ?? null,\n childParents: (childParents ?? []).map((entry) => ({\n childId: String(entry.childId),\n parentId: entry.parentId,\n })),\n ...(custom && Object.keys(custom).length ? { custom } : {}),\n },\n }\n}\n\nasync function loadChildParentSnapshots(\n em: EntityManager,\n tenantId: string | null,\n childIds: Iterable<string>\n): Promise<ChildParentSnapshot[]> {\n if (!tenantId) return []\n const ids = Array.from(new Set(Array.from(childIds ?? []).map((id) => String(id)).filter(Boolean)))\n if (!ids.length) return []\n const filter: FilterQuery<Organization> = {\n tenant: tenantId,\n deletedAt: null,\n id: { $in: ids },\n } as unknown as FilterQuery<Organization>\n const children = await em.find(Organization, filter)\n if (!children.length) return []\n const map = new Map(children.map((child) => [String(child.id), child.parentId ? String(child.parentId) : null]))\n return ids\n .filter((id) => map.has(id))\n .map((id) => ({\n childId: id,\n parentId: map.get(id) ?? null,\n }))\n}\n\nasync function restoreChildParents(em: EntityManager, tenantId: string, snapshots: ChildParentSnapshot[]) {\n if (!snapshots?.length) return\n const ids = Array.from(new Set(snapshots.map((entry) => entry.childId).filter(Boolean)))\n if (!ids.length) return\n const filter: FilterQuery<Organization> = {\n tenant: tenantId,\n deletedAt: null,\n id: { $in: ids },\n } as unknown as FilterQuery<Organization>\n const children = await em.find(Organization, filter)\n if (!children.length) return\n const desired = new Map(snapshots.map((entry) => [entry.childId, entry.parentId ?? null]))\n const toPersist: Organization[] = []\n for (const child of children) {\n const id = String(child.id)\n if (!desired.has(id)) continue\n const nextParent = desired.get(id) ?? null\n if (child.parentId !== nextParent) {\n child.parentId = nextParent\n toPersist.push(child)\n }\n }\n if (toPersist.length) await em.persist(toPersist).flush()\n}\n\nfunction normalizeChildIds(ids: readonly string[], exclude: string[]): string[] {\n const excludeSet = new Set(exclude)\n return Array.from(new Set(ids)).filter((id) => !excludeSet.has(id))\n}\n\nasync function ensureParentExists(em: EntityManager, tenantId: string, parentId: string | null): Promise<void> {\n if (!parentId) return\n const parentFilter: FilterQuery<Organization> = { id: parentId, tenant: tenantId, deletedAt: null }\n const parent = await em.findOne(Organization, parentFilter)\n if (!parent) throw new CrudHttpError(400, { error: 'Parent not found' })\n}\n\nasync function ensureChildrenValid(em: EntityManager, tenantId: string, childIds: string[]): Promise<void> {\n if (!childIds.length) return\n const childFilter: FilterQuery<Organization> = { id: { $in: childIds }, tenant: tenantId, deletedAt: null }\n const children = await em.find(Organization, childFilter)\n if (children.length !== childIds.length) throw new CrudHttpError(400, { error: 'Invalid child assignment' })\n}\n\nasync function assignChildren(\n em: EntityManager,\n tenantId: string,\n recordId: string,\n desiredChildIds: Iterable<string>\n): Promise<void> {\n const targetIds = Array.from(new Set(desiredChildIds)).filter((id) => id !== recordId)\n if (!targetIds.length) return\n const filter: FilterQuery<Organization> = { tenant: tenantId, deletedAt: null, id: { $in: targetIds } }\n const children = await em.find(Organization, filter)\n const toPersist: Organization[] = []\n for (const child of children) {\n if (String(child.id) === recordId) continue\n if (child.parentId !== recordId) {\n child.parentId = recordId\n toPersist.push(child)\n }\n }\n if (toPersist.length) await em.persist(toPersist).flush()\n}\n\nasync function clearRemovedChildren(em: EntityManager, tenantId: string, recordId: string, desiredChildIds: Set<string>): Promise<void> {\n const currentFilter: FilterQuery<Organization> = { tenant: tenantId, parentId: recordId, deletedAt: null }\n const current = await em.find(Organization, currentFilter)\n const toPersist = current.filter((child) => !desiredChildIds.has(String(child.id)))\n if (!toPersist.length) return\n for (const child of toPersist) child.parentId = null\n await em.persist(toPersist).flush()\n}\n\nasync function resolveUniqueSlug(em: EntityManager, tenantId: string, baseSlug: string, excludeId?: string): Promise<string> {\n let candidate = baseSlug\n let suffix = 0\n const maxAttempts = 50\n while (suffix < maxAttempts) {\n const filter: FilterQuery<Organization> = {\n tenant: tenantId,\n slug: candidate,\n } as unknown as FilterQuery<Organization>\n const existing = await em.findOne(Organization, filter)\n if (!existing || (excludeId && String(existing.id) === excludeId)) return candidate\n suffix += 1\n candidate = `${baseSlug}-${suffix}`\n }\n return `${baseSlug}-${Date.now()}`\n}\n\nconst createOrganizationCommand: CommandHandler<Record<string, unknown>, Organization> = {\n id: 'directory.organizations.create',\n async execute(rawInput, ctx) {\n const { parsed, custom } = parseWithCustomFields(organizationCreateSchema, rawInput)\n const em = (ctx.container.resolve('em') as EntityManager)\n const tenantId = await enforceTenantSelection(ctx, parsed.tenantId)\n if (!tenantId) throw new CrudHttpError(400, { error: 'Tenant scope required' })\n\n const parentId = parsed.parentId ?? null\n const childIds = normalizeChildIds(parsed.childIds ?? [], parentId ? [parentId] : [])\n if (parentId && childIds.includes(parentId)) throw new CrudHttpError(400, { error: 'Child cannot equal parent' })\n\n const tenantRef = em.getReference(Tenant, tenantId)\n const baseSlug = parsed.slug ? parsed.slug : slugify(parsed.name)\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n\n let organization!: Organization\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n if (parentId) {\n await ensureParentExists(em, tenantId, parentId)\n }\n await ensureChildrenValid(em, tenantId, childIds)\n const childParentsBefore = await loadChildParentSnapshots(em, tenantId, childIds)\n const slug = baseSlug ? await resolveUniqueSlug(em, tenantId, baseSlug) : null\n organization = await de.createOrmEntity({\n entity: Organization,\n data: {\n tenant: tenantRef,\n name: parsed.name,\n slug,\n logoUrl: parsed.logoUrl ?? null,\n logoPreserveAspectRatio: parsed.logoPreserveAspectRatio ?? false,\n isActive: parsed.isActive ?? true,\n parentId,\n },\n })\n setInternalTenantId(organization, tenantId)\n const recordId = String(organization.id)\n\n if (childIds.length) {\n await assignChildren(em, tenantId, recordId, childIds)\n }\n const childParentsAfter = await loadChildParentSnapshots(em, tenantId, childIds)\n setUndoMeta(organization, { childParentsBefore, childParentsAfter })\n\n await setCustomFieldsIfAny({\n dataEngine: de,\n entityId: E.directory.organization,\n recordId,\n tenantId,\n organizationId: recordId,\n values: custom,\n })\n\n await rebuildHierarchyForTenant(em, tenantId)\n },\n ], { transaction: true })\n\n const recordId = String(organization.id)\n const identifiers = { id: recordId, organizationId: recordId, tenantId }\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'created',\n entity: organization,\n identifiers,\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n\n return organization\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const tenantId = resolveTenantIdFromEntity(result)\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.directory.organization,\n recordId: String(result.id),\n tenantId,\n organizationId: String(result.id),\n })\n return serializeOrganization(result, custom)\n },\n buildLog: async ({ result, ctx }) => {\n const { translate } = await resolveTranslations()\n const meta = getUndoMeta(result)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const tenantId = resolveTenantIdFromEntity(result)\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.directory.organization,\n recordId: String(result.id),\n tenantId,\n organizationId: String(result.id),\n })\n const afterSnapshots = captureOrganizationSnapshots(result, meta.childParentsAfter ?? [], custom)\n return {\n actionLabel: translate('directory.audit.organizations.create', 'Create organization'),\n resourceKind: 'directory.organization',\n resourceId: String(result.id),\n tenantId,\n snapshotAfter: afterSnapshots.view,\n payload: {\n undo: {\n after: afterSnapshots.undo,\n childrenBefore: meta.childParentsBefore ?? [],\n },\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<OrganizationUndoPayload>(logEntry)\n const after = payload?.after\n const childrenBefore = payload?.childrenBefore ?? []\n if (!after) return\n const tenantId = after.tenantId\n if (!tenantId) return\n await assertOrganizationUndoTenantAccess(ctx, tenantId)\n const em = (ctx.container.resolve('em') as EntityManager)\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n let organization: Organization | null = null\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n await restoreChildParents(em, tenantId, childrenBefore)\n if (after.custom && Object.keys(after.custom).length) {\n const reset = buildCustomFieldResetMap(undefined, after.custom)\n if (Object.keys(reset).length) {\n const resetValues = reset as Parameters<DataEngine['setCustomFields']>[0]['values']\n await de.setCustomFields({\n entityId: E.directory.organization,\n recordId: after.id,\n tenantId,\n organizationId: after.id,\n values: resetValues,\n notify: false,\n })\n }\n }\n organization = await de.deleteOrmEntity({\n entity: Organization,\n where: buildOrganizationMutationFilter(after.id, tenantId),\n soft: true,\n softDeleteField: 'deletedAt',\n })\n if (organization) {\n organization.isActive = false\n organization.parentId = null\n await em.flush()\n }\n await rebuildHierarchyForTenant(em, tenantId)\n },\n ], { transaction: true })\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'deleted',\n entity: organization,\n identifiers: { id: after.id, tenantId, organizationId: after.id },\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n },\n redo: async ({ logEntry, ctx }) => {\n const after = resolveRedoSnapshot<OrganizationUndoSnapshot>(logEntry)\n if (!after) throw new CrudHttpError(400, { error: '[internal] redo snapshot unavailable for organization create' })\n const tenantId = after.tenantId\n if (!tenantId) throw new CrudHttpError(400, { error: '[internal] redo snapshot missing tenant for organization create' })\n await assertOrganizationUndoTenantAccess(ctx, tenantId)\n const em = (ctx.container.resolve('em') as EntityManager)\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n let organization!: Organization\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n const existing = await em.findOne(Organization, buildOrganizationUndoTargetFilter(after.id, tenantId))\n if (existing) {\n existing.deletedAt = null\n existing.name = after.name\n if (after.slug !== undefined) existing.slug = after.slug ?? null\n if (after.logoUrl !== undefined) existing.logoUrl = after.logoUrl ?? null\n if (after.logoPreserveAspectRatio !== undefined) existing.logoPreserveAspectRatio = !!after.logoPreserveAspectRatio\n existing.isActive = after.isActive\n existing.parentId = after.parentId\n await em.flush()\n organization = existing\n } else {\n const snapshotAfter = logEntry.snapshotAfter\n const createdAt = after.createdAt ?? (\n snapshotAfter && typeof snapshotAfter === 'object' && 'createdAt' in snapshotAfter\n ? snapshotAfter.createdAt\n : undefined\n )\n const creationTime = typeof createdAt === 'string' || createdAt instanceof Date\n ? reviveSnapshotDates({ createdAt }, ['createdAt'])\n : {}\n organization = await de.createOrmEntity({\n entity: Organization,\n data: {\n id: after.id,\n ...creationTime,\n name: after.name,\n slug: after.slug ?? null,\n logoUrl: after.logoUrl ?? null,\n logoPreserveAspectRatio: after.logoPreserveAspectRatio ?? false,\n tenant: em.getReference(Tenant, tenantId),\n isActive: after.isActive,\n parentId: after.parentId,\n },\n })\n }\n setInternalTenantId(organization, tenantId)\n\n if (after.custom && Object.keys(after.custom).length) {\n const reset = buildCustomFieldResetMap(after.custom, undefined)\n if (Object.keys(reset).length) {\n const resetValues = reset as Parameters<DataEngine['setCustomFields']>[0]['values']\n await de.setCustomFields({\n entityId: E.directory.organization,\n recordId: after.id,\n tenantId,\n organizationId: after.id,\n values: resetValues,\n notify: false,\n })\n }\n }\n\n await assignChildren(em, tenantId, after.id, after.childParents.map((entry) => entry.childId))\n await rebuildHierarchyForTenant(em, tenantId)\n },\n ], { transaction: true })\n\n const identifiers = { id: after.id, organizationId: after.id, tenantId }\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'created',\n entity: organization,\n identifiers,\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n\n return organization\n },\n}\n\nconst updateOrganizationCommand: CommandHandler<Record<string, unknown>, Organization> = {\n id: 'directory.organizations.update',\n async prepare(rawInput, ctx) {\n const { parsed } = parseWithCustomFields(organizationUpdateSchema, rawInput)\n const em = (ctx.container.resolve('em') as EntityManager)\n const { organization: current, tenantId } = await loadAuthorizedOrganizationTarget(em, parsed.id, ctx)\n const currentChildIds = Array.isArray(current.childIds) ? current.childIds : []\n const requestedChildIds = Array.isArray(parsed.childIds) ? parsed.childIds : []\n const combinedChildIds = new Set<string>([...currentChildIds.map(String), ...requestedChildIds.map(String)])\n const childParentsBefore = tenantId\n ? await loadChildParentSnapshots(em, tenantId, combinedChildIds)\n : []\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.directory.organization,\n recordId: String(current.id),\n tenantId,\n organizationId: String(current.id),\n })\n return { before: captureOrganizationSnapshots(current, childParentsBefore, custom) }\n },\n async execute(rawInput, ctx) {\n const { parsed, custom } = parseWithCustomFields(organizationUpdateSchema, rawInput)\n const em = (ctx.container.resolve('em') as EntityManager)\n const { tenantId } = await loadAuthorizedOrganizationTarget(em, parsed.id, ctx)\n\n const parentProvided = parsed.parentId !== undefined\n const childrenProvided = parsed.childIds !== undefined\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n\n let resolvedOrganization!: Organization\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n const existing = await em.findOne(\n Organization,\n buildOrganizationMutationFilter(parsed.id, tenantId),\n { refresh: true },\n )\n if (!existing) throw new CrudHttpError(404, { error: 'Not found' })\n\n const parentId = parentProvided ? parsed.parentId ?? null : existing.parentId ?? null\n if (parentProvided && parentId) {\n if (parentId === parsed.id) throw new CrudHttpError(400, { error: 'Organization cannot be its own parent' })\n if (Array.isArray(existing.descendantIds) && existing.descendantIds.includes(parentId)) {\n throw new CrudHttpError(400, { error: 'Cannot assign descendant as parent' })\n }\n await ensureParentExists(em, tenantId, parentId)\n }\n\n const normalizedChildIds = normalizeChildIds(parsed.childIds ?? [], [parsed.id, parentProvided ? parentId ?? '' : ''])\n if (normalizedChildIds.some((id) => id === parentId)) throw new CrudHttpError(400, { error: 'Child cannot equal parent' })\n if (Array.isArray(existing.ancestorIds) && normalizedChildIds.some((id) => existing.ancestorIds.includes(id))) {\n throw new CrudHttpError(400, { error: 'Cannot assign ancestor as child' })\n }\n\n if (normalizedChildIds.length) {\n await ensureChildrenValid(em, tenantId, normalizedChildIds)\n const childFilter = {\n tenant: tenantId,\n deletedAt: null,\n id: { $in: normalizedChildIds },\n } as unknown as FilterQuery<Organization>\n const children = await em.find(Organization, childFilter)\n for (const child of children) {\n if (Array.isArray(child.descendantIds) && child.descendantIds.includes(parsed.id)) {\n throw new CrudHttpError(400, { error: 'Cannot assign descendant cycle' })\n }\n }\n }\n\n const combinedChildIds = new Set<string>([\n ...normalizedChildIds.map(String),\n ...(Array.isArray(existing.childIds) ? existing.childIds.map(String) : []),\n ])\n const childParentsBefore = await loadChildParentSnapshots(em, tenantId, combinedChildIds)\n\n let resolvedSlug: string | null | undefined\n if (parsed.slug !== undefined) {\n resolvedSlug = parsed.slug ? await resolveUniqueSlug(em, tenantId, parsed.slug, parsed.id) : parsed.slug\n }\n\n const organization = await de.updateOrmEntity({\n entity: Organization,\n where: buildOrganizationMutationFilter(parsed.id, tenantId),\n apply: (entity) => {\n if (parsed.name !== undefined) entity.name = parsed.name\n if (resolvedSlug !== undefined) entity.slug = resolvedSlug\n if (parsed.logoUrl !== undefined) entity.logoUrl = parsed.logoUrl ?? null\n if (parsed.logoPreserveAspectRatio !== undefined) entity.logoPreserveAspectRatio = parsed.logoPreserveAspectRatio\n if (parsed.isActive !== undefined) entity.isActive = parsed.isActive\n if (parentProvided) entity.parentId = parentId\n },\n })\n if (!organization) throw new CrudHttpError(404, { error: 'Not found' })\n setInternalTenantId(organization, tenantId)\n\n const recordId = String(organization.id)\n const desiredChildIds = new Set(normalizedChildIds.filter((id) => id !== recordId))\n if (childrenProvided) {\n await clearRemovedChildren(em, tenantId, recordId, desiredChildIds)\n await assignChildren(em, tenantId, recordId, desiredChildIds)\n }\n const childParentsAfter = await loadChildParentSnapshots(em, tenantId, combinedChildIds)\n setUndoMeta(organization, { childParentsBefore, childParentsAfter })\n\n await setCustomFieldsIfAny({\n dataEngine: de,\n entityId: E.directory.organization,\n recordId,\n tenantId,\n organizationId: recordId,\n values: custom,\n })\n\n await rebuildHierarchyForTenant(em, tenantId)\n resolvedOrganization = organization\n },\n ], { transaction: true })\n\n const recordId = String(resolvedOrganization.id)\n const identifiers = { id: recordId, organizationId: recordId, tenantId }\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: resolvedOrganization,\n identifiers,\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n\n return resolvedOrganization\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const tenantId = resolveTenantIdFromEntity(result)\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.directory.organization,\n recordId: String(result.id),\n tenantId,\n organizationId: String(result.id),\n })\n return serializeOrganization(result, custom)\n },\n buildLog: async ({ snapshots, result, ctx }) => {\n const { translate } = await resolveTranslations()\n const meta = getUndoMeta(result)\n const beforeSnapshots = snapshots.before as OrganizationSnapshots | undefined\n const beforeRecord = beforeSnapshots?.view ?? null\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const tenantId = resolveTenantIdFromEntity(result)\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.directory.organization,\n recordId: String(result.id),\n tenantId,\n organizationId: String(result.id),\n })\n const after = serializeOrganization(result, custom)\n const changes = buildChanges(beforeRecord, after as Record<string, unknown>, ['name', 'slug', 'logoUrl', 'logoPreserveAspectRatio', 'isActive', 'parentId'])\n const customDiff = diffCustomFieldChanges(beforeRecord?.custom, custom)\n for (const [key, diff] of Object.entries(customDiff)) {\n changes[`cf_${key}`] = diff\n }\n return {\n actionLabel: translate('directory.audit.organizations.update', 'Update organization'),\n resourceKind: 'directory.organization',\n resourceId: String(result.id),\n changes,\n tenantId: after.tenantId,\n payload: {\n undo: {\n before: beforeSnapshots?.undo ?? null,\n after: captureOrganizationSnapshots(result, meta.childParentsAfter ?? [], custom).undo,\n },\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<OrganizationUndoPayload>(logEntry)\n const before = payload?.before\n const after = payload?.after\n if (!before) return\n const tenantId = before.tenantId\n if (!tenantId) return\n await assertOrganizationUndoTenantAccess(ctx, tenantId)\n const em = (ctx.container.resolve('em') as EntityManager)\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n let updated: Organization | null = null\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n updated = await de.updateOrmEntity({\n entity: Organization,\n where: buildOrganizationUndoTargetFilter(before.id, tenantId),\n apply: (entity) => {\n entity.name = before.name\n if (before.slug !== undefined) entity.slug = before.slug\n if (before.logoUrl !== undefined) entity.logoUrl = before.logoUrl ?? null\n if (before.logoPreserveAspectRatio !== undefined) entity.logoPreserveAspectRatio = !!before.logoPreserveAspectRatio\n entity.isActive = before.isActive\n entity.parentId = before.parentId\n },\n })\n if (!updated) throw new CrudHttpError(404, { error: 'Not found' })\n setInternalTenantId(updated, tenantId)\n const reset = buildCustomFieldResetMap(before.custom, after?.custom)\n if (Object.keys(reset).length) {\n const resetValues = reset as Parameters<DataEngine['setCustomFields']>[0]['values']\n await de.setCustomFields({\n entityId: E.directory.organization,\n recordId: before.id,\n tenantId,\n organizationId: before.id,\n values: resetValues,\n notify: false,\n })\n }\n const childSnapshots = before.childParents\n await restoreChildParents(em, tenantId, childSnapshots)\n await rebuildHierarchyForTenant(em, tenantId)\n },\n ], { transaction: true })\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: updated,\n identifiers: {\n id: before.id,\n tenantId,\n organizationId: before.id,\n },\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n },\n}\n\nconst deleteOrganizationCommand: CommandHandler<{ body: any; query: Record<string, string> }, Organization> = {\n id: 'directory.organizations.delete',\n async prepare(input, ctx) {\n const id = requireId(input, 'Organization id required')\n const em = (ctx.container.resolve('em') as EntityManager)\n const { organization: existing, tenantId } = await loadAuthorizedOrganizationTarget(em, id, ctx)\n const childParentsBefore = tenantId\n ? await loadChildParentSnapshots(em, tenantId, Array.isArray(existing.childIds) ? existing.childIds : [])\n : []\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.directory.organization,\n recordId: String(existing.id),\n tenantId,\n organizationId: String(existing.id),\n })\n return { before: captureOrganizationSnapshots(existing, childParentsBefore, custom) }\n },\n async execute(input, ctx) {\n const id = requireId(input, 'Organization id required')\n const em = (ctx.container.resolve('em') as EntityManager)\n const { organization: existing, tenantId } = await loadAuthorizedOrganizationTarget(em, id, ctx)\n\n const parentId = existing.parentId ?? null\n const childSnapshotsBefore = await loadChildParentSnapshots(\n em,\n tenantId,\n Array.isArray(existing.childIds) ? existing.childIds : [],\n )\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n\n let resolvedDeleted!: Organization\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n const deleted = await de.deleteOrmEntity({\n entity: Organization,\n where: buildOrganizationMutationFilter(id, tenantId),\n soft: true,\n softDeleteField: 'deletedAt',\n })\n if (!deleted) throw new CrudHttpError(404, { error: 'Not found' })\n setInternalTenantId(deleted, tenantId)\n deleted.isActive = false\n deleted.parentId = null\n resolvedDeleted = deleted\n },\n async () => {\n const deleted = resolvedDeleted\n const childrenFilter: FilterQuery<Organization> = { tenant: tenantId, parentId: id, deletedAt: null }\n const children = await em.find(Organization, childrenFilter)\n const toPersist: Organization[] = []\n for (const child of children) {\n child.parentId = parentId\n toPersist.push(child)\n }\n toPersist.push(deleted)\n if (toPersist.length) em.persist(toPersist)\n setUndoMeta(deleted, { childParentsBefore: childSnapshotsBefore })\n },\n async () => {\n await rebuildHierarchyForTenant(em, tenantId)\n },\n ], { transaction: true })\n\n const identifiers = { id, organizationId: id, tenantId }\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'deleted',\n entity: resolvedDeleted,\n identifiers,\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n\n return resolvedDeleted\n },\n buildLog: async ({ snapshots, input }) => {\n const { translate } = await resolveTranslations()\n const beforeSnapshots = snapshots.before as OrganizationSnapshots | undefined\n const beforeSnapshot = beforeSnapshots?.view ?? null\n const beforeUndo = beforeSnapshots?.undo ?? null\n const id = String(input?.body?.id ?? input?.query?.id ?? '')\n const fallbackId = beforeSnapshot?.id ?? null\n const fallbackTenant = beforeSnapshot?.tenantId ?? null\n return {\n actionLabel: translate('directory.audit.organizations.delete', 'Delete organization'),\n resourceKind: 'directory.organization',\n resourceId: id || fallbackId || null,\n snapshotBefore: beforeSnapshot ?? null,\n tenantId: beforeUndo?.tenantId ?? fallbackTenant,\n payload: {\n undo: {\n before: beforeUndo,\n },\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<OrganizationUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const tenantId = before.tenantId\n if (!tenantId) return\n await assertOrganizationUndoTenantAccess(ctx, tenantId)\n const em = (ctx.container.resolve('em') as EntityManager)\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n let organization: Organization | null = null\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n organization = await em.findOne(\n Organization,\n buildOrganizationUndoTargetFilter(before.id, tenantId),\n )\n if (organization) {\n organization.deletedAt = null\n organization.isActive = before.isActive\n organization.name = before.name\n if (before.slug !== undefined) organization.slug = before.slug\n organization.parentId = before.parentId\n await em.flush()\n if (tenantId) setInternalTenantId(organization, tenantId)\n } else {\n organization = await de.createOrmEntity({\n entity: Organization,\n data: {\n id: before.id,\n name: before.name,\n slug: before.slug ?? null,\n tenant: tenantId ? em.getReference(Tenant, tenantId) : undefined,\n isActive: before.isActive,\n parentId: before.parentId,\n },\n })\n if (tenantId) setInternalTenantId(organization, tenantId)\n }\n if (tenantId) {\n const customValues = buildCustomFieldResetMap(before.custom, undefined)\n if (Object.keys(customValues).length) {\n const resetValues = customValues as Parameters<DataEngine['setCustomFields']>[0]['values']\n await de.setCustomFields({\n entityId: E.directory.organization,\n recordId: before.id,\n tenantId,\n organizationId: before.id,\n values: resetValues,\n notify: false,\n })\n }\n }\n await restoreChildParents(em, tenantId, before.childParents)\n await rebuildHierarchyForTenant(em, tenantId)\n },\n ], { transaction: true })\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: organization,\n identifiers: {\n id: before.id,\n tenantId,\n organizationId: before.id,\n },\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n },\n}\n\ntype OrganizationUndoPayload = {\n before?: OrganizationUndoSnapshot | null\n after?: OrganizationUndoSnapshot | null\n childrenBefore?: ChildParentSnapshot[] | null\n}\n\ntype OrganizationUndoMeta = {\n childParentsBefore?: ChildParentSnapshot[]\n childParentsAfter?: ChildParentSnapshot[]\n}\n\nconst UNDO_META_KEY: unique symbol = Symbol('directory.organization.undoMeta')\n\nfunction getUndoMeta(entity: Organization): OrganizationUndoMeta {\n return (Reflect.get(entity, UNDO_META_KEY) as OrganizationUndoMeta | undefined) ?? {}\n}\n\nfunction setUndoMeta(entity: Organization, meta: Partial<OrganizationUndoMeta>) {\n const current = getUndoMeta(entity)\n Reflect.set(entity, UNDO_META_KEY, { ...current, ...meta })\n}\n\nregisterCommand(createOrganizationCommand)\nregisterCommand(updateOrganizationCommand)\nregisterCommand(deleteOrganizationCommand)\n\nfunction setInternalTenantId(entity: Organization, tenantId: string) {\n Reflect.set(entity, '__tenantId', tenantId)\n}\n\nfunction tenantIdFromContext(ctx: CrudEmitContext<Organization>): string | null {\n return resolveTenantIdFromEntity(ctx.entity) ?? ctx.identifiers.tenantId ?? null\n}\n"],
5
- "mappings": "AACA,SAAS,uBAAuB;AAChC,SAAS,qBAAqB;AAG9B,SAAS,cAAc,cAAc;AACrC,SAAS,0BAA0B,gCAAgC;AACnE;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,SAAS;AAElB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,oBAAoB,2BAA2B;AACxD,SAAS,2BAA2B;AACpC,SAAS,uBAAuB;AAChC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,2BAA2B;AACpC,SAAS,wBAAwB;AACjC,SAAS,eAAe;AAEjB,MAAM,yBAA2C;AAAA,EACtD,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,cAAc,CAAC,QAAQ;AACrB,UAAM,SAAS;AACf,WAAO;AAAA,MACL,IAAI,OAAO,YAAY;AAAA,MACvB,UAAU,oBAAoB,MAAM;AAAA,MACpC,gBAAgB,OAAO,YAAY;AAAA,IACrC;AAAA,EACF;AACF;AAEO,MAAM,0BAA6C;AAAA,EACxD,YAAY,EAAE,UAAU;AAAA,EACxB,oBAAoB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AAA,QACX,IAAI,OAAO;AAAA,QACX;AAAA,QACA,gBAAgB,OAAO;AAAA,MACzB;AAAA,MACA,QAAQ;AAAA,MACR,SAAS;AAAA,IACX,CAAC;AAAA,EACH;AACF;AAaA,MAAM,gBAA+B,uBAAO,iCAAiC;AAE7E,SAAS,YAAY,QAA4C;AAC/D,SAAQ,QAAQ,IAAI,QAAQ,aAAa,KAA0C,CAAC;AACtF;AAEA,SAAS,YAAY,QAAsB,MAAqC;AAC9E,QAAM,UAAU,YAAY,MAAM;AAClC,UAAQ,IAAI,QAAQ,eAAe,EAAE,GAAG,SAAS,GAAG,KAAK,CAAC;AAC5D;AAEA,gBAAgB,yBAAyB;AACzC,gBAAgB,yBAAyB;AACzC,gBAAgB,yBAAyB;AAEzC,SAAS,oBAAoB,QAAsB,UAAkB;AACnE,UAAQ,IAAI,QAAQ,cAAc,QAAQ;AAC5C;AAEA,SAAS,oBAAoB,KAAmD;AAC9E,SAAO,0BAA0B,IAAI,MAAM,KAAK,IAAI,YAAY,YAAY;AAC9E;",
4
+ "sourcesContent": ["import type { CommandHandler, CommandRuntimeContext } from '@open-mercato/shared/lib/commands'\nimport { registerCommand } from '@open-mercato/shared/lib/commands'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport type { EntityManager, FilterQuery } from '@mikro-orm/postgresql'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport { Organization, Tenant } from '@open-mercato/core/modules/directory/data/entities'\nimport { organizationCreateSchema, organizationUpdateSchema } from '@open-mercato/core/modules/directory/data/validators'\nimport {\n findOrganizationParentCycle,\n lockOrganizationHierarchyForTenant,\n rebuildHierarchyForTenant,\n} from '@open-mercato/core/modules/directory/lib/hierarchy'\nimport {\n enforceTenantSelection,\n normalizeTenantId,\n resolveIsSuperAdmin,\n} from '@open-mercato/core/modules/auth/lib/tenantAccess'\nimport { E } from '#generated/entities.ids.generated'\nimport type { CrudEmitContext, CrudEventsConfig, CrudIndexerConfig } from '@open-mercato/shared/lib/crud/types'\nimport {\n loadCustomFieldSnapshot,\n buildCustomFieldResetMap,\n diffCustomFieldChanges,\n} from '@open-mercato/shared/lib/commands/customFieldSnapshots'\nimport { extractUndoPayload, reviveSnapshotDates } from '@open-mercato/shared/lib/commands/undo'\nimport { resolveRedoSnapshot } from '@open-mercato/shared/lib/commands/redo'\nimport { withAtomicFlush } from '@open-mercato/shared/lib/commands/flush'\nimport {\n parseWithCustomFields,\n setCustomFieldsIfAny,\n emitCrudSideEffects,\n emitCrudUndoSideEffects,\n requireId,\n buildChanges,\n} from '@open-mercato/shared/lib/commands/helpers'\nimport { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { toOptionalString } from '@open-mercato/shared/lib/string/coerce'\nimport { slugify } from '@open-mercato/shared/lib/slugify'\n\nexport const organizationCrudEvents: CrudEventsConfig = {\n module: 'directory',\n entity: 'organization',\n persistent: true,\n buildPayload: (ctx) => {\n const orgCtx = ctx as CrudEmitContext<Organization>\n return {\n id: orgCtx.identifiers.id,\n tenantId: tenantIdFromContext(orgCtx),\n organizationId: orgCtx.identifiers.id,\n }\n },\n}\n\nexport const organizationCrudIndexer: CrudIndexerConfig = {\n entityType: E.directory.organization,\n buildUpsertPayload: (ctx) => {\n const orgCtx = ctx as CrudEmitContext<Organization>\n return {\n entityType: E.directory.organization,\n recordId: orgCtx.identifiers.id,\n organizationId: orgCtx.identifiers.id,\n tenantId: tenantIdFromContext(orgCtx),\n }\n },\n buildDeletePayload: (ctx) => {\n const orgCtx = ctx as CrudEmitContext<Organization>\n return {\n entityType: E.directory.organization,\n recordId: orgCtx.identifiers.id,\n organizationId: orgCtx.identifiers.id,\n tenantId: tenantIdFromContext(orgCtx),\n }\n },\n}\n\ntype OrganizationTenantShape = {\n __tenantId?: unknown\n tenant?: string | { id?: unknown; getEntity?: () => { id?: unknown } }\n tenantId?: unknown\n tenant_id?: unknown\n}\n\ntype SerializedOrganization = ReturnType<typeof serializeOrganization>\n\ntype ChildParentSnapshot = {\n childId: string\n parentId: string | null\n}\n\ntype OrganizationUndoSnapshot = {\n id: string\n tenantId: string | null\n createdAt?: string | null\n name: string\n slug?: string | null\n logoUrl?: string | null\n logoPreserveAspectRatio?: boolean\n isActive: boolean\n parentId: string | null\n childParents: ChildParentSnapshot[]\n custom?: Record<string, unknown>\n}\n\ntype OrganizationSnapshots = {\n view: SerializedOrganization\n undo: OrganizationUndoSnapshot\n}\n\ntype OrganizationActorScope = {\n isUnrestricted: boolean\n tenantId: string | null\n}\n\ntype AuthorizedOrganizationTarget = {\n organization: Organization\n tenantId: string\n}\n\nexport function resolveTenantIdFromEntity(entity: Organization): string | null {\n const shape = entity as unknown as OrganizationTenantShape\n const cached = toOptionalString(shape.__tenantId)\n if (cached) return cached\n const tenantRef = shape.tenant\n if (typeof tenantRef === 'string') return tenantRef\n if (tenantRef && typeof tenantRef === 'object') {\n const direct = toOptionalString(tenantRef.id)\n if (direct) return direct\n if (typeof tenantRef.getEntity === 'function') {\n const nested = tenantRef.getEntity()\n const nestedId = nested ? toOptionalString(nested.id) : null\n if (nestedId) return nestedId\n }\n }\n const fallback = toOptionalString(shape.tenantId) || toOptionalString(shape.tenant_id)\n return fallback\n}\n\nasync function resolveOrganizationActorScope(ctx: CommandRuntimeContext): Promise<OrganizationActorScope> {\n if (ctx.systemActor === true) return { isUnrestricted: true, tenantId: null }\n const tenantId = normalizeTenantId(ctx.auth?.tenantId ?? null) ?? null\n const isSuperAdmin = await resolveIsSuperAdmin(ctx)\n return { isUnrestricted: isSuperAdmin, tenantId }\n}\n\nfunction buildOrganizationTargetFilter(\n id: string,\n scope: OrganizationActorScope,\n): FilterQuery<Organization> | null {\n if (!scope.isUnrestricted && !scope.tenantId) return null\n return {\n id,\n deletedAt: null,\n ...(scope.isUnrestricted ? {} : { tenant: scope.tenantId }),\n } as FilterQuery<Organization>\n}\n\nasync function loadAuthorizedOrganizationTarget(\n em: EntityManager,\n id: string,\n ctx: CommandRuntimeContext,\n): Promise<AuthorizedOrganizationTarget> {\n const scope = await resolveOrganizationActorScope(ctx)\n const filter = buildOrganizationTargetFilter(id, scope)\n if (!filter) throw new CrudHttpError(404, { error: 'Not found' })\n const organization = await em.findOne(Organization, filter)\n if (!organization) throw new CrudHttpError(404, { error: 'Not found' })\n const tenantId = resolveTenantIdFromEntity(organization)\n if (!tenantId || (!scope.isUnrestricted && tenantId !== scope.tenantId)) {\n throw new CrudHttpError(404, { error: 'Not found' })\n }\n return { organization, tenantId }\n}\n\nfunction buildOrganizationMutationFilter(\n id: string,\n tenantId: string,\n): FilterQuery<Organization> {\n return {\n id,\n deletedAt: null,\n tenant: tenantId,\n } as FilterQuery<Organization>\n}\n\nasync function assertOrganizationUndoTenantAccess(\n ctx: CommandRuntimeContext,\n targetTenantId: string,\n): Promise<void> {\n const scope = await resolveOrganizationActorScope(ctx)\n if (!scope.isUnrestricted && scope.tenantId !== targetTenantId) {\n throw new CrudHttpError(404, { error: 'Not found' })\n }\n}\n\nfunction buildOrganizationUndoTargetFilter(\n id: string,\n tenantId: string,\n): FilterQuery<Organization> {\n return { id, tenant: tenantId } as FilterQuery<Organization>\n}\n\nfunction serializeOrganization(entity: Organization, custom?: Record<string, unknown> | null) {\n return {\n id: String(entity.id),\n tenantId: resolveTenantIdFromEntity(entity),\n name: entity.name,\n slug: entity.slug ?? null,\n logoUrl: entity.logoUrl ?? null,\n logoPreserveAspectRatio: !!entity.logoPreserveAspectRatio,\n isActive: !!entity.isActive,\n parentId: entity.parentId ?? null,\n ancestorIds: Array.isArray(entity.ancestorIds) ? [...entity.ancestorIds] : [],\n childIds: Array.isArray(entity.childIds) ? [...entity.childIds] : [],\n descendantIds: Array.isArray(entity.descendantIds) ? [...entity.descendantIds] : [],\n createdAt: entity.createdAt ? entity.createdAt.toISOString() : null,\n updatedAt: entity.updatedAt ? entity.updatedAt.toISOString() : null,\n ...(custom && Object.keys(custom).length ? { custom } : {}),\n }\n}\n\nfunction captureOrganizationSnapshots(\n entity: Organization,\n childParents: ChildParentSnapshot[],\n custom?: Record<string, unknown> | null\n): OrganizationSnapshots {\n const tenantId = resolveTenantIdFromEntity(entity)\n return {\n view: serializeOrganization(entity, custom),\n undo: {\n id: String(entity.id),\n tenantId,\n createdAt: entity.createdAt ? entity.createdAt.toISOString() : null,\n name: entity.name,\n slug: entity.slug ?? null,\n logoUrl: entity.logoUrl ?? null,\n logoPreserveAspectRatio: !!entity.logoPreserveAspectRatio,\n isActive: !!entity.isActive,\n parentId: entity.parentId ?? null,\n childParents: (childParents ?? []).map((entry) => ({\n childId: String(entry.childId),\n parentId: entry.parentId,\n })),\n ...(custom && Object.keys(custom).length ? { custom } : {}),\n },\n }\n}\n\nasync function loadChildParentSnapshots(\n em: EntityManager,\n tenantId: string | null,\n childIds: Iterable<string>\n): Promise<ChildParentSnapshot[]> {\n if (!tenantId) return []\n const ids = Array.from(new Set(Array.from(childIds ?? []).map((id) => String(id)).filter(Boolean)))\n if (!ids.length) return []\n const filter: FilterQuery<Organization> = {\n tenant: tenantId,\n deletedAt: null,\n id: { $in: ids },\n } as unknown as FilterQuery<Organization>\n const children = await em.find(Organization, filter)\n if (!children.length) return []\n const map = new Map(children.map((child) => [String(child.id), child.parentId ? String(child.parentId) : null]))\n return ids\n .filter((id) => map.has(id))\n .map((id) => ({\n childId: id,\n parentId: map.get(id) ?? null,\n }))\n}\n\nasync function restoreChildParents(em: EntityManager, tenantId: string, snapshots: ChildParentSnapshot[]) {\n if (!snapshots?.length) return\n const ids = Array.from(new Set(snapshots.map((entry) => entry.childId).filter(Boolean)))\n if (!ids.length) return\n const filter: FilterQuery<Organization> = {\n tenant: tenantId,\n deletedAt: null,\n id: { $in: ids },\n } as unknown as FilterQuery<Organization>\n const children = await em.find(Organization, filter)\n if (!children.length) return\n const desired = new Map(snapshots.map((entry) => [entry.childId, entry.parentId ?? null]))\n const toPersist: Organization[] = []\n for (const child of children) {\n const id = String(child.id)\n if (!desired.has(id)) continue\n const nextParent = desired.get(id) ?? null\n if (child.parentId !== nextParent) {\n child.parentId = nextParent\n toPersist.push(child)\n }\n }\n if (toPersist.length) await em.persist(toPersist).flush()\n}\n\nconst REPLAY_CYCLE_ERROR_KEY = 'directory.organizations.errors.replayCycle'\n\nasync function assertReplayKeepsHierarchyAcyclic(\n em: EntityManager,\n tenantId: string,\n organizationIds: Iterable<string>,\n): Promise<void> {\n const filter: FilterQuery<Organization> = {\n tenant: tenantId,\n deletedAt: null,\n } as unknown as FilterQuery<Organization>\n const organizations = await findWithDecryption(em, Organization, filter, undefined, {\n tenantId,\n organizationId: null,\n })\n if (!findOrganizationParentCycle(organizations, organizationIds)) return\n const { translate } = await resolveTranslations()\n throw new CrudHttpError(409, {\n error: translate(\n REPLAY_CYCLE_ERROR_KEY,\n \"The organization hierarchy changed since this action, so it can't be reverted without making an organization its own ancestor.\",\n ),\n code: REPLAY_CYCLE_ERROR_KEY,\n })\n}\n\nfunction normalizeChildIds(ids: readonly string[], exclude: string[]): string[] {\n const excludeSet = new Set(exclude)\n return Array.from(new Set(ids)).filter((id) => !excludeSet.has(id))\n}\n\nasync function ensureParentExists(em: EntityManager, tenantId: string, parentId: string | null): Promise<void> {\n if (!parentId) return\n const parentFilter: FilterQuery<Organization> = { id: parentId, tenant: tenantId, deletedAt: null }\n const parent = await em.findOne(Organization, parentFilter)\n if (!parent) throw new CrudHttpError(400, { error: 'Parent not found' })\n}\n\nasync function ensureChildrenValid(em: EntityManager, tenantId: string, childIds: string[]): Promise<void> {\n if (!childIds.length) return\n const childFilter: FilterQuery<Organization> = { id: { $in: childIds }, tenant: tenantId, deletedAt: null }\n const children = await em.find(Organization, childFilter)\n if (children.length !== childIds.length) throw new CrudHttpError(400, { error: 'Invalid child assignment' })\n}\n\nasync function assignChildren(\n em: EntityManager,\n tenantId: string,\n recordId: string,\n desiredChildIds: Iterable<string>\n): Promise<void> {\n const targetIds = Array.from(new Set(desiredChildIds)).filter((id) => id !== recordId)\n if (!targetIds.length) return\n const filter: FilterQuery<Organization> = { tenant: tenantId, deletedAt: null, id: { $in: targetIds } }\n const children = await em.find(Organization, filter)\n const toPersist: Organization[] = []\n for (const child of children) {\n if (String(child.id) === recordId) continue\n if (child.parentId !== recordId) {\n child.parentId = recordId\n toPersist.push(child)\n }\n }\n if (toPersist.length) await em.persist(toPersist).flush()\n}\n\nasync function clearRemovedChildren(em: EntityManager, tenantId: string, recordId: string, desiredChildIds: Set<string>): Promise<void> {\n const currentFilter: FilterQuery<Organization> = { tenant: tenantId, parentId: recordId, deletedAt: null }\n const current = await em.find(Organization, currentFilter)\n const toPersist = current.filter((child) => !desiredChildIds.has(String(child.id)))\n if (!toPersist.length) return\n for (const child of toPersist) child.parentId = null\n await em.persist(toPersist).flush()\n}\n\nasync function resolveUniqueSlug(em: EntityManager, tenantId: string, baseSlug: string, excludeId?: string): Promise<string> {\n let candidate = baseSlug\n let suffix = 0\n const maxAttempts = 50\n while (suffix < maxAttempts) {\n const filter: FilterQuery<Organization> = {\n tenant: tenantId,\n slug: candidate,\n } as unknown as FilterQuery<Organization>\n const existing = await em.findOne(Organization, filter)\n if (!existing || (excludeId && String(existing.id) === excludeId)) return candidate\n suffix += 1\n candidate = `${baseSlug}-${suffix}`\n }\n return `${baseSlug}-${Date.now()}`\n}\n\nconst createOrganizationCommand: CommandHandler<Record<string, unknown>, Organization> = {\n id: 'directory.organizations.create',\n async execute(rawInput, ctx) {\n const { parsed, custom } = parseWithCustomFields(organizationCreateSchema, rawInput)\n const em = (ctx.container.resolve('em') as EntityManager)\n const tenantId = await enforceTenantSelection(ctx, parsed.tenantId)\n if (!tenantId) throw new CrudHttpError(400, { error: 'Tenant scope required' })\n\n const parentId = parsed.parentId ?? null\n const childIds = normalizeChildIds(parsed.childIds ?? [], parentId ? [parentId] : [])\n if (parentId && childIds.includes(parentId)) throw new CrudHttpError(400, { error: 'Child cannot equal parent' })\n\n const tenantRef = em.getReference(Tenant, tenantId)\n const baseSlug = parsed.slug ? parsed.slug : slugify(parsed.name)\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n\n let organization!: Organization\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n if (parentId) {\n await ensureParentExists(em, tenantId, parentId)\n }\n await ensureChildrenValid(em, tenantId, childIds)\n const childParentsBefore = await loadChildParentSnapshots(em, tenantId, childIds)\n const slug = baseSlug ? await resolveUniqueSlug(em, tenantId, baseSlug) : null\n organization = await de.createOrmEntity({\n entity: Organization,\n data: {\n tenant: tenantRef,\n name: parsed.name,\n slug,\n logoUrl: parsed.logoUrl ?? null,\n logoPreserveAspectRatio: parsed.logoPreserveAspectRatio ?? false,\n isActive: parsed.isActive ?? true,\n parentId,\n },\n })\n setInternalTenantId(organization, tenantId)\n const recordId = String(organization.id)\n\n if (childIds.length) {\n await assignChildren(em, tenantId, recordId, childIds)\n }\n const childParentsAfter = await loadChildParentSnapshots(em, tenantId, childIds)\n setUndoMeta(organization, { childParentsBefore, childParentsAfter })\n\n await setCustomFieldsIfAny({\n dataEngine: de,\n entityId: E.directory.organization,\n recordId,\n tenantId,\n organizationId: recordId,\n values: custom,\n })\n\n await rebuildHierarchyForTenant(em, tenantId)\n },\n ], { transaction: true })\n\n const recordId = String(organization.id)\n const identifiers = { id: recordId, organizationId: recordId, tenantId }\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'created',\n entity: organization,\n identifiers,\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n\n return organization\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const tenantId = resolveTenantIdFromEntity(result)\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.directory.organization,\n recordId: String(result.id),\n tenantId,\n organizationId: String(result.id),\n })\n return serializeOrganization(result, custom)\n },\n buildLog: async ({ result, ctx }) => {\n const { translate } = await resolveTranslations()\n const meta = getUndoMeta(result)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const tenantId = resolveTenantIdFromEntity(result)\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.directory.organization,\n recordId: String(result.id),\n tenantId,\n organizationId: String(result.id),\n })\n const afterSnapshots = captureOrganizationSnapshots(result, meta.childParentsAfter ?? [], custom)\n return {\n actionLabel: translate('directory.audit.organizations.create', 'Create organization'),\n resourceKind: 'directory.organization',\n resourceId: String(result.id),\n tenantId,\n snapshotAfter: afterSnapshots.view,\n payload: {\n undo: {\n after: afterSnapshots.undo,\n childrenBefore: meta.childParentsBefore ?? [],\n },\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<OrganizationUndoPayload>(logEntry)\n const after = payload?.after\n const childrenBefore = payload?.childrenBefore ?? []\n if (!after) return\n const tenantId = after.tenantId\n if (!tenantId) return\n await assertOrganizationUndoTenantAccess(ctx, tenantId)\n const em = (ctx.container.resolve('em') as EntityManager)\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n let organization: Organization | null = null\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n await restoreChildParents(em, tenantId, childrenBefore)\n if (after.custom && Object.keys(after.custom).length) {\n const reset = buildCustomFieldResetMap(undefined, after.custom)\n if (Object.keys(reset).length) {\n const resetValues = reset as Parameters<DataEngine['setCustomFields']>[0]['values']\n await de.setCustomFields({\n entityId: E.directory.organization,\n recordId: after.id,\n tenantId,\n organizationId: after.id,\n values: resetValues,\n notify: false,\n })\n }\n }\n organization = await de.deleteOrmEntity({\n entity: Organization,\n where: buildOrganizationMutationFilter(after.id, tenantId),\n soft: true,\n softDeleteField: 'deletedAt',\n })\n if (organization) {\n organization.isActive = false\n organization.parentId = null\n await em.flush()\n }\n await assertReplayKeepsHierarchyAcyclic(\n em,\n tenantId,\n childrenBefore.map((entry) => entry.childId).filter((childId) => childId !== after.id),\n )\n await rebuildHierarchyForTenant(em, tenantId)\n },\n ], { transaction: true })\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'deleted',\n entity: organization,\n identifiers: { id: after.id, tenantId, organizationId: after.id },\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n },\n redo: async ({ logEntry, ctx }) => {\n const after = resolveRedoSnapshot<OrganizationUndoSnapshot>(logEntry)\n if (!after) throw new CrudHttpError(400, { error: '[internal] redo snapshot unavailable for organization create' })\n const tenantId = after.tenantId\n if (!tenantId) throw new CrudHttpError(400, { error: '[internal] redo snapshot missing tenant for organization create' })\n await assertOrganizationUndoTenantAccess(ctx, tenantId)\n const em = (ctx.container.resolve('em') as EntityManager)\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n let organization!: Organization\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n const existing = await em.findOne(Organization, buildOrganizationUndoTargetFilter(after.id, tenantId))\n if (existing) {\n existing.deletedAt = null\n existing.name = after.name\n if (after.slug !== undefined) existing.slug = after.slug ?? null\n if (after.logoUrl !== undefined) existing.logoUrl = after.logoUrl ?? null\n if (after.logoPreserveAspectRatio !== undefined) existing.logoPreserveAspectRatio = !!after.logoPreserveAspectRatio\n existing.isActive = after.isActive\n existing.parentId = after.parentId\n await em.flush()\n organization = existing\n } else {\n const snapshotAfter = logEntry.snapshotAfter\n const createdAt = after.createdAt ?? (\n snapshotAfter && typeof snapshotAfter === 'object' && 'createdAt' in snapshotAfter\n ? snapshotAfter.createdAt\n : undefined\n )\n const creationTime = typeof createdAt === 'string' || createdAt instanceof Date\n ? reviveSnapshotDates({ createdAt }, ['createdAt'])\n : {}\n organization = await de.createOrmEntity({\n entity: Organization,\n data: {\n id: after.id,\n ...creationTime,\n name: after.name,\n slug: after.slug ?? null,\n logoUrl: after.logoUrl ?? null,\n logoPreserveAspectRatio: after.logoPreserveAspectRatio ?? false,\n tenant: em.getReference(Tenant, tenantId),\n isActive: after.isActive,\n parentId: after.parentId,\n },\n })\n }\n setInternalTenantId(organization, tenantId)\n\n if (after.custom && Object.keys(after.custom).length) {\n const reset = buildCustomFieldResetMap(after.custom, undefined)\n if (Object.keys(reset).length) {\n const resetValues = reset as Parameters<DataEngine['setCustomFields']>[0]['values']\n await de.setCustomFields({\n entityId: E.directory.organization,\n recordId: after.id,\n tenantId,\n organizationId: after.id,\n values: resetValues,\n notify: false,\n })\n }\n }\n\n await assignChildren(em, tenantId, after.id, after.childParents.map((entry) => entry.childId))\n await assertReplayKeepsHierarchyAcyclic(em, tenantId, [\n after.id,\n ...after.childParents.map((entry) => entry.childId),\n ])\n await rebuildHierarchyForTenant(em, tenantId)\n },\n ], { transaction: true })\n\n const identifiers = { id: after.id, organizationId: after.id, tenantId }\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'created',\n entity: organization,\n identifiers,\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n\n return organization\n },\n}\n\nconst updateOrganizationCommand: CommandHandler<Record<string, unknown>, Organization> = {\n id: 'directory.organizations.update',\n async prepare(rawInput, ctx) {\n const { parsed } = parseWithCustomFields(organizationUpdateSchema, rawInput)\n const em = (ctx.container.resolve('em') as EntityManager)\n const { organization: current, tenantId } = await loadAuthorizedOrganizationTarget(em, parsed.id, ctx)\n const currentChildIds = Array.isArray(current.childIds) ? current.childIds : []\n const requestedChildIds = Array.isArray(parsed.childIds) ? parsed.childIds : []\n const combinedChildIds = new Set<string>([...currentChildIds.map(String), ...requestedChildIds.map(String)])\n const childParentsBefore = tenantId\n ? await loadChildParentSnapshots(em, tenantId, combinedChildIds)\n : []\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.directory.organization,\n recordId: String(current.id),\n tenantId,\n organizationId: String(current.id),\n })\n return { before: captureOrganizationSnapshots(current, childParentsBefore, custom) }\n },\n async execute(rawInput, ctx) {\n const { parsed, custom } = parseWithCustomFields(organizationUpdateSchema, rawInput)\n const em = (ctx.container.resolve('em') as EntityManager)\n const { tenantId } = await loadAuthorizedOrganizationTarget(em, parsed.id, ctx)\n\n const parentProvided = parsed.parentId !== undefined\n const childrenProvided = parsed.childIds !== undefined\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n\n let resolvedOrganization!: Organization\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n const existing = await em.findOne(\n Organization,\n buildOrganizationMutationFilter(parsed.id, tenantId),\n { refresh: true },\n )\n if (!existing) throw new CrudHttpError(404, { error: 'Not found' })\n\n const parentId = parentProvided ? parsed.parentId ?? null : existing.parentId ?? null\n if (parentProvided && parentId) {\n if (parentId === parsed.id) throw new CrudHttpError(400, { error: 'Organization cannot be its own parent' })\n if (Array.isArray(existing.descendantIds) && existing.descendantIds.includes(parentId)) {\n throw new CrudHttpError(400, { error: 'Cannot assign descendant as parent' })\n }\n await ensureParentExists(em, tenantId, parentId)\n }\n\n const normalizedChildIds = normalizeChildIds(parsed.childIds ?? [], [parsed.id, parentProvided ? parentId ?? '' : ''])\n if (normalizedChildIds.some((id) => id === parentId)) throw new CrudHttpError(400, { error: 'Child cannot equal parent' })\n if (Array.isArray(existing.ancestorIds) && normalizedChildIds.some((id) => existing.ancestorIds.includes(id))) {\n throw new CrudHttpError(400, { error: 'Cannot assign ancestor as child' })\n }\n\n if (normalizedChildIds.length) {\n await ensureChildrenValid(em, tenantId, normalizedChildIds)\n const childFilter = {\n tenant: tenantId,\n deletedAt: null,\n id: { $in: normalizedChildIds },\n } as unknown as FilterQuery<Organization>\n const children = await em.find(Organization, childFilter)\n for (const child of children) {\n if (Array.isArray(child.descendantIds) && child.descendantIds.includes(parsed.id)) {\n throw new CrudHttpError(400, { error: 'Cannot assign descendant cycle' })\n }\n }\n }\n\n const combinedChildIds = new Set<string>([\n ...normalizedChildIds.map(String),\n ...(Array.isArray(existing.childIds) ? existing.childIds.map(String) : []),\n ])\n const childParentsBefore = await loadChildParentSnapshots(em, tenantId, combinedChildIds)\n\n let resolvedSlug: string | null | undefined\n if (parsed.slug !== undefined) {\n resolvedSlug = parsed.slug ? await resolveUniqueSlug(em, tenantId, parsed.slug, parsed.id) : parsed.slug\n }\n\n const organization = await de.updateOrmEntity({\n entity: Organization,\n where: buildOrganizationMutationFilter(parsed.id, tenantId),\n apply: (entity) => {\n if (parsed.name !== undefined) entity.name = parsed.name\n if (resolvedSlug !== undefined) entity.slug = resolvedSlug\n if (parsed.logoUrl !== undefined) entity.logoUrl = parsed.logoUrl ?? null\n if (parsed.logoPreserveAspectRatio !== undefined) entity.logoPreserveAspectRatio = parsed.logoPreserveAspectRatio\n if (parsed.isActive !== undefined) entity.isActive = parsed.isActive\n if (parentProvided) entity.parentId = parentId\n },\n })\n if (!organization) throw new CrudHttpError(404, { error: 'Not found' })\n setInternalTenantId(organization, tenantId)\n\n const recordId = String(organization.id)\n const desiredChildIds = new Set(normalizedChildIds.filter((id) => id !== recordId))\n if (childrenProvided) {\n await clearRemovedChildren(em, tenantId, recordId, desiredChildIds)\n await assignChildren(em, tenantId, recordId, desiredChildIds)\n }\n const childParentsAfter = await loadChildParentSnapshots(em, tenantId, combinedChildIds)\n setUndoMeta(organization, { childParentsBefore, childParentsAfter })\n\n await setCustomFieldsIfAny({\n dataEngine: de,\n entityId: E.directory.organization,\n recordId,\n tenantId,\n organizationId: recordId,\n values: custom,\n })\n\n await rebuildHierarchyForTenant(em, tenantId)\n resolvedOrganization = organization\n },\n ], { transaction: true })\n\n const recordId = String(resolvedOrganization.id)\n const identifiers = { id: recordId, organizationId: recordId, tenantId }\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: resolvedOrganization,\n identifiers,\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n\n return resolvedOrganization\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const tenantId = resolveTenantIdFromEntity(result)\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.directory.organization,\n recordId: String(result.id),\n tenantId,\n organizationId: String(result.id),\n })\n return serializeOrganization(result, custom)\n },\n buildLog: async ({ snapshots, result, ctx }) => {\n const { translate } = await resolveTranslations()\n const meta = getUndoMeta(result)\n const beforeSnapshots = snapshots.before as OrganizationSnapshots | undefined\n const beforeRecord = beforeSnapshots?.view ?? null\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const tenantId = resolveTenantIdFromEntity(result)\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.directory.organization,\n recordId: String(result.id),\n tenantId,\n organizationId: String(result.id),\n })\n const after = serializeOrganization(result, custom)\n const changes = buildChanges(beforeRecord, after as Record<string, unknown>, ['name', 'slug', 'logoUrl', 'logoPreserveAspectRatio', 'isActive', 'parentId'])\n const customDiff = diffCustomFieldChanges(beforeRecord?.custom, custom)\n for (const [key, diff] of Object.entries(customDiff)) {\n changes[`cf_${key}`] = diff\n }\n return {\n actionLabel: translate('directory.audit.organizations.update', 'Update organization'),\n resourceKind: 'directory.organization',\n resourceId: String(result.id),\n changes,\n tenantId: after.tenantId,\n payload: {\n undo: {\n before: beforeSnapshots?.undo ?? null,\n after: captureOrganizationSnapshots(result, meta.childParentsAfter ?? [], custom).undo,\n },\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<OrganizationUndoPayload>(logEntry)\n const before = payload?.before\n const after = payload?.after\n if (!before) return\n const tenantId = before.tenantId\n if (!tenantId) return\n await assertOrganizationUndoTenantAccess(ctx, tenantId)\n const em = (ctx.container.resolve('em') as EntityManager)\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n let updated: Organization | null = null\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n updated = await de.updateOrmEntity({\n entity: Organization,\n where: buildOrganizationUndoTargetFilter(before.id, tenantId),\n apply: (entity) => {\n entity.name = before.name\n if (before.slug !== undefined) entity.slug = before.slug\n if (before.logoUrl !== undefined) entity.logoUrl = before.logoUrl ?? null\n if (before.logoPreserveAspectRatio !== undefined) entity.logoPreserveAspectRatio = !!before.logoPreserveAspectRatio\n entity.isActive = before.isActive\n entity.parentId = before.parentId\n },\n })\n if (!updated) throw new CrudHttpError(404, { error: 'Not found' })\n setInternalTenantId(updated, tenantId)\n const reset = buildCustomFieldResetMap(before.custom, after?.custom)\n if (Object.keys(reset).length) {\n const resetValues = reset as Parameters<DataEngine['setCustomFields']>[0]['values']\n await de.setCustomFields({\n entityId: E.directory.organization,\n recordId: before.id,\n tenantId,\n organizationId: before.id,\n values: resetValues,\n notify: false,\n })\n }\n const childSnapshots = before.childParents\n await restoreChildParents(em, tenantId, childSnapshots)\n await assertReplayKeepsHierarchyAcyclic(em, tenantId, [\n before.id,\n ...(childSnapshots ?? []).map((entry) => entry.childId),\n ])\n await rebuildHierarchyForTenant(em, tenantId)\n },\n ], { transaction: true })\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: updated,\n identifiers: {\n id: before.id,\n tenantId,\n organizationId: before.id,\n },\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n },\n}\n\nconst deleteOrganizationCommand: CommandHandler<{ body: any; query: Record<string, string> }, Organization> = {\n id: 'directory.organizations.delete',\n async prepare(input, ctx) {\n const id = requireId(input, 'Organization id required')\n const em = (ctx.container.resolve('em') as EntityManager)\n const { organization: existing, tenantId } = await loadAuthorizedOrganizationTarget(em, id, ctx)\n const childParentsBefore = tenantId\n ? await loadChildParentSnapshots(em, tenantId, Array.isArray(existing.childIds) ? existing.childIds : [])\n : []\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.directory.organization,\n recordId: String(existing.id),\n tenantId,\n organizationId: String(existing.id),\n })\n return { before: captureOrganizationSnapshots(existing, childParentsBefore, custom) }\n },\n async execute(input, ctx) {\n const id = requireId(input, 'Organization id required')\n const em = (ctx.container.resolve('em') as EntityManager)\n const { organization: existing, tenantId } = await loadAuthorizedOrganizationTarget(em, id, ctx)\n\n const parentId = existing.parentId ?? null\n const childSnapshotsBefore = await loadChildParentSnapshots(\n em,\n tenantId,\n Array.isArray(existing.childIds) ? existing.childIds : [],\n )\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n\n let resolvedDeleted!: Organization\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n const deleted = await de.deleteOrmEntity({\n entity: Organization,\n where: buildOrganizationMutationFilter(id, tenantId),\n soft: true,\n softDeleteField: 'deletedAt',\n })\n if (!deleted) throw new CrudHttpError(404, { error: 'Not found' })\n setInternalTenantId(deleted, tenantId)\n deleted.isActive = false\n deleted.parentId = null\n resolvedDeleted = deleted\n },\n async () => {\n const deleted = resolvedDeleted\n const childrenFilter: FilterQuery<Organization> = { tenant: tenantId, parentId: id, deletedAt: null }\n const children = await em.find(Organization, childrenFilter)\n const toPersist: Organization[] = []\n for (const child of children) {\n child.parentId = parentId\n toPersist.push(child)\n }\n toPersist.push(deleted)\n if (toPersist.length) em.persist(toPersist)\n setUndoMeta(deleted, { childParentsBefore: childSnapshotsBefore })\n },\n async () => {\n await rebuildHierarchyForTenant(em, tenantId)\n },\n ], { transaction: true })\n\n const identifiers = { id, organizationId: id, tenantId }\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'deleted',\n entity: resolvedDeleted,\n identifiers,\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n\n return resolvedDeleted\n },\n buildLog: async ({ snapshots, input }) => {\n const { translate } = await resolveTranslations()\n const beforeSnapshots = snapshots.before as OrganizationSnapshots | undefined\n const beforeSnapshot = beforeSnapshots?.view ?? null\n const beforeUndo = beforeSnapshots?.undo ?? null\n const id = String(input?.body?.id ?? input?.query?.id ?? '')\n const fallbackId = beforeSnapshot?.id ?? null\n const fallbackTenant = beforeSnapshot?.tenantId ?? null\n return {\n actionLabel: translate('directory.audit.organizations.delete', 'Delete organization'),\n resourceKind: 'directory.organization',\n resourceId: id || fallbackId || null,\n snapshotBefore: beforeSnapshot ?? null,\n tenantId: beforeUndo?.tenantId ?? fallbackTenant,\n payload: {\n undo: {\n before: beforeUndo,\n },\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<OrganizationUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const tenantId = before.tenantId\n if (!tenantId) return\n await assertOrganizationUndoTenantAccess(ctx, tenantId)\n const em = (ctx.container.resolve('em') as EntityManager)\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n let organization: Organization | null = null\n await withAtomicFlush(em, [\n async () => {\n await lockOrganizationHierarchyForTenant(em, tenantId)\n organization = await em.findOne(\n Organization,\n buildOrganizationUndoTargetFilter(before.id, tenantId),\n )\n if (organization) {\n organization.deletedAt = null\n organization.isActive = before.isActive\n organization.name = before.name\n if (before.slug !== undefined) organization.slug = before.slug\n organization.parentId = before.parentId\n await em.flush()\n if (tenantId) setInternalTenantId(organization, tenantId)\n } else {\n organization = await de.createOrmEntity({\n entity: Organization,\n data: {\n id: before.id,\n name: before.name,\n slug: before.slug ?? null,\n tenant: tenantId ? em.getReference(Tenant, tenantId) : undefined,\n isActive: before.isActive,\n parentId: before.parentId,\n },\n })\n if (tenantId) setInternalTenantId(organization, tenantId)\n }\n if (tenantId) {\n const customValues = buildCustomFieldResetMap(before.custom, undefined)\n if (Object.keys(customValues).length) {\n const resetValues = customValues as Parameters<DataEngine['setCustomFields']>[0]['values']\n await de.setCustomFields({\n entityId: E.directory.organization,\n recordId: before.id,\n tenantId,\n organizationId: before.id,\n values: resetValues,\n notify: false,\n })\n }\n }\n await restoreChildParents(em, tenantId, before.childParents)\n await assertReplayKeepsHierarchyAcyclic(em, tenantId, [\n before.id,\n ...(before.childParents ?? []).map((entry) => entry.childId),\n ])\n await rebuildHierarchyForTenant(em, tenantId)\n },\n ], { transaction: true })\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: organization,\n identifiers: {\n id: before.id,\n tenantId,\n organizationId: before.id,\n },\n events: organizationCrudEvents,\n indexer: organizationCrudIndexer,\n })\n },\n}\n\ntype OrganizationUndoPayload = {\n before?: OrganizationUndoSnapshot | null\n after?: OrganizationUndoSnapshot | null\n childrenBefore?: ChildParentSnapshot[] | null\n}\n\ntype OrganizationUndoMeta = {\n childParentsBefore?: ChildParentSnapshot[]\n childParentsAfter?: ChildParentSnapshot[]\n}\n\nconst UNDO_META_KEY: unique symbol = Symbol('directory.organization.undoMeta')\n\nfunction getUndoMeta(entity: Organization): OrganizationUndoMeta {\n return (Reflect.get(entity, UNDO_META_KEY) as OrganizationUndoMeta | undefined) ?? {}\n}\n\nfunction setUndoMeta(entity: Organization, meta: Partial<OrganizationUndoMeta>) {\n const current = getUndoMeta(entity)\n Reflect.set(entity, UNDO_META_KEY, { ...current, ...meta })\n}\n\nregisterCommand(createOrganizationCommand)\nregisterCommand(updateOrganizationCommand)\nregisterCommand(deleteOrganizationCommand)\n\nfunction setInternalTenantId(entity: Organization, tenantId: string) {\n Reflect.set(entity, '__tenantId', tenantId)\n}\n\nfunction tenantIdFromContext(ctx: CrudEmitContext<Organization>): string | null {\n return resolveTenantIdFromEntity(ctx.entity) ?? ctx.identifiers.tenantId ?? null\n}\n"],
5
+ "mappings": "AACA,SAAS,uBAAuB;AAChC,SAAS,qBAAqB;AAG9B,SAAS,cAAc,cAAc;AACrC,SAAS,0BAA0B,gCAAgC;AACnE;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,SAAS;AAElB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,oBAAoB,2BAA2B;AACxD,SAAS,2BAA2B;AACpC,SAAS,uBAAuB;AAChC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,0BAA0B;AACnC,SAAS,2BAA2B;AACpC,SAAS,wBAAwB;AACjC,SAAS,eAAe;AAEjB,MAAM,yBAA2C;AAAA,EACtD,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,cAAc,CAAC,QAAQ;AACrB,U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BAAgB,OAAO;AAAA,MACzB;AAAA,MACA,QAAQ;AAAA,MACR,SAAS;AAAA,IACX,CAAC;AAAA,EACH;AACF;AAaA,MAAM,gBAA+B,uBAAO,iCAAiC;AAE7E,SAAS,YAAY,QAA4C;AAC/D,SAAQ,QAAQ,IAAI,QAAQ,aAAa,KAA0C,CAAC;AACtF;AAEA,SAAS,YAAY,QAAsB,MAAqC;AAC9E,QAAM,UAAU,YAAY,MAAM;AAClC,UAAQ,IAAI,QAAQ,eAAe,EAAE,GAAG,SAAS,GAAG,KAAK,CAAC;AAC5D;AAEA,gBAAgB,yBAAyB;AACzC,gBAAgB,yBAAyB;AACzC,gBAAgB,yBAAyB;AAEzC,SAAS,oBAAoB,QAAsB,UAAkB;AACnE,UAAQ,IAAI,QAAQ,cAAc,QAAQ;AAC5C;AAEA,SAAS,oBAAoB,KAAmD;AAC9E,SAAO,0BAA0B,IAAI,MAAM,KAAK,IAAI,YAAY,YAAY;AAC9E;",
6
6
  "names": ["recordId"]
7
7
  }
@@ -22,6 +22,21 @@ function normalizeUuid(value) {
22
22
  if (!v || v.toLowerCase() === "null" || v.toLowerCase() === "undefined") return null;
23
23
  return v;
24
24
  }
25
+ function findOrganizationParentCycle(organizations, organizationIds) {
26
+ const parents = /* @__PURE__ */ new Map();
27
+ for (const org of organizations) parents.set(String(org.id), normalizeUuid(org.parentId));
28
+ for (const startId of organizationIds) {
29
+ const seen = /* @__PURE__ */ new Set([startId]);
30
+ let currentId = parents.get(startId) ?? null;
31
+ while (currentId && parents.has(currentId)) {
32
+ if (currentId === startId) return startId;
33
+ if (seen.has(currentId)) break;
34
+ seen.add(currentId);
35
+ currentId = parents.get(currentId) ?? null;
36
+ }
37
+ }
38
+ return null;
39
+ }
25
40
  function computeHierarchyForOrganizations(organizations, tenantId) {
26
41
  const nodes = /* @__PURE__ */ new Map();
27
42
  for (const org of organizations) {
@@ -123,9 +138,25 @@ function computeHierarchyForOrganizations(organizations, tenantId) {
123
138
  walk(id, []);
124
139
  }
125
140
  }
141
+ function findCycleCutId(startId) {
142
+ const positions = /* @__PURE__ */ new Map();
143
+ const path = [];
144
+ let currentId = startId;
145
+ while (currentId && nodes.has(currentId) && !visited.has(currentId) && !positions.has(currentId)) {
146
+ positions.set(currentId, path.length);
147
+ path.push(currentId);
148
+ currentId = nodes.get(currentId).parentId;
149
+ }
150
+ if (!currentId || !positions.has(currentId)) return null;
151
+ return path.slice(positions.get(currentId)).reduce((smallest, candidate) => candidate < smallest ? candidate : smallest);
152
+ }
126
153
  for (const id of nodes.keys()) {
127
- if (!visited.has(id)) {
128
- walk(id, []);
154
+ while (!visited.has(id)) {
155
+ const cutId = findCycleCutId(id) ?? id;
156
+ const cutNode = nodes.get(cutId);
157
+ if (cutNode.parentId) nodes.get(cutNode.parentId)?.children.delete(cutId);
158
+ cutNode.parentId = null;
159
+ walk(cutId, []);
129
160
  }
130
161
  }
131
162
  const ordered = orderedIds.map((id) => computed.get(id)).filter((node) => !!node);
@@ -162,6 +193,7 @@ async function rebuildHierarchyForTenant(em, tenantId) {
162
193
  }
163
194
  export {
164
195
  computeHierarchyForOrganizations,
196
+ findOrganizationParentCycle,
165
197
  lockOrganizationHierarchyForTenant,
166
198
  rebuildHierarchyForTenant
167
199
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/directory/lib/hierarchy.ts"],
4
- "sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { LockMode } from '@mikro-orm/core'\nimport { Organization, Tenant } from '@open-mercato/core/modules/directory/data/entities'\n\nexport type ComputedOrganizationNode = {\n id: string\n tenantId: string\n name: string\n pathLabel: string\n parentId: string | null\n depth: number\n rootId: string\n treePath: string\n ancestorIds: string[]\n childIds: string[]\n descendantIds: string[]\n isActive: boolean\n}\n\nexport type ComputedHierarchy = {\n map: Map<string, ComputedOrganizationNode>\n ordered: ComputedOrganizationNode[]\n}\n\nexport async function lockOrganizationHierarchyForTenant(\n em: EntityManager,\n tenantId: string,\n): Promise<Organization[]> {\n await em.findOne(\n Tenant,\n { id: tenantId },\n { lockMode: LockMode.PESSIMISTIC_WRITE, refresh: true },\n )\n return em.find(\n Organization,\n { tenant: tenantId as never, deletedAt: null },\n {\n lockMode: LockMode.PESSIMISTIC_WRITE,\n orderBy: { id: 'ASC' },\n refresh: true,\n },\n )\n}\n\ntype InternalNode = {\n org: Organization\n parentId: string | null\n children: Set<string>\n}\n\nfunction normalizeUuid(value: unknown): string | null {\n if (!value) return null\n const v = String(value).trim()\n if (!v || v.toLowerCase() === 'null' || v.toLowerCase() === 'undefined') return null\n return v\n}\n\nexport function computeHierarchyForOrganizations(organizations: Organization[], tenantId: string): ComputedHierarchy {\n const nodes = new Map<string, InternalNode>()\n\n for (const org of organizations) {\n const id = String(org.id)\n const node: InternalNode = {\n org,\n parentId: normalizeUuid(org.parentId),\n children: new Set<string>(),\n }\n nodes.set(id, node)\n }\n\n // Establish child relationships (ignore missing parents or self-references)\n for (const [id, node] of nodes) {\n const parentId = node.parentId\n if (!parentId || parentId === id) {\n node.parentId = null\n continue\n }\n const parent = nodes.get(parentId)\n if (!parent) {\n node.parentId = null\n continue\n }\n parent.children.add(id)\n }\n\n const computed = new Map<string, ComputedOrganizationNode>()\n const orderedIds: string[] = []\n const orderedSet = new Set<string>()\n const visited = new Set<string>()\n\n function walk(nodeId: string, ancestors: string[]): string[] {\n if (ancestors.includes(nodeId)) {\n // Cycle detected; break by treating as root\n const orgName = nodes.get(nodeId)?.org.name || ''\n computed.set(nodeId, {\n id: nodeId,\n tenantId,\n name: orgName,\n pathLabel: orgName,\n parentId: null,\n depth: 0,\n rootId: nodeId,\n treePath: nodeId,\n ancestorIds: [],\n childIds: [],\n descendantIds: [],\n isActive: nodes.get(nodeId)?.org.isActive ?? true,\n })\n if (!orderedSet.has(nodeId)) {\n orderedIds.push(nodeId)\n orderedSet.add(nodeId)\n }\n visited.add(nodeId)\n return []\n }\n\n const node = nodes.get(nodeId)\n if (!node) return []\n\n visited.add(nodeId)\n const org = node.org\n const id = String(org.id)\n const nextAncestors = [...ancestors, id]\n if (!orderedSet.has(id)) {\n orderedIds.push(id)\n orderedSet.add(id)\n }\n const childIds = Array.from(node.children)\n .filter((childId) => nodes.has(childId))\n .sort((a, b) => {\n const an = nodes.get(a)!.org.name.toLowerCase()\n const bn = nodes.get(b)!.org.name.toLowerCase()\n if (an === bn) return a.localeCompare(b)\n return an.localeCompare(bn)\n })\n\n const descendantIds: string[] = []\n for (const childId of childIds) {\n const desc = walk(childId, nextAncestors)\n descendantIds.push(childId, ...desc)\n }\n\n const ancestorIds = ancestors\n const depth = ancestorIds.length\n const rootId = ancestorIds.length ? ancestorIds[0] : id\n const treePath = nextAncestors.join('/')\n const ancestorNames = ancestors\n .map((ancestorId) => nodes.get(ancestorId)?.org.name)\n .filter((value): value is string => typeof value === 'string' && value.length > 0)\n const pathLabel = [...ancestorNames, org.name].join(' / ')\n\n const computedNode: ComputedOrganizationNode = {\n id,\n tenantId,\n name: org.name,\n pathLabel,\n parentId: node.parentId,\n depth,\n rootId,\n treePath,\n ancestorIds,\n childIds,\n descendantIds,\n isActive: !!org.isActive,\n }\n computed.set(id, computedNode)\n return descendantIds\n }\n\n // Walk roots first (nodes without parent or whose parent is missing)\n for (const [id, node] of nodes) {\n if (!node.parentId || !nodes.has(node.parentId)) {\n walk(id, [])\n }\n }\n // Handle orphaned nodes or cycles not reached above\n for (const id of nodes.keys()) {\n if (!visited.has(id)) {\n walk(id, [])\n }\n }\n\n const ordered = orderedIds\n .map((id) => computed.get(id))\n .filter((node): node is ComputedOrganizationNode => !!node)\n\n return { map: computed, ordered }\n}\n\nexport async function rebuildHierarchyForTenant(em: EntityManager, tenantId: string): Promise<ComputedHierarchy> {\n const organizations = await em.find(Organization, { tenant: tenantId as any, deletedAt: null }, { orderBy: { name: 'ASC' } })\n const hierarchy = computeHierarchyForOrganizations(organizations, tenantId)\n const now = new Date()\n\n for (const org of organizations) {\n const computed = hierarchy.map.get(String(org.id))\n if (!computed) {\n org.parentId = null\n org.rootId = String(org.id)\n org.treePath = String(org.id)\n org.depth = 0\n org.ancestorIds = []\n org.childIds = []\n org.descendantIds = []\n org.updatedAt = now\n continue\n }\n org.parentId = computed.parentId\n org.rootId = computed.rootId\n org.treePath = computed.treePath\n org.depth = computed.depth\n org.ancestorIds = computed.ancestorIds\n org.childIds = computed.childIds\n org.descendantIds = computed.descendantIds\n org.updatedAt = now\n }\n\n await em.flush()\n return hierarchy\n}\n"],
5
- "mappings": "AACA,SAAS,gBAAgB;AACzB,SAAS,cAAc,cAAc;AAsBrC,eAAsB,mCACpB,IACA,UACyB;AACzB,QAAM,GAAG;AAAA,IACP;AAAA,IACA,EAAE,IAAI,SAAS;AAAA,IACf,EAAE,UAAU,SAAS,mBAAmB,SAAS,KAAK;AAAA,EACxD;AACA,SAAO,GAAG;AAAA,IACR;AAAA,IACA,EAAE,QAAQ,UAAmB,WAAW,KAAK;AAAA,IAC7C;AAAA,MACE,UAAU,SAAS;AAAA,MACnB,SAAS,EAAE,IAAI,MAAM;AAAA,MACrB,SAAS;AAAA,IACX;AAAA,EACF;AACF;AAQA,SAAS,cAAc,OAA+B;AACpD,MAAI,CAAC,MAAO,QAAO;AACnB,QAAM,IAAI,OAAO,KAAK,EAAE,KAAK;AAC7B,MAAI,CAAC,KAAK,EAAE,YAAY,MAAM,UAAU,EAAE,YAAY,MAAM,YAAa,QAAO;AAChF,SAAO;AACT;AAEO,SAAS,iCAAiC,eAA+B,UAAqC;AACnH,QAAM,QAAQ,oBAAI,IAA0B;AAE5C,aAAW,OAAO,eAAe;AAC/B,UAAM,KAAK,OAAO,IAAI,EAAE;AACxB,UAAM,OAAqB;AAAA,MACzB;AAAA,MACA,UAAU,cAAc,IAAI,QAAQ;AAAA,MACpC,UAAU,oBAAI,IAAY;AAAA,IAC5B;AACA,UAAM,IAAI,IAAI,IAAI;AAAA,EACpB;AAGA,aAAW,CAAC,IAAI,IAAI,KAAK,OAAO;AAC9B,U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AAA,IACb;AAAA,EACF;AAEA,aAAW,MAAM,MAAM,KAAK,GAAG;AAC7B,QAAI,CAAC,QAAQ,IAAI,EAAE,GAAG;AACpB,WAAK,IAAI,CAAC,CAAC;AAAA,IACb;AAAA,EACF;AAEA,QAAM,UAAU,WACb,IAAI,CAAC,OAAO,SAAS,IAAI,EAAE,CAAC,EAC5B,OAAO,CAAC,SAA2C,CAAC,CAAC,IAAI;AAE5D,SAAO,EAAE,KAAK,UAAU,QAAQ;AAClC;AAEA,eAAsB,0BAA0B,IAAmB,UAA8C;AAC/G,QAAM,gBAAgB,MAAM,GAAG,KAAK,cAAc,EAAE,QAAQ,UAAiB,WAAW,KAAK,GAAG,EAAE,SAAS,EAAE,MAAM,MAAM,EAAE,CAAC;AAC5H,QAAM,YAAY,iCAAiC,eAAe,QAAQ;AAC1E,QAAM,MAAM,oBAAI,KAAK;AAErB,aAAW,OAAO,eAAe;AAC/B,UAAM,WAAW,UAAU,IAAI,IAAI,OAAO,IAAI,EAAE,CAAC;AACjD,QAAI,CAAC,UAAU;AACb,UAAI,WAAW;AACf,UAAI,SAAS,OAAO,IAAI,EAAE;AAC1B,UAAI,WAAW,OAAO,IAAI,EAAE;AAC5B,UAAI,QAAQ;AACZ,UAAI,cAAc,CAAC;AACnB,UAAI,WAAW,CAAC;AAChB,UAAI,gBAAgB,CAAC;AACrB,UAAI,YAAY;AAChB;AAAA,IACF;AACA,QAAI,WAAW,SAAS;AACxB,QAAI,SAAS,SAAS;AACtB,QAAI,WAAW,SAAS;AACxB,QAAI,QAAQ,SAAS;AACrB,QAAI,cAAc,SAAS;AAC3B,QAAI,WAAW,SAAS;AACxB,QAAI,gBAAgB,SAAS;AAC7B,QAAI,YAAY;AAAA,EAClB;AAEA,QAAM,GAAG,MAAM;AACf,SAAO;AACT;",
4
+ "sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { LockMode } from '@mikro-orm/core'\nimport { Organization, Tenant } from '@open-mercato/core/modules/directory/data/entities'\n\nexport type ComputedOrganizationNode = {\n id: string\n tenantId: string\n name: string\n pathLabel: string\n parentId: string | null\n depth: number\n rootId: string\n treePath: string\n ancestorIds: string[]\n childIds: string[]\n descendantIds: string[]\n isActive: boolean\n}\n\nexport type ComputedHierarchy = {\n map: Map<string, ComputedOrganizationNode>\n ordered: ComputedOrganizationNode[]\n}\n\nexport async function lockOrganizationHierarchyForTenant(\n em: EntityManager,\n tenantId: string,\n): Promise<Organization[]> {\n await em.findOne(\n Tenant,\n { id: tenantId },\n { lockMode: LockMode.PESSIMISTIC_WRITE, refresh: true },\n )\n return em.find(\n Organization,\n { tenant: tenantId as never, deletedAt: null },\n {\n lockMode: LockMode.PESSIMISTIC_WRITE,\n orderBy: { id: 'ASC' },\n refresh: true,\n },\n )\n}\n\ntype InternalNode = {\n org: Organization\n parentId: string | null\n children: Set<string>\n}\n\nfunction normalizeUuid(value: unknown): string | null {\n if (!value) return null\n const v = String(value).trim()\n if (!v || v.toLowerCase() === 'null' || v.toLowerCase() === 'undefined') return null\n return v\n}\n\nexport function findOrganizationParentCycle(\n organizations: ReadonlyArray<Pick<Organization, 'id' | 'parentId'>>,\n organizationIds: Iterable<string>,\n): string | null {\n const parents = new Map<string, string | null>()\n for (const org of organizations) parents.set(String(org.id), normalizeUuid(org.parentId))\n for (const startId of organizationIds) {\n const seen = new Set<string>([startId])\n let currentId = parents.get(startId) ?? null\n while (currentId && parents.has(currentId)) {\n if (currentId === startId) return startId\n if (seen.has(currentId)) break\n seen.add(currentId)\n currentId = parents.get(currentId) ?? null\n }\n }\n return null\n}\n\nexport function computeHierarchyForOrganizations(organizations: Organization[], tenantId: string): ComputedHierarchy {\n const nodes = new Map<string, InternalNode>()\n\n for (const org of organizations) {\n const id = String(org.id)\n const node: InternalNode = {\n org,\n parentId: normalizeUuid(org.parentId),\n children: new Set<string>(),\n }\n nodes.set(id, node)\n }\n\n // Establish child relationships (ignore missing parents or self-references)\n for (const [id, node] of nodes) {\n const parentId = node.parentId\n if (!parentId || parentId === id) {\n node.parentId = null\n continue\n }\n const parent = nodes.get(parentId)\n if (!parent) {\n node.parentId = null\n continue\n }\n parent.children.add(id)\n }\n\n const computed = new Map<string, ComputedOrganizationNode>()\n const orderedIds: string[] = []\n const orderedSet = new Set<string>()\n const visited = new Set<string>()\n\n function walk(nodeId: string, ancestors: string[]): string[] {\n if (ancestors.includes(nodeId)) {\n // Cycle detected; break by treating as root\n const orgName = nodes.get(nodeId)?.org.name || ''\n computed.set(nodeId, {\n id: nodeId,\n tenantId,\n name: orgName,\n pathLabel: orgName,\n parentId: null,\n depth: 0,\n rootId: nodeId,\n treePath: nodeId,\n ancestorIds: [],\n childIds: [],\n descendantIds: [],\n isActive: nodes.get(nodeId)?.org.isActive ?? true,\n })\n if (!orderedSet.has(nodeId)) {\n orderedIds.push(nodeId)\n orderedSet.add(nodeId)\n }\n visited.add(nodeId)\n return []\n }\n\n const node = nodes.get(nodeId)\n if (!node) return []\n\n visited.add(nodeId)\n const org = node.org\n const id = String(org.id)\n const nextAncestors = [...ancestors, id]\n if (!orderedSet.has(id)) {\n orderedIds.push(id)\n orderedSet.add(id)\n }\n const childIds = Array.from(node.children)\n .filter((childId) => nodes.has(childId))\n .sort((a, b) => {\n const an = nodes.get(a)!.org.name.toLowerCase()\n const bn = nodes.get(b)!.org.name.toLowerCase()\n if (an === bn) return a.localeCompare(b)\n return an.localeCompare(bn)\n })\n\n const descendantIds: string[] = []\n for (const childId of childIds) {\n const desc = walk(childId, nextAncestors)\n descendantIds.push(childId, ...desc)\n }\n\n const ancestorIds = ancestors\n const depth = ancestorIds.length\n const rootId = ancestorIds.length ? ancestorIds[0] : id\n const treePath = nextAncestors.join('/')\n const ancestorNames = ancestors\n .map((ancestorId) => nodes.get(ancestorId)?.org.name)\n .filter((value): value is string => typeof value === 'string' && value.length > 0)\n const pathLabel = [...ancestorNames, org.name].join(' / ')\n\n const computedNode: ComputedOrganizationNode = {\n id,\n tenantId,\n name: org.name,\n pathLabel,\n parentId: node.parentId,\n depth,\n rootId,\n treePath,\n ancestorIds,\n childIds,\n descendantIds,\n isActive: !!org.isActive,\n }\n computed.set(id, computedNode)\n return descendantIds\n }\n\n // Walk roots first (nodes without parent or whose parent is missing)\n for (const [id, node] of nodes) {\n if (!node.parentId || !nodes.has(node.parentId)) {\n walk(id, [])\n }\n }\n\n function findCycleCutId(startId: string): string | null {\n const positions = new Map<string, number>()\n const path: string[] = []\n let currentId: string | null = startId\n while (currentId && nodes.has(currentId) && !visited.has(currentId) && !positions.has(currentId)) {\n positions.set(currentId, path.length)\n path.push(currentId)\n currentId = nodes.get(currentId)!.parentId\n }\n if (!currentId || !positions.has(currentId)) return null\n return path\n .slice(positions.get(currentId))\n .reduce((smallest, candidate) => (candidate < smallest ? candidate : smallest))\n }\n\n // Handle orphaned nodes or cycles not reached above\n for (const id of nodes.keys()) {\n while (!visited.has(id)) {\n const cutId = findCycleCutId(id) ?? id\n const cutNode = nodes.get(cutId)!\n if (cutNode.parentId) nodes.get(cutNode.parentId)?.children.delete(cutId)\n cutNode.parentId = null\n walk(cutId, [])\n }\n }\n\n const ordered = orderedIds\n .map((id) => computed.get(id))\n .filter((node): node is ComputedOrganizationNode => !!node)\n\n return { map: computed, ordered }\n}\n\nexport async function rebuildHierarchyForTenant(em: EntityManager, tenantId: string): Promise<ComputedHierarchy> {\n const organizations = await em.find(Organization, { tenant: tenantId as any, deletedAt: null }, { orderBy: { name: 'ASC' } })\n const hierarchy = computeHierarchyForOrganizations(organizations, tenantId)\n const now = new Date()\n\n for (const org of organizations) {\n const computed = hierarchy.map.get(String(org.id))\n if (!computed) {\n org.parentId = null\n org.rootId = String(org.id)\n org.treePath = String(org.id)\n org.depth = 0\n org.ancestorIds = []\n org.childIds = []\n org.descendantIds = []\n org.updatedAt = now\n continue\n }\n org.parentId = computed.parentId\n org.rootId = computed.rootId\n org.treePath = computed.treePath\n org.depth = computed.depth\n org.ancestorIds = computed.ancestorIds\n org.childIds = computed.childIds\n org.descendantIds = computed.desc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AC/G,QAAM,gBAAgB,MAAM,GAAG,KAAK,cAAc,EAAE,QAAQ,UAAiB,WAAW,KAAK,GAAG,EAAE,SAAS,EAAE,MAAM,MAAM,EAAE,CAAC;AAC5H,QAAM,YAAY,iCAAiC,eAAe,QAAQ;AAC1E,QAAM,MAAM,oBAAI,KAAK;AAErB,aAAW,OAAO,eAAe;AAC/B,UAAM,WAAW,UAAU,IAAI,IAAI,OAAO,IAAI,EAAE,CAAC;AACjD,QAAI,CAAC,UAAU;AACb,UAAI,WAAW;AACf,UAAI,SAAS,OAAO,IAAI,EAAE;AAC1B,UAAI,WAAW,OAAO,IAAI,EAAE;AAC5B,UAAI,QAAQ;AACZ,UAAI,cAAc,CAAC;AACnB,UAAI,WAAW,CAAC;AAChB,UAAI,gBAAgB,CAAC;AACrB,UAAI,YAAY;AAChB;AAAA,IACF;AACA,QAAI,WAAW,SAAS;AACxB,QAAI,SAAS,SAAS;AACtB,QAAI,WAAW,SAAS;AACxB,QAAI,QAAQ,SAAS;AACrB,QAAI,cAAc,SAAS;AAC3B,QAAI,WAAW,SAAS;AACxB,QAAI,gBAAgB,SAAS;AAC7B,QAAI,YAAY;AAAA,EAClB;AAEA,QAAM,GAAG,MAAM;AACf,SAAO;AACT;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-mercato/core",
3
- "version": "0.10.1-develop.7726.1.a447cc5525",
3
+ "version": "0.10.1-develop.7727.1.0b89c4f800",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -266,16 +266,16 @@
266
266
  "zod": "^4.4.3"
267
267
  },
268
268
  "peerDependencies": {
269
- "@open-mercato/ai-assistant": "0.10.1-develop.7726.1.a447cc5525",
270
- "@open-mercato/shared": "0.10.1-develop.7726.1.a447cc5525",
271
- "@open-mercato/ui": "0.10.1-develop.7726.1.a447cc5525",
269
+ "@open-mercato/ai-assistant": "0.10.1-develop.7727.1.0b89c4f800",
270
+ "@open-mercato/shared": "0.10.1-develop.7727.1.0b89c4f800",
271
+ "@open-mercato/ui": "0.10.1-develop.7727.1.0b89c4f800",
272
272
  "react": "^19.0.0",
273
273
  "react-dom": "^19.0.0"
274
274
  },
275
275
  "devDependencies": {
276
- "@open-mercato/ai-assistant": "0.10.1-develop.7726.1.a447cc5525",
277
- "@open-mercato/shared": "0.10.1-develop.7726.1.a447cc5525",
278
- "@open-mercato/ui": "0.10.1-develop.7726.1.a447cc5525",
276
+ "@open-mercato/ai-assistant": "0.10.1-develop.7727.1.0b89c4f800",
277
+ "@open-mercato/shared": "0.10.1-develop.7727.1.0b89c4f800",
278
+ "@open-mercato/ui": "0.10.1-develop.7727.1.0b89c4f800",
279
279
  "@testing-library/dom": "^10.4.1",
280
280
  "@testing-library/jest-dom": "^7.0.1",
281
281
  "@testing-library/react": "^16.3.3",
@@ -6,6 +6,7 @@ import type { DataEngine } from '@open-mercato/shared/lib/data/engine'
6
6
  import { Organization, Tenant } from '@open-mercato/core/modules/directory/data/entities'
7
7
  import { organizationCreateSchema, organizationUpdateSchema } from '@open-mercato/core/modules/directory/data/validators'
8
8
  import {
9
+ findOrganizationParentCycle,
9
10
  lockOrganizationHierarchyForTenant,
10
11
  rebuildHierarchyForTenant,
11
12
  } from '@open-mercato/core/modules/directory/lib/hierarchy'
@@ -32,6 +33,7 @@ import {
32
33
  requireId,
33
34
  buildChanges,
34
35
  } from '@open-mercato/shared/lib/commands/helpers'
36
+ import { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'
35
37
  import { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'
36
38
  import { toOptionalString } from '@open-mercato/shared/lib/string/coerce'
37
39
  import { slugify } from '@open-mercato/shared/lib/slugify'
@@ -293,6 +295,32 @@ async function restoreChildParents(em: EntityManager, tenantId: string, snapshot
293
295
  if (toPersist.length) await em.persist(toPersist).flush()
294
296
  }
295
297
 
298
+ const REPLAY_CYCLE_ERROR_KEY = 'directory.organizations.errors.replayCycle'
299
+
300
+ async function assertReplayKeepsHierarchyAcyclic(
301
+ em: EntityManager,
302
+ tenantId: string,
303
+ organizationIds: Iterable<string>,
304
+ ): Promise<void> {
305
+ const filter: FilterQuery<Organization> = {
306
+ tenant: tenantId,
307
+ deletedAt: null,
308
+ } as unknown as FilterQuery<Organization>
309
+ const organizations = await findWithDecryption(em, Organization, filter, undefined, {
310
+ tenantId,
311
+ organizationId: null,
312
+ })
313
+ if (!findOrganizationParentCycle(organizations, organizationIds)) return
314
+ const { translate } = await resolveTranslations()
315
+ throw new CrudHttpError(409, {
316
+ error: translate(
317
+ REPLAY_CYCLE_ERROR_KEY,
318
+ "The organization hierarchy changed since this action, so it can't be reverted without making an organization its own ancestor.",
319
+ ),
320
+ code: REPLAY_CYCLE_ERROR_KEY,
321
+ })
322
+ }
323
+
296
324
  function normalizeChildIds(ids: readonly string[], exclude: string[]): string[] {
297
325
  const excludeSet = new Set(exclude)
298
326
  return Array.from(new Set(ids)).filter((id) => !excludeSet.has(id))
@@ -509,6 +537,11 @@ const createOrganizationCommand: CommandHandler<Record<string, unknown>, Organiz
509
537
  organization.parentId = null
510
538
  await em.flush()
511
539
  }
540
+ await assertReplayKeepsHierarchyAcyclic(
541
+ em,
542
+ tenantId,
543
+ childrenBefore.map((entry) => entry.childId).filter((childId) => childId !== after.id),
544
+ )
512
545
  await rebuildHierarchyForTenant(em, tenantId)
513
546
  },
514
547
  ], { transaction: true })
@@ -587,6 +620,10 @@ const createOrganizationCommand: CommandHandler<Record<string, unknown>, Organiz
587
620
  }
588
621
 
589
622
  await assignChildren(em, tenantId, after.id, after.childParents.map((entry) => entry.childId))
623
+ await assertReplayKeepsHierarchyAcyclic(em, tenantId, [
624
+ after.id,
625
+ ...after.childParents.map((entry) => entry.childId),
626
+ ])
590
627
  await rebuildHierarchyForTenant(em, tenantId)
591
628
  },
592
629
  ], { transaction: true })
@@ -823,6 +860,10 @@ const updateOrganizationCommand: CommandHandler<Record<string, unknown>, Organiz
823
860
  }
824
861
  const childSnapshots = before.childParents
825
862
  await restoreChildParents(em, tenantId, childSnapshots)
863
+ await assertReplayKeepsHierarchyAcyclic(em, tenantId, [
864
+ before.id,
865
+ ...(childSnapshots ?? []).map((entry) => entry.childId),
866
+ ])
826
867
  await rebuildHierarchyForTenant(em, tenantId)
827
868
  },
828
869
  ], { transaction: true })
@@ -993,6 +1034,10 @@ const deleteOrganizationCommand: CommandHandler<{ body: any; query: Record<strin
993
1034
  }
994
1035
  }
995
1036
  await restoreChildParents(em, tenantId, before.childParents)
1037
+ await assertReplayKeepsHierarchyAcyclic(em, tenantId, [
1038
+ before.id,
1039
+ ...(before.childParents ?? []).map((entry) => entry.childId),
1040
+ ])
996
1041
  await rebuildHierarchyForTenant(em, tenantId)
997
1042
  },
998
1043
  ], { transaction: true })
@@ -38,6 +38,7 @@
38
38
  "directory.organizations.errors.deleteFailed": "Organisation konnte nicht gelöscht werden",
39
39
  "directory.organizations.errors.invalidQuery": "Ungültige Abfrage",
40
40
  "directory.organizations.errors.orgIdRequired": "Organisationskennung ist erforderlich",
41
+ "directory.organizations.errors.replayCycle": "Die Organisationshierarchie hat sich seit dieser Aktion geändert, daher kann die Aktion nicht rückgängig gemacht werden, ohne dass eine Organisation sich selbst übergeordnet wird.",
41
42
  "directory.organizations.errors.tenantRequired": "Die Mandantenauswahl ist für Superadministratoren erforderlich",
42
43
  "directory.organizations.flash.created": "Organisation erstellt",
43
44
  "directory.organizations.flash.deleted": "Organisation gelöscht",
@@ -38,6 +38,7 @@
38
38
  "directory.organizations.errors.deleteFailed": "Failed to delete organization",
39
39
  "directory.organizations.errors.invalidQuery": "Invalid query",
40
40
  "directory.organizations.errors.orgIdRequired": "Organization identifier is required",
41
+ "directory.organizations.errors.replayCycle": "The organization hierarchy changed since this action, so it can't be reverted without making an organization its own ancestor.",
41
42
  "directory.organizations.errors.tenantRequired": "Tenant selection is required for super administrators",
42
43
  "directory.organizations.flash.created": "Organization created",
43
44
  "directory.organizations.flash.deleted": "Organization deleted",
@@ -38,6 +38,7 @@
38
38
  "directory.organizations.errors.deleteFailed": "No se pudo eliminar la organización",
39
39
  "directory.organizations.errors.invalidQuery": "Consulta no válida",
40
40
  "directory.organizations.errors.orgIdRequired": "Se requiere el identificador de la organización",
41
+ "directory.organizations.errors.replayCycle": "La jerarquía de organizaciones ha cambiado desde esta acción, por lo que no se puede revertir sin que una organización pase a ser su propio ancestro.",
41
42
  "directory.organizations.errors.tenantRequired": "La selección de inquilino es obligatoria para los superadministradores",
42
43
  "directory.organizations.flash.created": "Organización creada",
43
44
  "directory.organizations.flash.deleted": "Organización eliminada",
@@ -38,6 +38,7 @@
38
38
  "directory.organizations.errors.deleteFailed": "조직 삭제에 실패했습니다",
39
39
  "directory.organizations.errors.invalidQuery": "잘못된 쿼리입니다",
40
40
  "directory.organizations.errors.orgIdRequired": "조직 식별자는 필수입니다",
41
+ "directory.organizations.errors.replayCycle": "이 작업 이후 조직 계층 구조가 변경되어, 조직이 자기 자신의 상위 조직이 되지 않고는 되돌릴 수 없습니다.",
41
42
  "directory.organizations.errors.tenantRequired": "슈퍼 관리자는 테넌트 선택이 필요합니다",
42
43
  "directory.organizations.flash.created": "조직이 생성되었습니다",
43
44
  "directory.organizations.flash.deleted": "조직이 삭제되었습니다",
@@ -38,6 +38,7 @@
38
38
  "directory.organizations.errors.deleteFailed": "Nie udało się usunąć organizacji",
39
39
  "directory.organizations.errors.invalidQuery": "Nieprawidłowe zapytanie",
40
40
  "directory.organizations.errors.orgIdRequired": "Identyfikator organizacji jest wymagany",
41
+ "directory.organizations.errors.replayCycle": "Hierarchia organizacji zmieniła się od czasu tej akcji, więc nie można jej cofnąć bez uczynienia organizacji własnym przodkiem.",
41
42
  "directory.organizations.errors.tenantRequired": "Wybór najemcy jest wymagany dla superadministratorów",
42
43
  "directory.organizations.flash.created": "Organizacja utworzona",
43
44
  "directory.organizations.flash.deleted": "Organizacja usunięta",
@@ -55,6 +55,25 @@ function normalizeUuid(value: unknown): string | null {
55
55
  return v
56
56
  }
57
57
 
58
+ export function findOrganizationParentCycle(
59
+ organizations: ReadonlyArray<Pick<Organization, 'id' | 'parentId'>>,
60
+ organizationIds: Iterable<string>,
61
+ ): string | null {
62
+ const parents = new Map<string, string | null>()
63
+ for (const org of organizations) parents.set(String(org.id), normalizeUuid(org.parentId))
64
+ for (const startId of organizationIds) {
65
+ const seen = new Set<string>([startId])
66
+ let currentId = parents.get(startId) ?? null
67
+ while (currentId && parents.has(currentId)) {
68
+ if (currentId === startId) return startId
69
+ if (seen.has(currentId)) break
70
+ seen.add(currentId)
71
+ currentId = parents.get(currentId) ?? null
72
+ }
73
+ }
74
+ return null
75
+ }
76
+
58
77
  export function computeHierarchyForOrganizations(organizations: Organization[], tenantId: string): ComputedHierarchy {
59
78
  const nodes = new Map<string, InternalNode>()
60
79
 
@@ -173,10 +192,30 @@ export function computeHierarchyForOrganizations(organizations: Organization[],
173
192
  walk(id, [])
174
193
  }
175
194
  }
195
+
196
+ function findCycleCutId(startId: string): string | null {
197
+ const positions = new Map<string, number>()
198
+ const path: string[] = []
199
+ let currentId: string | null = startId
200
+ while (currentId && nodes.has(currentId) && !visited.has(currentId) && !positions.has(currentId)) {
201
+ positions.set(currentId, path.length)
202
+ path.push(currentId)
203
+ currentId = nodes.get(currentId)!.parentId
204
+ }
205
+ if (!currentId || !positions.has(currentId)) return null
206
+ return path
207
+ .slice(positions.get(currentId))
208
+ .reduce((smallest, candidate) => (candidate < smallest ? candidate : smallest))
209
+ }
210
+
176
211
  // Handle orphaned nodes or cycles not reached above
177
212
  for (const id of nodes.keys()) {
178
- if (!visited.has(id)) {
179
- walk(id, [])
213
+ while (!visited.has(id)) {
214
+ const cutId = findCycleCutId(id) ?? id
215
+ const cutNode = nodes.get(cutId)!
216
+ if (cutNode.parentId) nodes.get(cutNode.parentId)?.children.delete(cutId)
217
+ cutNode.parentId = null
218
+ walk(cutId, [])
180
219
  }
181
220
  }
182
221