@open-mercato/shared 0.10.1-develop.8062.1.f29b9ae445 → 0.10.1-develop.8072.1.592356afda
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.
- package/dist/lib/commands/command-bus.js +11 -6
- package/dist/lib/commands/command-bus.js.map +2 -2
- package/dist/lib/version.js +1 -1
- package/dist/lib/version.js.map +1 -1
- package/package.json +2 -2
- package/src/lib/commands/__tests__/command-bus.changes.test.ts +83 -0
- package/src/lib/commands/command-bus.ts +13 -6
|
@@ -103,12 +103,17 @@ function deepEqual(a, b, seen) {
|
|
|
103
103
|
if (seen.has(a) || seen.has(b)) return false;
|
|
104
104
|
seen.add(a);
|
|
105
105
|
seen.add(b);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
106
|
+
try {
|
|
107
|
+
const aRec = a;
|
|
108
|
+
const bRec = b;
|
|
109
|
+
const keysA = Object.keys(aRec);
|
|
110
|
+
const keysB = Object.keys(bRec);
|
|
111
|
+
if (keysA.length !== keysB.length) return false;
|
|
112
|
+
return keysA.every((key) => deepEqual(aRec[key], bRec[key], seen));
|
|
113
|
+
} finally {
|
|
114
|
+
seen.delete(a);
|
|
115
|
+
seen.delete(b);
|
|
116
|
+
}
|
|
112
117
|
}
|
|
113
118
|
return false;
|
|
114
119
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/lib/commands/command-bus.ts"],
|
|
4
|
-
"sourcesContent": ["import type { ActionLog } from '@open-mercato/core/modules/audit_logs/data/entities'\nimport type { ActionLogCreateInput } from '@open-mercato/core/modules/audit_logs/data/validators'\nimport { commandRegistry } from './registry'\nimport type {\n BulkImportSuppression,\n CommandExecutionOptions,\n CommandExecuteResult,\n CommandHandler,\n CommandLogBuilderArgs,\n CommandLogMetadata,\n CommandRuntimeContext,\n CommandUndoLogEntry,\n} from './types'\nimport { defaultUndoToken } from './types'\nimport type { AwilixContainer } from 'awilix'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport {\n canonicalizeResourceTag,\n deriveResourceFromCommandId,\n invalidateCrudCache,\n pickFirstIdentifier,\n isCrudCacheDebugEnabled,\n} from '@open-mercato/shared/lib/crud/cache'\nimport { normalizeCustomFieldKey } from '@open-mercato/shared/lib/custom-fields/keys'\nimport { getAllCommandInterceptorInstances } from './command-interceptor-store'\nimport {\n runCommandInterceptorsBefore,\n runCommandInterceptorsAfter,\n runCommandInterceptorsBeforeUndo,\n runCommandInterceptorsAfterUndo,\n} from './command-interceptor-runner'\nimport type { CommandInterceptorContext } from './command-interceptor'\nimport { CommandInterceptorError } from './errors'\nimport { isReadProjectionAlwaysConsistent } from '@open-mercato/shared/lib/data/consistency'\nimport { createLogger } from '../logger'\nimport { withAtomicFlush } from './flush'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport {\n getTransactionLifetime,\n onTransactionLifetimeComplete,\n ownsTransactionLifetime,\n type TransactionLifetime,\n} from './transaction-lifetime'\n\nconst logger = createLogger('shared').child({ component: 'commands' })\n\ntype CommandActionLogService = {\n findByUndoToken(undoToken: string): Promise<ActionLog | null>\n claimForUndo(id: string, transactionalEm?: EntityManager): Promise<boolean>\n releaseUndoClaim(id: string, transactionalEm?: EntityManager): Promise<boolean>\n markUndone(id: string, traceInput?: ActionLogCreateInput, transactionalEm?: EntityManager): Promise<ActionLog | null | void>\n claimForRedo(id: string, transactionalEm?: EntityManager): Promise<boolean>\n log(input: ActionLogCreateInput, transactionalEm?: EntityManager): Promise<ActionLog | null>\n}\n\ntype AtomicReplayTransaction = {\n em: EntityManager\n joinsAmbient: boolean\n}\n\nfunction isInTransaction(em: EntityManager): boolean {\n const probe = (em as { isInTransaction?: () => boolean }).isInTransaction\n return typeof probe === 'function' && probe.call(em)\n}\n\nfunction resolveAtomicReplayTransaction(ctx: CommandRuntimeContext): AtomicReplayTransaction {\n const em = ctx.transactionalEm ?? (ctx.container.resolve('em') as EntityManager)\n const joinsAmbient = isInTransaction(em)\n if (joinsAmbient && !ownsTransactionLifetime(em, ctx.transactionLifetime)) {\n throw new Error('[internal] Ambient atomic replay requires its outer transaction lifetime owner')\n }\n return { em, joinsAmbient }\n}\n\nfunction bindReplayTransactionContext(\n ctx: CommandRuntimeContext,\n em: EntityManager,\n expectedLifetime?: TransactionLifetime,\n): CommandRuntimeContext {\n const transactionLifetime = getTransactionLifetime(em)\n if (!transactionLifetime || (expectedLifetime && transactionLifetime !== expectedLifetime)) {\n throw new Error('[internal] Atomic replay transaction lifetime owner changed before replay')\n }\n return { ...ctx, transactionalEm: em, transactionLifetime }\n}\n\nconst SKIPPED_ACTION_LOG_RESOURCE_KINDS = new Set<string>([\n 'audit_logs.access',\n 'audit_logs.action',\n 'dashboards.layout',\n 'dashboards.user_widgets',\n 'dashboards.role_widgets',\n])\n\nfunction asRecord(input: unknown): Record<string, unknown> | null {\n if (!input || typeof input !== 'object' || Array.isArray(input)) return null\n return input as Record<string, unknown>\n}\n\n/** Command handlers often return domain keys (e.g. warehouseId) without `id`; cache invalidation must still resolve the record. */\nfunction extractPrimaryIdFromCommandResult(result: unknown): string | null {\n const r = asRecord(result)\n if (!r) return null\n const direct = pickFirstIdentifier(r.id, r.entityId, r.recordId)\n if (direct) return direct\n for (const key of [\n 'warehouseId',\n 'zoneId',\n 'locationId',\n 'lotId',\n 'reservationId',\n 'profileId',\n 'movementId',\n 'balanceId',\n ]) {\n const v = r[key]\n if (typeof v === 'string' && v.trim().length > 0) return v.trim()\n }\n return null\n}\n\nfunction toISOString(value: unknown): string | null {\n if (value instanceof Date) {\n const iso = value.toISOString()\n return Number.isNaN(value.getTime()) ? null : iso\n }\n if (typeof value === 'string') {\n const parsed = new Date(value)\n return Number.isNaN(parsed.getTime()) ? null : parsed.toISOString()\n }\n return null\n}\n\nfunction deepEqual(a: unknown, b: unknown, seen?: Set<unknown>): boolean {\n if (Object.is(a, b)) return true\n if (a instanceof Date || b instanceof Date) {\n const aIso = toISOString(a)\n const bIso = toISOString(b)\n if (aIso != null && bIso != null) return aIso === bIso\n return false\n }\n if (Array.isArray(a) && Array.isArray(b)) {\n if (a.length !== b.length) return false\n return a.every((value, index) => deepEqual(value, b[index], seen))\n }\n if (a && b && typeof a === 'object' && typeof b === 'object') {\n if (!seen) seen = new Set()\n if (seen.has(a) || seen.has(b)) return false\n seen.add(a)\n seen.add(b)\n const aRec = a as Record<string, unknown>\n const bRec = b as Record<string, unknown>\n const keysA = Object.keys(aRec)\n const keysB = Object.keys(bRec)\n if (keysA.length !== keysB.length) return false\n return keysA.every((key) => deepEqual(aRec[key], bRec[key], seen))\n }\n return false\n}\n\nconst CUSTOM_FIELD_CONTAINER_KEYS = new Set(['custom', 'customFields', 'customValues', 'cf'])\nconst SKIPPED_CHANGE_KEYS = new Set(['updatedAt', 'updated_at'])\n\nfunction appendCustomFieldChanges(\n changes: Record<string, { from: unknown; to: unknown }>,\n before: unknown,\n after: unknown\n): boolean {\n const beforeRec = asRecord(before)\n const afterRec = asRecord(after)\n if (!beforeRec && !afterRec) return false\n const left = beforeRec ?? {}\n const right = afterRec ?? {}\n const keys = new Set([...Object.keys(left), ...Object.keys(right)])\n for (const key of keys) {\n const from = left[key]\n const to = right[key]\n if (!deepEqual(from, to)) {\n changes[normalizeCustomFieldKey(key)] = { from, to }\n }\n }\n return true\n}\n\nfunction buildRecordChanges(\n before: Record<string, unknown>,\n after: Record<string, unknown>,\n): Record<string, { from: unknown; to: unknown }> {\n return buildRecordChangesDeep(before, after)\n}\n\nfunction buildRecordChangesDeep(\n before: Record<string, unknown>,\n after: Record<string, unknown>,\n prefix?: string,\n seen?: Set<unknown>,\n): Record<string, { from: unknown; to: unknown }> {\n const changes: Record<string, { from: unknown; to: unknown }> = {}\n if (!seen) seen = new Set()\n if (seen.has(before) || seen.has(after)) return changes\n seen.add(before)\n seen.add(after)\n const keys = new Set([...Object.keys(before), ...Object.keys(after)])\n for (const key of keys) {\n if (SKIPPED_CHANGE_KEYS.has(key)) continue\n if (CUSTOM_FIELD_CONTAINER_KEYS.has(key)) {\n const handled = appendCustomFieldChanges(changes, before[key], after[key])\n if (handled) continue\n }\n const from = before[key]\n const to = after[key]\n const path = prefix ? `${prefix}.${key}` : key\n const fromRec = asRecord(from)\n const toRec = asRecord(to)\n if (fromRec && toRec) {\n const nested = buildRecordChangesDeep(fromRec, toRec, path, seen)\n if (Object.keys(nested).length) {\n Object.assign(changes, nested)\n continue\n }\n }\n if (!deepEqual(from, to)) {\n changes[path] = { from, to }\n }\n }\n return changes\n}\n\nfunction deriveChangesFromSnapshots(\n before: unknown,\n after: unknown,\n): Record<string, { from: unknown; to: unknown }> | null {\n const beforeRec = asRecord(before)\n const afterRec = asRecord(after)\n if (!beforeRec || !afterRec) return null\n const changes = buildRecordChanges(beforeRec, afterRec)\n return Object.keys(changes).length ? changes : null\n}\n\nfunction invertRecordedChanges(\n changes: unknown,\n): Record<string, { from: unknown; to: unknown }> | null {\n const source = asRecord(changes)\n if (!source) return null\n const inverted: Record<string, { from: unknown; to: unknown }> = {}\n for (const [key, value] of Object.entries(source)) {\n const entry = asRecord(value)\n if (!entry || (!('from' in entry) && !('to' in entry))) continue\n inverted[key] = {\n from: entry.to,\n to: entry.from,\n }\n }\n return Object.keys(inverted).length ? inverted : null\n}\n\nfunction extractAliasList(source: unknown): string[] {\n if (!source || typeof source !== 'object' || Array.isArray(source)) return []\n const record = source as Record<string, unknown>\n const raw = record.cacheAliases\n if (!Array.isArray(raw)) return []\n const aliases = new Set<string>()\n for (const value of raw) {\n if (typeof value !== 'string') continue\n const normalized = canonicalizeResourceTag(value)\n if (normalized) aliases.add(normalized)\n }\n return Array.from(aliases)\n}\n\nexport class CommandBus {\n async execute<TInput = unknown, TResult = unknown>(\n commandId: string,\n options: CommandExecutionOptions<TInput>\n ): Promise<CommandExecuteResult<TResult>> {\n const handler = await this.resolveHandler<TInput, TResult>(commandId)\n\n const replayLogEntry = options.redoLogEntry ?? null\n const isAtomicReplay = replayLogEntry !== null && handler.atomicReplay === true\n const allInterceptors = getAllCommandInterceptorInstances()\n let interceptorMetadata = new Map<string, Record<string, unknown>>()\n let effectiveOptions = options\n let userFeatures: string[] = []\n const runBeforeExecuteInterceptors = async (baseOptions: CommandExecutionOptions<TInput>) => {\n const resolvedUserFeatures = allInterceptors.length\n ? await this.resolveUserFeaturesForInterceptors(baseOptions.ctx)\n : []\n if (!allInterceptors.length) {\n return {\n effectiveOptions: baseOptions,\n metadata: new Map<string, Record<string, unknown>>(),\n userFeatures: resolvedUserFeatures,\n }\n }\n const interceptorCtx: CommandInterceptorContext = {\n commandId,\n auth: baseOptions.ctx.auth ?? null,\n selectedOrganizationId: baseOptions.ctx.selectedOrganizationId ?? baseOptions.ctx.auth?.orgId ?? null,\n container: baseOptions.ctx.container,\n organizationScope: baseOptions.ctx.organizationScope ?? null,\n request: baseOptions.ctx.request ?? null,\n }\n const beforeResult = await runCommandInterceptorsBefore(\n allInterceptors, commandId, baseOptions.input, interceptorCtx, resolvedUserFeatures,\n )\n if (!beforeResult.ok) {\n const blocked = beforeResult.error!\n throw new CommandInterceptorError(blocked.message, { status: blocked.status, body: blocked.body })\n }\n const preparedOptions = beforeResult.modifiedInput\n ? {\n ...baseOptions,\n input: { ...(baseOptions.input as object), ...beforeResult.modifiedInput } as TInput,\n }\n : baseOptions\n return {\n effectiveOptions: preparedOptions,\n metadata: beforeResult.metadataByInterceptor,\n userFeatures: resolvedUserFeatures,\n }\n }\n\n if (replayLogEntry && !isAtomicReplay) {\n await this.runReplayTransactionGuard(options.ctx, 'redo', replayLogEntry)\n if (handler.authorizeReplay) {\n await handler.authorizeReplay({\n operation: 'redo',\n input: options.input,\n ctx: options.ctx,\n logEntry: replayLogEntry,\n })\n }\n }\n\n if (!isAtomicReplay) {\n const prepared = await runBeforeExecuteInterceptors(options)\n effectiveOptions = prepared.effectiveOptions\n interceptorMetadata = prepared.metadata\n userFeatures = prepared.userFeatures\n }\n\n const executeCore = async (coreOptions: CommandExecutionOptions<TInput>) => {\n const snapshots: { before?: unknown } = {}\n let executionOptions = coreOptions\n if (handler.prepareSnapshotInsideTransaction && handler.prepare) {\n const prepare = handler.prepare\n const originalCtx = coreOptions.ctx\n const originalBeforeTransactionalWrite = originalCtx.beforeTransactionalWrite\n let prepared = false\n let transactionalCtx: CommandRuntimeContext\n transactionalCtx = {\n ...originalCtx,\n beforeTransactionalWrite: async (em, input) => {\n await originalBeforeTransactionalWrite?.(em, input)\n if (prepared) return\n Object.assign(\n snapshots,\n (await prepare(coreOptions.input, { ...transactionalCtx, transactionalEm: em })) ?? {},\n )\n prepared = true\n },\n }\n executionOptions = { ...coreOptions, ctx: transactionalCtx }\n } else {\n Object.assign(snapshots, await this.prepareSnapshots(handler, coreOptions))\n }\n effectiveOptions = executionOptions\n const sourceLog = executionOptions.redoLogEntry ?? null\n const result =\n sourceLog && typeof handler.redo === 'function'\n ? await handler.redo({ input: executionOptions.input, ctx: executionOptions.ctx, logEntry: sourceLog })\n : await handler.execute(executionOptions.input, executionOptions.ctx)\n const afterSnapshot = await this.captureAfter(handler, executionOptions, result)\n const snapshotsWithAfter = { ...snapshots, after: afterSnapshot }\n const logMeta = await this.buildLog(handler, executionOptions, result, snapshotsWithAfter)\n let mergedMeta = this.mergeMetadata(executionOptions.metadata, logMeta)\n // Interceptors opt into audit-log enrichment with a reserved `logContext` key rather\n // than the generic `context` one, so the metadata an interceptor already passes to its\n // own afterExecute hook is never silently promoted into audit storage.\n // Map iteration order is interceptor priority order (see collectMatching), so a\n // later-priority interceptor overrides an earlier one on key collisions.\n let interceptorContextMerged: Record<string, unknown> = {}\n for (const meta of interceptorMetadata.values()) {\n const logContextRecord = asRecord(asRecord(meta)?.logContext)\n if (!logContextRecord) continue\n interceptorContextMerged = {\n ...interceptorContextMerged,\n ...logContextRecord,\n }\n }\n const baseContext = asRecord(executionOptions.metadata?.context) ?? {}\n const logMetaContext = asRecord(logMeta?.context) ?? {}\n if (Object.keys(interceptorContextMerged).length > 0 || Object.keys(baseContext).length > 0 || Object.keys(logMetaContext).length > 0) {\n mergedMeta = mergedMeta ?? {}\n mergedMeta.context = {\n ...baseContext,\n ...interceptorContextMerged,\n ...logMetaContext,\n }\n }\n const undoable = this.isUndoable(handler)\n if (undoable && mergedMeta?.replayable !== false) {\n mergedMeta = mergedMeta ?? {}\n if (!mergedMeta.undoToken) mergedMeta.undoToken = defaultUndoToken()\n if (mergedMeta.actorUserId === undefined) mergedMeta.actorUserId = executionOptions.ctx.auth?.sub ?? null\n } else if (mergedMeta?.replayable === false) {\n mergedMeta.undoToken = null\n }\n if (afterSnapshot !== undefined && afterSnapshot !== null) {\n if (!mergedMeta) {\n mergedMeta = { snapshotAfter: afterSnapshot }\n } else if (!mergedMeta.snapshotAfter) {\n mergedMeta.snapshotAfter = afterSnapshot\n }\n }\n if (snapshots.before) {\n if (!mergedMeta) {\n mergedMeta = { snapshotBefore: snapshots.before }\n } else if (!mergedMeta.snapshotBefore) {\n mergedMeta.snapshotBefore = snapshots.before\n }\n }\n if (mergedMeta?.snapshotBefore !== undefined && mergedMeta?.snapshotAfter !== undefined) {\n const currentChanges = mergedMeta.changes\n const shouldInfer =\n currentChanges === undefined ||\n currentChanges === null ||\n (typeof currentChanges === 'object' && !Array.isArray(currentChanges) && Object.keys(currentChanges).length === 0)\n if (shouldInfer) {\n const inferred = deriveChangesFromSnapshots(mergedMeta.snapshotBefore, mergedMeta.snapshotAfter)\n if (inferred) mergedMeta.changes = inferred\n }\n }\n const logEntry = await this.persistLog(commandId, executionOptions, mergedMeta)\n return { result, mergedMeta, logEntry }\n }\n\n let replaySourceFinalized = false\n let coreResult: Awaited<ReturnType<typeof executeCore>>\n const atomicSourceLog = options.redoLogEntry ?? null\n let ambientReplay: AtomicReplayTransaction | null = null\n let replayTransactionLifetime: TransactionLifetime | null = null\n if (atomicSourceLog && isAtomicReplay) {\n ambientReplay = resolveAtomicReplayTransaction(options.ctx)\n const replayEm = ambientReplay.em\n await withAtomicFlush(replayEm, [async () => {\n const replayCtx = bindReplayTransactionContext(\n options.ctx,\n replayEm,\n ambientReplay?.joinsAmbient ? options.ctx.transactionLifetime : undefined,\n )\n replayTransactionLifetime = replayCtx.transactionLifetime ?? null\n const transactionalBaseOptions: CommandExecutionOptions<TInput> = {\n ...options,\n ctx: replayCtx,\n }\n if (!atomicSourceLog.id) throw new Error('[internal] Atomic redo source log id is required')\n if (handler.stabilizeReplay) {\n await handler.stabilizeReplay({\n operation: 'redo',\n input: transactionalBaseOptions.input,\n ctx: transactionalBaseOptions.ctx,\n logEntry: atomicSourceLog,\n })\n }\n await this.runReplayTransactionGuard(\n transactionalBaseOptions.ctx,\n 'redo',\n atomicSourceLog,\n replayEm,\n )\n const prepared = await runBeforeExecuteInterceptors(transactionalBaseOptions)\n effectiveOptions = prepared.effectiveOptions\n interceptorMetadata = prepared.metadata\n userFeatures = prepared.userFeatures\n const service = effectiveOptions.ctx.container.resolve('actionLogService') as CommandActionLogService\n if (handler.authorizeReplay) {\n await handler.authorizeReplay({\n operation: 'redo',\n input: effectiveOptions.input,\n ctx: effectiveOptions.ctx,\n logEntry: atomicSourceLog,\n })\n }\n const claimed = await service.claimForRedo(atomicSourceLog.id, replayEm)\n if (!claimed) throw new Error('[internal] Redo source already consumed')\n coreResult = await executeCore(effectiveOptions)\n }], { transaction: true, label: `${commandId}.redo` })\n replaySourceFinalized = !ambientReplay.joinsAmbient\n } else {\n coreResult = await executeCore(effectiveOptions)\n }\n const { result, mergedMeta, logEntry } = coreResult!\n\n const runPostCommit = async () => {\n let finalResult = result\n if (allInterceptors.length) {\n const interceptorCtx: CommandInterceptorContext = {\n commandId,\n auth: effectiveOptions.ctx.auth ?? null,\n selectedOrganizationId: effectiveOptions.ctx.selectedOrganizationId ?? effectiveOptions.ctx.auth?.orgId ?? null,\n container: effectiveOptions.ctx.container,\n organizationScope: effectiveOptions.ctx.organizationScope ?? null,\n request: effectiveOptions.ctx.request ?? null,\n }\n const afterResult = await runCommandInterceptorsAfter(\n allInterceptors, commandId, effectiveOptions.input, result, interceptorCtx,\n userFeatures, interceptorMetadata,\n )\n if (afterResult.modifiedResult && typeof result === 'object' && result) {\n finalResult = { ...(result as object), ...afterResult.modifiedResult } as Awaited<TResult>\n }\n }\n\n if (!effectiveOptions.skipCacheInvalidation) {\n await this.invalidateCacheAfterExecute(commandId, effectiveOptions, finalResult, mergedMeta)\n }\n // Bulk-import backfills defer heavy per-record side effects: the ctx flags are read here and\n // threaded as a local into the flush (never stored on the shared dataEngine), so a concurrent\n // command with different flags can't observe them. Reindex is restored by the caller's\n // end-of-run `query_index rebuild`. Mirrors `skipCacheInvalidation` above.\n await this.flushCrudSideEffects(effectiveOptions.ctx.container, effectiveOptions.ctx?.bulkImport)\n return finalResult\n }\n\n const executionResult: CommandExecuteResult<TResult> = {\n result,\n logEntry,\n replaySourceFinalized,\n }\n if (ambientReplay?.joinsAmbient) {\n if (!replayTransactionLifetime || !ownsTransactionLifetime(ambientReplay.em, replayTransactionLifetime)) {\n throw new Error('[internal] Ambient atomic replay lost its outer transaction lifetime owner')\n }\n onTransactionLifetimeComplete(ambientReplay.em, async (outcome) => {\n if (outcome !== 'committed') return\n executionResult.result = await runPostCommit()\n executionResult.replaySourceFinalized = true\n })\n return executionResult\n }\n\n executionResult.result = await runPostCommit()\n return executionResult\n }\n\n async undo(undoToken: string, ctx: CommandRuntimeContext): Promise<void> {\n const service = (ctx.container.resolve('actionLogService') as CommandActionLogService)\n const log = await service.findByUndoToken(undoToken)\n if (!log) throw new Error('Undo token expired or not found')\n const handler = await this.resolveHandler(log.commandId)\n if (!handler.undo || this.isUndoable(handler) === false) {\n throw new Error(`Command ${log.commandId} is not undoable`)\n }\n\n const isAtomicReplay = handler.atomicReplay === true\n const allInterceptors = getAllCommandInterceptorInstances()\n let undoInterceptorMetadata = new Map<string, Record<string, unknown>>()\n let userFeatures: string[] = []\n const runBeforeUndoInterceptors = async (replayCtx: CommandRuntimeContext) => {\n const resolvedUserFeatures = allInterceptors.length\n ? await this.resolveUserFeaturesForInterceptors(replayCtx)\n : []\n if (!allInterceptors.length) {\n return {\n metadata: new Map<string, Record<string, unknown>>(),\n userFeatures: resolvedUserFeatures,\n }\n }\n const undoCtx = { input: log.commandPayload, logEntry: log, undoToken }\n const interceptorCtx: CommandInterceptorContext = {\n commandId: log.commandId,\n auth: replayCtx.auth ?? null,\n selectedOrganizationId: replayCtx.selectedOrganizationId ?? replayCtx.auth?.orgId ?? null,\n container: replayCtx.container,\n organizationScope: replayCtx.organizationScope ?? null,\n request: replayCtx.request ?? null,\n }\n const beforeResult = await runCommandInterceptorsBeforeUndo(\n allInterceptors, log.commandId, undoCtx, interceptorCtx, resolvedUserFeatures,\n )\n if (!beforeResult.ok) {\n const blocked = beforeResult.error!\n throw new CommandInterceptorError(blocked.message, { status: blocked.status, body: blocked.body })\n }\n return {\n metadata: beforeResult.metadataByInterceptor,\n userFeatures: resolvedUserFeatures,\n }\n }\n\n if (!isAtomicReplay) {\n await this.runReplayTransactionGuard(ctx, 'undo', log)\n if (handler.authorizeReplay) {\n await handler.authorizeReplay({\n operation: 'undo',\n input: log.commandPayload as Parameters<NonNullable<typeof handler.authorizeReplay>>[0]['input'],\n ctx,\n logEntry: log,\n })\n }\n }\n\n const claimUndo = async (replayEm?: EntityManager) => {\n // Two contenders can both pass the initial lookup; only the compare-and-set\n // below may proceed. On the atomic path this state is still uncommitted and\n // rolls back with every handler/log write if anything later fails.\n const claimed = replayEm\n ? await service.claimForUndo(log.id, replayEm)\n : await service.claimForUndo(log.id)\n if (!claimed) throw new Error('[internal] Undo token already consumed')\n }\n const finishUndo = async (replayCtx: CommandRuntimeContext, replayEm?: EntityManager) => {\n await handler.undo!({\n input: log.commandPayload as Parameters<NonNullable<typeof handler.undo>>[0]['input'],\n ctx: replayCtx,\n logEntry: log,\n })\n const traceLog = this.buildUndoTraceLog(log, replayCtx)\n if (replayEm) {\n await service.markUndone(log.id, traceLog, replayEm)\n } else {\n await service.markUndone(log.id, traceLog)\n }\n }\n\n let ambientReplay: AtomicReplayTransaction | null = null\n let replayTransactionLifetime: TransactionLifetime | null = null\n if (isAtomicReplay) {\n ambientReplay = resolveAtomicReplayTransaction(ctx)\n const replayEm = ambientReplay.em\n await withAtomicFlush(replayEm, [async () => {\n const replayCtx = bindReplayTransactionContext(\n ctx,\n replayEm,\n ambientReplay?.joinsAmbient ? ctx.transactionLifetime : undefined,\n )\n replayTransactionLifetime = replayCtx.transactionLifetime ?? null\n if (handler.stabilizeReplay) {\n await handler.stabilizeReplay({\n operation: 'undo',\n input: log.commandPayload as Parameters<NonNullable<typeof handler.stabilizeReplay>>[0]['input'],\n ctx: replayCtx,\n logEntry: log,\n })\n }\n await this.runReplayTransactionGuard(replayCtx, 'undo', log, replayEm)\n const prepared = await runBeforeUndoInterceptors(replayCtx)\n undoInterceptorMetadata = prepared.metadata\n userFeatures = prepared.userFeatures\n if (handler.authorizeReplay) {\n await handler.authorizeReplay({\n operation: 'undo',\n input: log.commandPayload as Parameters<NonNullable<typeof handler.authorizeReplay>>[0]['input'],\n ctx: replayCtx,\n logEntry: log,\n })\n }\n await claimUndo(replayEm)\n await finishUndo(replayCtx, replayEm)\n }], {\n transaction: true,\n label: `${log.commandId}.undo`,\n })\n } else {\n try {\n // Preserve the established non-atomic lifecycle exactly: authorization,\n // source claim, before-undo interceptors, handler, then finalization.\n await claimUndo()\n const prepared = await runBeforeUndoInterceptors(ctx)\n undoInterceptorMetadata = prepared.metadata\n userFeatures = prepared.userFeatures\n await finishUndo(ctx)\n } catch (err) {\n // Legacy handlers retain their compensating release behavior because\n // their domain mutation may not share the action-log transaction.\n await service.releaseUndoClaim(log.id).catch(() => {})\n throw err\n }\n }\n\n const runPostCommit = async () => {\n // Post-commit hooks and side effects run only after an atomic replay has\n // durably committed both the domain state and source/trace log state.\n if (allInterceptors.length) {\n const undoCtx = { input: log.commandPayload, logEntry: log, undoToken }\n const interceptorCtx: CommandInterceptorContext = {\n commandId: log.commandId,\n auth: ctx.auth ?? null,\n selectedOrganizationId: ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null,\n container: ctx.container,\n organizationScope: ctx.organizationScope ?? null,\n request: ctx.request ?? null,\n }\n await runCommandInterceptorsAfterUndo(\n allInterceptors, log.commandId, undoCtx, interceptorCtx,\n userFeatures, undoInterceptorMetadata,\n )\n }\n\n await this.invalidateCacheAfterUndo(log, ctx)\n await this.flushCrudSideEffects(ctx.container)\n }\n\n if (ambientReplay?.joinsAmbient) {\n if (!replayTransactionLifetime || !ownsTransactionLifetime(ambientReplay.em, replayTransactionLifetime)) {\n throw new Error('[internal] Ambient atomic replay lost its outer transaction lifetime owner')\n }\n onTransactionLifetimeComplete(ambientReplay.em, async (outcome) => {\n if (outcome === 'committed') await runPostCommit()\n })\n return\n }\n\n await runPostCommit()\n }\n\n private buildUndoTraceLog(log: ActionLog, ctx: CommandRuntimeContext): ActionLogCreateInput | undefined {\n const snapshotBefore = log.snapshotAfter ?? null\n const snapshotAfter = log.snapshotBefore ?? null\n const changes =\n deriveChangesFromSnapshots(snapshotBefore, snapshotAfter)\n ?? invertRecordedChanges(log.changesJson)\n ?? undefined\n\n const baseContext = asRecord(log.contextJson) ?? {}\n const context = {\n ...baseContext,\n historyAction: 'undo',\n sourceLogId: log.id,\n sourceCommandId: log.commandId,\n }\n\n return {\n tenantId: log.tenantId ?? ctx.auth?.tenantId ?? null,\n organizationId: log.organizationId ?? ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null,\n actorUserId: ctx.auth?.sub ?? log.actorUserId ?? null,\n commandId: log.commandId,\n actionLabel: log.actionLabel ?? undefined,\n resourceKind: log.resourceKind ?? undefined,\n resourceId: log.resourceId ?? undefined,\n parentResourceKind: log.parentResourceKind ?? null,\n parentResourceId: log.parentResourceId ?? null,\n relatedResourceKind: log.relatedResourceKind ?? null,\n relatedResourceId: log.relatedResourceId ?? null,\n snapshotBefore,\n snapshotAfter,\n changes,\n context,\n }\n }\n\n private async resolveUserFeaturesForInterceptors(ctx: CommandRuntimeContext): Promise<string[]> {\n if (!ctx.auth) return []\n type RbacLike = {\n getGrantedFeatures: (\n userId: string,\n opts: { tenantId: string | null; organizationId: string | null },\n ) => Promise<string[]>\n getGrantedFeaturesWithEntityManager?: (\n em: EntityManager,\n userId: string,\n opts: { tenantId: string | null; organizationId: string | null },\n ) => Promise<string[]>\n }\n let rbac: RbacLike | undefined\n try {\n rbac = ctx.container.resolve('rbacService') as RbacLike | undefined\n } catch {\n // Intentional: rbacService is not registered in all runtime contexts (CLI, tests, bootstrap).\n return []\n }\n const scope = {\n tenantId: ctx.auth.tenantId,\n organizationId: ctx.selectedOrganizationId ?? ctx.auth.orgId,\n }\n if (ctx.transactionalEm) {\n if (!rbac?.getGrantedFeaturesWithEntityManager) return []\n return await rbac.getGrantedFeaturesWithEntityManager(\n ctx.transactionalEm,\n ctx.auth.sub,\n scope,\n )\n }\n try {\n if (rbac?.getGrantedFeatures) {\n return await rbac.getGrantedFeatures(ctx.auth.sub, scope)\n }\n } catch {\n // Outside a transaction a failed lookup cannot poison anything; interceptors without\n // feature gating still run. Inside one the error is rethrown above, because the failed\n // query has already aborted the transaction.\n }\n return []\n }\n\n private async runReplayTransactionGuard(\n ctx: CommandRuntimeContext,\n operation: 'undo' | 'redo',\n logEntry: CommandUndoLogEntry,\n transactionalEm?: EntityManager,\n ): Promise<void> {\n if (!ctx.replayTransactionGuard) return\n if (transactionalEm) {\n await ctx.replayTransactionGuard({ operation, logEntry, transactionalEm })\n return\n }\n const requestEm = ctx.container.resolve('em') as EntityManager\n const guardEm = typeof requestEm.fork === 'function' ? requestEm.fork() : requestEm\n await withAtomicFlush(guardEm, [async () => {\n await ctx.replayTransactionGuard!({ operation, logEntry, transactionalEm: guardEm })\n }], { transaction: true, label: `${logEntry.commandId}.${operation}.request-guard` })\n }\n\n private async resolveHandler<TInput, TResult>(commandId: string): Promise<CommandHandler<TInput, TResult>> {\n const handler =\n commandRegistry.get<TInput, TResult>(commandId) ??\n ((await commandRegistry.load(commandId)) as CommandHandler<TInput, TResult> | null)\n if (!handler) {\n const moduleName = commandId.split('.')[0]\n const registered = commandRegistry.list()\n const sameModule = registered.filter((id) => id.split('.')[0] === moduleName)\n const registeredLoaders = commandRegistry.listLoaders()\n const sameModuleLoaders = registeredLoaders.filter((id) => id === commandId || id.startsWith(`${moduleName}:`))\n const hint = sameModule.length > 0\n ? ` Registered commands for module \"${moduleName}\": [${sameModule.join(', ')}].`\n : sameModuleLoaders.length > 0\n ? ` Command loaders for module \"${moduleName}\" were registered but none loaded \"${commandId}\".`\n : ` No commands or command loaders registered for module \"${moduleName}\". Ensure the command file is imported or generated lazy command loaders are registered.`\n throw new Error(`Command handler not registered for id ${commandId}.${hint}`)\n }\n return handler\n }\n\n private async prepareSnapshots<TInput, TResult>(\n handler: CommandHandler<TInput, TResult>,\n options: CommandExecutionOptions<TInput>\n ): Promise<{ before?: unknown }> {\n if (!handler.prepare) return {}\n try {\n return (await handler.prepare(options.input, options.ctx)) || {}\n } catch (err) {\n throw err\n }\n }\n\n private async captureAfter<TInput, TResult>(\n handler: CommandHandler<TInput, TResult>,\n options: CommandExecutionOptions<TInput>,\n result: TResult\n ): Promise<unknown> {\n if (!handler.captureAfter) return undefined\n return handler.captureAfter(options.input, result, options.ctx)\n }\n\n private async buildLog<TInput, TResult>(\n handler: CommandHandler<TInput, TResult>,\n options: CommandExecutionOptions<TInput>,\n result: TResult,\n snapshots: { before?: unknown; after?: unknown }\n ): Promise<CommandLogMetadata | null> {\n if (!handler.buildLog) return null\n const args: CommandLogBuilderArgs<TInput, TResult> = {\n input: options.input,\n result,\n ctx: options.ctx,\n snapshots,\n }\n return (await handler.buildLog(args)) || null\n }\n\n private mergeMetadata(primary?: CommandLogMetadata | null, secondary?: CommandLogMetadata | null): CommandLogMetadata | null {\n if (!primary && !secondary) return null\n return {\n skipLog: secondary?.skipLog ?? primary?.skipLog ?? false,\n replayable:\n secondary?.replayable === false || primary?.replayable === false\n ? false\n : secondary?.replayable ?? primary?.replayable,\n tenantId: secondary?.tenantId ?? primary?.tenantId ?? null,\n organizationId: secondary?.organizationId ?? primary?.organizationId ?? null,\n actorUserId: secondary?.actorUserId ?? primary?.actorUserId ?? null,\n onBehalfOfUserId: secondary?.onBehalfOfUserId ?? primary?.onBehalfOfUserId ?? null,\n actionLabel: secondary?.actionLabel ?? primary?.actionLabel ?? null,\n resourceKind: secondary?.resourceKind ?? primary?.resourceKind ?? null,\n resourceId: secondary?.resourceId ?? primary?.resourceId ?? null,\n parentResourceKind: secondary?.parentResourceKind ?? primary?.parentResourceKind ?? null,\n parentResourceId: secondary?.parentResourceId ?? primary?.parentResourceId ?? null,\n relatedResourceKind: secondary?.relatedResourceKind ?? primary?.relatedResourceKind ?? null,\n relatedResourceId: secondary?.relatedResourceId ?? primary?.relatedResourceId ?? null,\n undoToken: secondary?.undoToken ?? primary?.undoToken ?? null,\n // A redacted redo input always wins: it exists precisely because the raw\n // input carries a secret that must not reach `action_logs`.\n redoInput: secondary?.redoInput !== undefined ? secondary.redoInput : primary?.redoInput,\n payload: secondary?.payload ?? primary?.payload ?? null,\n snapshotBefore: secondary?.snapshotBefore ?? primary?.snapshotBefore ?? null,\n snapshotAfter: secondary?.snapshotAfter ?? primary?.snapshotAfter ?? null,\n changes: secondary?.changes ?? primary?.changes ?? null,\n context: secondary?.context ?? primary?.context ?? null,\n }\n }\n\n private async persistLog<TInput>(\n commandId: string,\n options: CommandExecutionOptions<TInput>,\n metadata: CommandLogMetadata | null\n ): Promise<ActionLog | null> {\n if (!metadata) return null\n if (metadata.skipLog) return null\n const resourceKind =\n typeof metadata.resourceKind === 'string' ? metadata.resourceKind : null\n if (resourceKind && SKIPPED_ACTION_LOG_RESOURCE_KINDS.has(resourceKind)) {\n return null\n }\n let service: CommandActionLogService | null = null\n try {\n service = (options.ctx.container.resolve('actionLogService') as CommandActionLogService)\n } catch {\n service = null\n }\n if (!service) return null\n\n const tenantId = metadata.tenantId ?? options.ctx.auth?.tenantId ?? null\n const organizationId =\n metadata.organizationId ?? options.ctx.selectedOrganizationId ?? options.ctx.auth?.orgId ?? null\n // On-behalf-of attribution (Wave 4 P2): when `ctx.runAs` is set the actor is\n // the agent principal and the human it acts for is recorded separately. This\n // funnels agent writes through the SAME ActionLog path as a human's \u2014 only the\n // attribution differs (actorUserId=agent, onBehalfOfUserId=human, source='agent').\n const runAs = options.ctx.runAs ?? null\n const actorUserId = runAs?.actorUserId ?? metadata.actorUserId ?? options.ctx.auth?.sub ?? null\n const onBehalfOfUserId = runAs ? (runAs.onBehalfOfUserId ?? null) : (metadata.onBehalfOfUserId ?? null)\n const systemActorContext = !actorUserId && options.ctx.systemActor === true\n ? { systemActor: 'system:command' }\n : null\n const payload: Record<string, unknown> = {\n tenantId: tenantId ?? undefined,\n organizationId: organizationId ?? undefined,\n actorUserId: actorUserId ?? undefined,\n onBehalfOfUserId: onBehalfOfUserId ?? undefined,\n commandId,\n }\n\n if (metadata) {\n if ('actionLabel' in metadata && metadata.actionLabel != null) payload.actionLabel = metadata.actionLabel\n if ('resourceKind' in metadata && metadata.resourceKind != null) payload.resourceKind = metadata.resourceKind\n if ('resourceId' in metadata && metadata.resourceId != null) payload.resourceId = metadata.resourceId\n if ('parentResourceKind' in metadata && metadata.parentResourceKind != null) payload.parentResourceKind = metadata.parentResourceKind\n if ('parentResourceId' in metadata && metadata.parentResourceId != null) payload.parentResourceId = metadata.parentResourceId\n if ('relatedResourceKind' in metadata && metadata.relatedResourceKind != null) payload.relatedResourceKind = metadata.relatedResourceKind\n if ('relatedResourceId' in metadata && metadata.relatedResourceId != null) payload.relatedResourceId = metadata.relatedResourceId\n if ('undoToken' in metadata && metadata.undoToken != null) payload.undoToken = metadata.undoToken\n if ('payload' in metadata && metadata.payload !== undefined) payload.commandPayload = metadata.payload\n if ('snapshotBefore' in metadata && metadata.snapshotBefore !== undefined) payload.snapshotBefore = metadata.snapshotBefore\n if ('snapshotAfter' in metadata && metadata.snapshotAfter !== undefined) payload.snapshotAfter = metadata.snapshotAfter\n if ('changes' in metadata && metadata.changes !== undefined && metadata.changes !== null) payload.changes = metadata.changes\n if ('context' in metadata && metadata.context !== undefined && metadata.context !== null) {\n payload.context = { ...(systemActorContext ?? {}), ...metadata.context }\n } else if (systemActorContext) {\n payload.context = systemActorContext\n }\n }\n\n if (runAs) {\n // Stamp the audit source so `deriveActionLogSource` projects `sourceKey='agent'`.\n // Merge into any caller-provided context rather than replacing it.\n const baseContext = asRecord(payload.context) ?? {}\n payload.context = { ...baseContext, source: runAs.source }\n }\n\n if (metadata.replayable === false) {\n delete payload.commandPayload\n } else {\n const redoInput = metadata.redoInput !== undefined ? metadata.redoInput : options.input\n const redoEnvelope = wrapRedoPayload('commandPayload' in payload ? (payload.commandPayload as unknown) : undefined, redoInput)\n payload.commandPayload = redoEnvelope\n }\n\n return options.ctx.transactionalEm\n ? await service.log(payload as ActionLogCreateInput, options.ctx.transactionalEm)\n : await service.log(payload as ActionLogCreateInput)\n }\n\n private isUndoable(handler: CommandHandler<unknown, unknown>): boolean {\n return handler.isUndoable !== false && typeof handler.undo === 'function'\n }\n\n private async invalidateCacheAfterExecute<TResult>(\n commandId: string,\n options: CommandExecutionOptions<unknown>,\n result: TResult,\n metadata: CommandLogMetadata | null\n ): Promise<void> {\n const resource = typeof metadata?.resourceKind === 'string' ? metadata.resourceKind : null\n if (!resource) return\n try {\n const ctx = options.ctx\n const resultRecord = asRecord(result)\n const resultEntity = asRecord(resultRecord?.entity)\n const inputRecord = asRecord(options.input)\n const inputEntity = asRecord(inputRecord?.entity)\n\n const recordId = pickFirstIdentifier(\n metadata?.resourceId,\n extractPrimaryIdFromCommandResult(result),\n resultRecord?.entityId,\n resultRecord?.id,\n resultRecord?.recordId,\n resultEntity?.id,\n inputRecord?.id,\n inputRecord?.entityId,\n inputRecord?.recordId,\n inputEntity?.id\n )\n\n const organizationId = pickFirstIdentifier(\n metadata?.organizationId,\n resultRecord?.organizationId,\n resultEntity?.organizationId,\n inputRecord?.organizationId,\n inputEntity?.organizationId,\n ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null\n )\n\n const tenantId = pickFirstIdentifier(\n metadata?.tenantId,\n resultRecord?.tenantId,\n resultEntity?.tenantId,\n inputRecord?.tenantId,\n inputEntity?.tenantId,\n ctx.auth?.tenantId ?? null\n )\n\n const fallbackTenant = pickFirstIdentifier(metadata?.tenantId, ctx.auth?.tenantId ?? null)\n\n const aliasSet = new Set<string>()\n for (const alias of extractAliasList(metadata?.context ?? null)) {\n aliasSet.add(alias)\n }\n const derived = deriveResourceFromCommandId(commandId)\n if (derived) aliasSet.add(derived)\n const aliasExtras = Array.from(aliasSet)\n await invalidateCrudCache(\n ctx.container,\n resource,\n { id: recordId, organizationId, tenantId },\n fallbackTenant,\n `command:${commandId}:execute`,\n aliasExtras\n )\n } catch (err) {\n if (isCrudCacheDebugEnabled()) {\n try {\n logger.debug('Cache execute-invalidation failed', { commandId, err })\n } catch {}\n }\n }\n }\n\n private async invalidateCacheAfterUndo(log: ActionLog, ctx: CommandRuntimeContext): Promise<void> {\n const resource = typeof log.resourceKind === 'string' ? log.resourceKind : null\n if (!resource) return\n try {\n const recordId = pickFirstIdentifier(log.resourceId)\n const organizationId = pickFirstIdentifier(log.organizationId, ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null)\n const tenantId = pickFirstIdentifier(log.tenantId, ctx.auth?.tenantId ?? null)\n const fallbackTenant = pickFirstIdentifier(log.tenantId, ctx.auth?.tenantId ?? null)\n const aliasSet = new Set<string>()\n for (const alias of extractAliasList(log.contextJson ?? null)) {\n aliasSet.add(alias)\n }\n const derived = deriveResourceFromCommandId(log.commandId)\n if (derived) aliasSet.add(derived)\n const aliasExtras = Array.from(aliasSet)\n await invalidateCrudCache(\n ctx.container,\n resource,\n { id: recordId, organizationId, tenantId },\n fallbackTenant,\n `command:${log.commandId}:undo`,\n aliasExtras\n )\n } catch (err) {\n if (isCrudCacheDebugEnabled()) {\n try {\n logger.debug('Cache undo-invalidation failed', { commandId: log.commandId, err })\n } catch {}\n }\n }\n }\n\n private async flushCrudSideEffects(container: AwilixContainer, suppress?: BulkImportSuppression): Promise<void> {\n try {\n const dataEngine = (container.resolve('dataEngine') as DataEngine)\n await dataEngine.flushOrmEntityChanges(suppress)\n } catch (error) {\n if (isReadProjectionAlwaysConsistent()) {\n throw error\n }\n // best-effort: failures should not block command execution\n }\n }\n}\n\ntype RedoEnvelope = {\n __redoInput: unknown\n [key: string]: unknown\n}\n\nfunction wrapRedoPayload(existing: unknown, input: unknown): RedoEnvelope {\n if (!existing || typeof existing !== 'object' || Array.isArray(existing)) {\n const envelope: RedoEnvelope = { __redoInput: input }\n if (existing !== undefined) envelope.value = existing\n return envelope\n }\n const current = existing as Record<string, unknown>\n if ('__redoInput' in current && current.__redoInput !== undefined) {\n return current as RedoEnvelope\n }\n return { __redoInput: input, ...current }\n}\n"],
|
|
5
|
-
"mappings": "AAEA,SAAS,uBAAuB;AAWhC,SAAS,wBAAwB;AAGjC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,+BAA+B;AACxC,SAAS,yCAAyC;AAClD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,+BAA+B;AACxC,SAAS,wCAAwC;AACjD,SAAS,oBAAoB;AAC7B,SAAS,uBAAuB;AAEhC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AAEP,MAAM,SAAS,aAAa,QAAQ,EAAE,MAAM,EAAE,WAAW,WAAW,CAAC;AAgBrE,SAAS,gBAAgB,IAA4B;AACnD,QAAM,QAAS,GAA2C;AAC1D,SAAO,OAAO,UAAU,cAAc,MAAM,KAAK,EAAE;AACrD;AAEA,SAAS,+BAA+B,KAAqD;AAC3F,QAAM,KAAK,IAAI,mBAAoB,IAAI,UAAU,QAAQ,IAAI;AAC7D,QAAM,eAAe,gBAAgB,EAAE;AACvC,MAA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OAA8B;AACxE,MAAI,CAAC,YAAY,OAAO,aAAa,YAAY,MAAM,QAAQ,QAAQ,GAAG;AACxE,UAAM,WAAyB,EAAE,aAAa,MAAM;AACpD,QAAI,aAAa,OAAW,UAAS,QAAQ;AAC7C,WAAO;AAAA,EACT;AACA,QAAM,UAAU;AAChB,MAAI,iBAAiB,WAAW,QAAQ,gBAAgB,QAAW;AACjE,WAAO;AAAA,EACT;AACA,SAAO,EAAE,aAAa,OAAO,GAAG,QAAQ;AAC1C;",
|
|
4
|
+
"sourcesContent": ["import type { ActionLog } from '@open-mercato/core/modules/audit_logs/data/entities'\nimport type { ActionLogCreateInput } from '@open-mercato/core/modules/audit_logs/data/validators'\nimport { commandRegistry } from './registry'\nimport type {\n BulkImportSuppression,\n CommandExecutionOptions,\n CommandExecuteResult,\n CommandHandler,\n CommandLogBuilderArgs,\n CommandLogMetadata,\n CommandRuntimeContext,\n CommandUndoLogEntry,\n} from './types'\nimport { defaultUndoToken } from './types'\nimport type { AwilixContainer } from 'awilix'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport {\n canonicalizeResourceTag,\n deriveResourceFromCommandId,\n invalidateCrudCache,\n pickFirstIdentifier,\n isCrudCacheDebugEnabled,\n} from '@open-mercato/shared/lib/crud/cache'\nimport { normalizeCustomFieldKey } from '@open-mercato/shared/lib/custom-fields/keys'\nimport { getAllCommandInterceptorInstances } from './command-interceptor-store'\nimport {\n runCommandInterceptorsBefore,\n runCommandInterceptorsAfter,\n runCommandInterceptorsBeforeUndo,\n runCommandInterceptorsAfterUndo,\n} from './command-interceptor-runner'\nimport type { CommandInterceptorContext } from './command-interceptor'\nimport { CommandInterceptorError } from './errors'\nimport { isReadProjectionAlwaysConsistent } from '@open-mercato/shared/lib/data/consistency'\nimport { createLogger } from '../logger'\nimport { withAtomicFlush } from './flush'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport {\n getTransactionLifetime,\n onTransactionLifetimeComplete,\n ownsTransactionLifetime,\n type TransactionLifetime,\n} from './transaction-lifetime'\n\nconst logger = createLogger('shared').child({ component: 'commands' })\n\ntype CommandActionLogService = {\n findByUndoToken(undoToken: string): Promise<ActionLog | null>\n claimForUndo(id: string, transactionalEm?: EntityManager): Promise<boolean>\n releaseUndoClaim(id: string, transactionalEm?: EntityManager): Promise<boolean>\n markUndone(id: string, traceInput?: ActionLogCreateInput, transactionalEm?: EntityManager): Promise<ActionLog | null | void>\n claimForRedo(id: string, transactionalEm?: EntityManager): Promise<boolean>\n log(input: ActionLogCreateInput, transactionalEm?: EntityManager): Promise<ActionLog | null>\n}\n\ntype AtomicReplayTransaction = {\n em: EntityManager\n joinsAmbient: boolean\n}\n\nfunction isInTransaction(em: EntityManager): boolean {\n const probe = (em as { isInTransaction?: () => boolean }).isInTransaction\n return typeof probe === 'function' && probe.call(em)\n}\n\nfunction resolveAtomicReplayTransaction(ctx: CommandRuntimeContext): AtomicReplayTransaction {\n const em = ctx.transactionalEm ?? (ctx.container.resolve('em') as EntityManager)\n const joinsAmbient = isInTransaction(em)\n if (joinsAmbient && !ownsTransactionLifetime(em, ctx.transactionLifetime)) {\n throw new Error('[internal] Ambient atomic replay requires its outer transaction lifetime owner')\n }\n return { em, joinsAmbient }\n}\n\nfunction bindReplayTransactionContext(\n ctx: CommandRuntimeContext,\n em: EntityManager,\n expectedLifetime?: TransactionLifetime,\n): CommandRuntimeContext {\n const transactionLifetime = getTransactionLifetime(em)\n if (!transactionLifetime || (expectedLifetime && transactionLifetime !== expectedLifetime)) {\n throw new Error('[internal] Atomic replay transaction lifetime owner changed before replay')\n }\n return { ...ctx, transactionalEm: em, transactionLifetime }\n}\n\nconst SKIPPED_ACTION_LOG_RESOURCE_KINDS = new Set<string>([\n 'audit_logs.access',\n 'audit_logs.action',\n 'dashboards.layout',\n 'dashboards.user_widgets',\n 'dashboards.role_widgets',\n])\n\nfunction asRecord(input: unknown): Record<string, unknown> | null {\n if (!input || typeof input !== 'object' || Array.isArray(input)) return null\n return input as Record<string, unknown>\n}\n\n/** Command handlers often return domain keys (e.g. warehouseId) without `id`; cache invalidation must still resolve the record. */\nfunction extractPrimaryIdFromCommandResult(result: unknown): string | null {\n const r = asRecord(result)\n if (!r) return null\n const direct = pickFirstIdentifier(r.id, r.entityId, r.recordId)\n if (direct) return direct\n for (const key of [\n 'warehouseId',\n 'zoneId',\n 'locationId',\n 'lotId',\n 'reservationId',\n 'profileId',\n 'movementId',\n 'balanceId',\n ]) {\n const v = r[key]\n if (typeof v === 'string' && v.trim().length > 0) return v.trim()\n }\n return null\n}\n\nfunction toISOString(value: unknown): string | null {\n if (value instanceof Date) {\n const iso = value.toISOString()\n return Number.isNaN(value.getTime()) ? null : iso\n }\n if (typeof value === 'string') {\n const parsed = new Date(value)\n return Number.isNaN(parsed.getTime()) ? null : parsed.toISOString()\n }\n return null\n}\n\nfunction deepEqual(a: unknown, b: unknown, seen?: Set<unknown>): boolean {\n if (Object.is(a, b)) return true\n if (a instanceof Date || b instanceof Date) {\n const aIso = toISOString(a)\n const bIso = toISOString(b)\n if (aIso != null && bIso != null) return aIso === bIso\n return false\n }\n if (Array.isArray(a) && Array.isArray(b)) {\n if (a.length !== b.length) return false\n return a.every((value, index) => deepEqual(value, b[index], seen))\n }\n if (a && b && typeof a === 'object' && typeof b === 'object') {\n if (!seen) seen = new Set()\n // `seen` is the current path: an object met again on it is a cycle. An object reached twice by\n // different paths (the items of one array held under two keys) is compared like any other.\n if (seen.has(a) || seen.has(b)) return false\n seen.add(a)\n seen.add(b)\n try {\n const aRec = a as Record<string, unknown>\n const bRec = b as Record<string, unknown>\n const keysA = Object.keys(aRec)\n const keysB = Object.keys(bRec)\n if (keysA.length !== keysB.length) return false\n return keysA.every((key) => deepEqual(aRec[key], bRec[key], seen))\n } finally {\n seen.delete(a)\n seen.delete(b)\n }\n }\n return false\n}\n\nconst CUSTOM_FIELD_CONTAINER_KEYS = new Set(['custom', 'customFields', 'customValues', 'cf'])\nconst SKIPPED_CHANGE_KEYS = new Set(['updatedAt', 'updated_at'])\n\nfunction appendCustomFieldChanges(\n changes: Record<string, { from: unknown; to: unknown }>,\n before: unknown,\n after: unknown\n): boolean {\n const beforeRec = asRecord(before)\n const afterRec = asRecord(after)\n if (!beforeRec && !afterRec) return false\n const left = beforeRec ?? {}\n const right = afterRec ?? {}\n const keys = new Set([...Object.keys(left), ...Object.keys(right)])\n for (const key of keys) {\n const from = left[key]\n const to = right[key]\n if (!deepEqual(from, to)) {\n changes[normalizeCustomFieldKey(key)] = { from, to }\n }\n }\n return true\n}\n\nfunction buildRecordChanges(\n before: Record<string, unknown>,\n after: Record<string, unknown>,\n): Record<string, { from: unknown; to: unknown }> {\n return buildRecordChangesDeep(before, after)\n}\n\nfunction buildRecordChangesDeep(\n before: Record<string, unknown>,\n after: Record<string, unknown>,\n prefix?: string,\n seen?: Set<unknown>,\n): Record<string, { from: unknown; to: unknown }> {\n const changes: Record<string, { from: unknown; to: unknown }> = {}\n if (!seen) seen = new Set()\n if (seen.has(before) || seen.has(after)) return changes\n seen.add(before)\n seen.add(after)\n const keys = new Set([...Object.keys(before), ...Object.keys(after)])\n for (const key of keys) {\n if (SKIPPED_CHANGE_KEYS.has(key)) continue\n if (CUSTOM_FIELD_CONTAINER_KEYS.has(key)) {\n const handled = appendCustomFieldChanges(changes, before[key], after[key])\n if (handled) continue\n }\n const from = before[key]\n const to = after[key]\n const path = prefix ? `${prefix}.${key}` : key\n const fromRec = asRecord(from)\n const toRec = asRecord(to)\n if (fromRec && toRec) {\n const nested = buildRecordChangesDeep(fromRec, toRec, path, seen)\n if (Object.keys(nested).length) {\n Object.assign(changes, nested)\n continue\n }\n }\n if (!deepEqual(from, to)) {\n changes[path] = { from, to }\n }\n }\n return changes\n}\n\nfunction deriveChangesFromSnapshots(\n before: unknown,\n after: unknown,\n): Record<string, { from: unknown; to: unknown }> | null {\n const beforeRec = asRecord(before)\n const afterRec = asRecord(after)\n if (!beforeRec || !afterRec) return null\n const changes = buildRecordChanges(beforeRec, afterRec)\n return Object.keys(changes).length ? changes : null\n}\n\nfunction invertRecordedChanges(\n changes: unknown,\n): Record<string, { from: unknown; to: unknown }> | null {\n const source = asRecord(changes)\n if (!source) return null\n const inverted: Record<string, { from: unknown; to: unknown }> = {}\n for (const [key, value] of Object.entries(source)) {\n const entry = asRecord(value)\n if (!entry || (!('from' in entry) && !('to' in entry))) continue\n inverted[key] = {\n from: entry.to,\n to: entry.from,\n }\n }\n return Object.keys(inverted).length ? inverted : null\n}\n\nfunction extractAliasList(source: unknown): string[] {\n if (!source || typeof source !== 'object' || Array.isArray(source)) return []\n const record = source as Record<string, unknown>\n const raw = record.cacheAliases\n if (!Array.isArray(raw)) return []\n const aliases = new Set<string>()\n for (const value of raw) {\n if (typeof value !== 'string') continue\n const normalized = canonicalizeResourceTag(value)\n if (normalized) aliases.add(normalized)\n }\n return Array.from(aliases)\n}\n\nexport class CommandBus {\n async execute<TInput = unknown, TResult = unknown>(\n commandId: string,\n options: CommandExecutionOptions<TInput>\n ): Promise<CommandExecuteResult<TResult>> {\n const handler = await this.resolveHandler<TInput, TResult>(commandId)\n\n const replayLogEntry = options.redoLogEntry ?? null\n const isAtomicReplay = replayLogEntry !== null && handler.atomicReplay === true\n const allInterceptors = getAllCommandInterceptorInstances()\n let interceptorMetadata = new Map<string, Record<string, unknown>>()\n let effectiveOptions = options\n let userFeatures: string[] = []\n const runBeforeExecuteInterceptors = async (baseOptions: CommandExecutionOptions<TInput>) => {\n const resolvedUserFeatures = allInterceptors.length\n ? await this.resolveUserFeaturesForInterceptors(baseOptions.ctx)\n : []\n if (!allInterceptors.length) {\n return {\n effectiveOptions: baseOptions,\n metadata: new Map<string, Record<string, unknown>>(),\n userFeatures: resolvedUserFeatures,\n }\n }\n const interceptorCtx: CommandInterceptorContext = {\n commandId,\n auth: baseOptions.ctx.auth ?? null,\n selectedOrganizationId: baseOptions.ctx.selectedOrganizationId ?? baseOptions.ctx.auth?.orgId ?? null,\n container: baseOptions.ctx.container,\n organizationScope: baseOptions.ctx.organizationScope ?? null,\n request: baseOptions.ctx.request ?? null,\n }\n const beforeResult = await runCommandInterceptorsBefore(\n allInterceptors, commandId, baseOptions.input, interceptorCtx, resolvedUserFeatures,\n )\n if (!beforeResult.ok) {\n const blocked = beforeResult.error!\n throw new CommandInterceptorError(blocked.message, { status: blocked.status, body: blocked.body })\n }\n const preparedOptions = beforeResult.modifiedInput\n ? {\n ...baseOptions,\n input: { ...(baseOptions.input as object), ...beforeResult.modifiedInput } as TInput,\n }\n : baseOptions\n return {\n effectiveOptions: preparedOptions,\n metadata: beforeResult.metadataByInterceptor,\n userFeatures: resolvedUserFeatures,\n }\n }\n\n if (replayLogEntry && !isAtomicReplay) {\n await this.runReplayTransactionGuard(options.ctx, 'redo', replayLogEntry)\n if (handler.authorizeReplay) {\n await handler.authorizeReplay({\n operation: 'redo',\n input: options.input,\n ctx: options.ctx,\n logEntry: replayLogEntry,\n })\n }\n }\n\n if (!isAtomicReplay) {\n const prepared = await runBeforeExecuteInterceptors(options)\n effectiveOptions = prepared.effectiveOptions\n interceptorMetadata = prepared.metadata\n userFeatures = prepared.userFeatures\n }\n\n const executeCore = async (coreOptions: CommandExecutionOptions<TInput>) => {\n const snapshots: { before?: unknown } = {}\n let executionOptions = coreOptions\n if (handler.prepareSnapshotInsideTransaction && handler.prepare) {\n const prepare = handler.prepare\n const originalCtx = coreOptions.ctx\n const originalBeforeTransactionalWrite = originalCtx.beforeTransactionalWrite\n let prepared = false\n let transactionalCtx: CommandRuntimeContext\n transactionalCtx = {\n ...originalCtx,\n beforeTransactionalWrite: async (em, input) => {\n await originalBeforeTransactionalWrite?.(em, input)\n if (prepared) return\n Object.assign(\n snapshots,\n (await prepare(coreOptions.input, { ...transactionalCtx, transactionalEm: em })) ?? {},\n )\n prepared = true\n },\n }\n executionOptions = { ...coreOptions, ctx: transactionalCtx }\n } else {\n Object.assign(snapshots, await this.prepareSnapshots(handler, coreOptions))\n }\n effectiveOptions = executionOptions\n const sourceLog = executionOptions.redoLogEntry ?? null\n const result =\n sourceLog && typeof handler.redo === 'function'\n ? await handler.redo({ input: executionOptions.input, ctx: executionOptions.ctx, logEntry: sourceLog })\n : await handler.execute(executionOptions.input, executionOptions.ctx)\n const afterSnapshot = await this.captureAfter(handler, executionOptions, result)\n const snapshotsWithAfter = { ...snapshots, after: afterSnapshot }\n const logMeta = await this.buildLog(handler, executionOptions, result, snapshotsWithAfter)\n let mergedMeta = this.mergeMetadata(executionOptions.metadata, logMeta)\n // Interceptors opt into audit-log enrichment with a reserved `logContext` key rather\n // than the generic `context` one, so the metadata an interceptor already passes to its\n // own afterExecute hook is never silently promoted into audit storage.\n // Map iteration order is interceptor priority order (see collectMatching), so a\n // later-priority interceptor overrides an earlier one on key collisions.\n let interceptorContextMerged: Record<string, unknown> = {}\n for (const meta of interceptorMetadata.values()) {\n const logContextRecord = asRecord(asRecord(meta)?.logContext)\n if (!logContextRecord) continue\n interceptorContextMerged = {\n ...interceptorContextMerged,\n ...logContextRecord,\n }\n }\n const baseContext = asRecord(executionOptions.metadata?.context) ?? {}\n const logMetaContext = asRecord(logMeta?.context) ?? {}\n if (Object.keys(interceptorContextMerged).length > 0 || Object.keys(baseContext).length > 0 || Object.keys(logMetaContext).length > 0) {\n mergedMeta = mergedMeta ?? {}\n mergedMeta.context = {\n ...baseContext,\n ...interceptorContextMerged,\n ...logMetaContext,\n }\n }\n const undoable = this.isUndoable(handler)\n if (undoable && mergedMeta?.replayable !== false) {\n mergedMeta = mergedMeta ?? {}\n if (!mergedMeta.undoToken) mergedMeta.undoToken = defaultUndoToken()\n if (mergedMeta.actorUserId === undefined) mergedMeta.actorUserId = executionOptions.ctx.auth?.sub ?? null\n } else if (mergedMeta?.replayable === false) {\n mergedMeta.undoToken = null\n }\n if (afterSnapshot !== undefined && afterSnapshot !== null) {\n if (!mergedMeta) {\n mergedMeta = { snapshotAfter: afterSnapshot }\n } else if (!mergedMeta.snapshotAfter) {\n mergedMeta.snapshotAfter = afterSnapshot\n }\n }\n if (snapshots.before) {\n if (!mergedMeta) {\n mergedMeta = { snapshotBefore: snapshots.before }\n } else if (!mergedMeta.snapshotBefore) {\n mergedMeta.snapshotBefore = snapshots.before\n }\n }\n if (mergedMeta?.snapshotBefore !== undefined && mergedMeta?.snapshotAfter !== undefined) {\n const currentChanges = mergedMeta.changes\n const shouldInfer =\n currentChanges === undefined ||\n currentChanges === null ||\n (typeof currentChanges === 'object' && !Array.isArray(currentChanges) && Object.keys(currentChanges).length === 0)\n if (shouldInfer) {\n const inferred = deriveChangesFromSnapshots(mergedMeta.snapshotBefore, mergedMeta.snapshotAfter)\n if (inferred) mergedMeta.changes = inferred\n }\n }\n const logEntry = await this.persistLog(commandId, executionOptions, mergedMeta)\n return { result, mergedMeta, logEntry }\n }\n\n let replaySourceFinalized = false\n let coreResult: Awaited<ReturnType<typeof executeCore>>\n const atomicSourceLog = options.redoLogEntry ?? null\n let ambientReplay: AtomicReplayTransaction | null = null\n let replayTransactionLifetime: TransactionLifetime | null = null\n if (atomicSourceLog && isAtomicReplay) {\n ambientReplay = resolveAtomicReplayTransaction(options.ctx)\n const replayEm = ambientReplay.em\n await withAtomicFlush(replayEm, [async () => {\n const replayCtx = bindReplayTransactionContext(\n options.ctx,\n replayEm,\n ambientReplay?.joinsAmbient ? options.ctx.transactionLifetime : undefined,\n )\n replayTransactionLifetime = replayCtx.transactionLifetime ?? null\n const transactionalBaseOptions: CommandExecutionOptions<TInput> = {\n ...options,\n ctx: replayCtx,\n }\n if (!atomicSourceLog.id) throw new Error('[internal] Atomic redo source log id is required')\n if (handler.stabilizeReplay) {\n await handler.stabilizeReplay({\n operation: 'redo',\n input: transactionalBaseOptions.input,\n ctx: transactionalBaseOptions.ctx,\n logEntry: atomicSourceLog,\n })\n }\n await this.runReplayTransactionGuard(\n transactionalBaseOptions.ctx,\n 'redo',\n atomicSourceLog,\n replayEm,\n )\n const prepared = await runBeforeExecuteInterceptors(transactionalBaseOptions)\n effectiveOptions = prepared.effectiveOptions\n interceptorMetadata = prepared.metadata\n userFeatures = prepared.userFeatures\n const service = effectiveOptions.ctx.container.resolve('actionLogService') as CommandActionLogService\n if (handler.authorizeReplay) {\n await handler.authorizeReplay({\n operation: 'redo',\n input: effectiveOptions.input,\n ctx: effectiveOptions.ctx,\n logEntry: atomicSourceLog,\n })\n }\n const claimed = await service.claimForRedo(atomicSourceLog.id, replayEm)\n if (!claimed) throw new Error('[internal] Redo source already consumed')\n coreResult = await executeCore(effectiveOptions)\n }], { transaction: true, label: `${commandId}.redo` })\n replaySourceFinalized = !ambientReplay.joinsAmbient\n } else {\n coreResult = await executeCore(effectiveOptions)\n }\n const { result, mergedMeta, logEntry } = coreResult!\n\n const runPostCommit = async () => {\n let finalResult = result\n if (allInterceptors.length) {\n const interceptorCtx: CommandInterceptorContext = {\n commandId,\n auth: effectiveOptions.ctx.auth ?? null,\n selectedOrganizationId: effectiveOptions.ctx.selectedOrganizationId ?? effectiveOptions.ctx.auth?.orgId ?? null,\n container: effectiveOptions.ctx.container,\n organizationScope: effectiveOptions.ctx.organizationScope ?? null,\n request: effectiveOptions.ctx.request ?? null,\n }\n const afterResult = await runCommandInterceptorsAfter(\n allInterceptors, commandId, effectiveOptions.input, result, interceptorCtx,\n userFeatures, interceptorMetadata,\n )\n if (afterResult.modifiedResult && typeof result === 'object' && result) {\n finalResult = { ...(result as object), ...afterResult.modifiedResult } as Awaited<TResult>\n }\n }\n\n if (!effectiveOptions.skipCacheInvalidation) {\n await this.invalidateCacheAfterExecute(commandId, effectiveOptions, finalResult, mergedMeta)\n }\n // Bulk-import backfills defer heavy per-record side effects: the ctx flags are read here and\n // threaded as a local into the flush (never stored on the shared dataEngine), so a concurrent\n // command with different flags can't observe them. Reindex is restored by the caller's\n // end-of-run `query_index rebuild`. Mirrors `skipCacheInvalidation` above.\n await this.flushCrudSideEffects(effectiveOptions.ctx.container, effectiveOptions.ctx?.bulkImport)\n return finalResult\n }\n\n const executionResult: CommandExecuteResult<TResult> = {\n result,\n logEntry,\n replaySourceFinalized,\n }\n if (ambientReplay?.joinsAmbient) {\n if (!replayTransactionLifetime || !ownsTransactionLifetime(ambientReplay.em, replayTransactionLifetime)) {\n throw new Error('[internal] Ambient atomic replay lost its outer transaction lifetime owner')\n }\n onTransactionLifetimeComplete(ambientReplay.em, async (outcome) => {\n if (outcome !== 'committed') return\n executionResult.result = await runPostCommit()\n executionResult.replaySourceFinalized = true\n })\n return executionResult\n }\n\n executionResult.result = await runPostCommit()\n return executionResult\n }\n\n async undo(undoToken: string, ctx: CommandRuntimeContext): Promise<void> {\n const service = (ctx.container.resolve('actionLogService') as CommandActionLogService)\n const log = await service.findByUndoToken(undoToken)\n if (!log) throw new Error('Undo token expired or not found')\n const handler = await this.resolveHandler(log.commandId)\n if (!handler.undo || this.isUndoable(handler) === false) {\n throw new Error(`Command ${log.commandId} is not undoable`)\n }\n\n const isAtomicReplay = handler.atomicReplay === true\n const allInterceptors = getAllCommandInterceptorInstances()\n let undoInterceptorMetadata = new Map<string, Record<string, unknown>>()\n let userFeatures: string[] = []\n const runBeforeUndoInterceptors = async (replayCtx: CommandRuntimeContext) => {\n const resolvedUserFeatures = allInterceptors.length\n ? await this.resolveUserFeaturesForInterceptors(replayCtx)\n : []\n if (!allInterceptors.length) {\n return {\n metadata: new Map<string, Record<string, unknown>>(),\n userFeatures: resolvedUserFeatures,\n }\n }\n const undoCtx = { input: log.commandPayload, logEntry: log, undoToken }\n const interceptorCtx: CommandInterceptorContext = {\n commandId: log.commandId,\n auth: replayCtx.auth ?? null,\n selectedOrganizationId: replayCtx.selectedOrganizationId ?? replayCtx.auth?.orgId ?? null,\n container: replayCtx.container,\n organizationScope: replayCtx.organizationScope ?? null,\n request: replayCtx.request ?? null,\n }\n const beforeResult = await runCommandInterceptorsBeforeUndo(\n allInterceptors, log.commandId, undoCtx, interceptorCtx, resolvedUserFeatures,\n )\n if (!beforeResult.ok) {\n const blocked = beforeResult.error!\n throw new CommandInterceptorError(blocked.message, { status: blocked.status, body: blocked.body })\n }\n return {\n metadata: beforeResult.metadataByInterceptor,\n userFeatures: resolvedUserFeatures,\n }\n }\n\n if (!isAtomicReplay) {\n await this.runReplayTransactionGuard(ctx, 'undo', log)\n if (handler.authorizeReplay) {\n await handler.authorizeReplay({\n operation: 'undo',\n input: log.commandPayload as Parameters<NonNullable<typeof handler.authorizeReplay>>[0]['input'],\n ctx,\n logEntry: log,\n })\n }\n }\n\n const claimUndo = async (replayEm?: EntityManager) => {\n // Two contenders can both pass the initial lookup; only the compare-and-set\n // below may proceed. On the atomic path this state is still uncommitted and\n // rolls back with every handler/log write if anything later fails.\n const claimed = replayEm\n ? await service.claimForUndo(log.id, replayEm)\n : await service.claimForUndo(log.id)\n if (!claimed) throw new Error('[internal] Undo token already consumed')\n }\n const finishUndo = async (replayCtx: CommandRuntimeContext, replayEm?: EntityManager) => {\n await handler.undo!({\n input: log.commandPayload as Parameters<NonNullable<typeof handler.undo>>[0]['input'],\n ctx: replayCtx,\n logEntry: log,\n })\n const traceLog = this.buildUndoTraceLog(log, replayCtx)\n if (replayEm) {\n await service.markUndone(log.id, traceLog, replayEm)\n } else {\n await service.markUndone(log.id, traceLog)\n }\n }\n\n let ambientReplay: AtomicReplayTransaction | null = null\n let replayTransactionLifetime: TransactionLifetime | null = null\n if (isAtomicReplay) {\n ambientReplay = resolveAtomicReplayTransaction(ctx)\n const replayEm = ambientReplay.em\n await withAtomicFlush(replayEm, [async () => {\n const replayCtx = bindReplayTransactionContext(\n ctx,\n replayEm,\n ambientReplay?.joinsAmbient ? ctx.transactionLifetime : undefined,\n )\n replayTransactionLifetime = replayCtx.transactionLifetime ?? null\n if (handler.stabilizeReplay) {\n await handler.stabilizeReplay({\n operation: 'undo',\n input: log.commandPayload as Parameters<NonNullable<typeof handler.stabilizeReplay>>[0]['input'],\n ctx: replayCtx,\n logEntry: log,\n })\n }\n await this.runReplayTransactionGuard(replayCtx, 'undo', log, replayEm)\n const prepared = await runBeforeUndoInterceptors(replayCtx)\n undoInterceptorMetadata = prepared.metadata\n userFeatures = prepared.userFeatures\n if (handler.authorizeReplay) {\n await handler.authorizeReplay({\n operation: 'undo',\n input: log.commandPayload as Parameters<NonNullable<typeof handler.authorizeReplay>>[0]['input'],\n ctx: replayCtx,\n logEntry: log,\n })\n }\n await claimUndo(replayEm)\n await finishUndo(replayCtx, replayEm)\n }], {\n transaction: true,\n label: `${log.commandId}.undo`,\n })\n } else {\n try {\n // Preserve the established non-atomic lifecycle exactly: authorization,\n // source claim, before-undo interceptors, handler, then finalization.\n await claimUndo()\n const prepared = await runBeforeUndoInterceptors(ctx)\n undoInterceptorMetadata = prepared.metadata\n userFeatures = prepared.userFeatures\n await finishUndo(ctx)\n } catch (err) {\n // Legacy handlers retain their compensating release behavior because\n // their domain mutation may not share the action-log transaction.\n await service.releaseUndoClaim(log.id).catch(() => {})\n throw err\n }\n }\n\n const runPostCommit = async () => {\n // Post-commit hooks and side effects run only after an atomic replay has\n // durably committed both the domain state and source/trace log state.\n if (allInterceptors.length) {\n const undoCtx = { input: log.commandPayload, logEntry: log, undoToken }\n const interceptorCtx: CommandInterceptorContext = {\n commandId: log.commandId,\n auth: ctx.auth ?? null,\n selectedOrganizationId: ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null,\n container: ctx.container,\n organizationScope: ctx.organizationScope ?? null,\n request: ctx.request ?? null,\n }\n await runCommandInterceptorsAfterUndo(\n allInterceptors, log.commandId, undoCtx, interceptorCtx,\n userFeatures, undoInterceptorMetadata,\n )\n }\n\n await this.invalidateCacheAfterUndo(log, ctx)\n await this.flushCrudSideEffects(ctx.container)\n }\n\n if (ambientReplay?.joinsAmbient) {\n if (!replayTransactionLifetime || !ownsTransactionLifetime(ambientReplay.em, replayTransactionLifetime)) {\n throw new Error('[internal] Ambient atomic replay lost its outer transaction lifetime owner')\n }\n onTransactionLifetimeComplete(ambientReplay.em, async (outcome) => {\n if (outcome === 'committed') await runPostCommit()\n })\n return\n }\n\n await runPostCommit()\n }\n\n private buildUndoTraceLog(log: ActionLog, ctx: CommandRuntimeContext): ActionLogCreateInput | undefined {\n const snapshotBefore = log.snapshotAfter ?? null\n const snapshotAfter = log.snapshotBefore ?? null\n const changes =\n deriveChangesFromSnapshots(snapshotBefore, snapshotAfter)\n ?? invertRecordedChanges(log.changesJson)\n ?? undefined\n\n const baseContext = asRecord(log.contextJson) ?? {}\n const context = {\n ...baseContext,\n historyAction: 'undo',\n sourceLogId: log.id,\n sourceCommandId: log.commandId,\n }\n\n return {\n tenantId: log.tenantId ?? ctx.auth?.tenantId ?? null,\n organizationId: log.organizationId ?? ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null,\n actorUserId: ctx.auth?.sub ?? log.actorUserId ?? null,\n commandId: log.commandId,\n actionLabel: log.actionLabel ?? undefined,\n resourceKind: log.resourceKind ?? undefined,\n resourceId: log.resourceId ?? undefined,\n parentResourceKind: log.parentResourceKind ?? null,\n parentResourceId: log.parentResourceId ?? null,\n relatedResourceKind: log.relatedResourceKind ?? null,\n relatedResourceId: log.relatedResourceId ?? null,\n snapshotBefore,\n snapshotAfter,\n changes,\n context,\n }\n }\n\n private async resolveUserFeaturesForInterceptors(ctx: CommandRuntimeContext): Promise<string[]> {\n if (!ctx.auth) return []\n type RbacLike = {\n getGrantedFeatures: (\n userId: string,\n opts: { tenantId: string | null; organizationId: string | null },\n ) => Promise<string[]>\n getGrantedFeaturesWithEntityManager?: (\n em: EntityManager,\n userId: string,\n opts: { tenantId: string | null; organizationId: string | null },\n ) => Promise<string[]>\n }\n let rbac: RbacLike | undefined\n try {\n rbac = ctx.container.resolve('rbacService') as RbacLike | undefined\n } catch {\n // Intentional: rbacService is not registered in all runtime contexts (CLI, tests, bootstrap).\n return []\n }\n const scope = {\n tenantId: ctx.auth.tenantId,\n organizationId: ctx.selectedOrganizationId ?? ctx.auth.orgId,\n }\n if (ctx.transactionalEm) {\n if (!rbac?.getGrantedFeaturesWithEntityManager) return []\n return await rbac.getGrantedFeaturesWithEntityManager(\n ctx.transactionalEm,\n ctx.auth.sub,\n scope,\n )\n }\n try {\n if (rbac?.getGrantedFeatures) {\n return await rbac.getGrantedFeatures(ctx.auth.sub, scope)\n }\n } catch {\n // Outside a transaction a failed lookup cannot poison anything; interceptors without\n // feature gating still run. Inside one the error is rethrown above, because the failed\n // query has already aborted the transaction.\n }\n return []\n }\n\n private async runReplayTransactionGuard(\n ctx: CommandRuntimeContext,\n operation: 'undo' | 'redo',\n logEntry: CommandUndoLogEntry,\n transactionalEm?: EntityManager,\n ): Promise<void> {\n if (!ctx.replayTransactionGuard) return\n if (transactionalEm) {\n await ctx.replayTransactionGuard({ operation, logEntry, transactionalEm })\n return\n }\n const requestEm = ctx.container.resolve('em') as EntityManager\n const guardEm = typeof requestEm.fork === 'function' ? requestEm.fork() : requestEm\n await withAtomicFlush(guardEm, [async () => {\n await ctx.replayTransactionGuard!({ operation, logEntry, transactionalEm: guardEm })\n }], { transaction: true, label: `${logEntry.commandId}.${operation}.request-guard` })\n }\n\n private async resolveHandler<TInput, TResult>(commandId: string): Promise<CommandHandler<TInput, TResult>> {\n const handler =\n commandRegistry.get<TInput, TResult>(commandId) ??\n ((await commandRegistry.load(commandId)) as CommandHandler<TInput, TResult> | null)\n if (!handler) {\n const moduleName = commandId.split('.')[0]\n const registered = commandRegistry.list()\n const sameModule = registered.filter((id) => id.split('.')[0] === moduleName)\n const registeredLoaders = commandRegistry.listLoaders()\n const sameModuleLoaders = registeredLoaders.filter((id) => id === commandId || id.startsWith(`${moduleName}:`))\n const hint = sameModule.length > 0\n ? ` Registered commands for module \"${moduleName}\": [${sameModule.join(', ')}].`\n : sameModuleLoaders.length > 0\n ? ` Command loaders for module \"${moduleName}\" were registered but none loaded \"${commandId}\".`\n : ` No commands or command loaders registered for module \"${moduleName}\". Ensure the command file is imported or generated lazy command loaders are registered.`\n throw new Error(`Command handler not registered for id ${commandId}.${hint}`)\n }\n return handler\n }\n\n private async prepareSnapshots<TInput, TResult>(\n handler: CommandHandler<TInput, TResult>,\n options: CommandExecutionOptions<TInput>\n ): Promise<{ before?: unknown }> {\n if (!handler.prepare) return {}\n try {\n return (await handler.prepare(options.input, options.ctx)) || {}\n } catch (err) {\n throw err\n }\n }\n\n private async captureAfter<TInput, TResult>(\n handler: CommandHandler<TInput, TResult>,\n options: CommandExecutionOptions<TInput>,\n result: TResult\n ): Promise<unknown> {\n if (!handler.captureAfter) return undefined\n return handler.captureAfter(options.input, result, options.ctx)\n }\n\n private async buildLog<TInput, TResult>(\n handler: CommandHandler<TInput, TResult>,\n options: CommandExecutionOptions<TInput>,\n result: TResult,\n snapshots: { before?: unknown; after?: unknown }\n ): Promise<CommandLogMetadata | null> {\n if (!handler.buildLog) return null\n const args: CommandLogBuilderArgs<TInput, TResult> = {\n input: options.input,\n result,\n ctx: options.ctx,\n snapshots,\n }\n return (await handler.buildLog(args)) || null\n }\n\n private mergeMetadata(primary?: CommandLogMetadata | null, secondary?: CommandLogMetadata | null): CommandLogMetadata | null {\n if (!primary && !secondary) return null\n return {\n skipLog: secondary?.skipLog ?? primary?.skipLog ?? false,\n replayable:\n secondary?.replayable === false || primary?.replayable === false\n ? false\n : secondary?.replayable ?? primary?.replayable,\n tenantId: secondary?.tenantId ?? primary?.tenantId ?? null,\n organizationId: secondary?.organizationId ?? primary?.organizationId ?? null,\n actorUserId: secondary?.actorUserId ?? primary?.actorUserId ?? null,\n onBehalfOfUserId: secondary?.onBehalfOfUserId ?? primary?.onBehalfOfUserId ?? null,\n actionLabel: secondary?.actionLabel ?? primary?.actionLabel ?? null,\n resourceKind: secondary?.resourceKind ?? primary?.resourceKind ?? null,\n resourceId: secondary?.resourceId ?? primary?.resourceId ?? null,\n parentResourceKind: secondary?.parentResourceKind ?? primary?.parentResourceKind ?? null,\n parentResourceId: secondary?.parentResourceId ?? primary?.parentResourceId ?? null,\n relatedResourceKind: secondary?.relatedResourceKind ?? primary?.relatedResourceKind ?? null,\n relatedResourceId: secondary?.relatedResourceId ?? primary?.relatedResourceId ?? null,\n undoToken: secondary?.undoToken ?? primary?.undoToken ?? null,\n // A redacted redo input always wins: it exists precisely because the raw\n // input carries a secret that must not reach `action_logs`.\n redoInput: secondary?.redoInput !== undefined ? secondary.redoInput : primary?.redoInput,\n payload: secondary?.payload ?? primary?.payload ?? null,\n snapshotBefore: secondary?.snapshotBefore ?? primary?.snapshotBefore ?? null,\n snapshotAfter: secondary?.snapshotAfter ?? primary?.snapshotAfter ?? null,\n changes: secondary?.changes ?? primary?.changes ?? null,\n context: secondary?.context ?? primary?.context ?? null,\n }\n }\n\n private async persistLog<TInput>(\n commandId: string,\n options: CommandExecutionOptions<TInput>,\n metadata: CommandLogMetadata | null\n ): Promise<ActionLog | null> {\n if (!metadata) return null\n if (metadata.skipLog) return null\n const resourceKind =\n typeof metadata.resourceKind === 'string' ? metadata.resourceKind : null\n if (resourceKind && SKIPPED_ACTION_LOG_RESOURCE_KINDS.has(resourceKind)) {\n return null\n }\n let service: CommandActionLogService | null = null\n try {\n service = (options.ctx.container.resolve('actionLogService') as CommandActionLogService)\n } catch {\n service = null\n }\n if (!service) return null\n\n const tenantId = metadata.tenantId ?? options.ctx.auth?.tenantId ?? null\n const organizationId =\n metadata.organizationId ?? options.ctx.selectedOrganizationId ?? options.ctx.auth?.orgId ?? null\n // On-behalf-of attribution (Wave 4 P2): when `ctx.runAs` is set the actor is\n // the agent principal and the human it acts for is recorded separately. This\n // funnels agent writes through the SAME ActionLog path as a human's \u2014 only the\n // attribution differs (actorUserId=agent, onBehalfOfUserId=human, source='agent').\n const runAs = options.ctx.runAs ?? null\n const actorUserId = runAs?.actorUserId ?? metadata.actorUserId ?? options.ctx.auth?.sub ?? null\n const onBehalfOfUserId = runAs ? (runAs.onBehalfOfUserId ?? null) : (metadata.onBehalfOfUserId ?? null)\n const systemActorContext = !actorUserId && options.ctx.systemActor === true\n ? { systemActor: 'system:command' }\n : null\n const payload: Record<string, unknown> = {\n tenantId: tenantId ?? undefined,\n organizationId: organizationId ?? undefined,\n actorUserId: actorUserId ?? undefined,\n onBehalfOfUserId: onBehalfOfUserId ?? undefined,\n commandId,\n }\n\n if (metadata) {\n if ('actionLabel' in metadata && metadata.actionLabel != null) payload.actionLabel = metadata.actionLabel\n if ('resourceKind' in metadata && metadata.resourceKind != null) payload.resourceKind = metadata.resourceKind\n if ('resourceId' in metadata && metadata.resourceId != null) payload.resourceId = metadata.resourceId\n if ('parentResourceKind' in metadata && metadata.parentResourceKind != null) payload.parentResourceKind = metadata.parentResourceKind\n if ('parentResourceId' in metadata && metadata.parentResourceId != null) payload.parentResourceId = metadata.parentResourceId\n if ('relatedResourceKind' in metadata && metadata.relatedResourceKind != null) payload.relatedResourceKind = metadata.relatedResourceKind\n if ('relatedResourceId' in metadata && metadata.relatedResourceId != null) payload.relatedResourceId = metadata.relatedResourceId\n if ('undoToken' in metadata && metadata.undoToken != null) payload.undoToken = metadata.undoToken\n if ('payload' in metadata && metadata.payload !== undefined) payload.commandPayload = metadata.payload\n if ('snapshotBefore' in metadata && metadata.snapshotBefore !== undefined) payload.snapshotBefore = metadata.snapshotBefore\n if ('snapshotAfter' in metadata && metadata.snapshotAfter !== undefined) payload.snapshotAfter = metadata.snapshotAfter\n if ('changes' in metadata && metadata.changes !== undefined && metadata.changes !== null) payload.changes = metadata.changes\n if ('context' in metadata && metadata.context !== undefined && metadata.context !== null) {\n payload.context = { ...(systemActorContext ?? {}), ...metadata.context }\n } else if (systemActorContext) {\n payload.context = systemActorContext\n }\n }\n\n if (runAs) {\n // Stamp the audit source so `deriveActionLogSource` projects `sourceKey='agent'`.\n // Merge into any caller-provided context rather than replacing it.\n const baseContext = asRecord(payload.context) ?? {}\n payload.context = { ...baseContext, source: runAs.source }\n }\n\n if (metadata.replayable === false) {\n delete payload.commandPayload\n } else {\n const redoInput = metadata.redoInput !== undefined ? metadata.redoInput : options.input\n const redoEnvelope = wrapRedoPayload('commandPayload' in payload ? (payload.commandPayload as unknown) : undefined, redoInput)\n payload.commandPayload = redoEnvelope\n }\n\n return options.ctx.transactionalEm\n ? await service.log(payload as ActionLogCreateInput, options.ctx.transactionalEm)\n : await service.log(payload as ActionLogCreateInput)\n }\n\n private isUndoable(handler: CommandHandler<unknown, unknown>): boolean {\n return handler.isUndoable !== false && typeof handler.undo === 'function'\n }\n\n private async invalidateCacheAfterExecute<TResult>(\n commandId: string,\n options: CommandExecutionOptions<unknown>,\n result: TResult,\n metadata: CommandLogMetadata | null\n ): Promise<void> {\n const resource = typeof metadata?.resourceKind === 'string' ? metadata.resourceKind : null\n if (!resource) return\n try {\n const ctx = options.ctx\n const resultRecord = asRecord(result)\n const resultEntity = asRecord(resultRecord?.entity)\n const inputRecord = asRecord(options.input)\n const inputEntity = asRecord(inputRecord?.entity)\n\n const recordId = pickFirstIdentifier(\n metadata?.resourceId,\n extractPrimaryIdFromCommandResult(result),\n resultRecord?.entityId,\n resultRecord?.id,\n resultRecord?.recordId,\n resultEntity?.id,\n inputRecord?.id,\n inputRecord?.entityId,\n inputRecord?.recordId,\n inputEntity?.id\n )\n\n const organizationId = pickFirstIdentifier(\n metadata?.organizationId,\n resultRecord?.organizationId,\n resultEntity?.organizationId,\n inputRecord?.organizationId,\n inputEntity?.organizationId,\n ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null\n )\n\n const tenantId = pickFirstIdentifier(\n metadata?.tenantId,\n resultRecord?.tenantId,\n resultEntity?.tenantId,\n inputRecord?.tenantId,\n inputEntity?.tenantId,\n ctx.auth?.tenantId ?? null\n )\n\n const fallbackTenant = pickFirstIdentifier(metadata?.tenantId, ctx.auth?.tenantId ?? null)\n\n const aliasSet = new Set<string>()\n for (const alias of extractAliasList(metadata?.context ?? null)) {\n aliasSet.add(alias)\n }\n const derived = deriveResourceFromCommandId(commandId)\n if (derived) aliasSet.add(derived)\n const aliasExtras = Array.from(aliasSet)\n await invalidateCrudCache(\n ctx.container,\n resource,\n { id: recordId, organizationId, tenantId },\n fallbackTenant,\n `command:${commandId}:execute`,\n aliasExtras\n )\n } catch (err) {\n if (isCrudCacheDebugEnabled()) {\n try {\n logger.debug('Cache execute-invalidation failed', { commandId, err })\n } catch {}\n }\n }\n }\n\n private async invalidateCacheAfterUndo(log: ActionLog, ctx: CommandRuntimeContext): Promise<void> {\n const resource = typeof log.resourceKind === 'string' ? log.resourceKind : null\n if (!resource) return\n try {\n const recordId = pickFirstIdentifier(log.resourceId)\n const organizationId = pickFirstIdentifier(log.organizationId, ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null)\n const tenantId = pickFirstIdentifier(log.tenantId, ctx.auth?.tenantId ?? null)\n const fallbackTenant = pickFirstIdentifier(log.tenantId, ctx.auth?.tenantId ?? null)\n const aliasSet = new Set<string>()\n for (const alias of extractAliasList(log.contextJson ?? null)) {\n aliasSet.add(alias)\n }\n const derived = deriveResourceFromCommandId(log.commandId)\n if (derived) aliasSet.add(derived)\n const aliasExtras = Array.from(aliasSet)\n await invalidateCrudCache(\n ctx.container,\n resource,\n { id: recordId, organizationId, tenantId },\n fallbackTenant,\n `command:${log.commandId}:undo`,\n aliasExtras\n )\n } catch (err) {\n if (isCrudCacheDebugEnabled()) {\n try {\n logger.debug('Cache undo-invalidation failed', { commandId: log.commandId, err })\n } catch {}\n }\n }\n }\n\n private async flushCrudSideEffects(container: AwilixContainer, suppress?: BulkImportSuppression): Promise<void> {\n try {\n const dataEngine = (container.resolve('dataEngine') as DataEngine)\n await dataEngine.flushOrmEntityChanges(suppress)\n } catch (error) {\n if (isReadProjectionAlwaysConsistent()) {\n throw error\n }\n // best-effort: failures should not block command execution\n }\n }\n}\n\ntype RedoEnvelope = {\n __redoInput: unknown\n [key: string]: unknown\n}\n\nfunction wrapRedoPayload(existing: unknown, input: unknown): RedoEnvelope {\n if (!existing || typeof existing !== 'object' || Array.isArray(existing)) {\n const envelope: RedoEnvelope = { __redoInput: input }\n if (existing !== undefined) envelope.value = existing\n return envelope\n }\n const current = existing as Record<string, unknown>\n if ('__redoInput' in current && current.__redoInput !== undefined) {\n return current as RedoEnvelope\n }\n return { __redoInput: input, ...current }\n}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
"version": 3,
|
|
3
3
|
"sources": ["../../src/lib/version.ts"],
|
|
4
|
-
"sourcesContent": ["// Build-time generated version\nexport const APP_VERSION = '0.10.1-develop.
|
|
4
|
+
"sourcesContent": ["// Build-time generated version\nexport const APP_VERSION = '0.10.1-develop.8072.1.592356afda';\nexport const appVersion = APP_VERSION;\n"],
|
|
5
5
|
"mappings": "AACO,MAAM,cAAc;AACpB,MAAM,aAAa;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/shared",
|
|
3
|
-
"version": "0.10.1-develop.
|
|
3
|
+
"version": "0.10.1-develop.8072.1.592356afda",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -130,7 +130,7 @@
|
|
|
130
130
|
"@mikro-orm/core": "^7.1.14",
|
|
131
131
|
"@mikro-orm/decorators": "^7.1.14",
|
|
132
132
|
"@mikro-orm/postgresql": "^7.1.14",
|
|
133
|
-
"@open-mercato/cache": "0.10.1-develop.
|
|
133
|
+
"@open-mercato/cache": "0.10.1-develop.8072.1.592356afda",
|
|
134
134
|
"@types/html-to-text": "^9.0.4",
|
|
135
135
|
"@types/sanitize-html": "^2.16.1",
|
|
136
136
|
"dotenv": "^17.4.2",
|
|
@@ -16,6 +16,8 @@ describe('CommandBus change inference', () => {
|
|
|
16
16
|
unregisterCommand('test.date.equal')
|
|
17
17
|
unregisterCommand('test.null.vs.object')
|
|
18
18
|
unregisterCommand('test.circular.ref')
|
|
19
|
+
unregisterCommand('test.shared.ref')
|
|
20
|
+
unregisterCommand('test.shared.ref.change')
|
|
19
21
|
})
|
|
20
22
|
|
|
21
23
|
it('flattens custom field diffs into cf_ changes', async () => {
|
|
@@ -278,6 +280,87 @@ describe('CommandBus change inference', () => {
|
|
|
278
280
|
})
|
|
279
281
|
})
|
|
280
282
|
|
|
283
|
+
it('compares an object referenced twice in one value by value', async () => {
|
|
284
|
+
const logMock = jest.fn(async (payload: LogRecord) => payload)
|
|
285
|
+
// Mirrors the sales shipment snapshot, which holds one array under `items` and `itemsSnapshot`.
|
|
286
|
+
const snapshot = (name: string) => {
|
|
287
|
+
const items = [{ id: 'item-1', quantity: 1 }]
|
|
288
|
+
return { id: 'rec-s1', name, shipments: [{ id: 'shipment-1', items, itemsSnapshot: items }] }
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
registerCommand({
|
|
292
|
+
id: 'test.shared.ref',
|
|
293
|
+
execute: jest.fn(async () => ({ id: 'rec-s1' })),
|
|
294
|
+
prepare: jest.fn(async () => ({ before: snapshot('Before') })),
|
|
295
|
+
captureAfter: jest.fn(async () => snapshot('After')),
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
const container = createContainer({ injectionMode: InjectionMode.CLASSIC })
|
|
299
|
+
container.register({
|
|
300
|
+
actionLogService: asValue({ log: logMock }),
|
|
301
|
+
dataEngine: asValue({ flushOrmEntityChanges: jest.fn() }),
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
const bus = new CommandBus()
|
|
305
|
+
const ctx = {
|
|
306
|
+
container,
|
|
307
|
+
auth: { sub: 'user-1', tenantId: 'tenant-1', orgId: 'org-1' },
|
|
308
|
+
organizationScope: null,
|
|
309
|
+
selectedOrganizationId: 'org-1',
|
|
310
|
+
organizationIds: null,
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
await bus.execute('test.shared.ref', {
|
|
314
|
+
input: {},
|
|
315
|
+
ctx,
|
|
316
|
+
metadata: { resourceKind: 'test.record', resourceId: 'rec-s1' },
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
const payload = logMock.mock.calls[0]?.[0] as LogRecord
|
|
320
|
+
expect(payload?.changes).toEqual({
|
|
321
|
+
name: { from: 'Before', to: 'After' },
|
|
322
|
+
})
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
it('still reports a change inside an object referenced twice', async () => {
|
|
326
|
+
const logMock = jest.fn(async (payload: LogRecord) => payload)
|
|
327
|
+
const snapshot = (quantity: number) => {
|
|
328
|
+
const items = [{ id: 'item-1', quantity }]
|
|
329
|
+
return { id: 'rec-s2', shipments: [{ id: 'shipment-1', items, itemsSnapshot: items }] }
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
registerCommand({
|
|
333
|
+
id: 'test.shared.ref.change',
|
|
334
|
+
execute: jest.fn(async () => ({ id: 'rec-s2' })),
|
|
335
|
+
prepare: jest.fn(async () => ({ before: snapshot(1) })),
|
|
336
|
+
captureAfter: jest.fn(async () => snapshot(2)),
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
const container = createContainer({ injectionMode: InjectionMode.CLASSIC })
|
|
340
|
+
container.register({
|
|
341
|
+
actionLogService: asValue({ log: logMock }),
|
|
342
|
+
dataEngine: asValue({ flushOrmEntityChanges: jest.fn() }),
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
const bus = new CommandBus()
|
|
346
|
+
const ctx = {
|
|
347
|
+
container,
|
|
348
|
+
auth: { sub: 'user-1', tenantId: 'tenant-1', orgId: 'org-1' },
|
|
349
|
+
organizationScope: null,
|
|
350
|
+
selectedOrganizationId: 'org-1',
|
|
351
|
+
organizationIds: null,
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
await bus.execute('test.shared.ref.change', {
|
|
355
|
+
input: {},
|
|
356
|
+
ctx,
|
|
357
|
+
metadata: { resourceKind: 'test.record', resourceId: 'rec-s2' },
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
const payload = logMock.mock.calls[0]?.[0] as LogRecord
|
|
361
|
+
expect(Object.keys(payload?.changes ?? {})).toEqual(['shipments'])
|
|
362
|
+
})
|
|
363
|
+
|
|
281
364
|
it('skips updatedAt fields when inferring changes', async () => {
|
|
282
365
|
const logMock = jest.fn(async (payload: LogRecord) => payload)
|
|
283
366
|
|
|
@@ -145,15 +145,22 @@ function deepEqual(a: unknown, b: unknown, seen?: Set<unknown>): boolean {
|
|
|
145
145
|
}
|
|
146
146
|
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
147
147
|
if (!seen) seen = new Set()
|
|
148
|
+
// `seen` is the current path: an object met again on it is a cycle. An object reached twice by
|
|
149
|
+
// different paths (the items of one array held under two keys) is compared like any other.
|
|
148
150
|
if (seen.has(a) || seen.has(b)) return false
|
|
149
151
|
seen.add(a)
|
|
150
152
|
seen.add(b)
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
153
|
+
try {
|
|
154
|
+
const aRec = a as Record<string, unknown>
|
|
155
|
+
const bRec = b as Record<string, unknown>
|
|
156
|
+
const keysA = Object.keys(aRec)
|
|
157
|
+
const keysB = Object.keys(bRec)
|
|
158
|
+
if (keysA.length !== keysB.length) return false
|
|
159
|
+
return keysA.every((key) => deepEqual(aRec[key], bRec[key], seen))
|
|
160
|
+
} finally {
|
|
161
|
+
seen.delete(a)
|
|
162
|
+
seen.delete(b)
|
|
163
|
+
}
|
|
157
164
|
}
|
|
158
165
|
return false
|
|
159
166
|
}
|