@open-mercato/core 0.4.2-canary-36ab8921da → 0.4.2-canary-07dbc98202

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.
Files changed (235) hide show
  1. package/dist/generated/entities/notification/index.js +57 -0
  2. package/dist/generated/entities/notification/index.js.map +7 -0
  3. package/dist/generated/entities.ids.generated.js +63 -59
  4. package/dist/generated/entities.ids.generated.js.map +2 -2
  5. package/dist/generated/entity-fields-registry.js +2 -0
  6. package/dist/generated/entity-fields-registry.js.map +2 -2
  7. package/dist/modules/api_docs/frontend/docs/api/page.js +3 -2
  8. package/dist/modules/api_docs/frontend/docs/api/page.js.map +2 -2
  9. package/dist/modules/auth/api/admin/nav.js +4 -3
  10. package/dist/modules/auth/api/admin/nav.js.map +2 -2
  11. package/dist/modules/auth/api/profile/route.js +155 -0
  12. package/dist/modules/auth/api/profile/route.js.map +7 -0
  13. package/dist/modules/auth/api/reset/confirm.js +25 -2
  14. package/dist/modules/auth/api/reset/confirm.js.map +2 -2
  15. package/dist/modules/auth/api/reset.js +23 -0
  16. package/dist/modules/auth/api/reset.js.map +2 -2
  17. package/dist/modules/auth/api/sidebar/preferences/route.js +14 -9
  18. package/dist/modules/auth/api/sidebar/preferences/route.js.map +2 -2
  19. package/dist/modules/auth/backend/auth/profile/page.js +99 -0
  20. package/dist/modules/auth/backend/auth/profile/page.js.map +7 -0
  21. package/dist/modules/auth/backend/auth/profile/page.meta.js +12 -0
  22. package/dist/modules/auth/backend/auth/profile/page.meta.js.map +7 -0
  23. package/dist/modules/auth/commands/users.js +55 -0
  24. package/dist/modules/auth/commands/users.js.map +2 -2
  25. package/dist/modules/auth/lib/setup-app.js +1 -0
  26. package/dist/modules/auth/lib/setup-app.js.map +2 -2
  27. package/dist/modules/auth/notifications.js +112 -0
  28. package/dist/modules/auth/notifications.js.map +7 -0
  29. package/dist/modules/auth/services/authService.js +3 -3
  30. package/dist/modules/auth/services/authService.js.map +2 -2
  31. package/dist/modules/business_rules/notifications.js +28 -0
  32. package/dist/modules/business_rules/notifications.js.map +7 -0
  33. package/dist/modules/business_rules/subscribers/rule-execution-failed-notification.js +37 -0
  34. package/dist/modules/business_rules/subscribers/rule-execution-failed-notification.js.map +7 -0
  35. package/dist/modules/catalog/notifications.js +28 -0
  36. package/dist/modules/catalog/notifications.js.map +7 -0
  37. package/dist/modules/catalog/subscribers/low-stock-notification.js +38 -0
  38. package/dist/modules/catalog/subscribers/low-stock-notification.js.map +7 -0
  39. package/dist/modules/configs/cli.js +6 -0
  40. package/dist/modules/configs/cli.js.map +2 -2
  41. package/dist/modules/customers/commands/deals.js +31 -0
  42. package/dist/modules/customers/commands/deals.js.map +2 -2
  43. package/dist/modules/customers/notifications.js +48 -0
  44. package/dist/modules/customers/notifications.js.map +7 -0
  45. package/dist/modules/notifications/acl.js +11 -0
  46. package/dist/modules/notifications/acl.js.map +7 -0
  47. package/dist/modules/notifications/api/[id]/action/route.js +74 -0
  48. package/dist/modules/notifications/api/[id]/action/route.js.map +7 -0
  49. package/dist/modules/notifications/api/[id]/dismiss/route.js +15 -0
  50. package/dist/modules/notifications/api/[id]/dismiss/route.js.map +7 -0
  51. package/dist/modules/notifications/api/[id]/read/route.js +15 -0
  52. package/dist/modules/notifications/api/[id]/read/route.js.map +7 -0
  53. package/dist/modules/notifications/api/[id]/restore/route.js +53 -0
  54. package/dist/modules/notifications/api/[id]/restore/route.js.map +7 -0
  55. package/dist/modules/notifications/api/batch/route.js +17 -0
  56. package/dist/modules/notifications/api/batch/route.js.map +7 -0
  57. package/dist/modules/notifications/api/feature/route.js +17 -0
  58. package/dist/modules/notifications/api/feature/route.js.map +7 -0
  59. package/dist/modules/notifications/api/mark-all-read/route.js +35 -0
  60. package/dist/modules/notifications/api/mark-all-read/route.js.map +7 -0
  61. package/dist/modules/notifications/api/openapi.js +76 -0
  62. package/dist/modules/notifications/api/openapi.js.map +7 -0
  63. package/dist/modules/notifications/api/role/route.js +17 -0
  64. package/dist/modules/notifications/api/role/route.js.map +7 -0
  65. package/dist/modules/notifications/api/route.js +85 -0
  66. package/dist/modules/notifications/api/route.js.map +7 -0
  67. package/dist/modules/notifications/api/settings/route.js +155 -0
  68. package/dist/modules/notifications/api/settings/route.js.map +7 -0
  69. package/dist/modules/notifications/api/unread-count/route.js +38 -0
  70. package/dist/modules/notifications/api/unread-count/route.js.map +7 -0
  71. package/dist/modules/notifications/backend/config/notifications/page.js +10 -0
  72. package/dist/modules/notifications/backend/config/notifications/page.js.map +7 -0
  73. package/dist/modules/notifications/backend/config/notifications/page.meta.js +24 -0
  74. package/dist/modules/notifications/backend/config/notifications/page.meta.js.map +7 -0
  75. package/dist/modules/notifications/cli.js +16 -0
  76. package/dist/modules/notifications/cli.js.map +7 -0
  77. package/dist/modules/notifications/data/entities.js +112 -0
  78. package/dist/modules/notifications/data/entities.js.map +7 -0
  79. package/dist/modules/notifications/data/validators.js +94 -0
  80. package/dist/modules/notifications/data/validators.js.map +7 -0
  81. package/dist/modules/notifications/di.js +13 -0
  82. package/dist/modules/notifications/di.js.map +7 -0
  83. package/dist/modules/notifications/emails/NotificationEmail.js +58 -0
  84. package/dist/modules/notifications/emails/NotificationEmail.js.map +7 -0
  85. package/dist/modules/notifications/frontend/NotificationInboxPageClient.js +44 -0
  86. package/dist/modules/notifications/frontend/NotificationInboxPageClient.js.map +7 -0
  87. package/dist/modules/notifications/frontend/NotificationSettingsPageClient.js +219 -0
  88. package/dist/modules/notifications/frontend/NotificationSettingsPageClient.js.map +7 -0
  89. package/dist/modules/notifications/index.js +14 -0
  90. package/dist/modules/notifications/index.js.map +7 -0
  91. package/dist/modules/notifications/lib/deliveryConfig.js +105 -0
  92. package/dist/modules/notifications/lib/deliveryConfig.js.map +7 -0
  93. package/dist/modules/notifications/lib/events.js +12 -0
  94. package/dist/modules/notifications/lib/events.js.map +7 -0
  95. package/dist/modules/notifications/lib/notificationBuilder.js +66 -0
  96. package/dist/modules/notifications/lib/notificationBuilder.js.map +7 -0
  97. package/dist/modules/notifications/lib/notificationFactory.js +54 -0
  98. package/dist/modules/notifications/lib/notificationFactory.js.map +7 -0
  99. package/dist/modules/notifications/lib/notificationMapper.js +34 -0
  100. package/dist/modules/notifications/lib/notificationMapper.js.map +7 -0
  101. package/dist/modules/notifications/lib/notificationRecipients.js +35 -0
  102. package/dist/modules/notifications/lib/notificationRecipients.js.map +7 -0
  103. package/dist/modules/notifications/lib/notificationService.js +279 -0
  104. package/dist/modules/notifications/lib/notificationService.js.map +7 -0
  105. package/dist/modules/notifications/lib/routeHelpers.js +101 -0
  106. package/dist/modules/notifications/lib/routeHelpers.js.map +7 -0
  107. package/dist/modules/notifications/lib/safeHref.js +24 -0
  108. package/dist/modules/notifications/lib/safeHref.js.map +7 -0
  109. package/dist/modules/notifications/migrations/Migration20260123000001.js +70 -0
  110. package/dist/modules/notifications/migrations/Migration20260123000001.js.map +7 -0
  111. package/dist/modules/notifications/migrations/Migration20260126150000.js +37 -0
  112. package/dist/modules/notifications/migrations/Migration20260126150000.js.map +7 -0
  113. package/dist/modules/notifications/subscribers/deliver-notification.js +139 -0
  114. package/dist/modules/notifications/subscribers/deliver-notification.js.map +7 -0
  115. package/dist/modules/notifications/workers/create-notification.worker.js +70 -0
  116. package/dist/modules/notifications/workers/create-notification.worker.js.map +7 -0
  117. package/dist/modules/sales/commands/documents.js +53 -0
  118. package/dist/modules/sales/commands/documents.js.map +2 -2
  119. package/dist/modules/sales/commands/payments.js +26 -0
  120. package/dist/modules/sales/commands/payments.js.map +2 -2
  121. package/dist/modules/sales/notifications.client.js +51 -0
  122. package/dist/modules/sales/notifications.client.js.map +7 -0
  123. package/dist/modules/sales/notifications.js +88 -0
  124. package/dist/modules/sales/notifications.js.map +7 -0
  125. package/dist/modules/sales/subscribers/quote-expiring-notification.js +38 -0
  126. package/dist/modules/sales/subscribers/quote-expiring-notification.js.map +7 -0
  127. package/dist/modules/sales/widgets/notifications/SalesOrderCreatedRenderer.js +137 -0
  128. package/dist/modules/sales/widgets/notifications/SalesOrderCreatedRenderer.js.map +7 -0
  129. package/dist/modules/sales/widgets/notifications/SalesQuoteCreatedRenderer.js +137 -0
  130. package/dist/modules/sales/widgets/notifications/SalesQuoteCreatedRenderer.js.map +7 -0
  131. package/dist/modules/sales/widgets/notifications/index.js +7 -0
  132. package/dist/modules/sales/widgets/notifications/index.js.map +7 -0
  133. package/dist/modules/sales/widgets/notifications/useSalesDocumentTotals.js +60 -0
  134. package/dist/modules/sales/widgets/notifications/useSalesDocumentTotals.js.map +7 -0
  135. package/dist/modules/staff/commands/leave-requests.js +79 -0
  136. package/dist/modules/staff/commands/leave-requests.js.map +2 -2
  137. package/dist/modules/staff/notifications.js +75 -0
  138. package/dist/modules/staff/notifications.js.map +7 -0
  139. package/dist/modules/workflows/notifications.js +28 -0
  140. package/dist/modules/workflows/notifications.js.map +7 -0
  141. package/dist/modules/workflows/subscribers/task-assigned-notification.js +38 -0
  142. package/dist/modules/workflows/subscribers/task-assigned-notification.js.map +7 -0
  143. package/generated/entities/notification/index.ts +27 -0
  144. package/generated/entities.ids.generated.ts +63 -59
  145. package/generated/entity-fields-registry.ts +2 -0
  146. package/package.json +2 -2
  147. package/src/modules/api_docs/frontend/docs/api/page.tsx +3 -2
  148. package/src/modules/auth/api/admin/nav.ts +10 -6
  149. package/src/modules/auth/api/profile/route.ts +160 -0
  150. package/src/modules/auth/api/reset/confirm.ts +25 -2
  151. package/src/modules/auth/api/reset.ts +23 -0
  152. package/src/modules/auth/api/sidebar/preferences/route.ts +21 -12
  153. package/src/modules/auth/backend/auth/profile/page.meta.ts +8 -0
  154. package/src/modules/auth/backend/auth/profile/page.tsx +127 -0
  155. package/src/modules/auth/commands/users.ts +68 -0
  156. package/src/modules/auth/i18n/de.json +29 -1
  157. package/src/modules/auth/i18n/en.json +29 -1
  158. package/src/modules/auth/i18n/es.json +29 -1
  159. package/src/modules/auth/i18n/pl.json +29 -1
  160. package/src/modules/auth/lib/setup-app.ts +1 -0
  161. package/src/modules/auth/notifications.ts +109 -0
  162. package/src/modules/auth/services/authService.ts +4 -4
  163. package/src/modules/business_rules/i18n/en.json +3 -1
  164. package/src/modules/business_rules/notifications.ts +25 -0
  165. package/src/modules/business_rules/subscribers/rule-execution-failed-notification.ts +50 -0
  166. package/src/modules/catalog/i18n/en.json +3 -1
  167. package/src/modules/catalog/notifications.ts +25 -0
  168. package/src/modules/catalog/subscribers/low-stock-notification.ts +52 -0
  169. package/src/modules/configs/cli.ts +6 -0
  170. package/src/modules/customers/commands/deals.ts +39 -0
  171. package/src/modules/customers/i18n/en.json +5 -1
  172. package/src/modules/customers/notifications.ts +44 -0
  173. package/src/modules/notifications/acl.ts +7 -0
  174. package/src/modules/notifications/api/[id]/action/route.ts +75 -0
  175. package/src/modules/notifications/api/[id]/dismiss/route.ts +12 -0
  176. package/src/modules/notifications/api/[id]/read/route.ts +12 -0
  177. package/src/modules/notifications/api/[id]/restore/route.ts +53 -0
  178. package/src/modules/notifications/api/batch/route.ts +14 -0
  179. package/src/modules/notifications/api/feature/route.ts +14 -0
  180. package/src/modules/notifications/api/mark-all-read/route.ts +34 -0
  181. package/src/modules/notifications/api/openapi.ts +76 -0
  182. package/src/modules/notifications/api/role/route.ts +14 -0
  183. package/src/modules/notifications/api/route.ts +92 -0
  184. package/src/modules/notifications/api/settings/route.ts +157 -0
  185. package/src/modules/notifications/api/unread-count/route.ts +38 -0
  186. package/src/modules/notifications/backend/config/notifications/page.meta.ts +22 -0
  187. package/src/modules/notifications/backend/config/notifications/page.tsx +12 -0
  188. package/src/modules/notifications/cli.ts +18 -0
  189. package/src/modules/notifications/data/entities.ts +99 -0
  190. package/src/modules/notifications/data/validators.ts +110 -0
  191. package/src/modules/notifications/di.ts +11 -0
  192. package/src/modules/notifications/emails/NotificationEmail.tsx +98 -0
  193. package/src/modules/notifications/frontend/NotificationInboxPageClient.tsx +42 -0
  194. package/src/modules/notifications/frontend/NotificationSettingsPageClient.tsx +231 -0
  195. package/src/modules/notifications/i18n/de.json +50 -0
  196. package/src/modules/notifications/i18n/en.json +50 -0
  197. package/src/modules/notifications/i18n/es.json +50 -0
  198. package/src/modules/notifications/i18n/pl.json +50 -0
  199. package/src/modules/notifications/index.ts +12 -0
  200. package/src/modules/notifications/lib/deliveryConfig.ts +145 -0
  201. package/src/modules/notifications/lib/events.ts +48 -0
  202. package/src/modules/notifications/lib/notificationBuilder.ts +121 -0
  203. package/src/modules/notifications/lib/notificationFactory.ts +76 -0
  204. package/src/modules/notifications/lib/notificationMapper.ts +33 -0
  205. package/src/modules/notifications/lib/notificationRecipients.ts +83 -0
  206. package/src/modules/notifications/lib/notificationService.ts +414 -0
  207. package/src/modules/notifications/lib/routeHelpers.ts +151 -0
  208. package/src/modules/notifications/lib/safeHref.ts +29 -0
  209. package/src/modules/notifications/migrations/.snapshot-open-mercato.json +300 -0
  210. package/src/modules/notifications/migrations/Migration20260123000001.ts +73 -0
  211. package/src/modules/notifications/migrations/Migration20260126150000.ts +39 -0
  212. package/src/modules/notifications/subscribers/deliver-notification.ts +175 -0
  213. package/src/modules/notifications/workers/create-notification.worker.ts +122 -0
  214. package/src/modules/sales/commands/documents.ts +65 -0
  215. package/src/modules/sales/commands/payments.ts +33 -0
  216. package/src/modules/sales/i18n/de.json +20 -0
  217. package/src/modules/sales/i18n/en.json +25 -1
  218. package/src/modules/sales/i18n/es.json +20 -0
  219. package/src/modules/sales/i18n/pl.json +20 -0
  220. package/src/modules/sales/notifications.client.ts +65 -0
  221. package/src/modules/sales/notifications.ts +82 -0
  222. package/src/modules/sales/subscribers/quote-expiring-notification.ts +53 -0
  223. package/src/modules/sales/widgets/notifications/SalesOrderCreatedRenderer.tsx +156 -0
  224. package/src/modules/sales/widgets/notifications/SalesQuoteCreatedRenderer.tsx +156 -0
  225. package/src/modules/sales/widgets/notifications/index.ts +2 -0
  226. package/src/modules/sales/widgets/notifications/useSalesDocumentTotals.ts +81 -0
  227. package/src/modules/staff/commands/leave-requests.ts +94 -0
  228. package/src/modules/staff/i18n/de.json +4 -0
  229. package/src/modules/staff/i18n/en.json +9 -1
  230. package/src/modules/staff/i18n/es.json +4 -0
  231. package/src/modules/staff/i18n/pl.json +4 -0
  232. package/src/modules/staff/notifications.ts +71 -0
  233. package/src/modules/workflows/i18n/en.json +3 -1
  234. package/src/modules/workflows/notifications.ts +25 -0
  235. package/src/modules/workflows/subscribers/task-assigned-notification.ts +53 -0
@@ -29,6 +29,9 @@ import { resolveDictionaryEntryValue } from "../lib/dictionaries.js";
29
29
  import { invalidateCrudCache } from "@open-mercato/shared/lib/crud/cache";
30
30
  import { emitCrudSideEffects } from "@open-mercato/shared/lib/commands/helpers";
31
31
  import { findOneWithDecryption, findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
32
+ import { resolveNotificationService } from "../../notifications/lib/notificationService.js";
33
+ import { buildFeatureNotificationFromType } from "../../notifications/lib/notificationBuilder.js";
34
+ import { notificationTypes } from "../notifications.js";
32
35
  const toNumber = (value) => {
33
36
  if (typeof value === "number" && Number.isFinite(value)) return value;
34
37
  if (typeof value === "string" && value.trim().length) {
@@ -311,6 +314,29 @@ const createPaymentCommand = {
311
314
  const totals = await recomputeOrderPaymentTotals(em, order);
312
315
  await em.flush();
313
316
  await invalidateOrderCache(ctx.container, order, ctx.auth?.tenantId ?? null);
317
+ try {
318
+ const notificationService = resolveNotificationService(ctx.container);
319
+ const typeDef = notificationTypes.find((type) => type.type === "sales.payment.received");
320
+ if (typeDef) {
321
+ const amountDisplay = payment.amount && payment.currencyCode ? `${payment.currencyCode} ${payment.amount}` : "";
322
+ const notificationInput = buildFeatureNotificationFromType(typeDef, {
323
+ requiredFeature: "sales.orders.manage",
324
+ bodyVariables: {
325
+ orderNumber: order.orderNumber ?? "",
326
+ amount: amountDisplay
327
+ },
328
+ sourceEntityType: "sales:order",
329
+ sourceEntityId: order.id,
330
+ linkHref: `/backend/sales/orders/${order.id}`
331
+ });
332
+ await notificationService.createForFeature(notificationInput, {
333
+ tenantId: payment.tenantId,
334
+ organizationId: payment.organizationId ?? null
335
+ });
336
+ }
337
+ } catch (err) {
338
+ console.error("[sales.payments.create] Failed to create notification:", err);
339
+ }
314
340
  return { paymentId: payment.id, orderTotals: totals };
315
341
  },
316
342
  captureAfter: async (_input, result, ctx) => {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/sales/commands/payments.ts"],
4
- "sourcesContent": ["// @ts-nocheck\n\nimport { registerCommand, type CommandHandler } from '@open-mercato/shared/lib/commands'\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 { loadCustomFieldValues } from '@open-mercato/shared/lib/crud/custom-fields'\nimport { setRecordCustomFields } from '@open-mercato/core/modules/entities/lib/helpers'\nimport { E } from '#generated/entities.ids.generated'\nimport {\n SalesInvoice,\n SalesOrder,\n SalesOrderLine,\n SalesPayment,\n SalesPaymentAllocation,\n SalesPaymentMethod,\n} from '../data/entities'\nimport {\n paymentCreateSchema,\n paymentUpdateSchema,\n type PaymentCreateInput,\n type PaymentUpdateInput,\n} from '../data/validators'\nimport {\n assertFound,\n cloneJson,\n ensureOrganizationScope,\n ensureSameScope,\n ensureTenantScope,\n extractUndoPayload,\n toNumericString,\n} from './shared'\nimport { resolveDictionaryEntryValue } from '../lib/dictionaries'\nimport { invalidateCrudCache } from '@open-mercato/shared/lib/crud/cache'\nimport { emitCrudSideEffects } from '@open-mercato/shared/lib/commands/helpers'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\n\nexport type PaymentAllocationSnapshot = {\n id: string\n orderId: string | null\n invoiceId: string | null\n amount: number\n currencyCode: string\n metadata: Record<string, unknown> | null\n}\n\nexport type PaymentSnapshot = {\n id: string\n orderId: string | null\n organizationId: string\n tenantId: string\n paymentMethodId: string | null\n paymentReference: string | null\n statusEntryId: string | null\n status: string | null\n amount: number\n currencyCode: string\n capturedAmount: number\n refundedAmount: number\n receivedAt: string | null\n capturedAt: string | null\n metadata: Record<string, unknown> | null\n customFields?: Record<string, unknown> | null\n customFieldSetId?: string | null\n allocations: PaymentAllocationSnapshot[]\n}\n\ntype PaymentUndoPayload = {\n before?: PaymentSnapshot | null\n after?: PaymentSnapshot | null\n}\n\nconst toNumber = (value: unknown): number => {\n if (typeof value === 'number' && Number.isFinite(value)) return value\n if (typeof value === 'string' && value.trim().length) {\n const parsed = Number(value)\n if (!Number.isNaN(parsed)) return parsed\n }\n return 0\n}\n\nconst normalizeCustomFieldsInput = (input: unknown): Record<string, unknown> =>\n input && typeof input === 'object' && !Array.isArray(input) ? (input as Record<string, unknown>) : {}\n\nconst ORDER_RESOURCE = 'sales.order'\n\nasync function invalidateOrderCache(container: any, order: SalesOrder | null | undefined, tenantId: string | null) {\n if (!order) return\n await invalidateCrudCache(\n container,\n ORDER_RESOURCE,\n { id: order.id, organizationId: order.organizationId, tenantId: order.tenantId },\n tenantId,\n 'updated'\n )\n}\n\nexport async function loadPaymentSnapshot(em: EntityManager, id: string): Promise<PaymentSnapshot | null> {\n const payment = await findOneWithDecryption(\n em,\n SalesPayment,\n { id },\n { populate: ['order', 'allocations', 'allocations.order', 'allocations.invoice'] },\n )\n if (!payment) return null\n const allocations: PaymentAllocationSnapshot[] = Array.from(payment.allocations ?? []).map((allocation) => ({\n id: allocation.id,\n orderId:\n typeof allocation.order === 'string'\n ? allocation.order\n : allocation.order?.id ?? (allocation as any).order_id ?? null,\n invoiceId:\n typeof allocation.invoice === 'string'\n ? allocation.invoice\n : allocation.invoice?.id ?? (allocation as any).invoice_id ?? null,\n amount: toNumber(allocation.amount),\n currencyCode: allocation.currencyCode,\n metadata: allocation.metadata ? cloneJson(allocation.metadata) : null,\n }))\n const customFieldValues = await loadCustomFieldValues({\n em,\n entityId: E.sales.sales_payment,\n recordIds: [payment.id],\n tenantIdByRecord: { [payment.id]: payment.tenantId ?? null },\n organizationIdByRecord: { [payment.id]: payment.organizationId ?? null },\n })\n const customFields = customFieldValues[payment.id]\n const normalizedCustomFields =\n customFields && Object.keys(customFields).length ? customFields : null\n return {\n id: payment.id,\n orderId: typeof payment.order === 'string' ? payment.order : payment.order?.id ?? null,\n organizationId: payment.organizationId,\n tenantId: payment.tenantId,\n paymentMethodId:\n typeof payment.paymentMethod === 'string'\n ? payment.paymentMethod\n : payment.paymentMethod?.id ?? null,\n paymentReference: payment.paymentReference ?? null,\n statusEntryId: payment.statusEntryId ?? null,\n status: payment.status ?? null,\n amount: toNumber(payment.amount),\n currencyCode: payment.currencyCode,\n capturedAmount: toNumber(payment.capturedAmount),\n refundedAmount: toNumber(payment.refundedAmount),\n receivedAt: payment.receivedAt ? payment.receivedAt.toISOString() : null,\n capturedAt: payment.capturedAt ? payment.capturedAt.toISOString() : null,\n metadata: payment.metadata ? cloneJson(payment.metadata) : null,\n customFields: normalizedCustomFields,\n customFieldSetId: (payment as any).customFieldSetId ?? (payment as any).custom_field_set_id ?? null,\n allocations,\n }\n}\n\nexport async function restorePaymentSnapshot(em: EntityManager, snapshot: PaymentSnapshot): Promise<void> {\n const orderRef = snapshot.orderId ? em.getReference(SalesOrder, snapshot.orderId) : null\n const methodRef = snapshot.paymentMethodId\n ? em.getReference(SalesPaymentMethod, snapshot.paymentMethodId)\n : null\n const entity =\n (await em.findOne(SalesPayment, { id: snapshot.id })) ??\n em.create(SalesPayment, {\n id: snapshot.id,\n createdAt: new Date(),\n organizationId: snapshot.organizationId,\n tenantId: snapshot.tenantId,\n })\n entity.order = orderRef\n entity.paymentMethod = methodRef\n entity.organizationId = snapshot.organizationId\n entity.tenantId = snapshot.tenantId\n entity.paymentReference = snapshot.paymentReference\n entity.statusEntryId = snapshot.statusEntryId\n entity.status = snapshot.status\n entity.amount = toNumericString(snapshot.amount) ?? '0'\n entity.currencyCode = snapshot.currencyCode\n entity.capturedAmount = toNumericString(snapshot.capturedAmount) ?? '0'\n entity.refundedAmount = toNumericString(snapshot.refundedAmount) ?? '0'\n entity.receivedAt = snapshot.receivedAt ? new Date(snapshot.receivedAt) : null\n entity.capturedAt = snapshot.capturedAt ? new Date(snapshot.capturedAt) : null\n entity.metadata = snapshot.metadata ? cloneJson(snapshot.metadata) : null\n entity.customFieldSetId =\n (snapshot as any).customFieldSetId ?? (snapshot as any).custom_field_set_id ?? null\n entity.updatedAt = new Date()\n\n if ((snapshot as any).customFields !== undefined) {\n await setRecordCustomFields(em, {\n entityId: E.sales.sales_payment,\n recordId: entity.id,\n organizationId: entity.organizationId,\n tenantId: entity.tenantId,\n values:\n snapshot.customFields && typeof snapshot.customFields === 'object'\n ? (snapshot.customFields as Record<string, unknown>)\n : {},\n })\n }\n\n const existingAllocations = await em.find(SalesPaymentAllocation, { payment: entity })\n existingAllocations.forEach((allocation) => em.remove(allocation))\n snapshot.allocations.forEach((allocation) => {\n const order =\n allocation.orderId && typeof allocation.orderId === 'string'\n ? em.getReference(SalesOrder, allocation.orderId)\n : null\n const invoice =\n allocation.invoiceId && typeof allocation.invoiceId === 'string'\n ? em.getReference(SalesInvoice, allocation.invoiceId)\n : null\n const newAllocation = em.create(SalesPaymentAllocation, {\n payment: entity,\n order,\n invoice,\n organizationId: snapshot.organizationId,\n tenantId: snapshot.tenantId,\n amount: toNumericString(allocation.amount) ?? '0',\n currencyCode: allocation.currencyCode,\n metadata: allocation.metadata ? cloneJson(allocation.metadata) : null,\n })\n em.persist(newAllocation)\n })\n em.persist(entity)\n}\n\nasync function recomputeOrderPaymentTotals(\n em: EntityManager,\n order: SalesOrder\n): Promise<{ paidTotalAmount: number; refundedTotalAmount: number; outstandingAmount: number }> {\n const orderId = order.id\n const scope = { organizationId: order.organizationId, tenantId: order.tenantId }\n\n const allocations = await findWithDecryption(\n em,\n SalesPaymentAllocation,\n { ...scope, order: orderId },\n { populate: ['payment'] },\n scope,\n )\n\n const paymentIds = new Set<string>()\n allocations.forEach((allocation) => {\n const paymentRef = allocation.payment\n const paymentId =\n typeof paymentRef === 'object' && paymentRef !== null\n ? paymentRef.id\n : typeof paymentRef === 'string'\n ? paymentRef\n : null\n if (paymentId) paymentIds.add(paymentId)\n })\n\n const payments =\n paymentIds.size > 0\n ? await em.find(SalesPayment, { id: { $in: Array.from(paymentIds) }, deletedAt: null, ...scope })\n : await em.find(SalesPayment, { order: orderId, deletedAt: null, ...scope })\n\n const resolvePaidAmount = (payment: SalesPayment) => {\n const captured = toNumber(payment.capturedAmount)\n return captured > 0 ? captured : toNumber(payment.amount)\n }\n\n const activePaymentIds = new Set(payments.map((payment) => payment.id))\n const paidTotal =\n allocations.length > 0\n ? allocations.reduce((sum, allocation) => {\n const paymentRef = allocation.payment\n const paymentId =\n typeof paymentRef === 'object' && paymentRef !== null\n ? paymentRef.id\n : typeof paymentRef === 'string'\n ? paymentRef\n : null\n if (paymentId && !activePaymentIds.has(paymentId)) return sum\n return sum + toNumber(allocation.amount)\n }, 0)\n : payments.reduce((sum, payment) => sum + resolvePaidAmount(payment), 0)\n\n const refundedTotal = payments.reduce(\n (sum, payment) => sum + toNumber(payment.refundedAmount),\n 0\n )\n\n const grandTotal = toNumber(order.grandTotalGrossAmount)\n const outstanding = Math.max(grandTotal - paidTotal + refundedTotal, 0)\n order.paidTotalAmount = toNumericString(paidTotal) ?? '0'\n order.refundedTotalAmount = toNumericString(refundedTotal) ?? '0'\n order.outstandingAmount = toNumericString(outstanding) ?? '0'\n return {\n paidTotalAmount: paidTotal,\n refundedTotalAmount: refundedTotal,\n outstandingAmount: outstanding,\n }\n}\n\nconst createPaymentCommand: CommandHandler<\n PaymentCreateInput,\n { paymentId: string; orderTotals?: { paidTotalAmount: number; refundedTotalAmount: number; outstandingAmount: number } }\n> = {\n id: 'sales.payments.create',\n async execute(rawInput, ctx) {\n const input = paymentCreateSchema.parse(rawInput ?? {})\n ensureTenantScope(ctx, input.tenantId)\n ensureOrganizationScope(ctx, input.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const { translate } = await resolveTranslations()\n if (!input.orderId) {\n throw new CrudHttpError(400, { error: translate('sales.payments.order_required', 'Order is required for payments.') })\n }\n const order = assertFound(\n await em.findOne(SalesOrder, { id: input.orderId }),\n 'sales.payments.order_not_found'\n )\n ensureSameScope(order, input.organizationId, input.tenantId)\n if (order.deletedAt) {\n throw new CrudHttpError(404, { error: 'sales.payments.order_not_found' })\n }\n if (\n order.currencyCode &&\n input.currencyCode &&\n order.currencyCode.toUpperCase() !== input.currencyCode.toUpperCase()\n ) {\n throw new CrudHttpError(400, {\n error: translate('sales.payments.currency_mismatch', 'Payment currency must match the order currency.'),\n })\n }\n let paymentMethod = null\n if (input.paymentMethodId) {\n const method = assertFound(\n await em.findOne(SalesPaymentMethod, { id: input.paymentMethodId }),\n 'sales.payments.method_not_found'\n )\n ensureSameScope(method, input.organizationId, input.tenantId)\n paymentMethod = method\n }\n if (input.documentStatusEntryId !== undefined) {\n const orderStatus = await resolveDictionaryEntryValue(em, input.documentStatusEntryId ?? null)\n if (input.documentStatusEntryId && !orderStatus) {\n throw new CrudHttpError(400, {\n error: translate('sales.documents.detail.statusInvalid', 'Selected status could not be found.'),\n })\n }\n order.statusEntryId = input.documentStatusEntryId ?? null\n order.status = orderStatus\n order.updatedAt = new Date()\n em.persist(order)\n }\n if (input.lineStatusEntryId !== undefined) {\n const lineStatus = await resolveDictionaryEntryValue(em, input.lineStatusEntryId ?? null)\n if (input.lineStatusEntryId && !lineStatus) {\n throw new CrudHttpError(400, {\n error: translate('sales.documents.detail.statusInvalid', 'Selected status could not be found.'),\n })\n }\n const orderLines = await em.find(SalesOrderLine, { order })\n orderLines.forEach((line) => {\n line.statusEntryId = input.lineStatusEntryId ?? null\n line.status = lineStatus\n line.updatedAt = new Date()\n })\n orderLines.forEach((line) => em.persist(line))\n }\n const status = await resolveDictionaryEntryValue(em, input.statusEntryId ?? null)\n const payment = em.create(SalesPayment, {\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n order,\n paymentMethod,\n paymentReference: input.paymentReference ?? null,\n statusEntryId: input.statusEntryId ?? null,\n status,\n amount: toNumericString(input.amount) ?? '0',\n currencyCode: input.currencyCode,\n capturedAmount: toNumericString(input.capturedAmount) ?? '0',\n refundedAmount: toNumericString(input.refundedAmount) ?? '0',\n receivedAt: input.receivedAt ?? null,\n capturedAt: input.capturedAt ?? null,\n metadata: input.metadata ? cloneJson(input.metadata) : null,\n customFieldSetId: input.customFieldSetId ?? null,\n })\n const allocationInputs = Array.isArray(input.allocations) ? input.allocations : []\n const allocations = allocationInputs.length\n ? allocationInputs\n : [\n {\n orderId: input.orderId,\n invoiceId: null,\n amount: input.amount,\n currencyCode: input.currencyCode,\n metadata: null,\n },\n ]\n allocations.forEach((allocation) => {\n const orderRef = allocation.orderId ? em.getReference(SalesOrder, allocation.orderId) : order\n const invoiceRef = allocation.invoiceId ? em.getReference(SalesInvoice, allocation.invoiceId) : null\n const entity = em.create(SalesPaymentAllocation, {\n payment,\n order: orderRef,\n invoice: invoiceRef,\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n amount: toNumericString(allocation.amount) ?? '0',\n currencyCode: allocation.currencyCode,\n metadata: allocation.metadata ? cloneJson(allocation.metadata) : null,\n })\n em.persist(entity)\n })\n em.persist(payment)\n if (input.customFields !== undefined) {\n if (!payment.id) {\n await em.flush()\n }\n await setRecordCustomFields(em, {\n entityId: E.sales.sales_payment,\n recordId: payment.id,\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n values: normalizeCustomFieldsInput(input.customFields),\n })\n }\n await em.flush()\n const totals = await recomputeOrderPaymentTotals(em, order)\n await em.flush()\n await invalidateOrderCache(ctx.container, order, ctx.auth?.tenantId ?? null)\n return { paymentId: payment.id, orderTotals: totals }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = ctx.container.resolve('em') as EntityManager\n return result?.paymentId ? loadPaymentSnapshot(em, result.paymentId) : null\n },\n buildLog: async ({ result, snapshots }) => {\n const after = snapshots.after as PaymentSnapshot | undefined\n if (!after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('sales.audit.payments.create', 'Create payment'),\n resourceKind: 'sales.payment',\n resourceId: result.paymentId,\n tenantId: after.tenantId,\n organizationId: after.organizationId,\n snapshotAfter: after,\n payload: { undo: { after } satisfies PaymentUndoPayload },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PaymentUndoPayload>(logEntry)\n const after = payload?.after\n if (!after) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const existing = await em.findOne(SalesPayment, { id: after.id })\n if (existing) {\n const orderRef =\n typeof existing.order === 'string' ? existing.order : existing.order?.id ?? null\n const allocations = await em.find(SalesPaymentAllocation, { payment: existing })\n const allocationOrders = allocations\n .map((allocation) =>\n typeof allocation.order === 'string'\n ? allocation.order\n : allocation.order?.id ?? null\n )\n .filter((value): value is string => typeof value === 'string' && value.length > 0)\n\n allocations.forEach((allocation) => em.remove(allocation))\n await em.flush()\n\n em.remove(existing)\n await em.flush()\n\n const orderIds = Array.from(\n new Set(\n [\n orderRef,\n ...allocationOrders,\n ].filter((value): value is string => typeof value === 'string' && value.length > 0)\n )\n )\n for (const id of orderIds) {\n const order = await em.findOne(SalesOrder, { id })\n if (!order) continue\n await recomputeOrderPaymentTotals(em, order)\n await em.flush()\n }\n }\n },\n}\n\nconst updatePaymentCommand: CommandHandler<\n PaymentUpdateInput,\n { paymentId: string; orderTotals?: { paidTotalAmount: number; refundedTotalAmount: number; outstandingAmount: number } }\n> = {\n id: 'sales.payments.update',\n async prepare(rawInput, ctx) {\n const parsed = paymentUpdateSchema.parse(rawInput ?? {})\n if (!parsed.id) return {}\n const em = ctx.container.resolve('em') as EntityManager\n const snapshot = await loadPaymentSnapshot(em, parsed.id)\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 input = paymentUpdateSchema.parse(rawInput ?? {})\n ensureTenantScope(ctx, input.tenantId)\n ensureOrganizationScope(ctx, input.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const { translate } = await resolveTranslations()\n const payment = assertFound(\n await findOneWithDecryption(\n em,\n SalesPayment,\n { id: input.id },\n { populate: ['order'] },\n { tenantId: input.tenantId, organizationId: input.organizationId },\n ),\n 'sales.payments.not_found'\n )\n ensureSameScope(payment, input.organizationId, input.tenantId)\n const previousOrder = payment.order as SalesOrder | null\n if (input.orderId !== undefined) {\n if (!input.orderId) {\n payment.order = null\n } else {\n const order = assertFound(\n await em.findOne(SalesOrder, { id: input.orderId }),\n 'sales.payments.order_not_found'\n )\n ensureSameScope(order, input.organizationId, input.tenantId)\n if (\n order.currencyCode &&\n input.currencyCode &&\n order.currencyCode.toUpperCase() !== input.currencyCode.toUpperCase()\n ) {\n throw new CrudHttpError(400, {\n error: translate('sales.payments.currency_mismatch', 'Payment currency must match the order currency.'),\n })\n }\n payment.order = order\n }\n }\n if (input.paymentMethodId !== undefined) {\n if (!input.paymentMethodId) {\n payment.paymentMethod = null\n } else {\n const method = assertFound(\n await em.findOne(SalesPaymentMethod, { id: input.paymentMethodId }),\n 'sales.payments.method_not_found'\n )\n ensureSameScope(method, input.organizationId, input.tenantId)\n payment.paymentMethod = method\n }\n }\n const currentOrder = payment.order as SalesOrder | null\n if ((input.documentStatusEntryId !== undefined || input.lineStatusEntryId !== undefined) && !currentOrder) {\n throw new CrudHttpError(400, { error: translate('sales.payments.order_required', 'Order is required for payments.') })\n }\n if (currentOrder && input.documentStatusEntryId !== undefined) {\n const orderStatus = await resolveDictionaryEntryValue(em, input.documentStatusEntryId ?? null)\n if (input.documentStatusEntryId && !orderStatus) {\n throw new CrudHttpError(400, {\n error: translate('sales.documents.detail.statusInvalid', 'Selected status could not be found.'),\n })\n }\n currentOrder.statusEntryId = input.documentStatusEntryId ?? null\n currentOrder.status = orderStatus\n currentOrder.updatedAt = new Date()\n em.persist(currentOrder)\n }\n if (currentOrder && input.lineStatusEntryId !== undefined) {\n const lineStatus = await resolveDictionaryEntryValue(em, input.lineStatusEntryId ?? null)\n if (input.lineStatusEntryId && !lineStatus) {\n throw new CrudHttpError(400, {\n error: translate('sales.documents.detail.statusInvalid', 'Selected status could not be found.'),\n })\n }\n const orderLines = await em.find(SalesOrderLine, { order: currentOrder })\n orderLines.forEach((line) => {\n line.statusEntryId = input.lineStatusEntryId ?? null\n line.status = lineStatus\n line.updatedAt = new Date()\n })\n orderLines.forEach((line) => em.persist(line))\n }\n if (input.paymentReference !== undefined) payment.paymentReference = input.paymentReference ?? null\n if (input.statusEntryId !== undefined) {\n payment.statusEntryId = input.statusEntryId ?? null\n payment.status = await resolveDictionaryEntryValue(em, input.statusEntryId ?? null)\n }\n if (input.amount !== undefined) payment.amount = toNumericString(input.amount) ?? '0'\n if (input.currencyCode !== undefined) payment.currencyCode = input.currencyCode\n if (input.capturedAmount !== undefined) {\n payment.capturedAmount = toNumericString(input.capturedAmount) ?? '0'\n }\n if (input.refundedAmount !== undefined) {\n payment.refundedAmount = toNumericString(input.refundedAmount) ?? '0'\n }\n if (input.receivedAt !== undefined) payment.receivedAt = input.receivedAt ?? null\n if (input.capturedAt !== undefined) payment.capturedAt = input.capturedAt ?? null\n if (input.metadata !== undefined) {\n payment.metadata = input.metadata ? cloneJson(input.metadata) : null\n }\n if (input.customFieldSetId !== undefined) {\n payment.customFieldSetId = input.customFieldSetId ?? null\n }\n if (input.customFields !== undefined) {\n if (!payment.id) {\n await em.flush()\n }\n await setRecordCustomFields(em, {\n entityId: E.sales.sales_payment,\n recordId: payment.id,\n organizationId: payment.organizationId,\n tenantId: payment.tenantId,\n values: normalizeCustomFieldsInput(input.customFields),\n })\n }\n if (input.allocations !== undefined) {\n const existingAllocations = await em.find(SalesPaymentAllocation, { payment })\n existingAllocations.forEach((allocation) => em.remove(allocation))\n const allocationInputs = Array.isArray(input.allocations) ? input.allocations : []\n allocationInputs.forEach((allocation) => {\n const orderRef =\n allocation.orderId ??\n (typeof payment.order === 'string' ? payment.order : payment.order?.id) ??\n null\n const order =\n orderRef && typeof orderRef === 'string' ? em.getReference(SalesOrder, orderRef) : null\n const invoice = allocation.invoiceId\n ? em.getReference(SalesInvoice, allocation.invoiceId)\n : null\n const entity = em.create(SalesPaymentAllocation, {\n payment,\n order,\n invoice,\n organizationId: payment.organizationId,\n tenantId: payment.tenantId,\n amount: toNumericString(allocation.amount) ?? '0',\n currencyCode: allocation.currencyCode,\n metadata: allocation.metadata ? cloneJson(allocation.metadata) : null,\n })\n em.persist(entity)\n })\n }\n payment.updatedAt = new Date()\n await em.flush()\n\n const nextOrder =\n (payment.order as SalesOrder | null) ??\n (typeof payment.order === 'string'\n ? await em.findOne(SalesOrder, { id: payment.order })\n : null)\n let totals: { paidTotalAmount: number; refundedTotalAmount: number; outstandingAmount: number } | undefined\n if (nextOrder) {\n totals = await recomputeOrderPaymentTotals(em, nextOrder)\n await em.flush()\n await invalidateOrderCache(ctx.container, nextOrder, ctx.auth?.tenantId ?? null)\n }\n if (previousOrder && (!nextOrder || previousOrder.id !== nextOrder.id)) {\n await recomputeOrderPaymentTotals(em, previousOrder)\n await em.flush()\n await invalidateOrderCache(ctx.container, previousOrder, ctx.auth?.tenantId ?? null)\n }\n\n return { paymentId: payment.id, orderTotals: totals }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = ctx.container.resolve('em') as EntityManager\n return result?.paymentId ? loadPaymentSnapshot(em, result.paymentId) : null\n },\n buildLog: async ({ snapshots, result }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as PaymentSnapshot | undefined\n const after = snapshots.after as PaymentSnapshot | undefined\n return {\n actionLabel: translate('sales.audit.payments.update', 'Update payment'),\n resourceKind: 'sales.payment',\n resourceId: result.paymentId,\n tenantId: after?.tenantId ?? before?.tenantId ?? null,\n organizationId: after?.organizationId ?? before?.organizationId ?? null,\n snapshotBefore: before ?? null,\n snapshotAfter: after ?? null,\n payload: { undo: { before, after } satisfies PaymentUndoPayload },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PaymentUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n await restorePaymentSnapshot(em, before)\n await em.flush()\n if (before.orderId) {\n const order = await em.findOne(SalesOrder, { id: before.orderId })\n if (order) {\n await recomputeOrderPaymentTotals(em, order)\n await em.flush()\n }\n }\n },\n}\n\nconst deletePaymentCommand: CommandHandler<\n { id: string; orderId?: string | null; organizationId: string; tenantId: string },\n { paymentId: string; orderTotals?: { paidTotalAmount: number; refundedTotalAmount: number; outstandingAmount: number } }\n> = {\n id: 'sales.payments.delete',\n async prepare(rawInput, ctx) {\n const parsed = paymentUpdateSchema.parse(rawInput ?? {})\n if (!parsed.id) return {}\n const em = ctx.container.resolve('em') as EntityManager\n const snapshot = await loadPaymentSnapshot(em, parsed.id)\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 input = paymentUpdateSchema.parse(rawInput ?? {})\n ensureTenantScope(ctx, input.tenantId)\n ensureOrganizationScope(ctx, input.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const payment = assertFound(\n await findOneWithDecryption(\n em,\n SalesPayment,\n { id: input.id },\n { populate: ['order'] },\n { tenantId: input.tenantId, organizationId: input.organizationId },\n ),\n 'sales.payments.not_found'\n )\n ensureSameScope(payment, input.organizationId, input.tenantId)\n const order = payment.order as SalesOrder | null\n const allocations = await em.find(SalesPaymentAllocation, { payment })\n const allocationOrders = allocations\n .map((allocation) =>\n typeof allocation.order === 'string'\n ? allocation.order\n : allocation.order?.id ?? null\n )\n .filter((value): value is string => typeof value === 'string' && value.length > 0)\n allocations.forEach((allocation) => em.remove(allocation))\n await em.flush()\n em.remove(payment)\n await em.flush()\n let totals: { paidTotalAmount: number; refundedTotalAmount: number; outstandingAmount: number } | undefined\n const orderIds = Array.from(\n new Set(\n [\n order && typeof order === 'object' ? order.id : null,\n ...allocationOrders,\n ].filter((value): value is string => typeof value === 'string' && value.length > 0)\n )\n )\n const primaryOrderId = order && typeof order === 'object' ? order.id : null\n for (const orderId of orderIds) {\n const target = typeof order === 'object' && order.id === orderId ? order : await em.findOne(SalesOrder, { id: orderId })\n if (!target) continue\n const recomputed = await recomputeOrderPaymentTotals(em, target)\n if (!totals || (primaryOrderId && orderId === primaryOrderId)) {\n totals = recomputed\n }\n await em.flush()\n await invalidateOrderCache(ctx.container, target, ctx.auth?.tenantId ?? null)\n }\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n if (allocations.length) {\n await Promise.all(\n allocations.map((allocation) =>\n emitCrudSideEffects({\n dataEngine,\n action: 'deleted',\n entity: allocation,\n identifiers: {\n id: allocation.id,\n organizationId: allocation.organizationId ?? null,\n tenantId: allocation.tenantId ?? null,\n },\n indexer: { entityType: E.sales.sales_payment_allocation },\n })\n )\n )\n }\n return { paymentId: payment.id, orderTotals: totals }\n },\n buildLog: async ({ snapshots, result }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as PaymentSnapshot | undefined\n return {\n actionLabel: translate('sales.audit.payments.delete', 'Delete payment'),\n resourceKind: 'sales.payment',\n resourceId: result.paymentId,\n tenantId: before?.tenantId ?? null,\n organizationId: before?.organizationId ?? null,\n snapshotBefore: before ?? null,\n payload: { undo: { before } satisfies PaymentUndoPayload },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PaymentUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n await restorePaymentSnapshot(em, before)\n await em.flush()\n if (before.orderId) {\n const order = await em.findOne(SalesOrder, { id: before.orderId })\n if (order) {\n await recomputeOrderPaymentTotals(em, order)\n await em.flush()\n }\n }\n },\n}\n\nexport const paymentCommands = [createPaymentCommand, updatePaymentCommand, deletePaymentCommand]\n\nregisterCommand(createPaymentCommand)\nregisterCommand(updatePaymentCommand)\nregisterCommand(deletePaymentCommand)\n"],
5
- "mappings": "AAEA,SAAS,uBAA4C;AAErD,SAAS,qBAAqB;AAC9B,SAAS,2BAA2B;AACpC,SAAS,6BAA6B;AACtC,SAAS,6BAA6B;AACtC,SAAS,SAAS;AAClB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,mCAAmC;AAC5C,SAAS,2BAA2B;AACpC,SAAS,2BAA2B;AAEpC,SAAS,uBAAuB,0BAA0B;AAqC1D,MAAM,WAAW,CAAC,UAA2B;AAC3C,MAAI,OAAO,UAAU,YAAY,OAAO,SAAS,KAAK,EAAG,QAAO;AAChE,MAAI,OAAO,UAAU,YAAY,MAAM,KAAK,EAAE,QAAQ;AACpD,UAAM,SAAS,OAAO,KAAK;AAC3B,QAAI,CAAC,OAAO,MAAM,MAAM,EAAG,QAAO;AAAA,EACpC;AACA,SAAO;AACT;AAEA,MAAM,6BAA6B,CAAC,UAClC,SAAS,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,IAAK,QAAoC,CAAC;AAEtG,MAAM,iBAAiB;AAEvB,eAAe,qBAAqB,WAAgB,OAAsC,UAAyB;AACjH,MAAI,CAAC,MAAO;AACZ,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,EAAE,IAAI,MAAM,IAAI,gBAAgB,MAAM,gBAAgB,UAAU,MAAM,SAAS;AAAA,IAC/E;AAAA,IACA;AAAA,EACF;AACF;AAEA,eAAsB,oBAAoB,IAAmB,IAA6C;AACxG,QAAM,UAAU,MAAM;AAAA,IACpB;AAAA,IACA;AAAA,IACA,EAAE,GAAG;AAAA,IACL,EAAE,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AAgB;AAAA,MACtE,cAAc;AAAA,MACd,YAAY,OAAO;AAAA,MACnB,UAAU,QAAQ,YAAY;AAAA,MAC9B,gBAAgB,QAAQ,kBAAkB;AAAA,MAC1C,gBAAgB,UAAU;AAAA,MAC1B,SAAS,EAAE,MAAM,EAAE,OAAO,EAA+B;AAAA,IAC3D;AAAA,EACF;AAAA,EACA,MAAM,OAAO,EAAE,UAAU,IAAI,MAAM;AACjC,UAAM,UAAU,mBAAuC,QAAQ;AAC/D,UAAM,SAAS,SAAS;AACxB,QAAI,CAAC,OAAQ;AACb,UAAM,KAAM,IAAI,UAAU,QAAQ,IAAI,EAAoB,KAAK;AAC/D,UAAM,uBAAuB,IAAI,MAAM;AACvC,UAAM,GAAG,MAAM;AACf,QAAI,OAAO,SAAS;AAClB,YAAM,QAAQ,MAAM,GAAG,QAAQ,YAAY,EAAE,IAAI,OAAO,QAAQ,CAAC;AACjE,UAAI,OAAO;AACT,cAAM,4BAA4B,IAAI,KAAK;AAC3C,cAAM,GAAG,MAAM;AAAA,MACjB;AAAA,IACF;AAAA,EACF;AACF;AAEO,MAAM,kBAAkB,CAAC,sBAAsB,sBAAsB,oBAAoB;AAEhG,gBAAgB,oBAAoB;AACpC,gBAAgB,oBAAoB;AACpC,gBAAgB,oBAAoB;",
4
+ "sourcesContent": ["// @ts-nocheck\n\nimport { registerCommand, type CommandHandler } from '@open-mercato/shared/lib/commands'\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 { loadCustomFieldValues } from '@open-mercato/shared/lib/crud/custom-fields'\nimport { setRecordCustomFields } from '@open-mercato/core/modules/entities/lib/helpers'\nimport { E } from '#generated/entities.ids.generated'\nimport {\n SalesInvoice,\n SalesOrder,\n SalesOrderLine,\n SalesPayment,\n SalesPaymentAllocation,\n SalesPaymentMethod,\n} from '../data/entities'\nimport {\n paymentCreateSchema,\n paymentUpdateSchema,\n type PaymentCreateInput,\n type PaymentUpdateInput,\n} from '../data/validators'\nimport {\n assertFound,\n cloneJson,\n ensureOrganizationScope,\n ensureSameScope,\n ensureTenantScope,\n extractUndoPayload,\n toNumericString,\n} from './shared'\nimport { resolveDictionaryEntryValue } from '../lib/dictionaries'\nimport { invalidateCrudCache } from '@open-mercato/shared/lib/crud/cache'\nimport { emitCrudSideEffects } from '@open-mercato/shared/lib/commands/helpers'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { resolveNotificationService } from '../../notifications/lib/notificationService'\nimport { buildFeatureNotificationFromType } from '../../notifications/lib/notificationBuilder'\nimport { notificationTypes } from '../notifications'\n\nexport type PaymentAllocationSnapshot = {\n id: string\n orderId: string | null\n invoiceId: string | null\n amount: number\n currencyCode: string\n metadata: Record<string, unknown> | null\n}\n\nexport type PaymentSnapshot = {\n id: string\n orderId: string | null\n organizationId: string\n tenantId: string\n paymentMethodId: string | null\n paymentReference: string | null\n statusEntryId: string | null\n status: string | null\n amount: number\n currencyCode: string\n capturedAmount: number\n refundedAmount: number\n receivedAt: string | null\n capturedAt: string | null\n metadata: Record<string, unknown> | null\n customFields?: Record<string, unknown> | null\n customFieldSetId?: string | null\n allocations: PaymentAllocationSnapshot[]\n}\n\ntype PaymentUndoPayload = {\n before?: PaymentSnapshot | null\n after?: PaymentSnapshot | null\n}\n\nconst toNumber = (value: unknown): number => {\n if (typeof value === 'number' && Number.isFinite(value)) return value\n if (typeof value === 'string' && value.trim().length) {\n const parsed = Number(value)\n if (!Number.isNaN(parsed)) return parsed\n }\n return 0\n}\n\nconst normalizeCustomFieldsInput = (input: unknown): Record<string, unknown> =>\n input && typeof input === 'object' && !Array.isArray(input) ? (input as Record<string, unknown>) : {}\n\nconst ORDER_RESOURCE = 'sales.order'\n\nasync function invalidateOrderCache(container: any, order: SalesOrder | null | undefined, tenantId: string | null) {\n if (!order) return\n await invalidateCrudCache(\n container,\n ORDER_RESOURCE,\n { id: order.id, organizationId: order.organizationId, tenantId: order.tenantId },\n tenantId,\n 'updated'\n )\n}\n\nexport async function loadPaymentSnapshot(em: EntityManager, id: string): Promise<PaymentSnapshot | null> {\n const payment = await findOneWithDecryption(\n em,\n SalesPayment,\n { id },\n { populate: ['order', 'allocations', 'allocations.order', 'allocations.invoice'] },\n )\n if (!payment) return null\n const allocations: PaymentAllocationSnapshot[] = Array.from(payment.allocations ?? []).map((allocation) => ({\n id: allocation.id,\n orderId:\n typeof allocation.order === 'string'\n ? allocation.order\n : allocation.order?.id ?? (allocation as any).order_id ?? null,\n invoiceId:\n typeof allocation.invoice === 'string'\n ? allocation.invoice\n : allocation.invoice?.id ?? (allocation as any).invoice_id ?? null,\n amount: toNumber(allocation.amount),\n currencyCode: allocation.currencyCode,\n metadata: allocation.metadata ? cloneJson(allocation.metadata) : null,\n }))\n const customFieldValues = await loadCustomFieldValues({\n em,\n entityId: E.sales.sales_payment,\n recordIds: [payment.id],\n tenantIdByRecord: { [payment.id]: payment.tenantId ?? null },\n organizationIdByRecord: { [payment.id]: payment.organizationId ?? null },\n })\n const customFields = customFieldValues[payment.id]\n const normalizedCustomFields =\n customFields && Object.keys(customFields).length ? customFields : null\n return {\n id: payment.id,\n orderId: typeof payment.order === 'string' ? payment.order : payment.order?.id ?? null,\n organizationId: payment.organizationId,\n tenantId: payment.tenantId,\n paymentMethodId:\n typeof payment.paymentMethod === 'string'\n ? payment.paymentMethod\n : payment.paymentMethod?.id ?? null,\n paymentReference: payment.paymentReference ?? null,\n statusEntryId: payment.statusEntryId ?? null,\n status: payment.status ?? null,\n amount: toNumber(payment.amount),\n currencyCode: payment.currencyCode,\n capturedAmount: toNumber(payment.capturedAmount),\n refundedAmount: toNumber(payment.refundedAmount),\n receivedAt: payment.receivedAt ? payment.receivedAt.toISOString() : null,\n capturedAt: payment.capturedAt ? payment.capturedAt.toISOString() : null,\n metadata: payment.metadata ? cloneJson(payment.metadata) : null,\n customFields: normalizedCustomFields,\n customFieldSetId: (payment as any).customFieldSetId ?? (payment as any).custom_field_set_id ?? null,\n allocations,\n }\n}\n\nexport async function restorePaymentSnapshot(em: EntityManager, snapshot: PaymentSnapshot): Promise<void> {\n const orderRef = snapshot.orderId ? em.getReference(SalesOrder, snapshot.orderId) : null\n const methodRef = snapshot.paymentMethodId\n ? em.getReference(SalesPaymentMethod, snapshot.paymentMethodId)\n : null\n const entity =\n (await em.findOne(SalesPayment, { id: snapshot.id })) ??\n em.create(SalesPayment, {\n id: snapshot.id,\n createdAt: new Date(),\n organizationId: snapshot.organizationId,\n tenantId: snapshot.tenantId,\n })\n entity.order = orderRef\n entity.paymentMethod = methodRef\n entity.organizationId = snapshot.organizationId\n entity.tenantId = snapshot.tenantId\n entity.paymentReference = snapshot.paymentReference\n entity.statusEntryId = snapshot.statusEntryId\n entity.status = snapshot.status\n entity.amount = toNumericString(snapshot.amount) ?? '0'\n entity.currencyCode = snapshot.currencyCode\n entity.capturedAmount = toNumericString(snapshot.capturedAmount) ?? '0'\n entity.refundedAmount = toNumericString(snapshot.refundedAmount) ?? '0'\n entity.receivedAt = snapshot.receivedAt ? new Date(snapshot.receivedAt) : null\n entity.capturedAt = snapshot.capturedAt ? new Date(snapshot.capturedAt) : null\n entity.metadata = snapshot.metadata ? cloneJson(snapshot.metadata) : null\n entity.customFieldSetId =\n (snapshot as any).customFieldSetId ?? (snapshot as any).custom_field_set_id ?? null\n entity.updatedAt = new Date()\n\n if ((snapshot as any).customFields !== undefined) {\n await setRecordCustomFields(em, {\n entityId: E.sales.sales_payment,\n recordId: entity.id,\n organizationId: entity.organizationId,\n tenantId: entity.tenantId,\n values:\n snapshot.customFields && typeof snapshot.customFields === 'object'\n ? (snapshot.customFields as Record<string, unknown>)\n : {},\n })\n }\n\n const existingAllocations = await em.find(SalesPaymentAllocation, { payment: entity })\n existingAllocations.forEach((allocation) => em.remove(allocation))\n snapshot.allocations.forEach((allocation) => {\n const order =\n allocation.orderId && typeof allocation.orderId === 'string'\n ? em.getReference(SalesOrder, allocation.orderId)\n : null\n const invoice =\n allocation.invoiceId && typeof allocation.invoiceId === 'string'\n ? em.getReference(SalesInvoice, allocation.invoiceId)\n : null\n const newAllocation = em.create(SalesPaymentAllocation, {\n payment: entity,\n order,\n invoice,\n organizationId: snapshot.organizationId,\n tenantId: snapshot.tenantId,\n amount: toNumericString(allocation.amount) ?? '0',\n currencyCode: allocation.currencyCode,\n metadata: allocation.metadata ? cloneJson(allocation.metadata) : null,\n })\n em.persist(newAllocation)\n })\n em.persist(entity)\n}\n\nasync function recomputeOrderPaymentTotals(\n em: EntityManager,\n order: SalesOrder\n): Promise<{ paidTotalAmount: number; refundedTotalAmount: number; outstandingAmount: number }> {\n const orderId = order.id\n const scope = { organizationId: order.organizationId, tenantId: order.tenantId }\n\n const allocations = await findWithDecryption(\n em,\n SalesPaymentAllocation,\n { ...scope, order: orderId },\n { populate: ['payment'] },\n scope,\n )\n\n const paymentIds = new Set<string>()\n allocations.forEach((allocation) => {\n const paymentRef = allocation.payment\n const paymentId =\n typeof paymentRef === 'object' && paymentRef !== null\n ? paymentRef.id\n : typeof paymentRef === 'string'\n ? paymentRef\n : null\n if (paymentId) paymentIds.add(paymentId)\n })\n\n const payments =\n paymentIds.size > 0\n ? await em.find(SalesPayment, { id: { $in: Array.from(paymentIds) }, deletedAt: null, ...scope })\n : await em.find(SalesPayment, { order: orderId, deletedAt: null, ...scope })\n\n const resolvePaidAmount = (payment: SalesPayment) => {\n const captured = toNumber(payment.capturedAmount)\n return captured > 0 ? captured : toNumber(payment.amount)\n }\n\n const activePaymentIds = new Set(payments.map((payment) => payment.id))\n const paidTotal =\n allocations.length > 0\n ? allocations.reduce((sum, allocation) => {\n const paymentRef = allocation.payment\n const paymentId =\n typeof paymentRef === 'object' && paymentRef !== null\n ? paymentRef.id\n : typeof paymentRef === 'string'\n ? paymentRef\n : null\n if (paymentId && !activePaymentIds.has(paymentId)) return sum\n return sum + toNumber(allocation.amount)\n }, 0)\n : payments.reduce((sum, payment) => sum + resolvePaidAmount(payment), 0)\n\n const refundedTotal = payments.reduce(\n (sum, payment) => sum + toNumber(payment.refundedAmount),\n 0\n )\n\n const grandTotal = toNumber(order.grandTotalGrossAmount)\n const outstanding = Math.max(grandTotal - paidTotal + refundedTotal, 0)\n order.paidTotalAmount = toNumericString(paidTotal) ?? '0'\n order.refundedTotalAmount = toNumericString(refundedTotal) ?? '0'\n order.outstandingAmount = toNumericString(outstanding) ?? '0'\n return {\n paidTotalAmount: paidTotal,\n refundedTotalAmount: refundedTotal,\n outstandingAmount: outstanding,\n }\n}\n\nconst createPaymentCommand: CommandHandler<\n PaymentCreateInput,\n { paymentId: string; orderTotals?: { paidTotalAmount: number; refundedTotalAmount: number; outstandingAmount: number } }\n> = {\n id: 'sales.payments.create',\n async execute(rawInput, ctx) {\n const input = paymentCreateSchema.parse(rawInput ?? {})\n ensureTenantScope(ctx, input.tenantId)\n ensureOrganizationScope(ctx, input.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const { translate } = await resolveTranslations()\n if (!input.orderId) {\n throw new CrudHttpError(400, { error: translate('sales.payments.order_required', 'Order is required for payments.') })\n }\n const order = assertFound(\n await em.findOne(SalesOrder, { id: input.orderId }),\n 'sales.payments.order_not_found'\n )\n ensureSameScope(order, input.organizationId, input.tenantId)\n if (order.deletedAt) {\n throw new CrudHttpError(404, { error: 'sales.payments.order_not_found' })\n }\n if (\n order.currencyCode &&\n input.currencyCode &&\n order.currencyCode.toUpperCase() !== input.currencyCode.toUpperCase()\n ) {\n throw new CrudHttpError(400, {\n error: translate('sales.payments.currency_mismatch', 'Payment currency must match the order currency.'),\n })\n }\n let paymentMethod = null\n if (input.paymentMethodId) {\n const method = assertFound(\n await em.findOne(SalesPaymentMethod, { id: input.paymentMethodId }),\n 'sales.payments.method_not_found'\n )\n ensureSameScope(method, input.organizationId, input.tenantId)\n paymentMethod = method\n }\n if (input.documentStatusEntryId !== undefined) {\n const orderStatus = await resolveDictionaryEntryValue(em, input.documentStatusEntryId ?? null)\n if (input.documentStatusEntryId && !orderStatus) {\n throw new CrudHttpError(400, {\n error: translate('sales.documents.detail.statusInvalid', 'Selected status could not be found.'),\n })\n }\n order.statusEntryId = input.documentStatusEntryId ?? null\n order.status = orderStatus\n order.updatedAt = new Date()\n em.persist(order)\n }\n if (input.lineStatusEntryId !== undefined) {\n const lineStatus = await resolveDictionaryEntryValue(em, input.lineStatusEntryId ?? null)\n if (input.lineStatusEntryId && !lineStatus) {\n throw new CrudHttpError(400, {\n error: translate('sales.documents.detail.statusInvalid', 'Selected status could not be found.'),\n })\n }\n const orderLines = await em.find(SalesOrderLine, { order })\n orderLines.forEach((line) => {\n line.statusEntryId = input.lineStatusEntryId ?? null\n line.status = lineStatus\n line.updatedAt = new Date()\n })\n orderLines.forEach((line) => em.persist(line))\n }\n const status = await resolveDictionaryEntryValue(em, input.statusEntryId ?? null)\n const payment = em.create(SalesPayment, {\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n order,\n paymentMethod,\n paymentReference: input.paymentReference ?? null,\n statusEntryId: input.statusEntryId ?? null,\n status,\n amount: toNumericString(input.amount) ?? '0',\n currencyCode: input.currencyCode,\n capturedAmount: toNumericString(input.capturedAmount) ?? '0',\n refundedAmount: toNumericString(input.refundedAmount) ?? '0',\n receivedAt: input.receivedAt ?? null,\n capturedAt: input.capturedAt ?? null,\n metadata: input.metadata ? cloneJson(input.metadata) : null,\n customFieldSetId: input.customFieldSetId ?? null,\n })\n const allocationInputs = Array.isArray(input.allocations) ? input.allocations : []\n const allocations = allocationInputs.length\n ? allocationInputs\n : [\n {\n orderId: input.orderId,\n invoiceId: null,\n amount: input.amount,\n currencyCode: input.currencyCode,\n metadata: null,\n },\n ]\n allocations.forEach((allocation) => {\n const orderRef = allocation.orderId ? em.getReference(SalesOrder, allocation.orderId) : order\n const invoiceRef = allocation.invoiceId ? em.getReference(SalesInvoice, allocation.invoiceId) : null\n const entity = em.create(SalesPaymentAllocation, {\n payment,\n order: orderRef,\n invoice: invoiceRef,\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n amount: toNumericString(allocation.amount) ?? '0',\n currencyCode: allocation.currencyCode,\n metadata: allocation.metadata ? cloneJson(allocation.metadata) : null,\n })\n em.persist(entity)\n })\n em.persist(payment)\n if (input.customFields !== undefined) {\n if (!payment.id) {\n await em.flush()\n }\n await setRecordCustomFields(em, {\n entityId: E.sales.sales_payment,\n recordId: payment.id,\n organizationId: input.organizationId,\n tenantId: input.tenantId,\n values: normalizeCustomFieldsInput(input.customFields),\n })\n }\n await em.flush()\n const totals = await recomputeOrderPaymentTotals(em, order)\n await em.flush()\n await invalidateOrderCache(ctx.container, order, ctx.auth?.tenantId ?? null)\n\n // Create notification for payment received\n try {\n const notificationService = resolveNotificationService(ctx.container)\n const typeDef = notificationTypes.find((type) => type.type === 'sales.payment.received')\n if (typeDef) {\n const amountDisplay = payment.amount && payment.currencyCode\n ? `${payment.currencyCode} ${payment.amount}`\n : ''\n const notificationInput = buildFeatureNotificationFromType(typeDef, {\n requiredFeature: 'sales.orders.manage',\n bodyVariables: {\n orderNumber: order.orderNumber ?? '',\n amount: amountDisplay,\n },\n sourceEntityType: 'sales:order',\n sourceEntityId: order.id,\n linkHref: `/backend/sales/orders/${order.id}`,\n })\n\n await notificationService.createForFeature(notificationInput, {\n tenantId: payment.tenantId,\n organizationId: payment.organizationId ?? null,\n })\n }\n } catch (err) {\n // Notification creation is non-critical, don't fail the command\n console.error('[sales.payments.create] Failed to create notification:', err)\n }\n\n return { paymentId: payment.id, orderTotals: totals }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = ctx.container.resolve('em') as EntityManager\n return result?.paymentId ? loadPaymentSnapshot(em, result.paymentId) : null\n },\n buildLog: async ({ result, snapshots }) => {\n const after = snapshots.after as PaymentSnapshot | undefined\n if (!after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate('sales.audit.payments.create', 'Create payment'),\n resourceKind: 'sales.payment',\n resourceId: result.paymentId,\n tenantId: after.tenantId,\n organizationId: after.organizationId,\n snapshotAfter: after,\n payload: { undo: { after } satisfies PaymentUndoPayload },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PaymentUndoPayload>(logEntry)\n const after = payload?.after\n if (!after) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const existing = await em.findOne(SalesPayment, { id: after.id })\n if (existing) {\n const orderRef =\n typeof existing.order === 'string' ? existing.order : existing.order?.id ?? null\n const allocations = await em.find(SalesPaymentAllocation, { payment: existing })\n const allocationOrders = allocations\n .map((allocation) =>\n typeof allocation.order === 'string'\n ? allocation.order\n : allocation.order?.id ?? null\n )\n .filter((value): value is string => typeof value === 'string' && value.length > 0)\n\n allocations.forEach((allocation) => em.remove(allocation))\n await em.flush()\n\n em.remove(existing)\n await em.flush()\n\n const orderIds = Array.from(\n new Set(\n [\n orderRef,\n ...allocationOrders,\n ].filter((value): value is string => typeof value === 'string' && value.length > 0)\n )\n )\n for (const id of orderIds) {\n const order = await em.findOne(SalesOrder, { id })\n if (!order) continue\n await recomputeOrderPaymentTotals(em, order)\n await em.flush()\n }\n }\n },\n}\n\nconst updatePaymentCommand: CommandHandler<\n PaymentUpdateInput,\n { paymentId: string; orderTotals?: { paidTotalAmount: number; refundedTotalAmount: number; outstandingAmount: number } }\n> = {\n id: 'sales.payments.update',\n async prepare(rawInput, ctx) {\n const parsed = paymentUpdateSchema.parse(rawInput ?? {})\n if (!parsed.id) return {}\n const em = ctx.container.resolve('em') as EntityManager\n const snapshot = await loadPaymentSnapshot(em, parsed.id)\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 input = paymentUpdateSchema.parse(rawInput ?? {})\n ensureTenantScope(ctx, input.tenantId)\n ensureOrganizationScope(ctx, input.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const { translate } = await resolveTranslations()\n const payment = assertFound(\n await findOneWithDecryption(\n em,\n SalesPayment,\n { id: input.id },\n { populate: ['order'] },\n { tenantId: input.tenantId, organizationId: input.organizationId },\n ),\n 'sales.payments.not_found'\n )\n ensureSameScope(payment, input.organizationId, input.tenantId)\n const previousOrder = payment.order as SalesOrder | null\n if (input.orderId !== undefined) {\n if (!input.orderId) {\n payment.order = null\n } else {\n const order = assertFound(\n await em.findOne(SalesOrder, { id: input.orderId }),\n 'sales.payments.order_not_found'\n )\n ensureSameScope(order, input.organizationId, input.tenantId)\n if (\n order.currencyCode &&\n input.currencyCode &&\n order.currencyCode.toUpperCase() !== input.currencyCode.toUpperCase()\n ) {\n throw new CrudHttpError(400, {\n error: translate('sales.payments.currency_mismatch', 'Payment currency must match the order currency.'),\n })\n }\n payment.order = order\n }\n }\n if (input.paymentMethodId !== undefined) {\n if (!input.paymentMethodId) {\n payment.paymentMethod = null\n } else {\n const method = assertFound(\n await em.findOne(SalesPaymentMethod, { id: input.paymentMethodId }),\n 'sales.payments.method_not_found'\n )\n ensureSameScope(method, input.organizationId, input.tenantId)\n payment.paymentMethod = method\n }\n }\n const currentOrder = payment.order as SalesOrder | null\n if ((input.documentStatusEntryId !== undefined || input.lineStatusEntryId !== undefined) && !currentOrder) {\n throw new CrudHttpError(400, { error: translate('sales.payments.order_required', 'Order is required for payments.') })\n }\n if (currentOrder && input.documentStatusEntryId !== undefined) {\n const orderStatus = await resolveDictionaryEntryValue(em, input.documentStatusEntryId ?? null)\n if (input.documentStatusEntryId && !orderStatus) {\n throw new CrudHttpError(400, {\n error: translate('sales.documents.detail.statusInvalid', 'Selected status could not be found.'),\n })\n }\n currentOrder.statusEntryId = input.documentStatusEntryId ?? null\n currentOrder.status = orderStatus\n currentOrder.updatedAt = new Date()\n em.persist(currentOrder)\n }\n if (currentOrder && input.lineStatusEntryId !== undefined) {\n const lineStatus = await resolveDictionaryEntryValue(em, input.lineStatusEntryId ?? null)\n if (input.lineStatusEntryId && !lineStatus) {\n throw new CrudHttpError(400, {\n error: translate('sales.documents.detail.statusInvalid', 'Selected status could not be found.'),\n })\n }\n const orderLines = await em.find(SalesOrderLine, { order: currentOrder })\n orderLines.forEach((line) => {\n line.statusEntryId = input.lineStatusEntryId ?? null\n line.status = lineStatus\n line.updatedAt = new Date()\n })\n orderLines.forEach((line) => em.persist(line))\n }\n if (input.paymentReference !== undefined) payment.paymentReference = input.paymentReference ?? null\n if (input.statusEntryId !== undefined) {\n payment.statusEntryId = input.statusEntryId ?? null\n payment.status = await resolveDictionaryEntryValue(em, input.statusEntryId ?? null)\n }\n if (input.amount !== undefined) payment.amount = toNumericString(input.amount) ?? '0'\n if (input.currencyCode !== undefined) payment.currencyCode = input.currencyCode\n if (input.capturedAmount !== undefined) {\n payment.capturedAmount = toNumericString(input.capturedAmount) ?? '0'\n }\n if (input.refundedAmount !== undefined) {\n payment.refundedAmount = toNumericString(input.refundedAmount) ?? '0'\n }\n if (input.receivedAt !== undefined) payment.receivedAt = input.receivedAt ?? null\n if (input.capturedAt !== undefined) payment.capturedAt = input.capturedAt ?? null\n if (input.metadata !== undefined) {\n payment.metadata = input.metadata ? cloneJson(input.metadata) : null\n }\n if (input.customFieldSetId !== undefined) {\n payment.customFieldSetId = input.customFieldSetId ?? null\n }\n if (input.customFields !== undefined) {\n if (!payment.id) {\n await em.flush()\n }\n await setRecordCustomFields(em, {\n entityId: E.sales.sales_payment,\n recordId: payment.id,\n organizationId: payment.organizationId,\n tenantId: payment.tenantId,\n values: normalizeCustomFieldsInput(input.customFields),\n })\n }\n if (input.allocations !== undefined) {\n const existingAllocations = await em.find(SalesPaymentAllocation, { payment })\n existingAllocations.forEach((allocation) => em.remove(allocation))\n const allocationInputs = Array.isArray(input.allocations) ? input.allocations : []\n allocationInputs.forEach((allocation) => {\n const orderRef =\n allocation.orderId ??\n (typeof payment.order === 'string' ? payment.order : payment.order?.id) ??\n null\n const order =\n orderRef && typeof orderRef === 'string' ? em.getReference(SalesOrder, orderRef) : null\n const invoice = allocation.invoiceId\n ? em.getReference(SalesInvoice, allocation.invoiceId)\n : null\n const entity = em.create(SalesPaymentAllocation, {\n payment,\n order,\n invoice,\n organizationId: payment.organizationId,\n tenantId: payment.tenantId,\n amount: toNumericString(allocation.amount) ?? '0',\n currencyCode: allocation.currencyCode,\n metadata: allocation.metadata ? cloneJson(allocation.metadata) : null,\n })\n em.persist(entity)\n })\n }\n payment.updatedAt = new Date()\n await em.flush()\n\n const nextOrder =\n (payment.order as SalesOrder | null) ??\n (typeof payment.order === 'string'\n ? await em.findOne(SalesOrder, { id: payment.order })\n : null)\n let totals: { paidTotalAmount: number; refundedTotalAmount: number; outstandingAmount: number } | undefined\n if (nextOrder) {\n totals = await recomputeOrderPaymentTotals(em, nextOrder)\n await em.flush()\n await invalidateOrderCache(ctx.container, nextOrder, ctx.auth?.tenantId ?? null)\n }\n if (previousOrder && (!nextOrder || previousOrder.id !== nextOrder.id)) {\n await recomputeOrderPaymentTotals(em, previousOrder)\n await em.flush()\n await invalidateOrderCache(ctx.container, previousOrder, ctx.auth?.tenantId ?? null)\n }\n\n return { paymentId: payment.id, orderTotals: totals }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = ctx.container.resolve('em') as EntityManager\n return result?.paymentId ? loadPaymentSnapshot(em, result.paymentId) : null\n },\n buildLog: async ({ snapshots, result }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as PaymentSnapshot | undefined\n const after = snapshots.after as PaymentSnapshot | undefined\n return {\n actionLabel: translate('sales.audit.payments.update', 'Update payment'),\n resourceKind: 'sales.payment',\n resourceId: result.paymentId,\n tenantId: after?.tenantId ?? before?.tenantId ?? null,\n organizationId: after?.organizationId ?? before?.organizationId ?? null,\n snapshotBefore: before ?? null,\n snapshotAfter: after ?? null,\n payload: { undo: { before, after } satisfies PaymentUndoPayload },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PaymentUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n await restorePaymentSnapshot(em, before)\n await em.flush()\n if (before.orderId) {\n const order = await em.findOne(SalesOrder, { id: before.orderId })\n if (order) {\n await recomputeOrderPaymentTotals(em, order)\n await em.flush()\n }\n }\n },\n}\n\nconst deletePaymentCommand: CommandHandler<\n { id: string; orderId?: string | null; organizationId: string; tenantId: string },\n { paymentId: string; orderTotals?: { paidTotalAmount: number; refundedTotalAmount: number; outstandingAmount: number } }\n> = {\n id: 'sales.payments.delete',\n async prepare(rawInput, ctx) {\n const parsed = paymentUpdateSchema.parse(rawInput ?? {})\n if (!parsed.id) return {}\n const em = ctx.container.resolve('em') as EntityManager\n const snapshot = await loadPaymentSnapshot(em, parsed.id)\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 input = paymentUpdateSchema.parse(rawInput ?? {})\n ensureTenantScope(ctx, input.tenantId)\n ensureOrganizationScope(ctx, input.organizationId)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const payment = assertFound(\n await findOneWithDecryption(\n em,\n SalesPayment,\n { id: input.id },\n { populate: ['order'] },\n { tenantId: input.tenantId, organizationId: input.organizationId },\n ),\n 'sales.payments.not_found'\n )\n ensureSameScope(payment, input.organizationId, input.tenantId)\n const order = payment.order as SalesOrder | null\n const allocations = await em.find(SalesPaymentAllocation, { payment })\n const allocationOrders = allocations\n .map((allocation) =>\n typeof allocation.order === 'string'\n ? allocation.order\n : allocation.order?.id ?? null\n )\n .filter((value): value is string => typeof value === 'string' && value.length > 0)\n allocations.forEach((allocation) => em.remove(allocation))\n await em.flush()\n em.remove(payment)\n await em.flush()\n let totals: { paidTotalAmount: number; refundedTotalAmount: number; outstandingAmount: number } | undefined\n const orderIds = Array.from(\n new Set(\n [\n order && typeof order === 'object' ? order.id : null,\n ...allocationOrders,\n ].filter((value): value is string => typeof value === 'string' && value.length > 0)\n )\n )\n const primaryOrderId = order && typeof order === 'object' ? order.id : null\n for (const orderId of orderIds) {\n const target = typeof order === 'object' && order.id === orderId ? order : await em.findOne(SalesOrder, { id: orderId })\n if (!target) continue\n const recomputed = await recomputeOrderPaymentTotals(em, target)\n if (!totals || (primaryOrderId && orderId === primaryOrderId)) {\n totals = recomputed\n }\n await em.flush()\n await invalidateOrderCache(ctx.container, target, ctx.auth?.tenantId ?? null)\n }\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n if (allocations.length) {\n await Promise.all(\n allocations.map((allocation) =>\n emitCrudSideEffects({\n dataEngine,\n action: 'deleted',\n entity: allocation,\n identifiers: {\n id: allocation.id,\n organizationId: allocation.organizationId ?? null,\n tenantId: allocation.tenantId ?? null,\n },\n indexer: { entityType: E.sales.sales_payment_allocation },\n })\n )\n )\n }\n return { paymentId: payment.id, orderTotals: totals }\n },\n buildLog: async ({ snapshots, result }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as PaymentSnapshot | undefined\n return {\n actionLabel: translate('sales.audit.payments.delete', 'Delete payment'),\n resourceKind: 'sales.payment',\n resourceId: result.paymentId,\n tenantId: before?.tenantId ?? null,\n organizationId: before?.organizationId ?? null,\n snapshotBefore: before ?? null,\n payload: { undo: { before } satisfies PaymentUndoPayload },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<PaymentUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n await restorePaymentSnapshot(em, before)\n await em.flush()\n if (before.orderId) {\n const order = await em.findOne(SalesOrder, { id: before.orderId })\n if (order) {\n await recomputeOrderPaymentTotals(em, order)\n await em.flush()\n }\n }\n },\n}\n\nexport const paymentCommands = [createPaymentCommand, updatePaymentCommand, deletePaymentCommand]\n\nregisterCommand(createPaymentCommand)\nregisterCommand(updatePaymentCommand)\nregisterCommand(deletePaymentCommand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AO;AACT,cAAM,4BAA4B,IAAI,KAAK;AAC3C,cAAM,GAAG,MAAM;AAAA,MACjB;AAAA,IACF;AAAA,EACF;AACF;AAEO,MAAM,kBAAkB,CAAC,sBAAsB,sBAAsB,oBAAoB;AAEhG,gBAAgB,oBAAoB;AACpC,gBAAgB,oBAAoB;AACpC,gBAAgB,oBAAoB;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,51 @@
1
+ "use client";
2
+ import { SalesOrderCreatedRenderer } from "./widgets/notifications/SalesOrderCreatedRenderer.js";
3
+ import { SalesQuoteCreatedRenderer } from "./widgets/notifications/SalesQuoteCreatedRenderer.js";
4
+ const salesNotificationTypes = [
5
+ {
6
+ type: "sales.order.created",
7
+ module: "sales",
8
+ titleKey: "sales.notifications.order.created.title",
9
+ bodyKey: "sales.notifications.order.created.body",
10
+ icon: "shopping-cart",
11
+ severity: "info",
12
+ actions: [
13
+ {
14
+ id: "view",
15
+ labelKey: "common.view",
16
+ variant: "outline",
17
+ href: "/backend/sales/orders/{sourceEntityId}",
18
+ icon: "external-link"
19
+ }
20
+ ],
21
+ linkHref: "/backend/sales/orders/{sourceEntityId}",
22
+ Renderer: SalesOrderCreatedRenderer,
23
+ expiresAfterHours: 168
24
+ },
25
+ {
26
+ type: "sales.quote.created",
27
+ module: "sales",
28
+ titleKey: "sales.notifications.quote.created.title",
29
+ bodyKey: "sales.notifications.quote.created.body",
30
+ icon: "file-text",
31
+ severity: "info",
32
+ actions: [
33
+ {
34
+ id: "view",
35
+ labelKey: "common.view",
36
+ variant: "outline",
37
+ href: "/backend/sales/quotes/{sourceEntityId}",
38
+ icon: "external-link"
39
+ }
40
+ ],
41
+ linkHref: "/backend/sales/quotes/{sourceEntityId}",
42
+ Renderer: SalesQuoteCreatedRenderer,
43
+ expiresAfterHours: 168
44
+ }
45
+ ];
46
+ var notifications_client_default = salesNotificationTypes;
47
+ export {
48
+ notifications_client_default as default,
49
+ salesNotificationTypes
50
+ };
51
+ //# sourceMappingURL=notifications.client.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/modules/sales/notifications.client.ts"],
4
+ "sourcesContent": ["'use client'\n\nimport type { NotificationTypeDefinition } from '@open-mercato/shared/modules/notifications/types'\nimport { SalesOrderCreatedRenderer } from './widgets/notifications/SalesOrderCreatedRenderer'\nimport { SalesQuoteCreatedRenderer } from './widgets/notifications/SalesQuoteCreatedRenderer'\n\n/**\n * Client-side notification type definitions with custom renderers.\n * These should be used in client components where custom rendering is needed.\n *\n * Example usage:\n * ```tsx\n * import { salesNotificationTypes } from '@open-mercato/core/modules/sales/notifications.client'\n *\n * // Use in NotificationPanel or NotificationItem\n * const renderer = salesNotificationTypes.find(t => t.type === notification.type)?.Renderer\n * if (renderer) {\n * return <renderer notification={notification} onAction={...} onDismiss={...} actions={...} />\n * }\n * ```\n */\nexport const salesNotificationTypes: NotificationTypeDefinition[] = [\n {\n type: 'sales.order.created',\n module: 'sales',\n titleKey: 'sales.notifications.order.created.title',\n bodyKey: 'sales.notifications.order.created.body',\n icon: 'shopping-cart',\n severity: 'info',\n actions: [\n {\n id: 'view',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/sales/orders/{sourceEntityId}',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/sales/orders/{sourceEntityId}',\n Renderer: SalesOrderCreatedRenderer,\n expiresAfterHours: 168,\n },\n {\n type: 'sales.quote.created',\n module: 'sales',\n titleKey: 'sales.notifications.quote.created.title',\n bodyKey: 'sales.notifications.quote.created.body',\n icon: 'file-text',\n severity: 'info',\n actions: [\n {\n id: 'view',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/sales/quotes/{sourceEntityId}',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/sales/quotes/{sourceEntityId}',\n Renderer: SalesQuoteCreatedRenderer,\n expiresAfterHours: 168,\n },\n]\n\nexport default salesNotificationTypes\n"],
5
+ "mappings": ";AAGA,SAAS,iCAAiC;AAC1C,SAAS,iCAAiC;AAiBnC,MAAM,yBAAuD;AAAA,EAClE;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,SAAS;AAAA,IACT,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,MACP;AAAA,QACE,IAAI;AAAA,QACJ,UAAU;AAAA,QACV,SAAS;AAAA,QACT,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAAA,IACF;AAAA,IACA,UAAU;AAAA,IACV,UAAU;AAAA,IACV,mBAAmB;AAAA,EACrB;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,SAAS;AAAA,IACT,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,MACP;AAAA,QACE,IAAI;AAAA,QACJ,UAAU;AAAA,QACV,SAAS;AAAA,QACT,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAAA,IACF;AAAA,IACA,UAAU;AAAA,IACV,UAAU;AAAA,IACV,mBAAmB;AAAA,EACrB;AACF;AAEA,IAAO,+BAAQ;",
6
+ "names": []
7
+ }
@@ -0,0 +1,88 @@
1
+ const notificationTypes = [
2
+ {
3
+ type: "sales.order.created",
4
+ module: "sales",
5
+ titleKey: "sales.notifications.order.created.title",
6
+ bodyKey: "sales.notifications.order.created.body",
7
+ icon: "shopping-cart",
8
+ severity: "info",
9
+ actions: [
10
+ {
11
+ id: "view",
12
+ labelKey: "common.view",
13
+ variant: "outline",
14
+ href: "/backend/sales/orders/{sourceEntityId}",
15
+ icon: "external-link"
16
+ }
17
+ ],
18
+ linkHref: "/backend/sales/orders/{sourceEntityId}",
19
+ expiresAfterHours: 168
20
+ // 7 days
21
+ },
22
+ {
23
+ type: "sales.quote.created",
24
+ module: "sales",
25
+ titleKey: "sales.notifications.quote.created.title",
26
+ bodyKey: "sales.notifications.quote.created.body",
27
+ icon: "file-text",
28
+ severity: "info",
29
+ actions: [
30
+ {
31
+ id: "view",
32
+ labelKey: "common.view",
33
+ variant: "outline",
34
+ href: "/backend/sales/quotes/{sourceEntityId}",
35
+ icon: "external-link"
36
+ }
37
+ ],
38
+ linkHref: "/backend/sales/quotes/{sourceEntityId}",
39
+ expiresAfterHours: 168
40
+ // 7 days
41
+ },
42
+ {
43
+ type: "sales.payment.received",
44
+ module: "sales",
45
+ titleKey: "sales.notifications.payment.received.title",
46
+ bodyKey: "sales.notifications.payment.received.body",
47
+ icon: "credit-card",
48
+ severity: "success",
49
+ actions: [
50
+ {
51
+ id: "view",
52
+ labelKey: "common.view",
53
+ variant: "outline",
54
+ href: "/backend/sales/orders/{sourceEntityId}",
55
+ icon: "external-link"
56
+ }
57
+ ],
58
+ linkHref: "/backend/sales/orders/{sourceEntityId}",
59
+ expiresAfterHours: 168
60
+ // 7 days
61
+ },
62
+ {
63
+ type: "sales.quote.expiring",
64
+ module: "sales",
65
+ titleKey: "sales.notifications.quote.expiring.title",
66
+ bodyKey: "sales.notifications.quote.expiring.body",
67
+ icon: "clock",
68
+ severity: "warning",
69
+ actions: [
70
+ {
71
+ id: "view",
72
+ labelKey: "common.view",
73
+ variant: "outline",
74
+ href: "/backend/sales/quotes/{sourceEntityId}",
75
+ icon: "external-link"
76
+ }
77
+ ],
78
+ linkHref: "/backend/sales/quotes/{sourceEntityId}",
79
+ expiresAfterHours: 72
80
+ // 3 days
81
+ }
82
+ ];
83
+ var notifications_default = notificationTypes;
84
+ export {
85
+ notifications_default as default,
86
+ notificationTypes
87
+ };
88
+ //# sourceMappingURL=notifications.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/modules/sales/notifications.ts"],
4
+ "sourcesContent": ["import type { NotificationTypeDefinition } from '@open-mercato/shared/modules/notifications/types'\n\nexport const notificationTypes: NotificationTypeDefinition[] = [\n {\n type: 'sales.order.created',\n module: 'sales',\n titleKey: 'sales.notifications.order.created.title',\n bodyKey: 'sales.notifications.order.created.body',\n icon: 'shopping-cart',\n severity: 'info',\n actions: [\n {\n id: 'view',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/sales/orders/{sourceEntityId}',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/sales/orders/{sourceEntityId}',\n expiresAfterHours: 168, // 7 days\n },\n {\n type: 'sales.quote.created',\n module: 'sales',\n titleKey: 'sales.notifications.quote.created.title',\n bodyKey: 'sales.notifications.quote.created.body',\n icon: 'file-text',\n severity: 'info',\n actions: [\n {\n id: 'view',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/sales/quotes/{sourceEntityId}',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/sales/quotes/{sourceEntityId}',\n expiresAfterHours: 168, // 7 days\n },\n {\n type: 'sales.payment.received',\n module: 'sales',\n titleKey: 'sales.notifications.payment.received.title',\n bodyKey: 'sales.notifications.payment.received.body',\n icon: 'credit-card',\n severity: 'success',\n actions: [\n {\n id: 'view',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/sales/orders/{sourceEntityId}',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/sales/orders/{sourceEntityId}',\n expiresAfterHours: 168, // 7 days\n },\n {\n type: 'sales.quote.expiring',\n module: 'sales',\n titleKey: 'sales.notifications.quote.expiring.title',\n bodyKey: 'sales.notifications.quote.expiring.body',\n icon: 'clock',\n severity: 'warning',\n actions: [\n {\n id: 'view',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/sales/quotes/{sourceEntityId}',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/sales/quotes/{sourceEntityId}',\n expiresAfterHours: 72, // 3 days\n },\n]\n\nexport default notificationTypes\n"],
5
+ "mappings": "AAEO,MAAM,oBAAkD;AAAA,EAC7D;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,SAAS;AAAA,IACT,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,MACP;AAAA,QACE,IAAI;AAAA,QACJ,UAAU;AAAA,QACV,SAAS;AAAA,QACT,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAAA,IACF;AAAA,IACA,UAAU;AAAA,IACV,mBAAmB;AAAA;AAAA,EACrB;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,SAAS;AAAA,IACT,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,MACP;AAAA,QACE,IAAI;AAAA,QACJ,UAAU;AAAA,QACV,SAAS;AAAA,QACT,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAAA,IACF;AAAA,IACA,UAAU;AAAA,IACV,mBAAmB;AAAA;AAAA,EACrB;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,SAAS;AAAA,IACT,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,MACP;AAAA,QACE,IAAI;AAAA,QACJ,UAAU;AAAA,QACV,SAAS;AAAA,QACT,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAAA,IACF;AAAA,IACA,UAAU;AAAA,IACV,mBAAmB;AAAA;AAAA,EACrB;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,SAAS;AAAA,IACT,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,MACP;AAAA,QACE,IAAI;AAAA,QACJ,UAAU;AAAA,QACV,SAAS;AAAA,QACT,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAAA,IACF;AAAA,IACA,UAAU;AAAA,IACV,mBAAmB;AAAA;AAAA,EACrB;AACF;AAEA,IAAO,wBAAQ;",
6
+ "names": []
7
+ }
@@ -0,0 +1,38 @@
1
+ import { resolveNotificationService } from "../../notifications/lib/notificationService.js";
2
+ import { buildFeatureNotificationFromType } from "../../notifications/lib/notificationBuilder.js";
3
+ import { notificationTypes } from "../notifications.js";
4
+ const metadata = {
5
+ event: "sales.quote.expiring",
6
+ persistent: true,
7
+ id: "sales:quote-expiring-notification"
8
+ };
9
+ async function handle(payload, ctx) {
10
+ try {
11
+ const notificationService = resolveNotificationService(ctx);
12
+ const typeDef = notificationTypes.find((type) => type.type === "sales.quote.expiring");
13
+ if (!typeDef) return;
14
+ const notificationInput = buildFeatureNotificationFromType(typeDef, {
15
+ requiredFeature: "sales.quotes.manage",
16
+ bodyVariables: {
17
+ quoteNumber: payload.quoteNumber,
18
+ expiresAt: payload.expiresAt,
19
+ daysUntilExpiry: String(payload.daysUntilExpiry),
20
+ customerName: payload.customerName ?? ""
21
+ },
22
+ sourceEntityType: "sales:quote",
23
+ sourceEntityId: payload.quoteId,
24
+ linkHref: `/backend/sales/quotes/${payload.quoteId}`
25
+ });
26
+ await notificationService.createForFeature(notificationInput, {
27
+ tenantId: payload.tenantId,
28
+ organizationId: payload.organizationId ?? null
29
+ });
30
+ } catch (err) {
31
+ console.error("[sales:quote-expiring-notification] Failed to create notification:", err);
32
+ }
33
+ }
34
+ export {
35
+ handle as default,
36
+ metadata
37
+ };
38
+ //# sourceMappingURL=quote-expiring-notification.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/modules/sales/subscribers/quote-expiring-notification.ts"],
4
+ "sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { resolveNotificationService } from '../../notifications/lib/notificationService'\nimport { buildFeatureNotificationFromType } from '../../notifications/lib/notificationBuilder'\nimport { notificationTypes } from '../notifications'\n\nexport const metadata = {\n event: 'sales.quote.expiring',\n persistent: true,\n id: 'sales:quote-expiring-notification',\n}\n\ntype QuoteExpiringPayload = {\n quoteId: string\n quoteNumber: string\n expiresAt: string\n daysUntilExpiry: number\n customerName?: string | null\n totalAmount?: string | null\n tenantId: string\n organizationId?: string | null\n}\n\ntype ResolverContext = {\n resolve: <T = unknown>(name: string) => T\n}\n\nexport default async function handle(payload: QuoteExpiringPayload, ctx: ResolverContext) {\n try {\n const notificationService = resolveNotificationService(ctx)\n const typeDef = notificationTypes.find((type) => type.type === 'sales.quote.expiring')\n if (!typeDef) return\n\n const notificationInput = buildFeatureNotificationFromType(typeDef, {\n requiredFeature: 'sales.quotes.manage',\n bodyVariables: {\n quoteNumber: payload.quoteNumber,\n expiresAt: payload.expiresAt,\n daysUntilExpiry: String(payload.daysUntilExpiry),\n customerName: payload.customerName ?? '',\n },\n sourceEntityType: 'sales:quote',\n sourceEntityId: payload.quoteId,\n linkHref: `/backend/sales/quotes/${payload.quoteId}`,\n })\n\n await notificationService.createForFeature(notificationInput, {\n tenantId: payload.tenantId,\n organizationId: payload.organizationId ?? null,\n })\n } catch (err) {\n console.error('[sales:quote-expiring-notification] Failed to create notification:', err)\n }\n}\n"],
5
+ "mappings": "AACA,SAAS,kCAAkC;AAC3C,SAAS,wCAAwC;AACjD,SAAS,yBAAyB;AAE3B,MAAM,WAAW;AAAA,EACtB,OAAO;AAAA,EACP,YAAY;AAAA,EACZ,IAAI;AACN;AAiBA,eAAO,OAA8B,SAA+B,KAAsB;AACxF,MAAI;AACF,UAAM,sBAAsB,2BAA2B,GAAG;AAC1D,UAAM,UAAU,kBAAkB,KAAK,CAAC,SAAS,KAAK,SAAS,sBAAsB;AACrF,QAAI,CAAC,QAAS;AAEd,UAAM,oBAAoB,iCAAiC,SAAS;AAAA,MAClE,iBAAiB;AAAA,MACjB,eAAe;AAAA,QACb,aAAa,QAAQ;AAAA,QACrB,WAAW,QAAQ;AAAA,QACnB,iBAAiB,OAAO,QAAQ,eAAe;AAAA,QAC/C,cAAc,QAAQ,gBAAgB;AAAA,MACxC;AAAA,MACA,kBAAkB;AAAA,MAClB,gBAAgB,QAAQ;AAAA,MACxB,UAAU,yBAAyB,QAAQ,OAAO;AAAA,IACpD,CAAC;AAED,UAAM,oBAAoB,iBAAiB,mBAAmB;AAAA,MAC5D,UAAU,QAAQ;AAAA,MAClB,gBAAgB,QAAQ,kBAAkB;AAAA,IAC5C,CAAC;AAAA,EACH,SAAS,KAAK;AACZ,YAAQ,MAAM,sEAAsE,GAAG;AAAA,EACzF;AACF;",
6
+ "names": []
7
+ }