@open-mercato/core 0.6.6-develop.6304.1.4cf2b975cb → 0.6.6-develop.6305.1.4503070c9d

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.
@@ -36,7 +36,7 @@ const noteCrudEvents = {
36
36
  })
37
37
  };
38
38
  async function loadNoteSnapshot(em, id) {
39
- const note = await em.findOne(SalesNote, { id });
39
+ const note = await findOneWithDecryption(em, SalesNote, { id }, {});
40
40
  if (!note) return null;
41
41
  return {
42
42
  id: note.id,
@@ -54,7 +54,7 @@ async function loadNoteSnapshot(em, id) {
54
54
  }
55
55
  async function requireContext(em, contextType, contextId, organizationId, tenantId) {
56
56
  if (contextType === "order") {
57
- const order = await em.findOne(SalesOrder, { id: contextId });
57
+ const order = await findOneWithDecryption(em, SalesOrder, { id: contextId }, {}, { tenantId, organizationId });
58
58
  if (!order) {
59
59
  throw new CrudHttpError(404, { error: "sales.notes.context_not_found" });
60
60
  }
@@ -69,7 +69,7 @@ async function requireContext(em, contextType, contextId, organizationId, tenant
69
69
  };
70
70
  }
71
71
  if (contextType === "quote") {
72
- const quote = await em.findOne(SalesQuote, { id: contextId });
72
+ const quote = await findOneWithDecryption(em, SalesQuote, { id: contextId }, {}, { tenantId, organizationId });
73
73
  if (!quote) {
74
74
  throw new CrudHttpError(404, { error: "sales.notes.context_not_found" });
75
75
  }
@@ -171,7 +171,7 @@ const createNoteCommand = {
171
171
  const noteId = logEntry?.resourceId ?? null;
172
172
  if (!noteId) return;
173
173
  const em = ctx.container.resolve("em").fork();
174
- const existing = await em.findOne(SalesNote, { id: noteId });
174
+ const existing = await findOneWithDecryption(em, SalesNote, { id: noteId }, {});
175
175
  if (existing) {
176
176
  em.remove(existing);
177
177
  await em.flush();
@@ -225,7 +225,7 @@ const updateNoteCommand = {
225
225
  async execute(rawInput, ctx) {
226
226
  const parsed = noteUpdateSchema.parse(rawInput ?? {});
227
227
  const em = ctx.container.resolve("em").fork();
228
- const note = await em.findOne(SalesNote, { id: parsed.id });
228
+ const note = await findOneWithDecryption(em, SalesNote, { id: parsed.id }, {});
229
229
  if (!note) throw new CrudHttpError(404, { error: "sales.notes.not_found" });
230
230
  ensureTenantScope(ctx, note.tenantId);
231
231
  ensureOrganizationScope(ctx, note.organizationId);
@@ -290,7 +290,7 @@ const updateNoteCommand = {
290
290
  const em = ctx.container.resolve("em").fork();
291
291
  const context = await requireContext(em, before.contextType, before.contextId).catch(() => null);
292
292
  if (!context) return;
293
- let note = await em.findOne(SalesNote, { id: before.id });
293
+ let note = await findOneWithDecryption(em, SalesNote, { id: before.id }, {}, { tenantId: before.tenantId, organizationId: before.organizationId });
294
294
  if (!note) {
295
295
  note = em.create(SalesNote, {
296
296
  id: before.id,
@@ -347,7 +347,7 @@ const deleteNoteCommand = {
347
347
  async execute(input, ctx) {
348
348
  const id = requireId(input, "Note id required");
349
349
  const em = ctx.container.resolve("em").fork();
350
- const note = await em.findOne(SalesNote, { id });
350
+ const note = await findOneWithDecryption(em, SalesNote, { id }, {});
351
351
  if (!note) throw new CrudHttpError(404, { error: "sales.notes.not_found" });
352
352
  ensureTenantScope(ctx, note.tenantId);
353
353
  ensureOrganizationScope(ctx, note.organizationId);
@@ -395,7 +395,7 @@ const deleteNoteCommand = {
395
395
  const em = ctx.container.resolve("em").fork();
396
396
  const context = await requireContext(em, before.contextType, before.contextId).catch(() => null);
397
397
  if (!context) return;
398
- let note = await em.findOne(SalesNote, { id: before.id });
398
+ let note = await findOneWithDecryption(em, SalesNote, { id: before.id }, {}, { tenantId: before.tenantId, organizationId: before.organizationId });
399
399
  if (!note) {
400
400
  note = em.create(SalesNote, {
401
401
  id: before.id,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/sales/commands/notes.ts"],
4
- "sourcesContent": ["import { registerCommand, type CommandHandler } from '@open-mercato/shared/lib/commands'\nimport {\n emitCrudSideEffects,\n emitCrudUndoSideEffects,\n buildChanges,\n requireId,\n} from '@open-mercato/shared/lib/commands/helpers'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport type { CrudEventsConfig } from '@open-mercato/shared/lib/crud/types'\nimport { E } from '#generated/entities.ids.generated'\nimport {\n SalesNote,\n SalesOrder,\n SalesQuote,\n SalesInvoice,\n SalesCreditMemo,\n type SalesDocumentKind,\n} from '../data/entities'\nimport {\n noteCreateSchema,\n noteUpdateSchema,\n type NoteCreateInput,\n type NoteUpdateInput,\n} from '../data/validators'\nimport { makeCreateRedo } from '@open-mercato/shared/lib/commands/redo'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { ensureOrganizationScope, ensureSameScope, ensureTenantScope, extractUndoPayload } from './shared'\n\ntype NoteSnapshot = {\n id: string\n organizationId: string\n tenantId: string\n contextType: SalesDocumentKind\n contextId: string\n orderId: string | null\n quoteId: string | null\n body: string\n authorUserId: string | null\n appearanceIcon: string | null\n appearanceColor: string | null\n}\n\ntype NoteUndoPayload = {\n before?: NoteSnapshot | null\n after?: NoteSnapshot | null\n}\n\nconst noteCrudIndexer = {\n entityType: E.sales.sales_note,\n}\n\nconst noteCrudEvents: CrudEventsConfig = {\n module: 'sales',\n entity: 'note',\n persistent: true,\n buildPayload: (ctx) => ({\n id: ctx.identifiers.id,\n organizationId: ctx.identifiers.organizationId,\n tenantId: ctx.identifiers.tenantId,\n }),\n}\n\nasync function loadNoteSnapshot(em: EntityManager, id: string): Promise<NoteSnapshot | null> {\n const note = await em.findOne(SalesNote, { id })\n if (!note) return null\n return {\n id: note.id,\n organizationId: note.organizationId,\n tenantId: note.tenantId,\n contextType: note.contextType,\n contextId: note.contextId,\n orderId: note.order ? (typeof note.order === 'string' ? note.order : note.order.id) : null,\n quoteId: note.quote ? (typeof note.quote === 'string' ? note.quote : note.quote.id) : null,\n body: note.body,\n authorUserId: note.authorUserId ?? null,\n appearanceIcon: note.appearanceIcon ?? null,\n appearanceColor: note.appearanceColor ?? null,\n }\n}\n\nasync function requireContext(\n em: EntityManager,\n contextType: SalesDocumentKind,\n contextId: string,\n organizationId?: string,\n tenantId?: string\n): Promise<{\n organizationId: string\n tenantId: string\n order?: SalesOrder | null\n quote?: SalesQuote | null\n}> {\n if (contextType === 'order') {\n const order = await em.findOne(SalesOrder, { id: contextId })\n if (!order) {\n throw new CrudHttpError(404, { error: 'sales.notes.context_not_found' })\n }\n if (organizationId && tenantId) {\n ensureSameScope(order, organizationId, tenantId)\n }\n return {\n organizationId: order.organizationId,\n tenantId: order.tenantId,\n order,\n quote: null,\n }\n }\n if (contextType === 'quote') {\n const quote = await em.findOne(SalesQuote, { id: contextId })\n if (!quote) {\n throw new CrudHttpError(404, { error: 'sales.notes.context_not_found' })\n }\n if (organizationId && tenantId) {\n ensureSameScope(quote, organizationId, tenantId)\n }\n return {\n organizationId: quote.organizationId,\n tenantId: quote.tenantId,\n order: null,\n quote,\n }\n }\n const repo = contextType === 'invoice' ? SalesInvoice : SalesCreditMemo\n const entity = await em.findOne(repo as any, { id: contextId }) as (SalesInvoice | SalesCreditMemo) | null\n if (!entity) {\n throw new CrudHttpError(404, { error: 'sales.notes.context_not_found' })\n }\n if (organizationId && tenantId) {\n ensureSameScope(entity, organizationId, tenantId)\n }\n return {\n organizationId: entity.organizationId,\n tenantId: entity.tenantId,\n order: null,\n quote: null,\n }\n}\n\nfunction resolveAuthor(inputAuthor: string | undefined, authSub: string | null): string | null {\n if (inputAuthor) return inputAuthor\n if (!authSub) return null\n const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/\n return uuidRegex.test(authSub) ? authSub : null\n}\n\nconst createNoteCommand: CommandHandler<NoteCreateInput, { noteId: string; authorUserId: string | null }> = {\n id: 'sales.notes.create',\n async execute(rawInput, ctx) {\n const parsed = noteCreateSchema.parse(rawInput ?? {})\n ensureTenantScope(ctx, parsed.tenantId)\n ensureOrganizationScope(ctx, parsed.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const context = await requireContext(em, parsed.contextType, parsed.contextId, parsed.organizationId, parsed.tenantId)\n const authorUserId = resolveAuthor(parsed.authorUserId, ctx.auth?.isApiKey ? null : ctx.auth?.sub ?? null)\n\n const note = em.create(SalesNote, {\n organizationId: parsed.organizationId,\n tenantId: parsed.tenantId,\n contextType: parsed.contextType,\n contextId: parsed.contextId,\n order: context.order ?? null,\n quote: context.quote ?? null,\n authorUserId,\n appearanceIcon: parsed.appearanceIcon ?? null,\n appearanceColor: parsed.appearanceColor ?? null,\n body: parsed.body,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n em.persist(note)\n await em.flush()\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine,\n action: 'created',\n entity: note,\n identifiers: {\n id: note.id,\n organizationId: note.organizationId,\n tenantId: note.tenantId,\n },\n indexer: noteCrudIndexer,\n events: noteCrudEvents,\n })\n\n return { noteId: note.id, authorUserId: note.authorUserId ?? null }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return loadNoteSnapshot(em, result.noteId)\n },\n buildLog: async ({ result, snapshots }) => {\n const { translate } = await resolveTranslations()\n const snapshot = snapshots.after as NoteSnapshot | undefined\n return {\n actionLabel: translate('sales.audit.notes.create', 'Create note'),\n resourceKind: 'sales.note',\n resourceId: result.noteId,\n parentResourceKind: snapshot?.contextType ? `sales.${snapshot.contextType}` : null,\n parentResourceId: snapshot?.contextId ?? null,\n tenantId: snapshot?.tenantId ?? null,\n organizationId: snapshot?.organizationId ?? null,\n snapshotAfter: snapshot ?? null,\n payload: {\n undo: {\n after: snapshot ?? null,\n } satisfies NoteUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const noteId = logEntry?.resourceId ?? null\n if (!noteId) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const existing = await em.findOne(SalesNote, { id: noteId })\n if (existing) {\n em.remove(existing)\n await em.flush()\n }\n },\n redo: makeCreateRedo<SalesNote, NoteSnapshot, NoteCreateInput, { noteId: string; authorUserId: string | null }>({\n entityClass: SalesNote,\n indexer: noteCrudIndexer,\n events: noteCrudEvents,\n findRow: ({ em, id, snapshot }) =>\n findOneWithDecryption(\n em,\n SalesNote,\n { id },\n undefined,\n { tenantId: snapshot.tenantId, organizationId: snapshot.organizationId },\n ),\n seedFromSnapshot: (snapshot) => ({\n id: snapshot.id,\n organizationId: snapshot.organizationId,\n tenantId: snapshot.tenantId,\n contextType: snapshot.contextType,\n contextId: snapshot.contextId,\n body: snapshot.body,\n authorUserId: snapshot.authorUserId,\n appearanceIcon: snapshot.appearanceIcon,\n appearanceColor: snapshot.appearanceColor,\n createdAt: new Date(),\n updatedAt: new Date(),\n }),\n beforeRestore: async ({ em, snapshot }) => {\n const context = await requireContext(em, snapshot.contextType, snapshot.contextId).catch(() => null)\n if (!context) {\n throw new CrudHttpError(404, { error: 'sales.notes.context_not_found' })\n }\n return {\n order: snapshot.orderId ? context.order ?? null : null,\n quote: snapshot.quoteId ? context.quote ?? null : null,\n }\n },\n buildResult: (entity) => ({ noteId: entity.id, authorUserId: entity.authorUserId ?? null }),\n }),\n}\n\nconst updateNoteCommand: CommandHandler<NoteUpdateInput, { noteId: string }> = {\n id: 'sales.notes.update',\n async prepare(rawInput, ctx) {\n const parsed = noteUpdateSchema.parse(rawInput ?? {})\n const em = ctx.container.resolve('em') as EntityManager\n const snapshot = await loadNoteSnapshot(em, parsed.id)\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const parsed = noteUpdateSchema.parse(rawInput ?? {})\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const note = await em.findOne(SalesNote, { id: parsed.id })\n if (!note) throw new CrudHttpError(404, { error: 'sales.notes.not_found' })\n ensureTenantScope(ctx, note.tenantId)\n ensureOrganizationScope(ctx, note.organizationId)\n\n if (parsed.body !== undefined) note.body = parsed.body\n if (parsed.authorUserId !== undefined) note.authorUserId = parsed.authorUserId ?? null\n if (parsed.appearanceIcon !== undefined) note.appearanceIcon = parsed.appearanceIcon ?? null\n if (parsed.appearanceColor !== undefined) note.appearanceColor = parsed.appearanceColor ?? null\n note.updatedAt = new Date()\n\n await em.flush()\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine,\n action: 'updated',\n entity: note,\n identifiers: {\n id: note.id,\n organizationId: note.organizationId,\n tenantId: note.tenantId,\n },\n indexer: noteCrudIndexer,\n events: noteCrudEvents,\n })\n\n return { noteId: note.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return await loadNoteSnapshot(em, result.noteId)\n },\n buildLog: async ({ snapshots }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as NoteSnapshot | undefined\n if (!before) return null\n const after = snapshots.after as NoteSnapshot | undefined\n const changes =\n after && before\n ? buildChanges(\n before as unknown as Record<string, unknown>,\n after as unknown as Record<string, unknown>,\n ['body', 'authorUserId', 'appearanceIcon', 'appearanceColor']\n )\n : {}\n return {\n actionLabel: translate('sales.audit.notes.update', 'Update note'),\n resourceKind: 'sales.note',\n resourceId: before.id,\n parentResourceKind: before.contextType ? `sales.${before.contextType}` : null,\n parentResourceId: before.contextId ?? null,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n snapshotAfter: after ?? null,\n changes,\n payload: {\n undo: {\n before,\n after: after ?? null,\n } satisfies NoteUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<NoteUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const context = await requireContext(em, before.contextType, before.contextId).catch(() => null)\n if (!context) return\n let note = await em.findOne(SalesNote, { id: before.id })\n if (!note) {\n note = em.create(SalesNote, {\n id: before.id,\n organizationId: before.organizationId,\n tenantId: before.tenantId,\n contextType: before.contextType,\n contextId: before.contextId,\n order: before.orderId ? context.order ?? null : null,\n quote: before.quoteId ? context.quote ?? null : null,\n body: before.body,\n authorUserId: before.authorUserId,\n appearanceIcon: before.appearanceIcon,\n appearanceColor: before.appearanceColor,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n em.persist(note)\n }\n note.organizationId = before.organizationId\n note.tenantId = before.tenantId\n note.contextType = before.contextType\n note.contextId = before.contextId\n note.order = before.orderId ? context.order ?? null : null\n note.quote = before.quoteId ? context.quote ?? null : null\n note.body = before.body\n note.authorUserId = before.authorUserId\n note.appearanceIcon = before.appearanceIcon\n note.appearanceColor = before.appearanceColor\n note.updatedAt = new Date()\n await em.flush()\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'updated',\n entity: note,\n identifiers: {\n id: note.id,\n organizationId: note.organizationId,\n tenantId: note.tenantId,\n },\n indexer: noteCrudIndexer,\n events: noteCrudEvents,\n })\n },\n}\n\nconst deleteNoteCommand: CommandHandler<{ body?: Record<string, unknown>; query?: Record<string, unknown> }, { noteId: string }> =\n {\n id: 'sales.notes.delete',\n async prepare(input, ctx) {\n const id = requireId(input, 'Note id required')\n const em = ctx.container.resolve('em') as EntityManager\n const snapshot = await loadNoteSnapshot(em, id)\n return snapshot ? { before: snapshot } : {}\n },\n async execute(input, ctx) {\n const id = requireId(input, 'Note id required')\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const note = await em.findOne(SalesNote, { id })\n if (!note) throw new CrudHttpError(404, { error: 'sales.notes.not_found' })\n ensureTenantScope(ctx, note.tenantId)\n ensureOrganizationScope(ctx, note.organizationId)\n em.remove(note)\n await em.flush()\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine,\n action: 'deleted',\n entity: note,\n identifiers: {\n id: note.id,\n organizationId: note.organizationId,\n tenantId: note.tenantId,\n },\n indexer: noteCrudIndexer,\n events: noteCrudEvents,\n })\n return { noteId: note.id }\n },\n buildLog: async ({ snapshots }) => {\n const before = snapshots.before as NoteSnapshot | undefined\n if (!before) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('sales.audit.notes.delete', 'Delete note'),\n resourceKind: 'sales.note',\n resourceId: before.id,\n parentResourceKind: before.contextType ? `sales.${before.contextType}` : null,\n parentResourceId: before.contextId ?? null,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n payload: {\n undo: {\n before,\n } satisfies NoteUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<NoteUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const context = await requireContext(em, before.contextType, before.contextId).catch(() => null)\n if (!context) return\n let note = await em.findOne(SalesNote, { id: before.id })\n if (!note) {\n note = em.create(SalesNote, {\n id: before.id,\n organizationId: before.organizationId,\n tenantId: before.tenantId,\n contextType: before.contextType,\n contextId: before.contextId,\n order: before.orderId ? context.order ?? null : null,\n quote: before.quoteId ? context.quote ?? null : null,\n body: before.body,\n authorUserId: before.authorUserId,\n appearanceIcon: before.appearanceIcon,\n appearanceColor: before.appearanceColor,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n em.persist(note)\n }\n note.organizationId = before.organizationId\n note.tenantId = before.tenantId\n note.contextType = before.contextType\n note.contextId = before.contextId\n note.order = before.orderId ? context.order ?? null : null\n note.quote = before.quoteId ? context.quote ?? null : null\n note.body = before.body\n note.authorUserId = before.authorUserId\n note.appearanceIcon = before.appearanceIcon\n note.appearanceColor = before.appearanceColor\n note.updatedAt = new Date()\n await em.flush()\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'created',\n entity: note,\n identifiers: {\n id: note.id,\n organizationId: note.organizationId,\n tenantId: note.tenantId,\n },\n indexer: noteCrudIndexer,\n events: noteCrudEvents,\n })\n },\n }\n\nregisterCommand(createNoteCommand)\nregisterCommand(updateNoteCommand)\nregisterCommand(deleteNoteCommand)\n"],
5
- "mappings": "AAAA,SAAS,uBAA4C;AACrD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAGP,SAAS,qBAAqB;AAC9B,SAAS,2BAA2B;AAEpC,SAAS,SAAS;AAClB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AACP,SAAS,sBAAsB;AAC/B,SAAS,6BAA6B;AACtC,SAAS,yBAAyB,iBAAiB,mBAAmB,0BAA0B;AAqBhG,MAAM,kBAAkB;AAAA,EACtB,YAAY,EAAE,MAAM;AACtB;AAEA,MAAM,iBAAmC;AAAA,EACvC,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,cAAc,CAAC,SAAS;AAAA,IACtB,IAAI,IAAI,YAAY;AAAA,IACpB,gBAAgB,IAAI,YAAY;AAAA,IAChC,UAAU,IAAI,YAAY;AAAA,EAC5B;AACF;AAEA,eAAe,iBAAiB,IAAmB,IAA0C;AAC3F,QAAM,OAAO,MAAM,GAAG,QAAQ,WAAW,EAAE,GAAG,CAAC;AAC/C,MAAI,CAAC,KAAM,QAAO;AAClB,SAAO;AAAA,IACL,IAAI,KAAK;AAAA,IACT,gBAAgB,KAAK;AAAA,IACrB,UAAU,KAAK;AAAA,IACf,aAAa,KAAK;AAAA,IAClB,WAAW,KAAK;AAAA,IAChB,SAAS,KAAK,QAAS,OAAO,KAAK,UAAU,WAAW,KAAK,QAAQ,KAAK,MAAM,KAAM;AAAA,IACtF,SAAS,KAAK,QAAS,OAAO,KAAK,UAAU,WAAW,KAA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;AAAA,MACR,aAAa;AAAA,QACX,IAAI,KAAK;AAAA,QACT,gBAAgB,KAAK;AAAA,QACrB,UAAU,KAAK;AAAA,MACjB;AAAA,MACA,SAAS;AAAA,MACT,QAAQ;AAAA,IACV,CAAC;AAAA,EACH;AACF;AAEF,gBAAgB,iBAAiB;AACjC,gBAAgB,iBAAiB;AACjC,gBAAgB,iBAAiB;",
4
+ "sourcesContent": ["import { registerCommand, type CommandHandler } from '@open-mercato/shared/lib/commands'\nimport {\n emitCrudSideEffects,\n emitCrudUndoSideEffects,\n buildChanges,\n requireId,\n} from '@open-mercato/shared/lib/commands/helpers'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport type { CrudEventsConfig } from '@open-mercato/shared/lib/crud/types'\nimport { E } from '#generated/entities.ids.generated'\nimport {\n SalesNote,\n SalesOrder,\n SalesQuote,\n SalesInvoice,\n SalesCreditMemo,\n type SalesDocumentKind,\n} from '../data/entities'\nimport {\n noteCreateSchema,\n noteUpdateSchema,\n type NoteCreateInput,\n type NoteUpdateInput,\n} from '../data/validators'\nimport { makeCreateRedo } from '@open-mercato/shared/lib/commands/redo'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { ensureOrganizationScope, ensureSameScope, ensureTenantScope, extractUndoPayload } from './shared'\n\ntype NoteSnapshot = {\n id: string\n organizationId: string\n tenantId: string\n contextType: SalesDocumentKind\n contextId: string\n orderId: string | null\n quoteId: string | null\n body: string\n authorUserId: string | null\n appearanceIcon: string | null\n appearanceColor: string | null\n}\n\ntype NoteUndoPayload = {\n before?: NoteSnapshot | null\n after?: NoteSnapshot | null\n}\n\nconst noteCrudIndexer = {\n entityType: E.sales.sales_note,\n}\n\nconst noteCrudEvents: CrudEventsConfig = {\n module: 'sales',\n entity: 'note',\n persistent: true,\n buildPayload: (ctx) => ({\n id: ctx.identifiers.id,\n organizationId: ctx.identifiers.organizationId,\n tenantId: ctx.identifiers.tenantId,\n }),\n}\n\nasync function loadNoteSnapshot(em: EntityManager, id: string): Promise<NoteSnapshot | null> {\n const note = await findOneWithDecryption(em, SalesNote, { id }, {})\n if (!note) return null\n return {\n id: note.id,\n organizationId: note.organizationId,\n tenantId: note.tenantId,\n contextType: note.contextType,\n contextId: note.contextId,\n orderId: note.order ? (typeof note.order === 'string' ? note.order : note.order.id) : null,\n quoteId: note.quote ? (typeof note.quote === 'string' ? note.quote : note.quote.id) : null,\n body: note.body,\n authorUserId: note.authorUserId ?? null,\n appearanceIcon: note.appearanceIcon ?? null,\n appearanceColor: note.appearanceColor ?? null,\n }\n}\n\nasync function requireContext(\n em: EntityManager,\n contextType: SalesDocumentKind,\n contextId: string,\n organizationId?: string,\n tenantId?: string\n): Promise<{\n organizationId: string\n tenantId: string\n order?: SalesOrder | null\n quote?: SalesQuote | null\n}> {\n if (contextType === 'order') {\n const order = await findOneWithDecryption(em, SalesOrder, { id: contextId }, {}, { tenantId, organizationId })\n if (!order) {\n throw new CrudHttpError(404, { error: 'sales.notes.context_not_found' })\n }\n if (organizationId && tenantId) {\n ensureSameScope(order, organizationId, tenantId)\n }\n return {\n organizationId: order.organizationId,\n tenantId: order.tenantId,\n order,\n quote: null,\n }\n }\n if (contextType === 'quote') {\n const quote = await findOneWithDecryption(em, SalesQuote, { id: contextId }, {}, { tenantId, organizationId })\n if (!quote) {\n throw new CrudHttpError(404, { error: 'sales.notes.context_not_found' })\n }\n if (organizationId && tenantId) {\n ensureSameScope(quote, organizationId, tenantId)\n }\n return {\n organizationId: quote.organizationId,\n tenantId: quote.tenantId,\n order: null,\n quote,\n }\n }\n const repo = contextType === 'invoice' ? SalesInvoice : SalesCreditMemo\n const entity = await em.findOne(repo as any, { id: contextId }) as (SalesInvoice | SalesCreditMemo) | null\n if (!entity) {\n throw new CrudHttpError(404, { error: 'sales.notes.context_not_found' })\n }\n if (organizationId && tenantId) {\n ensureSameScope(entity, organizationId, tenantId)\n }\n return {\n organizationId: entity.organizationId,\n tenantId: entity.tenantId,\n order: null,\n quote: null,\n }\n}\n\nfunction resolveAuthor(inputAuthor: string | undefined, authSub: string | null): string | null {\n if (inputAuthor) return inputAuthor\n if (!authSub) return null\n const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/\n return uuidRegex.test(authSub) ? authSub : null\n}\n\nconst createNoteCommand: CommandHandler<NoteCreateInput, { noteId: string; authorUserId: string | null }> = {\n id: 'sales.notes.create',\n async execute(rawInput, ctx) {\n const parsed = noteCreateSchema.parse(rawInput ?? {})\n ensureTenantScope(ctx, parsed.tenantId)\n ensureOrganizationScope(ctx, parsed.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const context = await requireContext(em, parsed.contextType, parsed.contextId, parsed.organizationId, parsed.tenantId)\n const authorUserId = resolveAuthor(parsed.authorUserId, ctx.auth?.isApiKey ? null : ctx.auth?.sub ?? null)\n\n const note = em.create(SalesNote, {\n organizationId: parsed.organizationId,\n tenantId: parsed.tenantId,\n contextType: parsed.contextType,\n contextId: parsed.contextId,\n order: context.order ?? null,\n quote: context.quote ?? null,\n authorUserId,\n appearanceIcon: parsed.appearanceIcon ?? null,\n appearanceColor: parsed.appearanceColor ?? null,\n body: parsed.body,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n em.persist(note)\n await em.flush()\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine,\n action: 'created',\n entity: note,\n identifiers: {\n id: note.id,\n organizationId: note.organizationId,\n tenantId: note.tenantId,\n },\n indexer: noteCrudIndexer,\n events: noteCrudEvents,\n })\n\n return { noteId: note.id, authorUserId: note.authorUserId ?? null }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return loadNoteSnapshot(em, result.noteId)\n },\n buildLog: async ({ result, snapshots }) => {\n const { translate } = await resolveTranslations()\n const snapshot = snapshots.after as NoteSnapshot | undefined\n return {\n actionLabel: translate('sales.audit.notes.create', 'Create note'),\n resourceKind: 'sales.note',\n resourceId: result.noteId,\n parentResourceKind: snapshot?.contextType ? `sales.${snapshot.contextType}` : null,\n parentResourceId: snapshot?.contextId ?? null,\n tenantId: snapshot?.tenantId ?? null,\n organizationId: snapshot?.organizationId ?? null,\n snapshotAfter: snapshot ?? null,\n payload: {\n undo: {\n after: snapshot ?? null,\n } satisfies NoteUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const noteId = logEntry?.resourceId ?? null\n if (!noteId) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const existing = await findOneWithDecryption(em, SalesNote, { id: noteId }, {})\n if (existing) {\n em.remove(existing)\n await em.flush()\n }\n },\n redo: makeCreateRedo<SalesNote, NoteSnapshot, NoteCreateInput, { noteId: string; authorUserId: string | null }>({\n entityClass: SalesNote,\n indexer: noteCrudIndexer,\n events: noteCrudEvents,\n findRow: ({ em, id, snapshot }) =>\n findOneWithDecryption(\n em,\n SalesNote,\n { id },\n undefined,\n { tenantId: snapshot.tenantId, organizationId: snapshot.organizationId },\n ),\n seedFromSnapshot: (snapshot) => ({\n id: snapshot.id,\n organizationId: snapshot.organizationId,\n tenantId: snapshot.tenantId,\n contextType: snapshot.contextType,\n contextId: snapshot.contextId,\n body: snapshot.body,\n authorUserId: snapshot.authorUserId,\n appearanceIcon: snapshot.appearanceIcon,\n appearanceColor: snapshot.appearanceColor,\n createdAt: new Date(),\n updatedAt: new Date(),\n }),\n beforeRestore: async ({ em, snapshot }) => {\n const context = await requireContext(em, snapshot.contextType, snapshot.contextId).catch(() => null)\n if (!context) {\n throw new CrudHttpError(404, { error: 'sales.notes.context_not_found' })\n }\n return {\n order: snapshot.orderId ? context.order ?? null : null,\n quote: snapshot.quoteId ? context.quote ?? null : null,\n }\n },\n buildResult: (entity) => ({ noteId: entity.id, authorUserId: entity.authorUserId ?? null }),\n }),\n}\n\nconst updateNoteCommand: CommandHandler<NoteUpdateInput, { noteId: string }> = {\n id: 'sales.notes.update',\n async prepare(rawInput, ctx) {\n const parsed = noteUpdateSchema.parse(rawInput ?? {})\n const em = ctx.container.resolve('em') as EntityManager\n const snapshot = await loadNoteSnapshot(em, parsed.id)\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const parsed = noteUpdateSchema.parse(rawInput ?? {})\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const note = await findOneWithDecryption(em, SalesNote, { id: parsed.id }, {})\n if (!note) throw new CrudHttpError(404, { error: 'sales.notes.not_found' })\n ensureTenantScope(ctx, note.tenantId)\n ensureOrganizationScope(ctx, note.organizationId)\n\n if (parsed.body !== undefined) note.body = parsed.body\n if (parsed.authorUserId !== undefined) note.authorUserId = parsed.authorUserId ?? null\n if (parsed.appearanceIcon !== undefined) note.appearanceIcon = parsed.appearanceIcon ?? null\n if (parsed.appearanceColor !== undefined) note.appearanceColor = parsed.appearanceColor ?? null\n note.updatedAt = new Date()\n\n await em.flush()\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine,\n action: 'updated',\n entity: note,\n identifiers: {\n id: note.id,\n organizationId: note.organizationId,\n tenantId: note.tenantId,\n },\n indexer: noteCrudIndexer,\n events: noteCrudEvents,\n })\n\n return { noteId: note.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return await loadNoteSnapshot(em, result.noteId)\n },\n buildLog: async ({ snapshots }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as NoteSnapshot | undefined\n if (!before) return null\n const after = snapshots.after as NoteSnapshot | undefined\n const changes =\n after && before\n ? buildChanges(\n before as unknown as Record<string, unknown>,\n after as unknown as Record<string, unknown>,\n ['body', 'authorUserId', 'appearanceIcon', 'appearanceColor']\n )\n : {}\n return {\n actionLabel: translate('sales.audit.notes.update', 'Update note'),\n resourceKind: 'sales.note',\n resourceId: before.id,\n parentResourceKind: before.contextType ? `sales.${before.contextType}` : null,\n parentResourceId: before.contextId ?? null,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n snapshotAfter: after ?? null,\n changes,\n payload: {\n undo: {\n before,\n after: after ?? null,\n } satisfies NoteUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<NoteUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const context = await requireContext(em, before.contextType, before.contextId).catch(() => null)\n if (!context) return\n let note = await findOneWithDecryption(em, SalesNote, { id: before.id }, {}, { tenantId: before.tenantId, organizationId: before.organizationId })\n if (!note) {\n note = em.create(SalesNote, {\n id: before.id,\n organizationId: before.organizationId,\n tenantId: before.tenantId,\n contextType: before.contextType,\n contextId: before.contextId,\n order: before.orderId ? context.order ?? null : null,\n quote: before.quoteId ? context.quote ?? null : null,\n body: before.body,\n authorUserId: before.authorUserId,\n appearanceIcon: before.appearanceIcon,\n appearanceColor: before.appearanceColor,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n em.persist(note)\n }\n note.organizationId = before.organizationId\n note.tenantId = before.tenantId\n note.contextType = before.contextType\n note.contextId = before.contextId\n note.order = before.orderId ? context.order ?? null : null\n note.quote = before.quoteId ? context.quote ?? null : null\n note.body = before.body\n note.authorUserId = before.authorUserId\n note.appearanceIcon = before.appearanceIcon\n note.appearanceColor = before.appearanceColor\n note.updatedAt = new Date()\n await em.flush()\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'updated',\n entity: note,\n identifiers: {\n id: note.id,\n organizationId: note.organizationId,\n tenantId: note.tenantId,\n },\n indexer: noteCrudIndexer,\n events: noteCrudEvents,\n })\n },\n}\n\nconst deleteNoteCommand: CommandHandler<{ body?: Record<string, unknown>; query?: Record<string, unknown> }, { noteId: string }> =\n {\n id: 'sales.notes.delete',\n async prepare(input, ctx) {\n const id = requireId(input, 'Note id required')\n const em = ctx.container.resolve('em') as EntityManager\n const snapshot = await loadNoteSnapshot(em, id)\n return snapshot ? { before: snapshot } : {}\n },\n async execute(input, ctx) {\n const id = requireId(input, 'Note id required')\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const note = await findOneWithDecryption(em, SalesNote, { id }, {})\n if (!note) throw new CrudHttpError(404, { error: 'sales.notes.not_found' })\n ensureTenantScope(ctx, note.tenantId)\n ensureOrganizationScope(ctx, note.organizationId)\n em.remove(note)\n await em.flush()\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine,\n action: 'deleted',\n entity: note,\n identifiers: {\n id: note.id,\n organizationId: note.organizationId,\n tenantId: note.tenantId,\n },\n indexer: noteCrudIndexer,\n events: noteCrudEvents,\n })\n return { noteId: note.id }\n },\n buildLog: async ({ snapshots }) => {\n const before = snapshots.before as NoteSnapshot | undefined\n if (!before) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('sales.audit.notes.delete', 'Delete note'),\n resourceKind: 'sales.note',\n resourceId: before.id,\n parentResourceKind: before.contextType ? `sales.${before.contextType}` : null,\n parentResourceId: before.contextId ?? null,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n payload: {\n undo: {\n before,\n } satisfies NoteUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<NoteUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const context = await requireContext(em, before.contextType, before.contextId).catch(() => null)\n if (!context) return\n let note = await findOneWithDecryption(em, SalesNote, { id: before.id }, {}, { tenantId: before.tenantId, organizationId: before.organizationId })\n if (!note) {\n note = em.create(SalesNote, {\n id: before.id,\n organizationId: before.organizationId,\n tenantId: before.tenantId,\n contextType: before.contextType,\n contextId: before.contextId,\n order: before.orderId ? context.order ?? null : null,\n quote: before.quoteId ? context.quote ?? null : null,\n body: before.body,\n authorUserId: before.authorUserId,\n appearanceIcon: before.appearanceIcon,\n appearanceColor: before.appearanceColor,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n em.persist(note)\n }\n note.organizationId = before.organizationId\n note.tenantId = before.tenantId\n note.contextType = before.contextType\n note.contextId = before.contextId\n note.order = before.orderId ? context.order ?? null : null\n note.quote = before.quoteId ? context.quote ?? null : null\n note.body = before.body\n note.authorUserId = before.authorUserId\n note.appearanceIcon = before.appearanceIcon\n note.appearanceColor = before.appearanceColor\n note.updatedAt = new Date()\n await em.flush()\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'created',\n entity: note,\n identifiers: {\n id: note.id,\n organizationId: note.organizationId,\n tenantId: note.tenantId,\n },\n indexer: noteCrudIndexer,\n events: noteCrudEvents,\n })\n },\n }\n\nregisterCommand(createNoteCommand)\nregisterCommand(updateNoteCommand)\nregisterCommand(deleteNoteCommand)\n"],
5
+ "mappings": "AAAA,SAAS,uBAA4C;AACrD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAGP,SAAS,qBAAqB;AAC9B,SAAS,2BAA2B;AAEpC,SAAS,SAAS;AAClB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AACP,SAAS,sBAAsB;AAC/B,SAAS,6BAA6B;AACtC,SAAS,yBAAyB,iBAAiB,mBAAmB,0BAA0B;AAqBhG,MAAM,kBAAkB;AAAA,EACtB,YAAY,EAAE,MAAM;AACtB;AAEA,MAAM,iBAAmC;AAAA,EACvC,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,YAAY;AAAA,EAC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;AAAA,QAClB,OAAO,OAAO,UAAU,QAAQ,SAAS,OAAO;AAAA,QAChD,OAAO,OAAO,UAAU,QAAQ,SAAS,OAAO;AAAA,QAChD,MAAM,OAAO;AAAA,QACb,cAAc,OAAO;AAAA,QACrB,gBAAgB,OAAO;AAAA,QACvB,iBAAiB,OAAO;AAAA,QACxB,WAAW,oBAAI,KAAK;AAAA,QACpB,WAAW,oBAAI,KAAK;AAAA,MACtB,CAAC;AACD,SAAG,QAAQ,IAAI;AAAA,IACjB;AACE,SAAK,iBAAiB,OAAO;AAC7B,SAAK,WAAW,OAAO;AACvB,SAAK,cAAc,OAAO;AAC1B,SAAK,YAAY,OAAO;AACxB,SAAK,QAAQ,OAAO,UAAU,QAAQ,SAAS,OAAO;AACtD,SAAK,QAAQ,OAAO,UAAU,QAAQ,SAAS,OAAO;AACtD,SAAK,OAAO,OAAO;AACnB,SAAK,eAAe,OAAO;AAC3B,SAAK,iBAAiB,OAAO;AAC7B,SAAK,kBAAkB,OAAO;AAC9B,SAAK,YAAY,oBAAI,KAAK;AAC1B,UAAM,GAAG,MAAM;AAEf,UAAM,aAAa,IAAI,UAAU,QAAQ,YAAY;AACrD,UAAM,wBAAwB;AAAA,MAC5B;AAAA,MACA,QAAQ;AAAA,MACR,QAAQ;AAAA,MACR,aAAa;AAAA,QACX,IAAI,KAAK;AAAA,QACT,gBAAgB,KAAK;AAAA,QACrB,UAAU,KAAK;AAAA,MACjB;AAAA,MACA,SAAS;AAAA,MACT,QAAQ;AAAA,IACV,CAAC;AAAA,EACH;AACF;AAEF,gBAAgB,iBAAiB;AACjC,gBAAgB,iBAAiB;AACjC,gBAAgB,iBAAiB;",
6
6
  "names": []
7
7
  }
@@ -169,7 +169,7 @@ async function recomputeOrderPaymentTotals(em, order, options) {
169
169
  const orderId = order.id;
170
170
  const scope = { organizationId: order.organizationId, tenantId: order.tenantId };
171
171
  if (options?.lock) {
172
- await em.findOne(SalesOrder, { id: orderId, ...scope }, { lockMode: LockMode.PESSIMISTIC_WRITE });
172
+ await findOneWithDecryption(em, SalesOrder, { id: orderId, ...scope }, { lockMode: LockMode.PESSIMISTIC_WRITE }, scope);
173
173
  }
174
174
  const allocations = await findWithDecryption(
175
175
  em,
@@ -525,7 +525,7 @@ const createPaymentCommand = {
525
525
  if (recomputed && (!totals || orderId === after.orderId)) {
526
526
  totals = recomputed;
527
527
  }
528
- const target = await em.findOne(SalesOrder, { id: orderId, organizationId: after.organizationId, tenantId: after.tenantId });
528
+ const target = await findOneWithDecryption(em, SalesOrder, { id: orderId, organizationId: after.organizationId, tenantId: after.tenantId }, {}, { tenantId: after.tenantId, organizationId: after.organizationId });
529
529
  if (target) {
530
530
  ensureSameScope(target, after.organizationId, after.tenantId);
531
531
  await invalidateOrderCache(ctx.container, target, ctx.auth?.tenantId ?? null);
@@ -760,10 +760,12 @@ const updatePaymentCommand = {
760
760
  let totals;
761
761
  if (nextOrderId) {
762
762
  totals = await em.transactional(async (tx) => {
763
- const lockedOrder = await tx.findOne(
763
+ const lockedOrder = await findOneWithDecryption(
764
+ tx,
764
765
  SalesOrder,
765
766
  { id: nextOrderId, organizationId: payment.organizationId, tenantId: payment.tenantId },
766
- { lockMode: LockMode.PESSIMISTIC_WRITE }
767
+ { lockMode: LockMode.PESSIMISTIC_WRITE },
768
+ { tenantId: payment.tenantId, organizationId: payment.organizationId }
767
769
  );
768
770
  if (!lockedOrder) return void 0;
769
771
  ensureSameScope(lockedOrder, payment.organizationId, payment.tenantId);
@@ -772,7 +774,7 @@ const updatePaymentCommand = {
772
774
  return result;
773
775
  });
774
776
  if (totals) {
775
- const nextOrder = await em.findOne(SalesOrder, { id: nextOrderId, organizationId: payment.organizationId, tenantId: payment.tenantId });
777
+ const nextOrder = await findOneWithDecryption(em, SalesOrder, { id: nextOrderId, organizationId: payment.organizationId, tenantId: payment.tenantId }, {}, { tenantId: payment.tenantId, organizationId: payment.organizationId });
776
778
  if (nextOrder) {
777
779
  ensureSameScope(nextOrder, payment.organizationId, payment.tenantId);
778
780
  await invalidateOrderCache(ctx.container, nextOrder, ctx.auth?.tenantId ?? null);
@@ -781,10 +783,12 @@ const updatePaymentCommand = {
781
783
  }
782
784
  if (previousOrder && (!nextOrderId || previousOrder.id !== nextOrderId)) {
783
785
  await em.transactional(async (tx) => {
784
- const lockedOrder = await tx.findOne(
786
+ const lockedOrder = await findOneWithDecryption(
787
+ tx,
785
788
  SalesOrder,
786
789
  { id: previousOrder.id, organizationId: payment.organizationId, tenantId: payment.tenantId },
787
- { lockMode: LockMode.PESSIMISTIC_WRITE }
790
+ { lockMode: LockMode.PESSIMISTIC_WRITE },
791
+ { tenantId: payment.tenantId, organizationId: payment.organizationId }
788
792
  );
789
793
  if (!lockedOrder) return;
790
794
  ensureSameScope(lockedOrder, payment.organizationId, payment.tenantId);
@@ -898,10 +902,12 @@ const deletePaymentCommand = {
898
902
  const primaryOrderId = order && typeof order === "object" ? order.id : null;
899
903
  for (const orderId of orderIds) {
900
904
  const recomputed = await em.transactional(async (tx) => {
901
- const lockedOrder = await tx.findOne(
905
+ const lockedOrder = await findOneWithDecryption(
906
+ tx,
902
907
  SalesOrder,
903
908
  { id: orderId, organizationId: payment.organizationId, tenantId: payment.tenantId },
904
- { lockMode: LockMode.PESSIMISTIC_WRITE }
909
+ { lockMode: LockMode.PESSIMISTIC_WRITE },
910
+ { tenantId: payment.tenantId, organizationId: payment.organizationId }
905
911
  );
906
912
  if (!lockedOrder) return void 0;
907
913
  ensureSameScope(lockedOrder, payment.organizationId, payment.tenantId);
@@ -912,7 +918,7 @@ const deletePaymentCommand = {
912
918
  if (recomputed && (!totals || primaryOrderId && orderId === primaryOrderId)) {
913
919
  totals = recomputed;
914
920
  }
915
- const target = await em.findOne(SalesOrder, { id: orderId, organizationId: payment.organizationId, tenantId: payment.tenantId });
921
+ const target = await findOneWithDecryption(em, SalesOrder, { id: orderId, organizationId: payment.organizationId, tenantId: payment.tenantId }, {}, { tenantId: payment.tenantId, organizationId: payment.organizationId });
916
922
  if (target) {
917
923
  ensureSameScope(target, payment.organizationId, payment.tenantId);
918
924
  await invalidateOrderCache(ctx.container, target, ctx.auth?.tenantId ?? null);