@open-mercato/core 0.10.1-develop.7781.1.079f42c863 → 0.10.1-develop.7782.1.91d78919ba

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.
@@ -196,6 +196,19 @@ function resolveAmountInputFromParsed(parsed) {
196
196
  }
197
197
  return null;
198
198
  }
199
+ function hasAmountChanged(incoming, current) {
200
+ if (typeof incoming !== "number" || !Number.isFinite(incoming)) return false;
201
+ return numericStringToNumber(current) !== incoming;
202
+ }
203
+ function resolveAmountInputForUpdate(parsed, record) {
204
+ if (hasAmountChanged(parsed.unitPriceNet, record.unitPriceNet)) {
205
+ return { amount: parsed.unitPriceNet, mode: "net" };
206
+ }
207
+ if (hasAmountChanged(parsed.unitPriceGross, record.unitPriceGross)) {
208
+ return { amount: parsed.unitPriceGross, mode: "gross" };
209
+ }
210
+ return resolveAmountInputFromParsed(parsed);
211
+ }
199
212
  function resolveAmountInputFromRecord(record) {
200
213
  const net = numericStringToNumber(record.unitPriceNet);
201
214
  if (net !== null) return { amount: net, mode: "net" };
@@ -515,7 +528,7 @@ const updatePriceCommand = {
515
528
  }
516
529
  ensureSameTenant(targetPriceKind, targetProduct.tenantId);
517
530
  const taxCalculationService = ctx.container.resolve("taxCalculationService");
518
- const amountInput = resolveAmountInputFromParsed(parsed);
531
+ const amountInput = resolveAmountInputForUpdate(parsed, record);
519
532
  const hasNetInput = Object.prototype.hasOwnProperty.call(parsed, "unitPriceNet");
520
533
  const hasGrossInput = Object.prototype.hasOwnProperty.call(parsed, "unitPriceGross");
521
534
  const hasTaxRateInput = Object.prototype.hasOwnProperty.call(parsed, "taxRate");
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/catalog/commands/prices.ts"],
4
- "sourcesContent": ["import { registerCommand } from '@open-mercato/shared/lib/commands'\nimport type { CommandHandler } from '@open-mercato/shared/lib/commands'\nimport { buildChanges, requireId, parseWithCustomFields, setCustomFieldsIfAny, emitCrudSideEffects } from '@open-mercato/shared/lib/commands/helpers'\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 { CatalogOffer, CatalogProduct, CatalogProductPrice, CatalogProductVariant, CatalogPriceKind } from '../data/entities'\nimport { loadCustomFieldSnapshot, buildCustomFieldResetMap } from '@open-mercato/shared/lib/commands/customFieldSnapshots'\nimport { E } from '#generated/entities.ids.generated'\nimport {\n priceCreateSchema,\n priceUpdateSchema,\n type PriceCreateInput,\n type PriceUpdateInput,\n} from '../data/validators'\nimport type { TaxCalculationService } from '@open-mercato/core/modules/sales/services/taxCalculationService'\nimport {\n cloneJson,\n commandActorScope,\n commandScopeWhere,\n ensureOrganizationScope,\n ensureSameScope,\n ensureSameTenant,\n ensureTenantScope,\n extractUndoPayload,\n requireVariant,\n requireProduct,\n requireOffer,\n requirePriceKind,\n toNumericString,\n} from './shared'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { makeCreateRedo } from '@open-mercato/shared/lib/commands/redo'\nimport type { CrudEventsConfig } from '@open-mercato/shared/lib/crud/types'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport {\n capturePriceHistoryEntry,\n hasPriceHistoryRelevantChange,\n priceHistoryInputFromRecord,\n} from '../lib/omnibus'\nimport { resolveOmnibusCache } from '../lib/omnibusCache'\n\nconst priceCrudEvents: CrudEventsConfig = {\n module: 'catalog',\n entity: 'price',\n persistent: true,\n buildPayload: (ctx) => ({\n id: ctx.identifiers.id,\n organizationId: ctx.identifiers.organizationId,\n tenantId: ctx.identifiers.tenantId,\n }),\n}\n\ntype PriceSnapshot = {\n id: string\n variantId: string | null\n productId: string | null\n offerId: string | null\n organizationId: string\n tenantId: string\n currencyCode: string\n priceKindId: string\n priceKindCode: string\n kind: string\n minQuantity: number\n maxQuantity: number | null\n unitPriceNet: string | null\n unitPriceGross: string | null\n taxRate: string | null\n taxAmount: string | null\n channelId: string | null\n userId: string | null\n userGroupId: string | null\n customerId: string | null\n customerGroupId: string | null\n metadata: Record<string, unknown> | null\n startsAt: string | null\n endsAt: string | null\n createdAt: string\n updatedAt: string\n custom: Record<string, unknown> | null\n}\n\ntype PriceUndoPayload = {\n before?: PriceSnapshot | null\n after?: PriceSnapshot | null\n}\n\nconst PRICE_CHANGE_KEYS = [\n 'currencyCode',\n 'priceKindId',\n 'kind',\n 'minQuantity',\n 'maxQuantity',\n 'unitPriceNet',\n 'unitPriceGross',\n 'taxRate',\n 'taxAmount',\n 'channelId',\n 'userId',\n 'userGroupId',\n 'customerId',\n 'customerGroupId',\n 'metadata',\n 'startsAt',\n 'endsAt',\n] as const satisfies readonly string[]\n\nasync function resolveSnapshotAssociations(\n em: EntityManager,\n snapshot: PriceSnapshot\n): Promise<{\n variant: CatalogProductVariant | null\n product: CatalogProduct\n offer: CatalogOffer | null\n}> {\n const scope = { tenantId: snapshot.tenantId, organizationId: snapshot.organizationId }\n let variant: CatalogProductVariant | null = null\n if (snapshot.variantId) {\n variant = await requireVariant(em, snapshot.variantId, scope)\n }\n let product: CatalogProduct | null = null\n if (snapshot.productId) {\n product = await requireProduct(em, snapshot.productId, scope)\n } else if (variant) {\n product =\n typeof variant.product === 'string'\n ? await requireProduct(em, variant.product, scope)\n : variant.product\n }\n if (!product) {\n throw new CrudHttpError(400, { error: 'Price snapshot missing product association.' })\n }\n let offer: CatalogOffer | null = null\n if (snapshot.offerId) {\n offer = await requireOffer(em, snapshot.offerId, scope)\n }\n return { variant, product, offer }\n}\n\nasync function loadPriceSnapshot(\n em: EntityManager,\n id: string,\n scopeWhere: Record<string, unknown> = {},\n): Promise<PriceSnapshot | null> {\n const record = await findOneWithDecryption(\n em,\n CatalogProductPrice,\n { id, ...scopeWhere },\n { populate: ['priceKind', 'product', 'variant', 'offer'] },\n )\n if (!record) return null\n const variantId =\n typeof record.variant === 'string'\n ? record.variant\n : record.variant\n ? record.variant.id\n : null\n const productRef = record.product\n ? record.product\n : typeof record.variant === 'object' && record.variant\n ? record.variant.product\n : null\n const productId =\n typeof productRef === 'string'\n ? productRef\n : productRef\n ? productRef.id\n : null\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.catalog.catalog_product_price,\n recordId: record.id,\n tenantId: record.tenantId,\n organizationId: record.organizationId,\n })\n const priceKind = record.priceKind\n const priceKindId =\n typeof priceKind === 'string'\n ? priceKind\n : priceKind\n ? priceKind.id\n : null\n if (!priceKindId) {\n throw new CrudHttpError(400, { error: 'Price is missing price kind metadata.' })\n }\n const priceKindCode =\n typeof priceKind === 'object' && priceKind ? priceKind.code : record.kind\n return {\n id: record.id,\n variantId,\n productId,\n offerId: typeof record.offer === 'string' ? record.offer : record.offer ? record.offer.id : null,\n organizationId: record.organizationId,\n tenantId: record.tenantId,\n currencyCode: record.currencyCode,\n priceKindId,\n priceKindCode,\n kind: record.kind,\n minQuantity: record.minQuantity,\n maxQuantity: record.maxQuantity ?? null,\n unitPriceNet: record.unitPriceNet ?? null,\n unitPriceGross: record.unitPriceGross ?? null,\n taxRate: record.taxRate ?? null,\n taxAmount: record.taxAmount ?? null,\n channelId: record.channelId ?? null,\n userId: record.userId ?? null,\n userGroupId: record.userGroupId ?? null,\n customerId: record.customerId ?? null,\n customerGroupId: record.customerGroupId ?? null,\n metadata: record.metadata ? cloneJson(record.metadata) : null,\n startsAt: record.startsAt ? record.startsAt.toISOString() : null,\n endsAt: record.endsAt ? record.endsAt.toISOString() : null,\n createdAt: record.createdAt.toISOString(),\n updatedAt: record.updatedAt.toISOString(),\n custom: Object.keys(custom).length ? custom : null,\n }\n}\n\nfunction applyPriceSnapshot(em: EntityManager, record: CatalogProductPrice, snapshot: PriceSnapshot): void {\n record.organizationId = snapshot.organizationId\n record.tenantId = snapshot.tenantId\n record.currencyCode = snapshot.currencyCode\n record.priceKind = em.getReference(CatalogPriceKind, snapshot.priceKindId)\n record.kind = snapshot.priceKindCode || snapshot.kind\n record.minQuantity = snapshot.minQuantity\n record.maxQuantity = snapshot.maxQuantity ?? null\n record.unitPriceNet = snapshot.unitPriceNet ?? null\n record.unitPriceGross = snapshot.unitPriceGross ?? null\n record.taxRate = snapshot.taxRate ?? null\n record.taxAmount = snapshot.taxAmount ?? null\n record.channelId = snapshot.channelId ?? null\n record.userId = snapshot.userId ?? null\n record.userGroupId = snapshot.userGroupId ?? null\n record.customerId = snapshot.customerId ?? null\n record.customerGroupId = snapshot.customerGroupId ?? null\n record.metadata = snapshot.metadata ? cloneJson(snapshot.metadata) : null\n record.startsAt = snapshot.startsAt ? new Date(snapshot.startsAt) : null\n record.endsAt = snapshot.endsAt ? new Date(snapshot.endsAt) : null\n record.createdAt = new Date(snapshot.createdAt)\n record.updatedAt = new Date(snapshot.updatedAt)\n}\n\nfunction priceSeedFromSnapshot(snapshot: PriceSnapshot): Record<string, unknown> {\n return {\n id: snapshot.id,\n variant: snapshot.variantId ?? null,\n product: snapshot.productId ?? null,\n offer: snapshot.offerId ?? null,\n organizationId: snapshot.organizationId,\n tenantId: snapshot.tenantId,\n priceKind: snapshot.priceKindId,\n currencyCode: snapshot.currencyCode,\n kind: snapshot.priceKindCode || snapshot.kind,\n minQuantity: snapshot.minQuantity,\n maxQuantity: snapshot.maxQuantity ?? null,\n unitPriceNet: snapshot.unitPriceNet ?? null,\n unitPriceGross: snapshot.unitPriceGross ?? null,\n taxRate: snapshot.taxRate ?? null,\n taxAmount: snapshot.taxAmount ?? null,\n channelId: snapshot.channelId ?? null,\n userId: snapshot.userId ?? null,\n userGroupId: snapshot.userGroupId ?? null,\n customerId: snapshot.customerId ?? null,\n customerGroupId: snapshot.customerGroupId ?? null,\n metadata: snapshot.metadata ? cloneJson(snapshot.metadata) : null,\n startsAt: snapshot.startsAt ? new Date(snapshot.startsAt) : null,\n endsAt: snapshot.endsAt ? new Date(snapshot.endsAt) : null,\n createdAt: new Date(snapshot.createdAt),\n updatedAt: new Date(snapshot.updatedAt),\n }\n}\n\ntype PriceAmountInput = {\n amount: number\n mode: 'net' | 'gross'\n}\n\nfunction resolveAmountInputFromParsed(\n parsed: Partial<Pick<PriceCreateInput, 'unitPriceNet' | 'unitPriceGross'>>\n): PriceAmountInput | null {\n if (typeof parsed.unitPriceNet === 'number' && Number.isFinite(parsed.unitPriceNet)) {\n return { amount: parsed.unitPriceNet, mode: 'net' }\n }\n if (typeof parsed.unitPriceGross === 'number' && Number.isFinite(parsed.unitPriceGross)) {\n return { amount: parsed.unitPriceGross, mode: 'gross' }\n }\n return null\n}\n\nfunction resolveAmountInputFromRecord(record: CatalogProductPrice): PriceAmountInput | null {\n const net = numericStringToNumber(record.unitPriceNet)\n if (net !== null) return { amount: net, mode: 'net' }\n const gross = numericStringToNumber(record.unitPriceGross)\n if (gross !== null) return { amount: gross, mode: 'gross' }\n return null\n}\n\nfunction numericStringToNumber(value: string | null | undefined): number | null {\n if (value === undefined || value === null) return null\n const numeric = Number(value)\n return Number.isFinite(numeric) ? numeric : null\n}\n\nconst createPriceCommand: CommandHandler<PriceCreateInput, { priceId: string }> = {\n id: 'catalog.prices.create',\n async execute(rawInput, ctx) {\n const { parsed, custom } = parseWithCustomFields(priceCreateSchema, rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const actorScope = commandActorScope(ctx)\n let variant: CatalogProductVariant | null = null\n let product: CatalogProduct | null = null\n if (parsed.variantId) {\n variant = await requireVariant(em, parsed.variantId, actorScope)\n product =\n typeof variant.product === 'string'\n ? await requireProduct(em, variant.product, {\n tenantId: variant.tenantId,\n organizationId: variant.organizationId,\n })\n : variant.product\n }\n if (parsed.productId) {\n const explicitProduct = await requireProduct(em, parsed.productId, actorScope)\n if (product && explicitProduct.id !== product.id) {\n throw new CrudHttpError(400, { error: 'Variant does not belong to the provided product.' })\n }\n product = explicitProduct\n }\n if (!variant && !product) {\n throw new CrudHttpError(400, { error: 'Provide either a variantId or productId for pricing.' })\n }\n const scopeSource = variant ?? product!\n ensureTenantScope(ctx, scopeSource.tenantId)\n ensureOrganizationScope(ctx, scopeSource.organizationId)\n const scopeSourceScope = {\n tenantId: scopeSource.tenantId,\n organizationId: scopeSource.organizationId,\n }\n\n const priceKind = await requirePriceKind(em, parsed.priceKindId, scopeSourceScope)\n ensureSameTenant(priceKind, scopeSource.tenantId)\n\n let offer: CatalogOffer | null = null\n if (parsed.offerId) {\n offer = await requireOffer(em, parsed.offerId, scopeSourceScope)\n ensureSameScope(offer, scopeSource.organizationId, scopeSource.tenantId)\n const offerProduct =\n typeof offer.product === 'string'\n ? await requireProduct(em, offer.product, {\n tenantId: offer.tenantId,\n organizationId: offer.organizationId,\n })\n : offer.product\n if (product && offerProduct.id !== product.id) {\n throw new CrudHttpError(400, { error: 'Offer does not belong to the selected product.' })\n }\n product = offerProduct\n }\n const productEntity = product\n const channelId = parsed.channelId ?? (offer ? offer.channelId : null)\n const taxCalculationService = ctx.container.resolve<TaxCalculationService>('taxCalculationService')\n const amountInput = resolveAmountInputFromParsed(parsed)\n let unitPriceNetValue = toNumericString(parsed.unitPriceNet)\n let unitPriceGrossValue = toNumericString(parsed.unitPriceGross)\n let taxRateValue = toNumericString(parsed.taxRate)\n let taxAmountValue: string | null = null\n if (amountInput) {\n const calculation = await taxCalculationService.calculateUnitAmounts({\n amount: amountInput.amount,\n mode: amountInput.mode,\n organizationId: scopeSource.organizationId,\n tenantId: scopeSource.tenantId,\n taxRateId: parsed.taxRateId ?? null,\n taxRate: parsed.taxRate ?? null,\n })\n unitPriceNetValue = toNumericString(calculation.netAmount)\n unitPriceGrossValue = toNumericString(calculation.grossAmount)\n taxAmountValue = toNumericString(calculation.taxAmount)\n taxRateValue = toNumericString(calculation.taxRate)\n }\n\n const now = new Date()\n const record = em.create(CatalogProductPrice, {\n organizationId: scopeSource.organizationId,\n tenantId: scopeSource.tenantId,\n variant,\n product: productEntity ?? undefined,\n offer: offer ?? undefined,\n priceKind,\n currencyCode: parsed.currencyCode,\n kind: priceKind.code,\n minQuantity: parsed.minQuantity ?? 1,\n maxQuantity: parsed.maxQuantity ?? null,\n unitPriceNet: unitPriceNetValue,\n unitPriceGross: unitPriceGrossValue,\n taxRate: taxRateValue,\n taxAmount: taxAmountValue,\n channelId,\n userId: parsed.userId ?? null,\n userGroupId: parsed.userGroupId ?? null,\n customerId: parsed.customerId ?? null,\n customerGroupId: parsed.customerGroupId ?? null,\n metadata: parsed.metadata ? cloneJson(parsed.metadata) : null,\n startsAt: parsed.startsAt ?? null,\n endsAt: parsed.endsAt ?? null,\n createdAt: now,\n updatedAt: now,\n })\n em.persist(record)\n await em.flush()\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: record.id,\n organizationId: record.organizationId,\n tenantId: record.tenantId,\n values: custom,\n })\n const de = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'created',\n entity: record,\n identifiers: {\n id: record.id,\n organizationId: record.organizationId,\n tenantId: record.tenantId,\n },\n events: priceCrudEvents,\n })\n await capturePriceHistoryEntry(em, priceHistoryInputFromRecord(record), 'create', {\n cache: resolveOmnibusCache(ctx.container),\n })\n return { priceId: record.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return loadPriceSnapshot(em, result.priceId)\n },\n buildLog: async ({ result, snapshots }) => {\n const after = snapshots.after as PriceSnapshot | undefined\n if (!after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('catalog.audit.prices.create', 'Create product price'),\n resourceKind: 'catalog.price',\n resourceId: result.priceId,\n tenantId: after.tenantId,\n organizationId: after.organizationId,\n snapshotAfter: after,\n payload: {\n undo: {\n after,\n } satisfies PriceUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PriceUndoPayload>(logEntry)\n const after = payload?.after\n if (!after) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const record = await em.findOne(CatalogProductPrice, { id: after.id })\n if (!record) return\n ensureTenantScope(ctx, record.tenantId)\n ensureOrganizationScope(ctx, record.organizationId)\n em.remove(record)\n await em.flush()\n await capturePriceHistoryEntry(em, after, 'undo', {\n metadata: { undoneCommand: 'catalog.prices.create' },\n cache: resolveOmnibusCache(ctx.container),\n })\n const resetValues = buildCustomFieldResetMap(undefined, after.custom ?? undefined)\n if (Object.keys(resetValues).length) {\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: after.id,\n organizationId: after.organizationId,\n tenantId: after.tenantId,\n values: resetValues,\n })\n }\n },\n redo: makeCreateRedo<CatalogProductPrice, PriceSnapshot, PriceCreateInput, { priceId: string }>({\n entityClass: CatalogProductPrice,\n getSnapshotId: (snapshot) => snapshot.id,\n seedFromSnapshot: priceSeedFromSnapshot,\n buildResult: (entity) => ({ priceId: entity.id }),\n events: priceCrudEvents,\n afterRestore: async ({ em, ctx, entity, snapshot }) => {\n if (snapshot.custom && Object.keys(snapshot.custom).length) {\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: entity.id,\n organizationId: entity.organizationId,\n tenantId: entity.tenantId,\n values: snapshot.custom,\n })\n }\n await capturePriceHistoryEntry(em, snapshot, 'create', {\n metadata: { redoneCommand: 'catalog.prices.create' },\n cache: resolveOmnibusCache(ctx.container),\n })\n },\n }),\n}\n\nconst updatePriceCommand: CommandHandler<PriceUpdateInput, { priceId: string }> = {\n id: 'catalog.prices.update',\n async prepare(input, ctx) {\n const id = requireId(input, 'Price id is required')\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadPriceSnapshot(em, id, commandScopeWhere(ctx))\n if (snapshot) {\n ensureTenantScope(ctx, snapshot.tenantId)\n ensureOrganizationScope(ctx, snapshot.organizationId)\n }\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const { parsed, custom } = parseWithCustomFields(priceUpdateSchema, rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const record = await findOneWithDecryption(\n em,\n CatalogProductPrice,\n { id: parsed.id, ...commandScopeWhere(ctx) },\n { populate: ['priceKind'] },\n { tenantId: parsed.tenantId, organizationId: parsed.organizationId },\n )\n if (!record) throw new CrudHttpError(404, { error: 'Catalog price not found' })\n const recordScope = { tenantId: record.tenantId, organizationId: record.organizationId }\n const currentVariantRef = record.variant\n let targetVariant: CatalogProductVariant | null = null\n if (typeof currentVariantRef === 'string') {\n targetVariant = await requireVariant(em, currentVariantRef, recordScope)\n } else if (currentVariantRef) {\n targetVariant = currentVariantRef\n }\n const currentProductRef = record.product ?? (targetVariant ? targetVariant.product : null)\n let targetProduct: CatalogProduct | null = null\n if (typeof currentProductRef === 'string') {\n targetProduct = await requireProduct(em, currentProductRef, recordScope)\n } else if (currentProductRef) {\n targetProduct = currentProductRef\n }\n const recordHistoryInput = priceHistoryInputFromRecord(record)\n const historyBefore = {\n ...recordHistoryInput,\n productId: recordHistoryInput.productId ?? targetProduct?.id ?? null,\n }\n\n if (parsed.variantId !== undefined) {\n if (!parsed.variantId) {\n targetVariant = null\n } else {\n targetVariant = await requireVariant(em, parsed.variantId, recordScope)\n targetProduct =\n typeof targetVariant.product === 'string'\n ? await requireProduct(em, targetVariant.product, recordScope)\n : targetVariant.product\n }\n }\n\n if (targetVariant && (targetVariant as CatalogProductVariant | null)?.product === undefined) {\n targetVariant = await requireVariant(em, targetVariant.id, recordScope)\n }\n\n if (parsed.productId !== undefined) {\n if (!parsed.productId) {\n targetProduct = null\n } else {\n const explicitProduct = await requireProduct(em, parsed.productId, recordScope)\n if (targetVariant) {\n const variantProductId =\n typeof targetVariant.product === 'string'\n ? targetVariant.product\n : targetVariant.product.id\n if (variantProductId !== explicitProduct.id) {\n throw new CrudHttpError(400, { error: 'Variant does not belong to the provided product.' })\n }\n }\n targetProduct = explicitProduct\n }\n }\n\n if (!targetVariant && !targetProduct) {\n throw new CrudHttpError(400, { error: 'Price must remain associated with a product or variant.' })\n }\n if (!targetProduct && targetVariant) {\n targetProduct =\n typeof targetVariant.product === 'string'\n ? await requireProduct(em, targetVariant.product, recordScope)\n : targetVariant.product\n }\n if (!targetProduct) {\n throw new CrudHttpError(400, { error: 'Unable to resolve product for price.' })\n }\n\n let targetOffer: CatalogOffer | null = null\n if (record.offer) {\n targetOffer =\n typeof record.offer === 'string'\n ? await requireOffer(em, record.offer, recordScope)\n : record.offer\n }\n if (parsed.offerId !== undefined) {\n if (!parsed.offerId) {\n targetOffer = null\n } else {\n const explicitOffer = await requireOffer(em, parsed.offerId, recordScope)\n ensureSameScope(explicitOffer, targetProduct.organizationId, targetProduct.tenantId)\n const offerProductId =\n typeof explicitOffer.product === 'string'\n ? explicitOffer.product\n : explicitOffer.product.id\n if (offerProductId !== targetProduct.id) {\n throw new CrudHttpError(400, { error: 'Offer does not belong to the selected product.' })\n }\n targetOffer = explicitOffer\n }\n }\n\n ensureTenantScope(ctx, targetProduct.tenantId)\n ensureOrganizationScope(ctx, targetProduct.organizationId)\n\n let targetPriceKind: CatalogPriceKind | null = null\n if (record.priceKind) {\n targetPriceKind =\n typeof record.priceKind === 'string'\n ? await requirePriceKind(em, record.priceKind, recordScope)\n : record.priceKind\n }\n if (parsed.priceKindId !== undefined) {\n if (!parsed.priceKindId) {\n throw new CrudHttpError(400, { error: 'Price kind is required.' })\n }\n targetPriceKind = await requirePriceKind(em, parsed.priceKindId, recordScope)\n }\n if (!targetPriceKind) {\n throw new CrudHttpError(400, { error: 'Price kind is required.' })\n }\n ensureSameTenant(targetPriceKind, targetProduct.tenantId)\n\n const taxCalculationService = ctx.container.resolve<TaxCalculationService>('taxCalculationService')\n const amountInput = resolveAmountInputFromParsed(parsed)\n const hasNetInput = Object.prototype.hasOwnProperty.call(parsed, 'unitPriceNet')\n const hasGrossInput = Object.prototype.hasOwnProperty.call(parsed, 'unitPriceGross')\n const hasTaxRateInput = Object.prototype.hasOwnProperty.call(parsed, 'taxRate')\n const hasTaxRateIdInput = Object.prototype.hasOwnProperty.call(parsed, 'taxRateId')\n let taxCalculationResult: Awaited<ReturnType<TaxCalculationService['calculateUnitAmounts']>> | null = null\n let calculationBase = amountInput\n if (!calculationBase && (hasTaxRateInput || hasTaxRateIdInput)) {\n calculationBase = resolveAmountInputFromRecord(record)\n }\n if (calculationBase) {\n const taxRateIdForCalculation = hasTaxRateIdInput ? parsed.taxRateId ?? null : undefined\n const taxRateForCalculation = hasTaxRateInput\n ? parsed.taxRate ?? null\n : taxRateIdForCalculation === null\n ? null\n : record.taxRate ?? null\n taxCalculationResult = await taxCalculationService.calculateUnitAmounts({\n amount: calculationBase.amount,\n mode: calculationBase.mode,\n organizationId: targetProduct.organizationId,\n tenantId: targetProduct.tenantId,\n taxRateId: taxRateIdForCalculation,\n taxRate: taxRateForCalculation,\n })\n }\n\n record.variant = targetVariant\n record.product = targetProduct\n record.offer = targetOffer\n record.organizationId = targetProduct.organizationId\n record.tenantId = targetProduct.tenantId\n record.priceKind = targetPriceKind\n record.kind = targetPriceKind.code\n\n if (parsed.currencyCode !== undefined) record.currencyCode = parsed.currencyCode\n if (parsed.minQuantity !== undefined) record.minQuantity = parsed.minQuantity ?? 1\n if (parsed.maxQuantity !== undefined) record.maxQuantity = parsed.maxQuantity ?? null\n if (taxCalculationResult) {\n record.unitPriceNet = toNumericString(taxCalculationResult.netAmount)\n record.unitPriceGross = toNumericString(taxCalculationResult.grossAmount)\n record.taxRate = toNumericString(taxCalculationResult.taxRate)\n record.taxAmount = toNumericString(taxCalculationResult.taxAmount)\n } else {\n if (hasNetInput) {\n record.unitPriceNet = toNumericString(parsed.unitPriceNet)\n }\n if (hasGrossInput) {\n record.unitPriceGross = toNumericString(parsed.unitPriceGross)\n }\n if (hasTaxRateInput) {\n record.taxRate = toNumericString(parsed.taxRate)\n if (parsed.taxRate == null) {\n record.taxAmount = null\n }\n } else if (hasTaxRateIdInput && parsed.taxRateId === null) {\n record.taxRate = null\n record.taxAmount = null\n }\n }\n if (parsed.channelId !== undefined) {\n record.channelId = parsed.channelId ?? null\n } else if (parsed.offerId !== undefined && targetOffer) {\n record.channelId = targetOffer.channelId\n }\n if (parsed.userId !== undefined) record.userId = parsed.userId ?? null\n if (parsed.userGroupId !== undefined) record.userGroupId = parsed.userGroupId ?? null\n if (parsed.customerId !== undefined) record.customerId = parsed.customerId ?? null\n if (parsed.customerGroupId !== undefined) record.customerGroupId = parsed.customerGroupId ?? null\n if (parsed.metadata !== undefined) {\n record.metadata = parsed.metadata ? cloneJson(parsed.metadata) : null\n }\n if (Object.prototype.hasOwnProperty.call(parsed, 'startsAt')) {\n record.startsAt = parsed.startsAt ?? null\n }\n if (Object.prototype.hasOwnProperty.call(parsed, 'endsAt')) {\n record.endsAt = parsed.endsAt ?? null\n }\n await em.flush()\n if (custom && Object.keys(custom).length) {\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: record.id,\n organizationId: record.organizationId,\n tenantId: record.tenantId,\n values: custom,\n })\n }\n const de = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: record,\n identifiers: {\n id: record.id,\n organizationId: record.organizationId,\n tenantId: record.tenantId,\n },\n events: priceCrudEvents,\n })\n const historyAfter = priceHistoryInputFromRecord(record)\n if (hasPriceHistoryRelevantChange(historyBefore, historyAfter)) {\n await capturePriceHistoryEntry(em, historyAfter, 'update', {\n cache: resolveOmnibusCache(ctx.container),\n invalidatePrices: [historyBefore],\n previous: historyBefore,\n })\n }\n return { priceId: record.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return loadPriceSnapshot(em, result.priceId)\n },\n buildLog: async ({ snapshots }) => {\n const before = snapshots.before as PriceSnapshot | undefined\n const after = snapshots.after as PriceSnapshot | undefined\n if (!before || !after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('catalog.audit.prices.update', 'Update product price'),\n resourceKind: 'catalog.price',\n resourceId: before.id,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n snapshotAfter: after,\n changes: buildChanges(\n before as Record<string, unknown>,\n after as Record<string, unknown>,\n PRICE_CHANGE_KEYS\n ),\n payload: {\n undo: {\n before,\n after,\n } satisfies PriceUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PriceUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const after = payload?.after\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n let record = await em.findOne(CatalogProductPrice, { id: before.id })\n if (!record) {\n const { variant, product, offer } = await resolveSnapshotAssociations(em, before)\n record = em.create(CatalogProductPrice, {\n id: before.id,\n variant,\n product,\n offer,\n organizationId: before.organizationId,\n tenantId: before.tenantId,\n currencyCode: before.currencyCode,\n priceKind: em.getReference(CatalogPriceKind, before.priceKindId),\n kind: before.priceKindCode || before.kind,\n minQuantity: before.minQuantity,\n maxQuantity: before.maxQuantity ?? null,\n unitPriceNet: before.unitPriceNet ?? null,\n unitPriceGross: before.unitPriceGross ?? null,\n taxRate: before.taxRate ?? null,\n taxAmount: before.taxAmount ?? null,\n channelId: before.channelId ?? null,\n userId: before.userId ?? null,\n userGroupId: before.userGroupId ?? null,\n customerId: before.customerId ?? null,\n customerGroupId: before.customerGroupId ?? null,\n metadata: before.metadata ? cloneJson(before.metadata) : null,\n startsAt: before.startsAt ? new Date(before.startsAt) : null,\n endsAt: before.endsAt ? new Date(before.endsAt) : null,\n createdAt: new Date(before.createdAt),\n updatedAt: new Date(before.updatedAt),\n })\n em.persist(record)\n }\n ensureTenantScope(ctx, before.tenantId)\n ensureOrganizationScope(ctx, before.organizationId)\n applyPriceSnapshot(em, record, before)\n await em.flush()\n await capturePriceHistoryEntry(em, before, 'undo', {\n metadata: { undoneCommand: 'catalog.prices.update' },\n cache: resolveOmnibusCache(ctx.container),\n invalidatePrices: [after],\n previous: after,\n })\n const resetValues = buildCustomFieldResetMap(\n before.custom ?? undefined,\n after?.custom ?? undefined\n )\n if (Object.keys(resetValues).length) {\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: before.id,\n organizationId: before.organizationId,\n tenantId: before.tenantId,\n values: resetValues,\n })\n }\n },\n}\n\nconst deletePriceCommand: CommandHandler<\n { body?: Record<string, unknown>; query?: Record<string, unknown> },\n { priceId: string }\n> = {\n id: 'catalog.prices.delete',\n async prepare(input, ctx) {\n const id = requireId(input, 'Price id is required')\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadPriceSnapshot(em, id, commandScopeWhere(ctx))\n if (snapshot) {\n ensureTenantScope(ctx, snapshot.tenantId)\n ensureOrganizationScope(ctx, snapshot.organizationId)\n }\n return snapshot ? { before: snapshot } : {}\n },\n async execute(input, ctx) {\n const id = requireId(input, 'Price id is required')\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const record = await findOneWithDecryption(em, CatalogProductPrice, { id, ...commandScopeWhere(ctx) })\n if (!record) throw new CrudHttpError(404, { error: 'Catalog price not found' })\n ensureTenantScope(ctx, record.tenantId)\n ensureOrganizationScope(ctx, record.organizationId)\n const { product } = await resolvePriceRecordAssociations(em, record)\n\n const baseEm = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadPriceSnapshot(baseEm, id)\n\n const deletedPrice = snapshot ?? priceHistoryInputFromRecord(record)\n em.remove(record)\n await em.flush()\n await capturePriceHistoryEntry(em, deletedPrice, 'delete', { cache: resolveOmnibusCache(ctx.container) })\n if (snapshot?.custom && Object.keys(snapshot.custom).length) {\n const resetValues = buildCustomFieldResetMap(snapshot.custom, undefined)\n if (Object.keys(resetValues).length) {\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: id,\n organizationId: snapshot.organizationId,\n tenantId: snapshot.tenantId,\n values: resetValues,\n })\n }\n }\n const de = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'deleted',\n entity: record,\n identifiers: {\n id,\n organizationId: snapshot?.organizationId ?? record.organizationId,\n tenantId: snapshot?.tenantId ?? record.tenantId,\n },\n events: priceCrudEvents,\n })\n return { priceId: id }\n },\n buildLog: async ({ snapshots }) => {\n const before = snapshots.before as PriceSnapshot | undefined\n if (!before) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('catalog.audit.prices.delete', 'Delete product price'),\n resourceKind: 'catalog.price',\n resourceId: before.id,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n payload: {\n undo: {\n before,\n } satisfies PriceUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PriceUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n let record = await em.findOne(CatalogProductPrice, { id: before.id })\n if (!record) {\n const { variant, product, offer } = await resolveSnapshotAssociations(em, before)\n record = em.create(CatalogProductPrice, {\n id: before.id,\n variant,\n product,\n offer,\n organizationId: before.organizationId,\n tenantId: before.tenantId,\n currencyCode: before.currencyCode,\n priceKind: em.getReference(CatalogPriceKind, before.priceKindId),\n kind: before.priceKindCode || before.kind,\n minQuantity: before.minQuantity,\n maxQuantity: before.maxQuantity ?? null,\n unitPriceNet: before.unitPriceNet ?? null,\n unitPriceGross: before.unitPriceGross ?? null,\n taxRate: before.taxRate ?? null,\n taxAmount: before.taxAmount ?? null,\n channelId: before.channelId ?? null,\n userId: before.userId ?? null,\n userGroupId: before.userGroupId ?? null,\n customerId: before.customerId ?? null,\n customerGroupId: before.customerGroupId ?? null,\n metadata: before.metadata ? cloneJson(before.metadata) : null,\n startsAt: before.startsAt ? new Date(before.startsAt) : null,\n endsAt: before.endsAt ? new Date(before.endsAt) : null,\n createdAt: new Date(before.createdAt),\n updatedAt: new Date(before.updatedAt),\n })\n em.persist(record)\n }\n ensureTenantScope(ctx, before.tenantId)\n ensureOrganizationScope(ctx, before.organizationId)\n applyPriceSnapshot(em, record, before)\n await em.flush()\n await capturePriceHistoryEntry(em, before, 'undo', {\n metadata: { undoneCommand: 'catalog.prices.delete' },\n cache: resolveOmnibusCache(ctx.container),\n })\n if (before.custom && Object.keys(before.custom).length) {\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: before.id,\n organizationId: before.organizationId,\n tenantId: before.tenantId,\n values: before.custom,\n })\n }\n },\n}\n\nregisterCommand(createPriceCommand)\nregisterCommand(updatePriceCommand)\nregisterCommand(deletePriceCommand)\n\nasync function resolvePriceRecordAssociations(\n em: EntityManager,\n record: CatalogProductPrice,\n): Promise<{ product: CatalogProduct; variant: CatalogProductVariant | null }> {\n const scope = { tenantId: record.tenantId, organizationId: record.organizationId }\n const variant = record.variant\n ? (typeof record.variant === 'string'\n ? await requireVariant(em, record.variant, scope)\n : record.variant)\n : null\n if (record.product) {\n const product =\n typeof record.product === 'string'\n ? await requireProduct(em, record.product, scope)\n : record.product\n return { product, variant }\n }\n if (variant?.product) {\n const productRef = variant.product\n const product =\n typeof productRef === 'string'\n ? await requireProduct(em, productRef, scope)\n : productRef\n return { product, variant }\n }\n if (record.offer) {\n const offer =\n typeof record.offer === 'string'\n ? await requireOffer(em, record.offer, scope)\n : record.offer\n const productRef = offer?.product ?? null\n if (productRef) {\n const product =\n typeof productRef === 'string'\n ? await requireProduct(em, productRef, scope)\n : productRef\n return { product, variant }\n }\n }\n throw new CrudHttpError(400, { error: 'Catalog price is not linked to a product.' })\n}\n"],
5
- "mappings": "AAAA,SAAS,uBAAuB;AAEhC,SAAS,cAAc,WAAW,uBAAuB,sBAAsB,2BAA2B;AAE1G,SAAS,qBAAqB;AAC9B,SAAS,2BAA2B;AACpC,SAAuC,qBAA4C,wBAAwB;AAC3G,SAAS,y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B;AAClC,gBAAgB,kBAAkB;AAClC,gBAAgB,kBAAkB;AAElC,eAAe,+BACb,IACA,QAC6E;AAC7E,QAAM,QAAQ,EAAE,UAAU,OAAO,UAAU,gBAAgB,OAAO,eAAe;AACjF,QAAM,UAAU,OAAO,UAClB,OAAO,OAAO,YAAY,WACvB,MAAM,eAAe,IAAI,OAAO,SAAS,KAAK,IAC9C,OAAO,UACX;AACJ,MAAI,OAAO,SAAS;AAClB,UAAM,UACJ,OAAO,OAAO,YAAY,WACtB,MAAM,eAAe,IAAI,OAAO,SAAS,KAAK,IAC9C,OAAO;AACb,WAAO,EAAE,SAAS,QAAQ;AAAA,EAC5B;AACA,MAAI,SAAS,SAAS;AACpB,UAAM,aAAa,QAAQ;AAC3B,UAAM,UACJ,OAAO,eAAe,WAClB,MAAM,eAAe,IAAI,YAAY,KAAK,IAC1C;AACN,WAAO,EAAE,SAAS,QAAQ;AAAA,EAC5B;AACA,MAAI,OAAO,OAAO;AAChB,UAAM,QACJ,OAAO,OAAO,UAAU,WACpB,MAAM,aAAa,IAAI,OAAO,OAAO,KAAK,IAC1C,OAAO;AACb,UAAM,aAAa,OAAO,WAAW;AACrC,QAAI,YAAY;AACd,YAAM,UACJ,OAAO,eAAe,WAClB,MAAM,eAAe,IAAI,YAAY,KAAK,IAC1C;AACN,aAAO,EAAE,SAAS,QAAQ;AAAA,IAC5B;AAAA,EACF;AACA,QAAM,IAAI,cAAc,KAAK,EAAE,OAAO,4CAA4C,CAAC;AACrF;",
4
+ "sourcesContent": ["import { registerCommand } from '@open-mercato/shared/lib/commands'\nimport type { CommandHandler } from '@open-mercato/shared/lib/commands'\nimport { buildChanges, requireId, parseWithCustomFields, setCustomFieldsIfAny, emitCrudSideEffects } from '@open-mercato/shared/lib/commands/helpers'\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 { CatalogOffer, CatalogProduct, CatalogProductPrice, CatalogProductVariant, CatalogPriceKind } from '../data/entities'\nimport { loadCustomFieldSnapshot, buildCustomFieldResetMap } from '@open-mercato/shared/lib/commands/customFieldSnapshots'\nimport { E } from '#generated/entities.ids.generated'\nimport {\n priceCreateSchema,\n priceUpdateSchema,\n type PriceCreateInput,\n type PriceUpdateInput,\n} from '../data/validators'\nimport type { TaxCalculationService } from '@open-mercato/core/modules/sales/services/taxCalculationService'\nimport {\n cloneJson,\n commandActorScope,\n commandScopeWhere,\n ensureOrganizationScope,\n ensureSameScope,\n ensureSameTenant,\n ensureTenantScope,\n extractUndoPayload,\n requireVariant,\n requireProduct,\n requireOffer,\n requirePriceKind,\n toNumericString,\n} from './shared'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { makeCreateRedo } from '@open-mercato/shared/lib/commands/redo'\nimport type { CrudEventsConfig } from '@open-mercato/shared/lib/crud/types'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport {\n capturePriceHistoryEntry,\n hasPriceHistoryRelevantChange,\n priceHistoryInputFromRecord,\n} from '../lib/omnibus'\nimport { resolveOmnibusCache } from '../lib/omnibusCache'\n\nconst priceCrudEvents: CrudEventsConfig = {\n module: 'catalog',\n entity: 'price',\n persistent: true,\n buildPayload: (ctx) => ({\n id: ctx.identifiers.id,\n organizationId: ctx.identifiers.organizationId,\n tenantId: ctx.identifiers.tenantId,\n }),\n}\n\ntype PriceSnapshot = {\n id: string\n variantId: string | null\n productId: string | null\n offerId: string | null\n organizationId: string\n tenantId: string\n currencyCode: string\n priceKindId: string\n priceKindCode: string\n kind: string\n minQuantity: number\n maxQuantity: number | null\n unitPriceNet: string | null\n unitPriceGross: string | null\n taxRate: string | null\n taxAmount: string | null\n channelId: string | null\n userId: string | null\n userGroupId: string | null\n customerId: string | null\n customerGroupId: string | null\n metadata: Record<string, unknown> | null\n startsAt: string | null\n endsAt: string | null\n createdAt: string\n updatedAt: string\n custom: Record<string, unknown> | null\n}\n\ntype PriceUndoPayload = {\n before?: PriceSnapshot | null\n after?: PriceSnapshot | null\n}\n\nconst PRICE_CHANGE_KEYS = [\n 'currencyCode',\n 'priceKindId',\n 'kind',\n 'minQuantity',\n 'maxQuantity',\n 'unitPriceNet',\n 'unitPriceGross',\n 'taxRate',\n 'taxAmount',\n 'channelId',\n 'userId',\n 'userGroupId',\n 'customerId',\n 'customerGroupId',\n 'metadata',\n 'startsAt',\n 'endsAt',\n] as const satisfies readonly string[]\n\nasync function resolveSnapshotAssociations(\n em: EntityManager,\n snapshot: PriceSnapshot\n): Promise<{\n variant: CatalogProductVariant | null\n product: CatalogProduct\n offer: CatalogOffer | null\n}> {\n const scope = { tenantId: snapshot.tenantId, organizationId: snapshot.organizationId }\n let variant: CatalogProductVariant | null = null\n if (snapshot.variantId) {\n variant = await requireVariant(em, snapshot.variantId, scope)\n }\n let product: CatalogProduct | null = null\n if (snapshot.productId) {\n product = await requireProduct(em, snapshot.productId, scope)\n } else if (variant) {\n product =\n typeof variant.product === 'string'\n ? await requireProduct(em, variant.product, scope)\n : variant.product\n }\n if (!product) {\n throw new CrudHttpError(400, { error: 'Price snapshot missing product association.' })\n }\n let offer: CatalogOffer | null = null\n if (snapshot.offerId) {\n offer = await requireOffer(em, snapshot.offerId, scope)\n }\n return { variant, product, offer }\n}\n\nasync function loadPriceSnapshot(\n em: EntityManager,\n id: string,\n scopeWhere: Record<string, unknown> = {},\n): Promise<PriceSnapshot | null> {\n const record = await findOneWithDecryption(\n em,\n CatalogProductPrice,\n { id, ...scopeWhere },\n { populate: ['priceKind', 'product', 'variant', 'offer'] },\n )\n if (!record) return null\n const variantId =\n typeof record.variant === 'string'\n ? record.variant\n : record.variant\n ? record.variant.id\n : null\n const productRef = record.product\n ? record.product\n : typeof record.variant === 'object' && record.variant\n ? record.variant.product\n : null\n const productId =\n typeof productRef === 'string'\n ? productRef\n : productRef\n ? productRef.id\n : null\n const custom = await loadCustomFieldSnapshot(em, {\n entityId: E.catalog.catalog_product_price,\n recordId: record.id,\n tenantId: record.tenantId,\n organizationId: record.organizationId,\n })\n const priceKind = record.priceKind\n const priceKindId =\n typeof priceKind === 'string'\n ? priceKind\n : priceKind\n ? priceKind.id\n : null\n if (!priceKindId) {\n throw new CrudHttpError(400, { error: 'Price is missing price kind metadata.' })\n }\n const priceKindCode =\n typeof priceKind === 'object' && priceKind ? priceKind.code : record.kind\n return {\n id: record.id,\n variantId,\n productId,\n offerId: typeof record.offer === 'string' ? record.offer : record.offer ? record.offer.id : null,\n organizationId: record.organizationId,\n tenantId: record.tenantId,\n currencyCode: record.currencyCode,\n priceKindId,\n priceKindCode,\n kind: record.kind,\n minQuantity: record.minQuantity,\n maxQuantity: record.maxQuantity ?? null,\n unitPriceNet: record.unitPriceNet ?? null,\n unitPriceGross: record.unitPriceGross ?? null,\n taxRate: record.taxRate ?? null,\n taxAmount: record.taxAmount ?? null,\n channelId: record.channelId ?? null,\n userId: record.userId ?? null,\n userGroupId: record.userGroupId ?? null,\n customerId: record.customerId ?? null,\n customerGroupId: record.customerGroupId ?? null,\n metadata: record.metadata ? cloneJson(record.metadata) : null,\n startsAt: record.startsAt ? record.startsAt.toISOString() : null,\n endsAt: record.endsAt ? record.endsAt.toISOString() : null,\n createdAt: record.createdAt.toISOString(),\n updatedAt: record.updatedAt.toISOString(),\n custom: Object.keys(custom).length ? custom : null,\n }\n}\n\nfunction applyPriceSnapshot(em: EntityManager, record: CatalogProductPrice, snapshot: PriceSnapshot): void {\n record.organizationId = snapshot.organizationId\n record.tenantId = snapshot.tenantId\n record.currencyCode = snapshot.currencyCode\n record.priceKind = em.getReference(CatalogPriceKind, snapshot.priceKindId)\n record.kind = snapshot.priceKindCode || snapshot.kind\n record.minQuantity = snapshot.minQuantity\n record.maxQuantity = snapshot.maxQuantity ?? null\n record.unitPriceNet = snapshot.unitPriceNet ?? null\n record.unitPriceGross = snapshot.unitPriceGross ?? null\n record.taxRate = snapshot.taxRate ?? null\n record.taxAmount = snapshot.taxAmount ?? null\n record.channelId = snapshot.channelId ?? null\n record.userId = snapshot.userId ?? null\n record.userGroupId = snapshot.userGroupId ?? null\n record.customerId = snapshot.customerId ?? null\n record.customerGroupId = snapshot.customerGroupId ?? null\n record.metadata = snapshot.metadata ? cloneJson(snapshot.metadata) : null\n record.startsAt = snapshot.startsAt ? new Date(snapshot.startsAt) : null\n record.endsAt = snapshot.endsAt ? new Date(snapshot.endsAt) : null\n record.createdAt = new Date(snapshot.createdAt)\n record.updatedAt = new Date(snapshot.updatedAt)\n}\n\nfunction priceSeedFromSnapshot(snapshot: PriceSnapshot): Record<string, unknown> {\n return {\n id: snapshot.id,\n variant: snapshot.variantId ?? null,\n product: snapshot.productId ?? null,\n offer: snapshot.offerId ?? null,\n organizationId: snapshot.organizationId,\n tenantId: snapshot.tenantId,\n priceKind: snapshot.priceKindId,\n currencyCode: snapshot.currencyCode,\n kind: snapshot.priceKindCode || snapshot.kind,\n minQuantity: snapshot.minQuantity,\n maxQuantity: snapshot.maxQuantity ?? null,\n unitPriceNet: snapshot.unitPriceNet ?? null,\n unitPriceGross: snapshot.unitPriceGross ?? null,\n taxRate: snapshot.taxRate ?? null,\n taxAmount: snapshot.taxAmount ?? null,\n channelId: snapshot.channelId ?? null,\n userId: snapshot.userId ?? null,\n userGroupId: snapshot.userGroupId ?? null,\n customerId: snapshot.customerId ?? null,\n customerGroupId: snapshot.customerGroupId ?? null,\n metadata: snapshot.metadata ? cloneJson(snapshot.metadata) : null,\n startsAt: snapshot.startsAt ? new Date(snapshot.startsAt) : null,\n endsAt: snapshot.endsAt ? new Date(snapshot.endsAt) : null,\n createdAt: new Date(snapshot.createdAt),\n updatedAt: new Date(snapshot.updatedAt),\n }\n}\n\ntype PriceAmountInput = {\n amount: number\n mode: 'net' | 'gross'\n}\n\nfunction resolveAmountInputFromParsed(\n parsed: Partial<Pick<PriceCreateInput, 'unitPriceNet' | 'unitPriceGross'>>\n): PriceAmountInput | null {\n if (typeof parsed.unitPriceNet === 'number' && Number.isFinite(parsed.unitPriceNet)) {\n return { amount: parsed.unitPriceNet, mode: 'net' }\n }\n if (typeof parsed.unitPriceGross === 'number' && Number.isFinite(parsed.unitPriceGross)) {\n return { amount: parsed.unitPriceGross, mode: 'gross' }\n }\n return null\n}\n\nfunction hasAmountChanged(incoming: unknown, current: string | null | undefined): incoming is number {\n if (typeof incoming !== 'number' || !Number.isFinite(incoming)) return false\n return numericStringToNumber(current) !== incoming\n}\n\nfunction resolveAmountInputForUpdate(\n parsed: Partial<Pick<PriceCreateInput, 'unitPriceNet' | 'unitPriceGross'>>,\n record: CatalogProductPrice\n): PriceAmountInput | null {\n if (hasAmountChanged(parsed.unitPriceNet, record.unitPriceNet)) {\n return { amount: parsed.unitPriceNet, mode: 'net' }\n }\n if (hasAmountChanged(parsed.unitPriceGross, record.unitPriceGross)) {\n return { amount: parsed.unitPriceGross, mode: 'gross' }\n }\n return resolveAmountInputFromParsed(parsed)\n}\n\nfunction resolveAmountInputFromRecord(record: CatalogProductPrice): PriceAmountInput | null {\n const net = numericStringToNumber(record.unitPriceNet)\n if (net !== null) return { amount: net, mode: 'net' }\n const gross = numericStringToNumber(record.unitPriceGross)\n if (gross !== null) return { amount: gross, mode: 'gross' }\n return null\n}\n\nfunction numericStringToNumber(value: string | null | undefined): number | null {\n if (value === undefined || value === null) return null\n const numeric = Number(value)\n return Number.isFinite(numeric) ? numeric : null\n}\n\nconst createPriceCommand: CommandHandler<PriceCreateInput, { priceId: string }> = {\n id: 'catalog.prices.create',\n async execute(rawInput, ctx) {\n const { parsed, custom } = parseWithCustomFields(priceCreateSchema, rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const actorScope = commandActorScope(ctx)\n let variant: CatalogProductVariant | null = null\n let product: CatalogProduct | null = null\n if (parsed.variantId) {\n variant = await requireVariant(em, parsed.variantId, actorScope)\n product =\n typeof variant.product === 'string'\n ? await requireProduct(em, variant.product, {\n tenantId: variant.tenantId,\n organizationId: variant.organizationId,\n })\n : variant.product\n }\n if (parsed.productId) {\n const explicitProduct = await requireProduct(em, parsed.productId, actorScope)\n if (product && explicitProduct.id !== product.id) {\n throw new CrudHttpError(400, { error: 'Variant does not belong to the provided product.' })\n }\n product = explicitProduct\n }\n if (!variant && !product) {\n throw new CrudHttpError(400, { error: 'Provide either a variantId or productId for pricing.' })\n }\n const scopeSource = variant ?? product!\n ensureTenantScope(ctx, scopeSource.tenantId)\n ensureOrganizationScope(ctx, scopeSource.organizationId)\n const scopeSourceScope = {\n tenantId: scopeSource.tenantId,\n organizationId: scopeSource.organizationId,\n }\n\n const priceKind = await requirePriceKind(em, parsed.priceKindId, scopeSourceScope)\n ensureSameTenant(priceKind, scopeSource.tenantId)\n\n let offer: CatalogOffer | null = null\n if (parsed.offerId) {\n offer = await requireOffer(em, parsed.offerId, scopeSourceScope)\n ensureSameScope(offer, scopeSource.organizationId, scopeSource.tenantId)\n const offerProduct =\n typeof offer.product === 'string'\n ? await requireProduct(em, offer.product, {\n tenantId: offer.tenantId,\n organizationId: offer.organizationId,\n })\n : offer.product\n if (product && offerProduct.id !== product.id) {\n throw new CrudHttpError(400, { error: 'Offer does not belong to the selected product.' })\n }\n product = offerProduct\n }\n const productEntity = product\n const channelId = parsed.channelId ?? (offer ? offer.channelId : null)\n const taxCalculationService = ctx.container.resolve<TaxCalculationService>('taxCalculationService')\n const amountInput = resolveAmountInputFromParsed(parsed)\n let unitPriceNetValue = toNumericString(parsed.unitPriceNet)\n let unitPriceGrossValue = toNumericString(parsed.unitPriceGross)\n let taxRateValue = toNumericString(parsed.taxRate)\n let taxAmountValue: string | null = null\n if (amountInput) {\n const calculation = await taxCalculationService.calculateUnitAmounts({\n amount: amountInput.amount,\n mode: amountInput.mode,\n organizationId: scopeSource.organizationId,\n tenantId: scopeSource.tenantId,\n taxRateId: parsed.taxRateId ?? null,\n taxRate: parsed.taxRate ?? null,\n })\n unitPriceNetValue = toNumericString(calculation.netAmount)\n unitPriceGrossValue = toNumericString(calculation.grossAmount)\n taxAmountValue = toNumericString(calculation.taxAmount)\n taxRateValue = toNumericString(calculation.taxRate)\n }\n\n const now = new Date()\n const record = em.create(CatalogProductPrice, {\n organizationId: scopeSource.organizationId,\n tenantId: scopeSource.tenantId,\n variant,\n product: productEntity ?? undefined,\n offer: offer ?? undefined,\n priceKind,\n currencyCode: parsed.currencyCode,\n kind: priceKind.code,\n minQuantity: parsed.minQuantity ?? 1,\n maxQuantity: parsed.maxQuantity ?? null,\n unitPriceNet: unitPriceNetValue,\n unitPriceGross: unitPriceGrossValue,\n taxRate: taxRateValue,\n taxAmount: taxAmountValue,\n channelId,\n userId: parsed.userId ?? null,\n userGroupId: parsed.userGroupId ?? null,\n customerId: parsed.customerId ?? null,\n customerGroupId: parsed.customerGroupId ?? null,\n metadata: parsed.metadata ? cloneJson(parsed.metadata) : null,\n startsAt: parsed.startsAt ?? null,\n endsAt: parsed.endsAt ?? null,\n createdAt: now,\n updatedAt: now,\n })\n em.persist(record)\n await em.flush()\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: record.id,\n organizationId: record.organizationId,\n tenantId: record.tenantId,\n values: custom,\n })\n const de = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'created',\n entity: record,\n identifiers: {\n id: record.id,\n organizationId: record.organizationId,\n tenantId: record.tenantId,\n },\n events: priceCrudEvents,\n })\n await capturePriceHistoryEntry(em, priceHistoryInputFromRecord(record), 'create', {\n cache: resolveOmnibusCache(ctx.container),\n })\n return { priceId: record.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return loadPriceSnapshot(em, result.priceId)\n },\n buildLog: async ({ result, snapshots }) => {\n const after = snapshots.after as PriceSnapshot | undefined\n if (!after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('catalog.audit.prices.create', 'Create product price'),\n resourceKind: 'catalog.price',\n resourceId: result.priceId,\n tenantId: after.tenantId,\n organizationId: after.organizationId,\n snapshotAfter: after,\n payload: {\n undo: {\n after,\n } satisfies PriceUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PriceUndoPayload>(logEntry)\n const after = payload?.after\n if (!after) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const record = await em.findOne(CatalogProductPrice, { id: after.id })\n if (!record) return\n ensureTenantScope(ctx, record.tenantId)\n ensureOrganizationScope(ctx, record.organizationId)\n em.remove(record)\n await em.flush()\n await capturePriceHistoryEntry(em, after, 'undo', {\n metadata: { undoneCommand: 'catalog.prices.create' },\n cache: resolveOmnibusCache(ctx.container),\n })\n const resetValues = buildCustomFieldResetMap(undefined, after.custom ?? undefined)\n if (Object.keys(resetValues).length) {\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: after.id,\n organizationId: after.organizationId,\n tenantId: after.tenantId,\n values: resetValues,\n })\n }\n },\n redo: makeCreateRedo<CatalogProductPrice, PriceSnapshot, PriceCreateInput, { priceId: string }>({\n entityClass: CatalogProductPrice,\n getSnapshotId: (snapshot) => snapshot.id,\n seedFromSnapshot: priceSeedFromSnapshot,\n buildResult: (entity) => ({ priceId: entity.id }),\n events: priceCrudEvents,\n afterRestore: async ({ em, ctx, entity, snapshot }) => {\n if (snapshot.custom && Object.keys(snapshot.custom).length) {\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: entity.id,\n organizationId: entity.organizationId,\n tenantId: entity.tenantId,\n values: snapshot.custom,\n })\n }\n await capturePriceHistoryEntry(em, snapshot, 'create', {\n metadata: { redoneCommand: 'catalog.prices.create' },\n cache: resolveOmnibusCache(ctx.container),\n })\n },\n }),\n}\n\nconst updatePriceCommand: CommandHandler<PriceUpdateInput, { priceId: string }> = {\n id: 'catalog.prices.update',\n async prepare(input, ctx) {\n const id = requireId(input, 'Price id is required')\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadPriceSnapshot(em, id, commandScopeWhere(ctx))\n if (snapshot) {\n ensureTenantScope(ctx, snapshot.tenantId)\n ensureOrganizationScope(ctx, snapshot.organizationId)\n }\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const { parsed, custom } = parseWithCustomFields(priceUpdateSchema, rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const record = await findOneWithDecryption(\n em,\n CatalogProductPrice,\n { id: parsed.id, ...commandScopeWhere(ctx) },\n { populate: ['priceKind'] },\n { tenantId: parsed.tenantId, organizationId: parsed.organizationId },\n )\n if (!record) throw new CrudHttpError(404, { error: 'Catalog price not found' })\n const recordScope = { tenantId: record.tenantId, organizationId: record.organizationId }\n const currentVariantRef = record.variant\n let targetVariant: CatalogProductVariant | null = null\n if (typeof currentVariantRef === 'string') {\n targetVariant = await requireVariant(em, currentVariantRef, recordScope)\n } else if (currentVariantRef) {\n targetVariant = currentVariantRef\n }\n const currentProductRef = record.product ?? (targetVariant ? targetVariant.product : null)\n let targetProduct: CatalogProduct | null = null\n if (typeof currentProductRef === 'string') {\n targetProduct = await requireProduct(em, currentProductRef, recordScope)\n } else if (currentProductRef) {\n targetProduct = currentProductRef\n }\n const recordHistoryInput = priceHistoryInputFromRecord(record)\n const historyBefore = {\n ...recordHistoryInput,\n productId: recordHistoryInput.productId ?? targetProduct?.id ?? null,\n }\n\n if (parsed.variantId !== undefined) {\n if (!parsed.variantId) {\n targetVariant = null\n } else {\n targetVariant = await requireVariant(em, parsed.variantId, recordScope)\n targetProduct =\n typeof targetVariant.product === 'string'\n ? await requireProduct(em, targetVariant.product, recordScope)\n : targetVariant.product\n }\n }\n\n if (targetVariant && (targetVariant as CatalogProductVariant | null)?.product === undefined) {\n targetVariant = await requireVariant(em, targetVariant.id, recordScope)\n }\n\n if (parsed.productId !== undefined) {\n if (!parsed.productId) {\n targetProduct = null\n } else {\n const explicitProduct = await requireProduct(em, parsed.productId, recordScope)\n if (targetVariant) {\n const variantProductId =\n typeof targetVariant.product === 'string'\n ? targetVariant.product\n : targetVariant.product.id\n if (variantProductId !== explicitProduct.id) {\n throw new CrudHttpError(400, { error: 'Variant does not belong to the provided product.' })\n }\n }\n targetProduct = explicitProduct\n }\n }\n\n if (!targetVariant && !targetProduct) {\n throw new CrudHttpError(400, { error: 'Price must remain associated with a product or variant.' })\n }\n if (!targetProduct && targetVariant) {\n targetProduct =\n typeof targetVariant.product === 'string'\n ? await requireProduct(em, targetVariant.product, recordScope)\n : targetVariant.product\n }\n if (!targetProduct) {\n throw new CrudHttpError(400, { error: 'Unable to resolve product for price.' })\n }\n\n let targetOffer: CatalogOffer | null = null\n if (record.offer) {\n targetOffer =\n typeof record.offer === 'string'\n ? await requireOffer(em, record.offer, recordScope)\n : record.offer\n }\n if (parsed.offerId !== undefined) {\n if (!parsed.offerId) {\n targetOffer = null\n } else {\n const explicitOffer = await requireOffer(em, parsed.offerId, recordScope)\n ensureSameScope(explicitOffer, targetProduct.organizationId, targetProduct.tenantId)\n const offerProductId =\n typeof explicitOffer.product === 'string'\n ? explicitOffer.product\n : explicitOffer.product.id\n if (offerProductId !== targetProduct.id) {\n throw new CrudHttpError(400, { error: 'Offer does not belong to the selected product.' })\n }\n targetOffer = explicitOffer\n }\n }\n\n ensureTenantScope(ctx, targetProduct.tenantId)\n ensureOrganizationScope(ctx, targetProduct.organizationId)\n\n let targetPriceKind: CatalogPriceKind | null = null\n if (record.priceKind) {\n targetPriceKind =\n typeof record.priceKind === 'string'\n ? await requirePriceKind(em, record.priceKind, recordScope)\n : record.priceKind\n }\n if (parsed.priceKindId !== undefined) {\n if (!parsed.priceKindId) {\n throw new CrudHttpError(400, { error: 'Price kind is required.' })\n }\n targetPriceKind = await requirePriceKind(em, parsed.priceKindId, recordScope)\n }\n if (!targetPriceKind) {\n throw new CrudHttpError(400, { error: 'Price kind is required.' })\n }\n ensureSameTenant(targetPriceKind, targetProduct.tenantId)\n\n const taxCalculationService = ctx.container.resolve<TaxCalculationService>('taxCalculationService')\n const amountInput = resolveAmountInputForUpdate(parsed, record)\n const hasNetInput = Object.prototype.hasOwnProperty.call(parsed, 'unitPriceNet')\n const hasGrossInput = Object.prototype.hasOwnProperty.call(parsed, 'unitPriceGross')\n const hasTaxRateInput = Object.prototype.hasOwnProperty.call(parsed, 'taxRate')\n const hasTaxRateIdInput = Object.prototype.hasOwnProperty.call(parsed, 'taxRateId')\n let taxCalculationResult: Awaited<ReturnType<TaxCalculationService['calculateUnitAmounts']>> | null = null\n let calculationBase = amountInput\n if (!calculationBase && (hasTaxRateInput || hasTaxRateIdInput)) {\n calculationBase = resolveAmountInputFromRecord(record)\n }\n if (calculationBase) {\n const taxRateIdForCalculation = hasTaxRateIdInput ? parsed.taxRateId ?? null : undefined\n const taxRateForCalculation = hasTaxRateInput\n ? parsed.taxRate ?? null\n : taxRateIdForCalculation === null\n ? null\n : record.taxRate ?? null\n taxCalculationResult = await taxCalculationService.calculateUnitAmounts({\n amount: calculationBase.amount,\n mode: calculationBase.mode,\n organizationId: targetProduct.organizationId,\n tenantId: targetProduct.tenantId,\n taxRateId: taxRateIdForCalculation,\n taxRate: taxRateForCalculation,\n })\n }\n\n record.variant = targetVariant\n record.product = targetProduct\n record.offer = targetOffer\n record.organizationId = targetProduct.organizationId\n record.tenantId = targetProduct.tenantId\n record.priceKind = targetPriceKind\n record.kind = targetPriceKind.code\n\n if (parsed.currencyCode !== undefined) record.currencyCode = parsed.currencyCode\n if (parsed.minQuantity !== undefined) record.minQuantity = parsed.minQuantity ?? 1\n if (parsed.maxQuantity !== undefined) record.maxQuantity = parsed.maxQuantity ?? null\n if (taxCalculationResult) {\n record.unitPriceNet = toNumericString(taxCalculationResult.netAmount)\n record.unitPriceGross = toNumericString(taxCalculationResult.grossAmount)\n record.taxRate = toNumericString(taxCalculationResult.taxRate)\n record.taxAmount = toNumericString(taxCalculationResult.taxAmount)\n } else {\n if (hasNetInput) {\n record.unitPriceNet = toNumericString(parsed.unitPriceNet)\n }\n if (hasGrossInput) {\n record.unitPriceGross = toNumericString(parsed.unitPriceGross)\n }\n if (hasTaxRateInput) {\n record.taxRate = toNumericString(parsed.taxRate)\n if (parsed.taxRate == null) {\n record.taxAmount = null\n }\n } else if (hasTaxRateIdInput && parsed.taxRateId === null) {\n record.taxRate = null\n record.taxAmount = null\n }\n }\n if (parsed.channelId !== undefined) {\n record.channelId = parsed.channelId ?? null\n } else if (parsed.offerId !== undefined && targetOffer) {\n record.channelId = targetOffer.channelId\n }\n if (parsed.userId !== undefined) record.userId = parsed.userId ?? null\n if (parsed.userGroupId !== undefined) record.userGroupId = parsed.userGroupId ?? null\n if (parsed.customerId !== undefined) record.customerId = parsed.customerId ?? null\n if (parsed.customerGroupId !== undefined) record.customerGroupId = parsed.customerGroupId ?? null\n if (parsed.metadata !== undefined) {\n record.metadata = parsed.metadata ? cloneJson(parsed.metadata) : null\n }\n if (Object.prototype.hasOwnProperty.call(parsed, 'startsAt')) {\n record.startsAt = parsed.startsAt ?? null\n }\n if (Object.prototype.hasOwnProperty.call(parsed, 'endsAt')) {\n record.endsAt = parsed.endsAt ?? null\n }\n await em.flush()\n if (custom && Object.keys(custom).length) {\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: record.id,\n organizationId: record.organizationId,\n tenantId: record.tenantId,\n values: custom,\n })\n }\n const de = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: record,\n identifiers: {\n id: record.id,\n organizationId: record.organizationId,\n tenantId: record.tenantId,\n },\n events: priceCrudEvents,\n })\n const historyAfter = priceHistoryInputFromRecord(record)\n if (hasPriceHistoryRelevantChange(historyBefore, historyAfter)) {\n await capturePriceHistoryEntry(em, historyAfter, 'update', {\n cache: resolveOmnibusCache(ctx.container),\n invalidatePrices: [historyBefore],\n previous: historyBefore,\n })\n }\n return { priceId: record.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return loadPriceSnapshot(em, result.priceId)\n },\n buildLog: async ({ snapshots }) => {\n const before = snapshots.before as PriceSnapshot | undefined\n const after = snapshots.after as PriceSnapshot | undefined\n if (!before || !after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('catalog.audit.prices.update', 'Update product price'),\n resourceKind: 'catalog.price',\n resourceId: before.id,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n snapshotAfter: after,\n changes: buildChanges(\n before as Record<string, unknown>,\n after as Record<string, unknown>,\n PRICE_CHANGE_KEYS\n ),\n payload: {\n undo: {\n before,\n after,\n } satisfies PriceUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PriceUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const after = payload?.after\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n let record = await em.findOne(CatalogProductPrice, { id: before.id })\n if (!record) {\n const { variant, product, offer } = await resolveSnapshotAssociations(em, before)\n record = em.create(CatalogProductPrice, {\n id: before.id,\n variant,\n product,\n offer,\n organizationId: before.organizationId,\n tenantId: before.tenantId,\n currencyCode: before.currencyCode,\n priceKind: em.getReference(CatalogPriceKind, before.priceKindId),\n kind: before.priceKindCode || before.kind,\n minQuantity: before.minQuantity,\n maxQuantity: before.maxQuantity ?? null,\n unitPriceNet: before.unitPriceNet ?? null,\n unitPriceGross: before.unitPriceGross ?? null,\n taxRate: before.taxRate ?? null,\n taxAmount: before.taxAmount ?? null,\n channelId: before.channelId ?? null,\n userId: before.userId ?? null,\n userGroupId: before.userGroupId ?? null,\n customerId: before.customerId ?? null,\n customerGroupId: before.customerGroupId ?? null,\n metadata: before.metadata ? cloneJson(before.metadata) : null,\n startsAt: before.startsAt ? new Date(before.startsAt) : null,\n endsAt: before.endsAt ? new Date(before.endsAt) : null,\n createdAt: new Date(before.createdAt),\n updatedAt: new Date(before.updatedAt),\n })\n em.persist(record)\n }\n ensureTenantScope(ctx, before.tenantId)\n ensureOrganizationScope(ctx, before.organizationId)\n applyPriceSnapshot(em, record, before)\n await em.flush()\n await capturePriceHistoryEntry(em, before, 'undo', {\n metadata: { undoneCommand: 'catalog.prices.update' },\n cache: resolveOmnibusCache(ctx.container),\n invalidatePrices: [after],\n previous: after,\n })\n const resetValues = buildCustomFieldResetMap(\n before.custom ?? undefined,\n after?.custom ?? undefined\n )\n if (Object.keys(resetValues).length) {\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: before.id,\n organizationId: before.organizationId,\n tenantId: before.tenantId,\n values: resetValues,\n })\n }\n },\n}\n\nconst deletePriceCommand: CommandHandler<\n { body?: Record<string, unknown>; query?: Record<string, unknown> },\n { priceId: string }\n> = {\n id: 'catalog.prices.delete',\n async prepare(input, ctx) {\n const id = requireId(input, 'Price id is required')\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadPriceSnapshot(em, id, commandScopeWhere(ctx))\n if (snapshot) {\n ensureTenantScope(ctx, snapshot.tenantId)\n ensureOrganizationScope(ctx, snapshot.organizationId)\n }\n return snapshot ? { before: snapshot } : {}\n },\n async execute(input, ctx) {\n const id = requireId(input, 'Price id is required')\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const record = await findOneWithDecryption(em, CatalogProductPrice, { id, ...commandScopeWhere(ctx) })\n if (!record) throw new CrudHttpError(404, { error: 'Catalog price not found' })\n ensureTenantScope(ctx, record.tenantId)\n ensureOrganizationScope(ctx, record.organizationId)\n const { product } = await resolvePriceRecordAssociations(em, record)\n\n const baseEm = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadPriceSnapshot(baseEm, id)\n\n const deletedPrice = snapshot ?? priceHistoryInputFromRecord(record)\n em.remove(record)\n await em.flush()\n await capturePriceHistoryEntry(em, deletedPrice, 'delete', { cache: resolveOmnibusCache(ctx.container) })\n if (snapshot?.custom && Object.keys(snapshot.custom).length) {\n const resetValues = buildCustomFieldResetMap(snapshot.custom, undefined)\n if (Object.keys(resetValues).length) {\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: id,\n organizationId: snapshot.organizationId,\n tenantId: snapshot.tenantId,\n values: resetValues,\n })\n }\n }\n const de = ctx.container.resolve('dataEngine') as DataEngine\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'deleted',\n entity: record,\n identifiers: {\n id,\n organizationId: snapshot?.organizationId ?? record.organizationId,\n tenantId: snapshot?.tenantId ?? record.tenantId,\n },\n events: priceCrudEvents,\n })\n return { priceId: id }\n },\n buildLog: async ({ snapshots }) => {\n const before = snapshots.before as PriceSnapshot | undefined\n if (!before) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('catalog.audit.prices.delete', 'Delete product price'),\n resourceKind: 'catalog.price',\n resourceId: before.id,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n payload: {\n undo: {\n before,\n } satisfies PriceUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PriceUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n let record = await em.findOne(CatalogProductPrice, { id: before.id })\n if (!record) {\n const { variant, product, offer } = await resolveSnapshotAssociations(em, before)\n record = em.create(CatalogProductPrice, {\n id: before.id,\n variant,\n product,\n offer,\n organizationId: before.organizationId,\n tenantId: before.tenantId,\n currencyCode: before.currencyCode,\n priceKind: em.getReference(CatalogPriceKind, before.priceKindId),\n kind: before.priceKindCode || before.kind,\n minQuantity: before.minQuantity,\n maxQuantity: before.maxQuantity ?? null,\n unitPriceNet: before.unitPriceNet ?? null,\n unitPriceGross: before.unitPriceGross ?? null,\n taxRate: before.taxRate ?? null,\n taxAmount: before.taxAmount ?? null,\n channelId: before.channelId ?? null,\n userId: before.userId ?? null,\n userGroupId: before.userGroupId ?? null,\n customerId: before.customerId ?? null,\n customerGroupId: before.customerGroupId ?? null,\n metadata: before.metadata ? cloneJson(before.metadata) : null,\n startsAt: before.startsAt ? new Date(before.startsAt) : null,\n endsAt: before.endsAt ? new Date(before.endsAt) : null,\n createdAt: new Date(before.createdAt),\n updatedAt: new Date(before.updatedAt),\n })\n em.persist(record)\n }\n ensureTenantScope(ctx, before.tenantId)\n ensureOrganizationScope(ctx, before.organizationId)\n applyPriceSnapshot(em, record, before)\n await em.flush()\n await capturePriceHistoryEntry(em, before, 'undo', {\n metadata: { undoneCommand: 'catalog.prices.delete' },\n cache: resolveOmnibusCache(ctx.container),\n })\n if (before.custom && Object.keys(before.custom).length) {\n await setCustomFieldsIfAny({\n dataEngine: ctx.container.resolve('dataEngine'),\n entityId: E.catalog.catalog_product_price,\n recordId: before.id,\n organizationId: before.organizationId,\n tenantId: before.tenantId,\n values: before.custom,\n })\n }\n },\n}\n\nregisterCommand(createPriceCommand)\nregisterCommand(updatePriceCommand)\nregisterCommand(deletePriceCommand)\n\nasync function resolvePriceRecordAssociations(\n em: EntityManager,\n record: CatalogProductPrice,\n): Promise<{ product: CatalogProduct; variant: CatalogProductVariant | null }> {\n const scope = { tenantId: record.tenantId, organizationId: record.organizationId }\n const variant = record.variant\n ? (typeof record.variant === 'string'\n ? await requireVariant(em, record.variant, scope)\n : record.variant)\n : null\n if (record.product) {\n const product =\n typeof record.product === 'string'\n ? await requireProduct(em, record.product, scope)\n : record.product\n return { product, variant }\n }\n if (variant?.product) {\n const productRef = variant.product\n const product =\n typeof productRef === 'string'\n ? await requireProduct(em, productRef, scope)\n : productRef\n return { product, variant }\n }\n if (record.offer) {\n const offer =\n typeof record.offer === 'string'\n ? await requireOffer(em, record.offer, scope)\n : record.offer\n const productRef = offer?.product ?? null\n if (productRef) {\n const product =\n typeof productRef === 'string'\n ? await requireProduct(em, productRef, scope)\n : productRef\n return { product, variant }\n }\n }\n throw new CrudHttpError(400, { error: 'Catalog price is not linked to a product.' })\n}\n"],
5
+ "mappings": "AAAA,SAAS,uBAAuB;AAEhC,SAAS,cAAc,WAAW,uBAAuB,sBAAsB,2BAA2B;AAE1G,SAAS,qBAAqB;AAC9B,SAAS,2BAA2B;AACpC,SAAuC,qBAA4C,wBAAwB;AAC3G,SAAS,yBAAyB,gCAAgC;AAClE,SAAS,SAAS;AAClB;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAEP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,6BAA6B;AACtC,SAAS,sBAAsB;AAG/B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,2BAA2B;AAEpC,MAAM,kBAAoC;AAAA,EACxC,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;AAqCA,MAAM,oBAAoB;AAAA,EACxB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,eAAe,4BACb,IACA,UAKC;AACD,QAAM,QAAQ,EAAE,UAAU,SAAS,UAAU,gBAAgB,SAAS,eAAe;AACrF,MAAI,UAAwC;AAC5C,MAAI,SAAS,WAAW;AACtB,cAAU,MAAM,eAAe,IAAI,SAAS,WAAW,KAAK;AAAA,EAC9D;AACA,MAAI,UAAiC;AACrC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E,SAAS,QAAQ;AAAA,EAC5B;AACA,MAAI,OAAO,OAAO;AAChB,UAAM,QACJ,OAAO,OAAO,UAAU,WACpB,MAAM,aAAa,IAAI,OAAO,OAAO,KAAK,IAC1C,OAAO;AACb,UAAM,aAAa,OAAO,WAAW;AACrC,QAAI,YAAY;AACd,YAAM,UACJ,OAAO,eAAe,WAClB,MAAM,eAAe,IAAI,YAAY,KAAK,IAC1C;AACN,aAAO,EAAE,SAAS,QAAQ;AAAA,IAC5B;AAAA,EACF;AACA,QAAM,IAAI,cAAc,KAAK,EAAE,OAAO,4CAA4C,CAAC;AACrF;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-mercato/core",
3
- "version": "0.10.1-develop.7781.1.079f42c863",
3
+ "version": "0.10.1-develop.7782.1.91d78919ba",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -266,16 +266,16 @@
266
266
  "zod": "^4.4.3"
267
267
  },
268
268
  "peerDependencies": {
269
- "@open-mercato/ai-assistant": "0.10.1-develop.7781.1.079f42c863",
270
- "@open-mercato/shared": "0.10.1-develop.7781.1.079f42c863",
271
- "@open-mercato/ui": "0.10.1-develop.7781.1.079f42c863",
269
+ "@open-mercato/ai-assistant": "0.10.1-develop.7782.1.91d78919ba",
270
+ "@open-mercato/shared": "0.10.1-develop.7782.1.91d78919ba",
271
+ "@open-mercato/ui": "0.10.1-develop.7782.1.91d78919ba",
272
272
  "react": "^19.0.0",
273
273
  "react-dom": "^19.0.0"
274
274
  },
275
275
  "devDependencies": {
276
- "@open-mercato/ai-assistant": "0.10.1-develop.7781.1.079f42c863",
277
- "@open-mercato/shared": "0.10.1-develop.7781.1.079f42c863",
278
- "@open-mercato/ui": "0.10.1-develop.7781.1.079f42c863",
276
+ "@open-mercato/ai-assistant": "0.10.1-develop.7782.1.91d78919ba",
277
+ "@open-mercato/shared": "0.10.1-develop.7782.1.91d78919ba",
278
+ "@open-mercato/ui": "0.10.1-develop.7782.1.91d78919ba",
279
279
  "@testing-library/dom": "^10.4.1",
280
280
  "@testing-library/jest-dom": "^7.0.1",
281
281
  "@testing-library/react": "^16.3.3",
@@ -287,6 +287,24 @@ function resolveAmountInputFromParsed(
287
287
  return null
288
288
  }
289
289
 
290
+ function hasAmountChanged(incoming: unknown, current: string | null | undefined): incoming is number {
291
+ if (typeof incoming !== 'number' || !Number.isFinite(incoming)) return false
292
+ return numericStringToNumber(current) !== incoming
293
+ }
294
+
295
+ function resolveAmountInputForUpdate(
296
+ parsed: Partial<Pick<PriceCreateInput, 'unitPriceNet' | 'unitPriceGross'>>,
297
+ record: CatalogProductPrice
298
+ ): PriceAmountInput | null {
299
+ if (hasAmountChanged(parsed.unitPriceNet, record.unitPriceNet)) {
300
+ return { amount: parsed.unitPriceNet, mode: 'net' }
301
+ }
302
+ if (hasAmountChanged(parsed.unitPriceGross, record.unitPriceGross)) {
303
+ return { amount: parsed.unitPriceGross, mode: 'gross' }
304
+ }
305
+ return resolveAmountInputFromParsed(parsed)
306
+ }
307
+
290
308
  function resolveAmountInputFromRecord(record: CatalogProductPrice): PriceAmountInput | null {
291
309
  const net = numericStringToNumber(record.unitPriceNet)
292
310
  if (net !== null) return { amount: net, mode: 'net' }
@@ -644,7 +662,7 @@ const updatePriceCommand: CommandHandler<PriceUpdateInput, { priceId: string }>
644
662
  ensureSameTenant(targetPriceKind, targetProduct.tenantId)
645
663
 
646
664
  const taxCalculationService = ctx.container.resolve<TaxCalculationService>('taxCalculationService')
647
- const amountInput = resolveAmountInputFromParsed(parsed)
665
+ const amountInput = resolveAmountInputForUpdate(parsed, record)
648
666
  const hasNetInput = Object.prototype.hasOwnProperty.call(parsed, 'unitPriceNet')
649
667
  const hasGrossInput = Object.prototype.hasOwnProperty.call(parsed, 'unitPriceGross')
650
668
  const hasTaxRateInput = Object.prototype.hasOwnProperty.call(parsed, 'taxRate')