@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.
@@ -10,6 +10,7 @@ import { SalesDocumentAddress, SalesOrder, SalesQuote } from "../data/entities.j
10
10
  import { ensureOrganizationScope, ensureSameScope, ensureTenantScope, assertFound, extractUndoPayload } from "./shared.js";
11
11
  import { loadSalesSettings } from "./settings.js";
12
12
  import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
13
+ import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
13
14
  import { E } from "../../../generated/entities.ids.generated.js";
14
15
  const DOCUMENT_ADDRESS_ENTITY_TYPE = E.sales.sales_document_address;
15
16
  function snapshotDocumentAddress(entity) {
@@ -36,7 +37,7 @@ function snapshotDocumentAddress(entity) {
36
37
  };
37
38
  }
38
39
  async function loadDocumentAddressSnapshot(em, id) {
39
- const entity = await em.findOne(SalesDocumentAddress, { id });
40
+ const entity = await findOneWithDecryption(em, SalesDocumentAddress, { id }, {});
40
41
  return entity ? snapshotDocumentAddress(entity) : null;
41
42
  }
42
43
  function applyDocumentAddressSnapshot(em, entity, snapshot) {
@@ -83,7 +84,7 @@ async function emitDocumentAddressIndexSideEffects(ctx, action, snapshot) {
83
84
  }
84
85
  async function requireDocument(em, kind, id, organizationId, tenantId) {
85
86
  const repo = kind === "order" ? SalesOrder : SalesQuote;
86
- const doc = await em.findOne(repo, { id, organizationId, tenantId });
87
+ const doc = await findOneWithDecryption(em, repo, { id, organizationId, tenantId }, {}, { tenantId, organizationId });
87
88
  if (!doc) {
88
89
  throw new CrudHttpError(404, { error: "sales.document.not_found" });
89
90
  }
@@ -171,7 +172,7 @@ const createDocumentAddress = {
171
172
  ensureTenantScope(ctx, after.tenantId);
172
173
  ensureOrganizationScope(ctx, after.organizationId);
173
174
  const em = ctx.container.resolve("em").fork();
174
- const entity = await em.findOne(SalesDocumentAddress, { id: after.id });
175
+ const entity = await findOneWithDecryption(em, SalesDocumentAddress, { id: after.id }, {}, { tenantId: after.tenantId, organizationId: after.organizationId });
175
176
  if (!entity) return;
176
177
  await em.remove(entity).flush();
177
178
  await emitDocumentAddressIndexSideEffects(ctx, "deleted", after);
@@ -191,7 +192,7 @@ const updateDocumentAddress = {
191
192
  ensureOrganizationScope(ctx, input.organizationId);
192
193
  const em = ctx.container.resolve("em").fork();
193
194
  const entity = assertFound(
194
- await em.findOne(SalesDocumentAddress, { id: input.id }),
195
+ await findOneWithDecryption(em, SalesDocumentAddress, { id: input.id }, {}, { tenantId: input.tenantId, organizationId: input.organizationId }),
195
196
  "sales.document.address.not_found"
196
197
  );
197
198
  ensureSameScope(entity, input.organizationId, input.tenantId);
@@ -251,7 +252,7 @@ const updateDocumentAddress = {
251
252
  ensureTenantScope(ctx, before.tenantId);
252
253
  ensureOrganizationScope(ctx, before.organizationId);
253
254
  const em = ctx.container.resolve("em").fork();
254
- const entity = await em.findOne(SalesDocumentAddress, { id: before.id }) ?? em.create(SalesDocumentAddress, { id: before.id });
255
+ const entity = await findOneWithDecryption(em, SalesDocumentAddress, { id: before.id }, {}, { tenantId: before.tenantId, organizationId: before.organizationId }) ?? em.create(SalesDocumentAddress, { id: before.id });
255
256
  applyDocumentAddressSnapshot(em, entity, before);
256
257
  await em.persist(entity).flush();
257
258
  await emitDocumentAddressIndexSideEffects(ctx, "updated", before);
@@ -271,7 +272,7 @@ const deleteDocumentAddress = {
271
272
  ensureOrganizationScope(ctx, input.organizationId);
272
273
  const em = ctx.container.resolve("em").fork();
273
274
  const entity = assertFound(
274
- await em.findOne(SalesDocumentAddress, { id: input.id }),
275
+ await findOneWithDecryption(em, SalesDocumentAddress, { id: input.id }, {}, { tenantId: input.tenantId, organizationId: input.organizationId }),
275
276
  "sales.document.address.not_found"
276
277
  );
277
278
  ensureSameScope(entity, input.organizationId, input.tenantId);
@@ -309,7 +310,7 @@ const deleteDocumentAddress = {
309
310
  ensureTenantScope(ctx, before.tenantId);
310
311
  ensureOrganizationScope(ctx, before.organizationId);
311
312
  const em = ctx.container.resolve("em").fork();
312
- const existing = await em.findOne(SalesDocumentAddress, { id: before.id });
313
+ const existing = await findOneWithDecryption(em, SalesDocumentAddress, { id: before.id }, {}, { tenantId: before.tenantId, organizationId: before.organizationId });
313
314
  const entity = existing ?? em.create(SalesDocumentAddress, { id: before.id });
314
315
  applyDocumentAddressSnapshot(em, entity, before);
315
316
  await em.persist(entity).flush();
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/sales/commands/documentAddresses.ts"],
4
- "sourcesContent": ["// @ts-nocheck\n\nimport { registerCommand, type CommandHandler } from '@open-mercato/shared/lib/commands'\nimport { emitCrudSideEffects } 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 {\n documentAddressCreateSchema,\n documentAddressDeleteSchema,\n documentAddressUpdateSchema,\n type DocumentAddressCreateInput,\n type DocumentAddressDeleteInput,\n type DocumentAddressUpdateInput,\n} from '../data/validators'\nimport { SalesDocumentAddress, SalesOrder, SalesQuote } from '../data/entities'\nimport { ensureOrganizationScope, ensureSameScope, ensureTenantScope, assertFound, extractUndoPayload } from './shared'\nimport { loadSalesSettings } from './settings'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { E } from '#generated/entities.ids.generated'\n\nconst DOCUMENT_ADDRESS_ENTITY_TYPE = E.sales.sales_document_address\n\ntype DocumentAddressSnapshot = {\n id: string\n organizationId: string\n tenantId: string\n documentId: string\n documentKind: 'order' | 'quote'\n customerAddressId: string | null\n name: string | null\n purpose: string | null\n companyName: string | null\n addressLine1: string\n addressLine2: string | null\n buildingNumber: string | null\n flatNumber: string | null\n city: string | null\n region: string | null\n postalCode: string | null\n country: string | null\n latitude: number | null\n longitude: number | null\n}\n\ntype DocumentAddressUndoPayload = {\n before?: DocumentAddressSnapshot | null\n after?: DocumentAddressSnapshot | null\n}\n\nfunction snapshotDocumentAddress(entity: SalesDocumentAddress): DocumentAddressSnapshot {\n return {\n id: entity.id,\n organizationId: entity.organizationId,\n tenantId: entity.tenantId,\n documentId: entity.documentId,\n documentKind: entity.documentKind as 'order' | 'quote',\n customerAddressId: entity.customerAddressId ?? null,\n name: entity.name ?? null,\n purpose: entity.purpose ?? null,\n companyName: entity.companyName ?? null,\n addressLine1: entity.addressLine1,\n addressLine2: entity.addressLine2 ?? null,\n buildingNumber: entity.buildingNumber ?? null,\n flatNumber: entity.flatNumber ?? null,\n city: entity.city ?? null,\n region: entity.region ?? null,\n postalCode: entity.postalCode ?? null,\n country: entity.country ?? null,\n latitude: entity.latitude ?? null,\n longitude: entity.longitude ?? null,\n }\n}\n\nasync function loadDocumentAddressSnapshot(\n em: EntityManager,\n id: string\n): Promise<DocumentAddressSnapshot | null> {\n const entity = await em.findOne(SalesDocumentAddress, { id })\n return entity ? snapshotDocumentAddress(entity) : null\n}\n\nfunction applyDocumentAddressSnapshot(em: EntityManager, entity: SalesDocumentAddress, snapshot: DocumentAddressSnapshot): void {\n entity.organizationId = snapshot.organizationId\n entity.tenantId = snapshot.tenantId\n entity.documentId = snapshot.documentId\n entity.documentKind = snapshot.documentKind\n entity.customerAddressId = snapshot.customerAddressId\n entity.name = snapshot.name\n entity.purpose = snapshot.purpose\n entity.companyName = snapshot.companyName\n entity.addressLine1 = snapshot.addressLine1\n entity.addressLine2 = snapshot.addressLine2\n entity.buildingNumber = snapshot.buildingNumber\n entity.flatNumber = snapshot.flatNumber\n entity.city = snapshot.city\n entity.region = snapshot.region\n entity.postalCode = snapshot.postalCode\n entity.country = snapshot.country\n entity.latitude = snapshot.latitude\n entity.longitude = snapshot.longitude\n entity.order = snapshot.documentKind === 'order' ? em.getReference(SalesOrder, snapshot.documentId) : null\n entity.quote = snapshot.documentKind === 'quote' ? em.getReference(SalesQuote, snapshot.documentId) : null\n}\n\nasync function emitDocumentAddressIndexSideEffects(\n ctx: { container: { resolve: (name: string) => unknown } },\n action: 'created' | 'updated' | 'deleted',\n snapshot: DocumentAddressSnapshot\n): Promise<void> {\n let dataEngine: DataEngine | null = null\n try {\n dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n } catch {\n dataEngine = null\n }\n if (!dataEngine) return\n await emitCrudSideEffects({\n dataEngine,\n action,\n entity: snapshot,\n identifiers: {\n id: snapshot.id,\n organizationId: snapshot.organizationId,\n tenantId: snapshot.tenantId,\n },\n indexer: { entityType: DOCUMENT_ADDRESS_ENTITY_TYPE },\n })\n}\n\nasync function requireDocument(\n em: EntityManager,\n kind: 'order' | 'quote',\n id: string,\n organizationId: string,\n tenantId: string\n): Promise<SalesOrder | SalesQuote> {\n const repo = kind === 'order' ? SalesOrder : SalesQuote\n const doc = await em.findOne(repo, { id, organizationId, tenantId })\n if (!doc) {\n throw new CrudHttpError(404, { error: 'sales.document.not_found' })\n }\n return doc\n}\n\nasync function assertAddressEditable(\n em: EntityManager,\n params: { organizationId: string; tenantId: string; status: string | null }\n): Promise<void> {\n const settings = await loadSalesSettings(em, {\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n })\n const allowed = settings?.orderAddressEditableStatuses ?? null\n if (!Array.isArray(allowed)) return\n const { translate } = await resolveTranslations()\n if (allowed.length === 0) {\n throw new CrudHttpError(400, { error: translate('sales.orders.edit_addresses_blocked', 'Addresses cannot be changed for the current status.') })\n }\n if (!params.status || !allowed.includes(params.status)) {\n throw new CrudHttpError(400, { error: translate('sales.orders.edit_addresses_blocked', 'Addresses cannot be changed for the current status.') })\n }\n}\n\nconst createDocumentAddress: CommandHandler<DocumentAddressCreateInput, { id: string }> = {\n id: 'sales.document-addresses.create',\n async execute(rawInput, ctx) {\n const input = documentAddressCreateSchema.parse(rawInput)\n ensureTenantScope(ctx, input.tenantId)\n ensureOrganizationScope(ctx, input.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const document = await requireDocument(em, input.documentKind, input.documentId, input.organizationId, input.tenantId)\n if (input.documentKind === 'order') {\n await assertAddressEditable(em, {\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n status: (document as SalesOrder).status ?? null,\n })\n }\n\n const entity = em.create(SalesDocumentAddress, {\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n documentId: input.documentId,\n documentKind: input.documentKind,\n customerAddressId: input.customerAddressId ?? null,\n name: input.name ?? null,\n purpose: input.purpose ?? null,\n companyName: input.companyName ?? null,\n addressLine1: input.addressLine1,\n addressLine2: input.addressLine2 ?? null,\n buildingNumber: input.buildingNumber ?? null,\n flatNumber: input.flatNumber ?? null,\n city: input.city ?? null,\n region: input.region ?? null,\n postalCode: input.postalCode ?? null,\n country: input.country ?? null,\n latitude: input.latitude ?? null,\n longitude: input.longitude ?? null,\n order: input.documentKind === 'order' ? (document as SalesOrder) : null,\n quote: input.documentKind === 'quote' ? (document as SalesQuote) : null,\n })\n await em.persist(entity).flush()\n return { id: entity.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return result?.id ? loadDocumentAddressSnapshot(em, result.id) : null\n },\n buildLog: async ({ result, snapshots }) => {\n const after = snapshots.after as DocumentAddressSnapshot | undefined\n if (!after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('sales.audit.document_addresses.create', 'Add document address'),\n resourceKind: 'sales.document_address',\n resourceId: result.id,\n parentResourceKind: after.documentKind === 'order' ? 'sales.order' : 'sales.quote',\n parentResourceId: after.documentId,\n tenantId: after.tenantId,\n organizationId: after.organizationId,\n snapshotAfter: after,\n payload: { undo: { after } satisfies DocumentAddressUndoPayload },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<DocumentAddressUndoPayload>(logEntry)\n const after = payload?.after\n if (!after) return\n ensureTenantScope(ctx, after.tenantId)\n ensureOrganizationScope(ctx, after.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const entity = await em.findOne(SalesDocumentAddress, { id: after.id })\n if (!entity) return\n await em.remove(entity).flush()\n await emitDocumentAddressIndexSideEffects(ctx, 'deleted', after)\n },\n}\n\nconst updateDocumentAddress: CommandHandler<DocumentAddressUpdateInput, { id: string }> = {\n id: 'sales.document-addresses.update',\n async prepare(rawInput, ctx) {\n const parsed = documentAddressUpdateSchema.parse(rawInput)\n const em = ctx.container.resolve('em') as EntityManager\n const snapshot = await loadDocumentAddressSnapshot(em, parsed.id)\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const input = documentAddressUpdateSchema.parse(rawInput)\n ensureTenantScope(ctx, input.tenantId)\n ensureOrganizationScope(ctx, input.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const entity = assertFound(\n await em.findOne(SalesDocumentAddress, { id: input.id }),\n 'sales.document.address.not_found'\n )\n ensureSameScope(entity, input.organizationId, input.tenantId)\n const document = await requireDocument(em, input.documentKind, input.documentId, input.organizationId, input.tenantId)\n if (input.documentKind === 'order') {\n await assertAddressEditable(em, {\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n status: (document as SalesOrder).status ?? null,\n })\n }\n\n entity.documentId = input.documentId\n entity.documentKind = input.documentKind\n entity.customerAddressId = input.customerAddressId ?? null\n entity.name = input.name ?? null\n entity.purpose = input.purpose ?? null\n entity.companyName = input.companyName ?? null\n entity.addressLine1 = input.addressLine1\n entity.addressLine2 = input.addressLine2 ?? null\n entity.buildingNumber = input.buildingNumber ?? null\n entity.flatNumber = input.flatNumber ?? null\n entity.city = input.city ?? null\n entity.region = input.region ?? null\n entity.postalCode = input.postalCode ?? null\n entity.country = input.country ?? null\n entity.latitude = input.latitude ?? null\n entity.longitude = input.longitude ?? null\n\n await em.flush()\n return { id: entity.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return result?.id ? loadDocumentAddressSnapshot(em, result.id) : null\n },\n buildLog: async ({ result, snapshots }) => {\n const before = snapshots.before as DocumentAddressSnapshot | undefined\n const after = snapshots.after as DocumentAddressSnapshot | undefined\n if (!after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('sales.audit.document_addresses.update', 'Update document address'),\n resourceKind: 'sales.document_address',\n resourceId: result.id,\n parentResourceKind: after.documentKind === 'order' ? 'sales.order' : 'sales.quote',\n parentResourceId: after.documentId,\n tenantId: after.tenantId,\n organizationId: after.organizationId,\n snapshotBefore: before ?? null,\n snapshotAfter: after,\n payload: { undo: { before: before ?? null, after } satisfies DocumentAddressUndoPayload },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<DocumentAddressUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n ensureTenantScope(ctx, before.tenantId)\n ensureOrganizationScope(ctx, before.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const entity =\n (await em.findOne(SalesDocumentAddress, { id: before.id })) ??\n em.create(SalesDocumentAddress, { id: before.id } as Partial<SalesDocumentAddress>)\n applyDocumentAddressSnapshot(em, entity, before)\n await em.persist(entity).flush()\n await emitDocumentAddressIndexSideEffects(ctx, 'updated', before)\n },\n}\n\nconst deleteDocumentAddress: CommandHandler<\n DocumentAddressDeleteInput,\n { ok: true; id: string }\n> = {\n id: 'sales.document-addresses.delete',\n async prepare(rawInput, ctx) {\n const parsed = documentAddressDeleteSchema.parse(rawInput)\n const em = ctx.container.resolve('em') as EntityManager\n const snapshot = await loadDocumentAddressSnapshot(em, parsed.id)\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const input = documentAddressDeleteSchema.parse(rawInput)\n ensureTenantScope(ctx, input.tenantId)\n ensureOrganizationScope(ctx, input.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const entity = assertFound(\n await em.findOne(SalesDocumentAddress, { id: input.id }),\n 'sales.document.address.not_found'\n )\n ensureSameScope(entity, input.organizationId, input.tenantId)\n const document = await requireDocument(em, input.documentKind, input.documentId, input.organizationId, input.tenantId)\n if (input.documentKind === 'order') {\n await assertAddressEditable(em, {\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n status: (document as SalesOrder).status ?? null,\n })\n }\n await em.remove(entity).flush()\n return { ok: true, id: input.id }\n },\n buildLog: async ({ result, snapshots }) => {\n const before = snapshots.before as DocumentAddressSnapshot | undefined\n if (!before) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('sales.audit.document_addresses.delete', 'Remove document address'),\n resourceKind: 'sales.document_address',\n resourceId: result.id,\n parentResourceKind: before.documentKind === 'order' ? 'sales.order' : 'sales.quote',\n parentResourceId: before.documentId,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n payload: { undo: { before } satisfies DocumentAddressUndoPayload },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<DocumentAddressUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n ensureTenantScope(ctx, before.tenantId)\n ensureOrganizationScope(ctx, before.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const existing = await em.findOne(SalesDocumentAddress, { id: before.id })\n const entity = existing ?? em.create(SalesDocumentAddress, { id: before.id } as Partial<SalesDocumentAddress>)\n applyDocumentAddressSnapshot(em, entity, before)\n await em.persist(entity).flush()\n await emitDocumentAddressIndexSideEffects(ctx, 'created', before)\n },\n}\n\nexport const documentAddressCommands = [createDocumentAddress, updateDocumentAddress, deleteDocumentAddress]\n\nregisterCommand(createDocumentAddress)\nregisterCommand(updateDocumentAddress)\nregisterCommand(deleteDocumentAddress)\n"],
5
- "mappings": "AAEA,SAAS,uBAA4C;AACrD,SAAS,2BAA2B;AAGpC,SAAS,qBAAqB;AAC9B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AACP,SAAS,sBAAsB,YAAY,kBAAkB;AAC7D,SAAS,yBAAyB,iBAAiB,mBAAmB,aAAa,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",
4
+ "sourcesContent": ["// @ts-nocheck\n\nimport { registerCommand, type CommandHandler } from '@open-mercato/shared/lib/commands'\nimport { emitCrudSideEffects } 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 {\n documentAddressCreateSchema,\n documentAddressDeleteSchema,\n documentAddressUpdateSchema,\n type DocumentAddressCreateInput,\n type DocumentAddressDeleteInput,\n type DocumentAddressUpdateInput,\n} from '../data/validators'\nimport { SalesDocumentAddress, SalesOrder, SalesQuote } from '../data/entities'\nimport { ensureOrganizationScope, ensureSameScope, ensureTenantScope, assertFound, extractUndoPayload } from './shared'\nimport { loadSalesSettings } from './settings'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { E } from '#generated/entities.ids.generated'\n\nconst DOCUMENT_ADDRESS_ENTITY_TYPE = E.sales.sales_document_address\n\ntype DocumentAddressSnapshot = {\n id: string\n organizationId: string\n tenantId: string\n documentId: string\n documentKind: 'order' | 'quote'\n customerAddressId: string | null\n name: string | null\n purpose: string | null\n companyName: string | null\n addressLine1: string\n addressLine2: string | null\n buildingNumber: string | null\n flatNumber: string | null\n city: string | null\n region: string | null\n postalCode: string | null\n country: string | null\n latitude: number | null\n longitude: number | null\n}\n\ntype DocumentAddressUndoPayload = {\n before?: DocumentAddressSnapshot | null\n after?: DocumentAddressSnapshot | null\n}\n\nfunction snapshotDocumentAddress(entity: SalesDocumentAddress): DocumentAddressSnapshot {\n return {\n id: entity.id,\n organizationId: entity.organizationId,\n tenantId: entity.tenantId,\n documentId: entity.documentId,\n documentKind: entity.documentKind as 'order' | 'quote',\n customerAddressId: entity.customerAddressId ?? null,\n name: entity.name ?? null,\n purpose: entity.purpose ?? null,\n companyName: entity.companyName ?? null,\n addressLine1: entity.addressLine1,\n addressLine2: entity.addressLine2 ?? null,\n buildingNumber: entity.buildingNumber ?? null,\n flatNumber: entity.flatNumber ?? null,\n city: entity.city ?? null,\n region: entity.region ?? null,\n postalCode: entity.postalCode ?? null,\n country: entity.country ?? null,\n latitude: entity.latitude ?? null,\n longitude: entity.longitude ?? null,\n }\n}\n\nasync function loadDocumentAddressSnapshot(\n em: EntityManager,\n id: string\n): Promise<DocumentAddressSnapshot | null> {\n const entity = await findOneWithDecryption(em, SalesDocumentAddress, { id }, {})\n return entity ? snapshotDocumentAddress(entity) : null\n}\n\nfunction applyDocumentAddressSnapshot(em: EntityManager, entity: SalesDocumentAddress, snapshot: DocumentAddressSnapshot): void {\n entity.organizationId = snapshot.organizationId\n entity.tenantId = snapshot.tenantId\n entity.documentId = snapshot.documentId\n entity.documentKind = snapshot.documentKind\n entity.customerAddressId = snapshot.customerAddressId\n entity.name = snapshot.name\n entity.purpose = snapshot.purpose\n entity.companyName = snapshot.companyName\n entity.addressLine1 = snapshot.addressLine1\n entity.addressLine2 = snapshot.addressLine2\n entity.buildingNumber = snapshot.buildingNumber\n entity.flatNumber = snapshot.flatNumber\n entity.city = snapshot.city\n entity.region = snapshot.region\n entity.postalCode = snapshot.postalCode\n entity.country = snapshot.country\n entity.latitude = snapshot.latitude\n entity.longitude = snapshot.longitude\n entity.order = snapshot.documentKind === 'order' ? em.getReference(SalesOrder, snapshot.documentId) : null\n entity.quote = snapshot.documentKind === 'quote' ? em.getReference(SalesQuote, snapshot.documentId) : null\n}\n\nasync function emitDocumentAddressIndexSideEffects(\n ctx: { container: { resolve: (name: string) => unknown } },\n action: 'created' | 'updated' | 'deleted',\n snapshot: DocumentAddressSnapshot\n): Promise<void> {\n let dataEngine: DataEngine | null = null\n try {\n dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n } catch {\n dataEngine = null\n }\n if (!dataEngine) return\n await emitCrudSideEffects({\n dataEngine,\n action,\n entity: snapshot,\n identifiers: {\n id: snapshot.id,\n organizationId: snapshot.organizationId,\n tenantId: snapshot.tenantId,\n },\n indexer: { entityType: DOCUMENT_ADDRESS_ENTITY_TYPE },\n })\n}\n\nasync function requireDocument(\n em: EntityManager,\n kind: 'order' | 'quote',\n id: string,\n organizationId: string,\n tenantId: string\n): Promise<SalesOrder | SalesQuote> {\n const repo = kind === 'order' ? SalesOrder : SalesQuote\n const doc = await findOneWithDecryption(em, repo, { id, organizationId, tenantId }, {}, { tenantId, organizationId })\n if (!doc) {\n throw new CrudHttpError(404, { error: 'sales.document.not_found' })\n }\n return doc\n}\n\nasync function assertAddressEditable(\n em: EntityManager,\n params: { organizationId: string; tenantId: string; status: string | null }\n): Promise<void> {\n const settings = await loadSalesSettings(em, {\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n })\n const allowed = settings?.orderAddressEditableStatuses ?? null\n if (!Array.isArray(allowed)) return\n const { translate } = await resolveTranslations()\n if (allowed.length === 0) {\n throw new CrudHttpError(400, { error: translate('sales.orders.edit_addresses_blocked', 'Addresses cannot be changed for the current status.') })\n }\n if (!params.status || !allowed.includes(params.status)) {\n throw new CrudHttpError(400, { error: translate('sales.orders.edit_addresses_blocked', 'Addresses cannot be changed for the current status.') })\n }\n}\n\nconst createDocumentAddress: CommandHandler<DocumentAddressCreateInput, { id: string }> = {\n id: 'sales.document-addresses.create',\n async execute(rawInput, ctx) {\n const input = documentAddressCreateSchema.parse(rawInput)\n ensureTenantScope(ctx, input.tenantId)\n ensureOrganizationScope(ctx, input.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const document = await requireDocument(em, input.documentKind, input.documentId, input.organizationId, input.tenantId)\n if (input.documentKind === 'order') {\n await assertAddressEditable(em, {\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n status: (document as SalesOrder).status ?? null,\n })\n }\n\n const entity = em.create(SalesDocumentAddress, {\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n documentId: input.documentId,\n documentKind: input.documentKind,\n customerAddressId: input.customerAddressId ?? null,\n name: input.name ?? null,\n purpose: input.purpose ?? null,\n companyName: input.companyName ?? null,\n addressLine1: input.addressLine1,\n addressLine2: input.addressLine2 ?? null,\n buildingNumber: input.buildingNumber ?? null,\n flatNumber: input.flatNumber ?? null,\n city: input.city ?? null,\n region: input.region ?? null,\n postalCode: input.postalCode ?? null,\n country: input.country ?? null,\n latitude: input.latitude ?? null,\n longitude: input.longitude ?? null,\n order: input.documentKind === 'order' ? (document as SalesOrder) : null,\n quote: input.documentKind === 'quote' ? (document as SalesQuote) : null,\n })\n await em.persist(entity).flush()\n return { id: entity.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return result?.id ? loadDocumentAddressSnapshot(em, result.id) : null\n },\n buildLog: async ({ result, snapshots }) => {\n const after = snapshots.after as DocumentAddressSnapshot | undefined\n if (!after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('sales.audit.document_addresses.create', 'Add document address'),\n resourceKind: 'sales.document_address',\n resourceId: result.id,\n parentResourceKind: after.documentKind === 'order' ? 'sales.order' : 'sales.quote',\n parentResourceId: after.documentId,\n tenantId: after.tenantId,\n organizationId: after.organizationId,\n snapshotAfter: after,\n payload: { undo: { after } satisfies DocumentAddressUndoPayload },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<DocumentAddressUndoPayload>(logEntry)\n const after = payload?.after\n if (!after) return\n ensureTenantScope(ctx, after.tenantId)\n ensureOrganizationScope(ctx, after.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const entity = await findOneWithDecryption(em, SalesDocumentAddress, { id: after.id }, {}, { tenantId: after.tenantId, organizationId: after.organizationId })\n if (!entity) return\n await em.remove(entity).flush()\n await emitDocumentAddressIndexSideEffects(ctx, 'deleted', after)\n },\n}\n\nconst updateDocumentAddress: CommandHandler<DocumentAddressUpdateInput, { id: string }> = {\n id: 'sales.document-addresses.update',\n async prepare(rawInput, ctx) {\n const parsed = documentAddressUpdateSchema.parse(rawInput)\n const em = ctx.container.resolve('em') as EntityManager\n const snapshot = await loadDocumentAddressSnapshot(em, parsed.id)\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const input = documentAddressUpdateSchema.parse(rawInput)\n ensureTenantScope(ctx, input.tenantId)\n ensureOrganizationScope(ctx, input.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const entity = assertFound(\n await findOneWithDecryption(em, SalesDocumentAddress, { id: input.id }, {}, { tenantId: input.tenantId, organizationId: input.organizationId }),\n 'sales.document.address.not_found'\n )\n ensureSameScope(entity, input.organizationId, input.tenantId)\n const document = await requireDocument(em, input.documentKind, input.documentId, input.organizationId, input.tenantId)\n if (input.documentKind === 'order') {\n await assertAddressEditable(em, {\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n status: (document as SalesOrder).status ?? null,\n })\n }\n\n entity.documentId = input.documentId\n entity.documentKind = input.documentKind\n entity.customerAddressId = input.customerAddressId ?? null\n entity.name = input.name ?? null\n entity.purpose = input.purpose ?? null\n entity.companyName = input.companyName ?? null\n entity.addressLine1 = input.addressLine1\n entity.addressLine2 = input.addressLine2 ?? null\n entity.buildingNumber = input.buildingNumber ?? null\n entity.flatNumber = input.flatNumber ?? null\n entity.city = input.city ?? null\n entity.region = input.region ?? null\n entity.postalCode = input.postalCode ?? null\n entity.country = input.country ?? null\n entity.latitude = input.latitude ?? null\n entity.longitude = input.longitude ?? null\n\n await em.flush()\n return { id: entity.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return result?.id ? loadDocumentAddressSnapshot(em, result.id) : null\n },\n buildLog: async ({ result, snapshots }) => {\n const before = snapshots.before as DocumentAddressSnapshot | undefined\n const after = snapshots.after as DocumentAddressSnapshot | undefined\n if (!after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('sales.audit.document_addresses.update', 'Update document address'),\n resourceKind: 'sales.document_address',\n resourceId: result.id,\n parentResourceKind: after.documentKind === 'order' ? 'sales.order' : 'sales.quote',\n parentResourceId: after.documentId,\n tenantId: after.tenantId,\n organizationId: after.organizationId,\n snapshotBefore: before ?? null,\n snapshotAfter: after,\n payload: { undo: { before: before ?? null, after } satisfies DocumentAddressUndoPayload },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<DocumentAddressUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n ensureTenantScope(ctx, before.tenantId)\n ensureOrganizationScope(ctx, before.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const entity =\n (await findOneWithDecryption(em, SalesDocumentAddress, { id: before.id }, {}, { tenantId: before.tenantId, organizationId: before.organizationId })) ??\n em.create(SalesDocumentAddress, { id: before.id } as Partial<SalesDocumentAddress>)\n applyDocumentAddressSnapshot(em, entity, before)\n await em.persist(entity).flush()\n await emitDocumentAddressIndexSideEffects(ctx, 'updated', before)\n },\n}\n\nconst deleteDocumentAddress: CommandHandler<\n DocumentAddressDeleteInput,\n { ok: true; id: string }\n> = {\n id: 'sales.document-addresses.delete',\n async prepare(rawInput, ctx) {\n const parsed = documentAddressDeleteSchema.parse(rawInput)\n const em = ctx.container.resolve('em') as EntityManager\n const snapshot = await loadDocumentAddressSnapshot(em, parsed.id)\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const input = documentAddressDeleteSchema.parse(rawInput)\n ensureTenantScope(ctx, input.tenantId)\n ensureOrganizationScope(ctx, input.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const entity = assertFound(\n await findOneWithDecryption(em, SalesDocumentAddress, { id: input.id }, {}, { tenantId: input.tenantId, organizationId: input.organizationId }),\n 'sales.document.address.not_found'\n )\n ensureSameScope(entity, input.organizationId, input.tenantId)\n const document = await requireDocument(em, input.documentKind, input.documentId, input.organizationId, input.tenantId)\n if (input.documentKind === 'order') {\n await assertAddressEditable(em, {\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n status: (document as SalesOrder).status ?? null,\n })\n }\n await em.remove(entity).flush()\n return { ok: true, id: input.id }\n },\n buildLog: async ({ result, snapshots }) => {\n const before = snapshots.before as DocumentAddressSnapshot | undefined\n if (!before) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('sales.audit.document_addresses.delete', 'Remove document address'),\n resourceKind: 'sales.document_address',\n resourceId: result.id,\n parentResourceKind: before.documentKind === 'order' ? 'sales.order' : 'sales.quote',\n parentResourceId: before.documentId,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n payload: { undo: { before } satisfies DocumentAddressUndoPayload },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<DocumentAddressUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n ensureTenantScope(ctx, before.tenantId)\n ensureOrganizationScope(ctx, before.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const existing = await findOneWithDecryption(em, SalesDocumentAddress, { id: before.id }, {}, { tenantId: before.tenantId, organizationId: before.organizationId })\n const entity = existing ?? em.create(SalesDocumentAddress, { id: before.id } as Partial<SalesDocumentAddress>)\n applyDocumentAddressSnapshot(em, entity, before)\n await em.persist(entity).flush()\n await emitDocumentAddressIndexSideEffects(ctx, 'created', before)\n },\n}\n\nexport const documentAddressCommands = [createDocumentAddress, updateDocumentAddress, deleteDocumentAddress]\n\nregisterCommand(createDocumentAddress)\nregisterCommand(updateDocumentAddress)\nregisterCommand(deleteDocumentAddress)\n"],
5
+ "mappings": "AAEA,SAAS,uBAA4C;AACrD,SAAS,2BAA2B;AAGpC,SAAS,qBAAqB;AAC9B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AACP,SAAS,sBAAsB,YAAY,kBAAkB;AAC7D,SAAS,yBAAyB,iBAAiB,mBAAmB,aAAa,0BAA0B;AAC7G,SAAS,yBAAyB;AAClC,SAAS,2BAA2B;AACpC,SAAS,6BAA6B;AACtC,SAAS,SAAS;AAElB,MAAM,+BAA+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AAE,IAAI,OAAO,GAAG,CAAkC;AAC7G,iCAA6B,IAAI,QAAQ,MAAM;AAC/C,UAAM,GAAG,QAAQ,MAAM,EAAE,MAAM;AAC/B,UAAM,oCAAoC,KAAK,WAAW,MAAM;AAAA,EAClE;AACF;AAEO,MAAM,0BAA0B,CAAC,uBAAuB,uBAAuB,qBAAqB;AAE3G,gBAAgB,qBAAqB;AACrC,gBAAgB,qBAAqB;AACrC,gBAAgB,qBAAqB;",
6
6
  "names": []
7
7
  }
@@ -456,12 +456,18 @@ async function applyDocumentUpdate({
456
456
  if (input.channelId === null) {
457
457
  entity.channelId = null;
458
458
  } else {
459
- const channel = await em.findOne(SalesChannel, {
460
- id: input.channelId,
461
- organizationId,
462
- tenantId,
463
- deletedAt: null
464
- });
459
+ const channel = await findOneWithDecryption(
460
+ em,
461
+ SalesChannel,
462
+ {
463
+ id: input.channelId,
464
+ organizationId,
465
+ tenantId,
466
+ deletedAt: null
467
+ },
468
+ {},
469
+ { tenantId, organizationId }
470
+ );
465
471
  if (!channel) {
466
472
  throw new CrudHttpError(400, {
467
473
  error: translate(
@@ -2518,7 +2524,16 @@ function applyOrderSnapshot(order, snapshot) {
2518
2524
  order.lineItemCount = snapshot.lineItemCount;
2519
2525
  }
2520
2526
  async function restoreQuoteGraph(em, snapshot) {
2521
- let quote = await em.findOne(SalesQuote, { id: snapshot.quote.id });
2527
+ let quote = await findOneWithDecryption(
2528
+ em,
2529
+ SalesQuote,
2530
+ { id: snapshot.quote.id },
2531
+ {},
2532
+ {
2533
+ tenantId: snapshot.quote.tenantId,
2534
+ organizationId: snapshot.quote.organizationId
2535
+ }
2536
+ );
2522
2537
  if (!quote) {
2523
2538
  quote = em.create(SalesQuote, {
2524
2539
  id: snapshot.quote.id,
@@ -2771,7 +2786,16 @@ async function restoreQuoteGraph(em, snapshot) {
2771
2786
  return quote;
2772
2787
  }
2773
2788
  async function restoreOrderGraph(em, snapshot) {
2774
- let order = await em.findOne(SalesOrder, { id: snapshot.order.id });
2789
+ let order = await findOneWithDecryption(
2790
+ em,
2791
+ SalesOrder,
2792
+ { id: snapshot.order.id },
2793
+ {},
2794
+ {
2795
+ tenantId: snapshot.order.tenantId,
2796
+ organizationId: snapshot.order.organizationId
2797
+ }
2798
+ );
2775
2799
  if (!order) {
2776
2800
  order = em.create(SalesOrder, {
2777
2801
  id: snapshot.order.id,
@@ -3357,7 +3381,16 @@ const createQuoteCommand = {
3357
3381
  const after = payload?.after;
3358
3382
  if (!after) return;
3359
3383
  const em = ctx.container.resolve("em").fork();
3360
- const quote = await em.findOne(SalesQuote, { id: after.quote.id });
3384
+ const quote = await findOneWithDecryption(
3385
+ em,
3386
+ SalesQuote,
3387
+ { id: after.quote.id },
3388
+ {},
3389
+ {
3390
+ tenantId: after.quote.tenantId,
3391
+ organizationId: after.quote.organizationId
3392
+ }
3393
+ );
3361
3394
  if (!quote) return;
3362
3395
  ensureQuoteScope(ctx, quote.organizationId, quote.tenantId);
3363
3396
  await em.nativeDelete(SalesQuoteAdjustment, { quote: quote.id });
@@ -3384,7 +3417,7 @@ const deleteQuoteCommand = {
3384
3417
  async execute(input, ctx) {
3385
3418
  const id = requireId(input, "Quote id is required");
3386
3419
  const em = ctx.container.resolve("em").fork();
3387
- const quote = await em.findOne(SalesQuote, { id });
3420
+ const quote = await findOneWithDecryption(em, SalesQuote, { id });
3388
3421
  if (!quote)
3389
3422
  throw new CrudHttpError(404, { error: "Sales quote not found" });
3390
3423
  ensureQuoteScope(ctx, quote.organizationId, quote.tenantId);
@@ -3505,7 +3538,7 @@ const updateQuoteCommand = {
3505
3538
  async execute(rawInput, ctx) {
3506
3539
  const parsed = documentUpdateSchema.parse(rawInput ?? {});
3507
3540
  const em = ctx.container.resolve("em").fork();
3508
- const quote = await em.findOne(SalesQuote, {
3541
+ const quote = await findOneWithDecryption(em, SalesQuote, {
3509
3542
  id: parsed.id,
3510
3543
  deletedAt: null
3511
3544
  });
@@ -3717,7 +3750,7 @@ const updateOrderCommand = {
3717
3750
  async execute(rawInput, ctx) {
3718
3751
  const parsed = documentUpdateSchema.parse(rawInput ?? {});
3719
3752
  const em = ctx.container.resolve("em").fork();
3720
- const order = await em.findOne(SalesOrder, {
3753
+ const order = await findOneWithDecryption(em, SalesOrder, {
3721
3754
  id: parsed.id,
3722
3755
  deletedAt: null
3723
3756
  });
@@ -4233,7 +4266,16 @@ const createOrderCommand = {
4233
4266
  const after = payload?.after;
4234
4267
  if (!after) return;
4235
4268
  const em = ctx.container.resolve("em").fork();
4236
- const order = await em.findOne(SalesOrder, { id: after.order.id });
4269
+ const order = await findOneWithDecryption(
4270
+ em,
4271
+ SalesOrder,
4272
+ { id: after.order.id },
4273
+ {},
4274
+ {
4275
+ tenantId: after.order.tenantId,
4276
+ organizationId: after.order.organizationId
4277
+ }
4278
+ );
4237
4279
  if (!order) return;
4238
4280
  ensureOrderScope(ctx, order.organizationId, order.tenantId);
4239
4281
  await em.nativeDelete(SalesOrderAdjustment, { order: order.id });
@@ -4260,7 +4302,7 @@ const deleteOrderCommand = {
4260
4302
  async execute(input, ctx) {
4261
4303
  const id = requireId(input, "Order id is required");
4262
4304
  const em = ctx.container.resolve("em").fork();
4263
- const order = await em.findOne(SalesOrder, { id });
4305
+ const order = await findOneWithDecryption(em, SalesOrder, { id });
4264
4306
  if (!order)
4265
4307
  throw new CrudHttpError(404, { error: "Sales order not found" });
4266
4308
  ensureOrderScope(ctx, order.organizationId, order.tenantId);
@@ -4631,14 +4673,32 @@ const convertQuoteToOrderCommand = {
4631
4673
  em.persist(entity);
4632
4674
  });
4633
4675
  const [addresses, notes, tags] = await Promise.all([
4634
- em.find(SalesDocumentAddress, {
4635
- documentId: snapshot.quote.id,
4636
- documentKind: "quote"
4637
- }),
4638
- em.find(SalesNote, {
4639
- contextType: "quote",
4640
- contextId: snapshot.quote.id
4641
- }),
4676
+ findWithDecryption(
4677
+ em,
4678
+ SalesDocumentAddress,
4679
+ {
4680
+ documentId: snapshot.quote.id,
4681
+ documentKind: "quote"
4682
+ },
4683
+ {},
4684
+ {
4685
+ tenantId: snapshot.quote.tenantId,
4686
+ organizationId: snapshot.quote.organizationId
4687
+ }
4688
+ ),
4689
+ findWithDecryption(
4690
+ em,
4691
+ SalesNote,
4692
+ {
4693
+ contextType: "quote",
4694
+ contextId: snapshot.quote.id
4695
+ },
4696
+ {},
4697
+ {
4698
+ tenantId: snapshot.quote.tenantId,
4699
+ organizationId: snapshot.quote.organizationId
4700
+ }
4701
+ ),
4642
4702
  em.find(SalesDocumentTagAssignment, {
4643
4703
  documentId: snapshot.quote.id,
4644
4704
  documentKind: "quote"
@@ -4833,7 +4893,7 @@ const orderLineUpsertCommand = {
4833
4893
  const rawBody = input?.body ?? {};
4834
4894
  const parsed = orderLineUpsertSchema.parse(rawBody);
4835
4895
  const em = ctx.container.resolve("em").fork();
4836
- const order = await em.findOne(SalesOrder, {
4896
+ const order = await findOneWithDecryption(em, SalesOrder, {
4837
4897
  id: parsed.orderId,
4838
4898
  deletedAt: null
4839
4899
  });
@@ -5085,7 +5145,7 @@ const orderLineDeleteCommand = {
5085
5145
  input?.body ?? {}
5086
5146
  );
5087
5147
  const em = ctx.container.resolve("em").fork();
5088
- const order = await em.findOne(SalesOrder, {
5148
+ const order = await findOneWithDecryption(em, SalesOrder, {
5089
5149
  id: parsed.orderId,
5090
5150
  deletedAt: null
5091
5151
  });
@@ -5250,7 +5310,7 @@ const quoteLineUpsertCommand = {
5250
5310
  const rawBody = input?.body ?? {};
5251
5311
  const parsed = quoteLineUpsertSchema.parse(rawBody);
5252
5312
  const em = ctx.container.resolve("em").fork();
5253
- const quote = await em.findOne(SalesQuote, {
5313
+ const quote = await findOneWithDecryption(em, SalesQuote, {
5254
5314
  id: parsed.quoteId,
5255
5315
  deletedAt: null
5256
5316
  });
@@ -5500,7 +5560,7 @@ const quoteLineDeleteCommand = {
5500
5560
  input?.body ?? {}
5501
5561
  );
5502
5562
  const em = ctx.container.resolve("em").fork();
5503
- const quote = await em.findOne(SalesQuote, {
5563
+ const quote = await findOneWithDecryption(em, SalesQuote, {
5504
5564
  id: parsed.quoteId,
5505
5565
  deletedAt: null
5506
5566
  });
@@ -5643,7 +5703,7 @@ const orderAdjustmentUpsertCommand = {
5643
5703
  input?.body ?? {}
5644
5704
  );
5645
5705
  const em = ctx.container.resolve("em").fork();
5646
- const order = await em.findOne(SalesOrder, {
5706
+ const order = await findOneWithDecryption(em, SalesOrder, {
5647
5707
  id: parsed.orderId,
5648
5708
  deletedAt: null
5649
5709
  });
@@ -5896,7 +5956,7 @@ const orderAdjustmentDeleteCommand = {
5896
5956
  input?.body ?? {}
5897
5957
  );
5898
5958
  const em = ctx.container.resolve("em").fork();
5899
- const order = await em.findOne(SalesOrder, {
5959
+ const order = await findOneWithDecryption(em, SalesOrder, {
5900
5960
  id: parsed.orderId,
5901
5961
  deletedAt: null
5902
5962
  });
@@ -6053,7 +6113,7 @@ const quoteAdjustmentUpsertCommand = {
6053
6113
  input?.body ?? {}
6054
6114
  );
6055
6115
  const em = ctx.container.resolve("em").fork();
6056
- const quote = await em.findOne(SalesQuote, {
6116
+ const quote = await findOneWithDecryption(em, SalesQuote, {
6057
6117
  id: parsed.quoteId,
6058
6118
  deletedAt: null
6059
6119
  });
@@ -6304,7 +6364,7 @@ const quoteAdjustmentDeleteCommand = {
6304
6364
  input?.body ?? {}
6305
6365
  );
6306
6366
  const em = ctx.container.resolve("em").fork();
6307
- const quote = await em.findOne(SalesQuote, {
6367
+ const quote = await findOneWithDecryption(em, SalesQuote, {
6308
6368
  id: parsed.quoteId,
6309
6369
  deletedAt: null
6310
6370
  });
@@ -6475,12 +6535,21 @@ const createInvoiceCommand = {
6475
6535
  { tenantId: parsed.tenantId }
6476
6536
  );
6477
6537
  if (parsed.orderId) {
6478
- const orderExists = await em.findOne(SalesOrder, {
6479
- id: parsed.orderId,
6480
- organizationId: parsed.organizationId,
6481
- tenantId: parsed.tenantId,
6482
- deletedAt: null
6483
- });
6538
+ const orderExists = await findOneWithDecryption(
6539
+ em,
6540
+ SalesOrder,
6541
+ {
6542
+ id: parsed.orderId,
6543
+ organizationId: parsed.organizationId,
6544
+ tenantId: parsed.tenantId,
6545
+ deletedAt: null
6546
+ },
6547
+ {},
6548
+ {
6549
+ tenantId: parsed.tenantId,
6550
+ organizationId: parsed.organizationId
6551
+ }
6552
+ );
6484
6553
  if (!orderExists) {
6485
6554
  throw new CrudHttpError(400, { error: "Referenced order not found in current scope." });
6486
6555
  }
@@ -6921,12 +6990,21 @@ const createCreditMemoCommand = {
6921
6990
  { tenantId: parsed.tenantId }
6922
6991
  );
6923
6992
  if (parsed.orderId) {
6924
- const orderExists = await em.findOne(SalesOrder, {
6925
- id: parsed.orderId,
6926
- organizationId: parsed.organizationId,
6927
- tenantId: parsed.tenantId,
6928
- deletedAt: null
6929
- });
6993
+ const orderExists = await findOneWithDecryption(
6994
+ em,
6995
+ SalesOrder,
6996
+ {
6997
+ id: parsed.orderId,
6998
+ organizationId: parsed.organizationId,
6999
+ tenantId: parsed.tenantId,
7000
+ deletedAt: null
7001
+ },
7002
+ {},
7003
+ {
7004
+ tenantId: parsed.tenantId,
7005
+ organizationId: parsed.organizationId
7006
+ }
7007
+ );
6930
7008
  if (!orderExists) {
6931
7009
  throw new CrudHttpError(400, { error: "Referenced order not found in current scope." });
6932
7010
  }