@open-mercato/core 0.6.7-develop.6661.1.0043ed1d03 → 0.6.7-develop.6670.1.4efa7961c6

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 (494) hide show
  1. package/.turbo/turbo-build.log +2 -2
  2. package/dist/generated/entities/inventory_balance/index.js +21 -0
  3. package/dist/generated/entities/inventory_balance/index.js.map +7 -0
  4. package/dist/generated/entities/inventory_lot/index.js +19 -0
  5. package/dist/generated/entities/inventory_lot/index.js.map +7 -0
  6. package/dist/generated/entities/inventory_movement/index.js +35 -0
  7. package/dist/generated/entities/inventory_movement/index.js.map +7 -0
  8. package/dist/generated/entities/inventory_reservation/index.js +23 -0
  9. package/dist/generated/entities/inventory_reservation/index.js.map +7 -0
  10. package/dist/generated/entities/product_inventory_profile/index.js +21 -0
  11. package/dist/generated/entities/product_inventory_profile/index.js.map +7 -0
  12. package/dist/generated/entities/sales_order_warehouse_assignment/index.js +11 -0
  13. package/dist/generated/entities/sales_order_warehouse_assignment/index.js.map +7 -0
  14. package/dist/generated/entities/warehouse/index.js +25 -0
  15. package/dist/generated/entities/warehouse/index.js.map +7 -0
  16. package/dist/generated/entities/warehouse_location/index.js +21 -0
  17. package/dist/generated/entities/warehouse_location/index.js.map +7 -0
  18. package/dist/generated/entities/warehouse_zone/index.js +11 -0
  19. package/dist/generated/entities/warehouse_zone/index.js.map +7 -0
  20. package/dist/generated/entities.ids.generated.js +12 -0
  21. package/dist/generated/entities.ids.generated.js.map +2 -2
  22. package/dist/generated/entity-fields-registry.js +98 -0
  23. package/dist/generated/entity-fields-registry.js.map +2 -2
  24. package/dist/modules/auth/backend/roles/page.js +12 -4
  25. package/dist/modules/auth/backend/roles/page.js.map +2 -2
  26. package/dist/modules/auth/lib/backendChrome.js +1 -0
  27. package/dist/modules/auth/lib/backendChrome.js.map +2 -2
  28. package/dist/modules/auth/services/rbacService.js +16 -17
  29. package/dist/modules/auth/services/rbacService.js.map +2 -2
  30. package/dist/modules/catalog/api/products/route.js +3 -0
  31. package/dist/modules/catalog/api/products/route.js.map +2 -2
  32. package/dist/modules/catalog/api/variants/route.js +3 -0
  33. package/dist/modules/catalog/api/variants/route.js.map +2 -2
  34. package/dist/modules/customer_accounts/migrations/Migration20260724120000_customer_accounts.js +56 -0
  35. package/dist/modules/customer_accounts/migrations/Migration20260724120000_customer_accounts.js.map +7 -0
  36. package/dist/modules/directory/api/organizations/route.js +1 -1
  37. package/dist/modules/directory/api/organizations/route.js.map +1 -1
  38. package/dist/modules/entities/api/definitions.js +2 -0
  39. package/dist/modules/entities/api/definitions.js.map +2 -2
  40. package/dist/modules/entities/lib/helpers.js +1 -0
  41. package/dist/modules/entities/lib/helpers.js.map +2 -2
  42. package/dist/modules/query_index/lib/engine.js +6 -6
  43. package/dist/modules/query_index/lib/engine.js.map +2 -2
  44. package/dist/modules/sales/api/documents/factory.js +1 -0
  45. package/dist/modules/sales/api/documents/factory.js.map +2 -2
  46. package/dist/modules/sales/api/quotes/accept/route.js +7 -2
  47. package/dist/modules/sales/api/quotes/accept/route.js.map +2 -2
  48. package/dist/modules/sales/api/quotes/public/[token]/route.js +2 -1
  49. package/dist/modules/sales/api/quotes/public/[token]/route.js.map +2 -2
  50. package/dist/modules/sales/commands/documents.js +53 -0
  51. package/dist/modules/sales/commands/documents.js.map +2 -2
  52. package/dist/modules/sales/components/documents/ItemsSection.js +51 -2
  53. package/dist/modules/sales/components/documents/ItemsSection.js.map +2 -2
  54. package/dist/modules/sales/events.js +2 -0
  55. package/dist/modules/sales/events.js.map +2 -2
  56. package/dist/modules/sales/lib/publicQuoteTenantScope.js +24 -0
  57. package/dist/modules/sales/lib/publicQuoteTenantScope.js.map +7 -0
  58. package/dist/modules/sales/widgets/injection/order-items-context.js +11 -0
  59. package/dist/modules/sales/widgets/injection/order-items-context.js.map +7 -0
  60. package/dist/modules/wms/acl.js +18 -0
  61. package/dist/modules/wms/acl.js.map +7 -0
  62. package/dist/modules/wms/analytics.js +11 -0
  63. package/dist/modules/wms/analytics.js.map +7 -0
  64. package/dist/modules/wms/api/dashboard/operational/route.js +80 -0
  65. package/dist/modules/wms/api/dashboard/operational/route.js.map +7 -0
  66. package/dist/modules/wms/api/interceptors.js +90 -0
  67. package/dist/modules/wms/api/interceptors.js.map +7 -0
  68. package/dist/modules/wms/api/inventory/adjust/route.js +51 -0
  69. package/dist/modules/wms/api/inventory/adjust/route.js.map +7 -0
  70. package/dist/modules/wms/api/inventory/allocate/route.js +51 -0
  71. package/dist/modules/wms/api/inventory/allocate/route.js.map +7 -0
  72. package/dist/modules/wms/api/inventory/balances/route.js +172 -0
  73. package/dist/modules/wms/api/inventory/balances/route.js.map +7 -0
  74. package/dist/modules/wms/api/inventory/cycle-count/route.js +52 -0
  75. package/dist/modules/wms/api/inventory/cycle-count/route.js.map +7 -0
  76. package/dist/modules/wms/api/inventory/helpers.js +113 -0
  77. package/dist/modules/wms/api/inventory/helpers.js.map +7 -0
  78. package/dist/modules/wms/api/inventory/import/apply/route.js +46 -0
  79. package/dist/modules/wms/api/inventory/import/apply/route.js.map +7 -0
  80. package/dist/modules/wms/api/inventory/import/helpers.js +195 -0
  81. package/dist/modules/wms/api/inventory/import/helpers.js.map +7 -0
  82. package/dist/modules/wms/api/inventory/import/template/route.js +32 -0
  83. package/dist/modules/wms/api/inventory/import/template/route.js.map +7 -0
  84. package/dist/modules/wms/api/inventory/import/validate/route.js +54 -0
  85. package/dist/modules/wms/api/inventory/import/validate/route.js.map +7 -0
  86. package/dist/modules/wms/api/inventory/move/route.js +51 -0
  87. package/dist/modules/wms/api/inventory/move/route.js.map +7 -0
  88. package/dist/modules/wms/api/inventory/movements/route.js +164 -0
  89. package/dist/modules/wms/api/inventory/movements/route.js.map +7 -0
  90. package/dist/modules/wms/api/inventory/receive/route.js +50 -0
  91. package/dist/modules/wms/api/inventory/receive/route.js.map +7 -0
  92. package/dist/modules/wms/api/inventory/release/route.js +44 -0
  93. package/dist/modules/wms/api/inventory/release/route.js.map +7 -0
  94. package/dist/modules/wms/api/inventory/reservations/route.js +128 -0
  95. package/dist/modules/wms/api/inventory/reservations/route.js.map +7 -0
  96. package/dist/modules/wms/api/inventory/reserve/route.js +59 -0
  97. package/dist/modules/wms/api/inventory/reserve/route.js.map +7 -0
  98. package/dist/modules/wms/api/inventory-profiles/route.js +178 -0
  99. package/dist/modules/wms/api/inventory-profiles/route.js.map +7 -0
  100. package/dist/modules/wms/api/listEnrichers.js +181 -0
  101. package/dist/modules/wms/api/listEnrichers.js.map +7 -0
  102. package/dist/modules/wms/api/listSearch.js +77 -0
  103. package/dist/modules/wms/api/listSearch.js.map +7 -0
  104. package/dist/modules/wms/api/locations/route.js +172 -0
  105. package/dist/modules/wms/api/locations/route.js.map +7 -0
  106. package/dist/modules/wms/api/lots/route.js +204 -0
  107. package/dist/modules/wms/api/lots/route.js.map +7 -0
  108. package/dist/modules/wms/api/openapi.js +27 -0
  109. package/dist/modules/wms/api/openapi.js.map +7 -0
  110. package/dist/modules/wms/api/sales-orders/[salesOrderId]/re-run-reservation/route.js +54 -0
  111. package/dist/modules/wms/api/sales-orders/[salesOrderId]/re-run-reservation/route.js.map +7 -0
  112. package/dist/modules/wms/api/sales-orders/[salesOrderId]/warehouse-assignment/route.js +279 -0
  113. package/dist/modules/wms/api/sales-orders/[salesOrderId]/warehouse-assignment/route.js.map +7 -0
  114. package/dist/modules/wms/api/sales-orders/assign-warehouse/route.js +56 -0
  115. package/dist/modules/wms/api/sales-orders/assign-warehouse/route.js.map +7 -0
  116. package/dist/modules/wms/api/sales-orders/unassign-warehouse/route.js +43 -0
  117. package/dist/modules/wms/api/sales-orders/unassign-warehouse/route.js.map +7 -0
  118. package/dist/modules/wms/api/warehouses/route.js +181 -0
  119. package/dist/modules/wms/api/warehouses/route.js.map +7 -0
  120. package/dist/modules/wms/api/zones/route.js +159 -0
  121. package/dist/modules/wms/api/zones/route.js.map +7 -0
  122. package/dist/modules/wms/backend/config/wms/page.js +9 -0
  123. package/dist/modules/wms/backend/config/wms/page.js.map +7 -0
  124. package/dist/modules/wms/backend/config/wms/page.meta.js +18 -0
  125. package/dist/modules/wms/backend/config/wms/page.meta.js.map +7 -0
  126. package/dist/modules/wms/backend/page.js +9 -0
  127. package/dist/modules/wms/backend/page.js.map +7 -0
  128. package/dist/modules/wms/backend/page.meta.js +16 -0
  129. package/dist/modules/wms/backend/page.meta.js.map +7 -0
  130. package/dist/modules/wms/backend/wms/inventory/page.js +9 -0
  131. package/dist/modules/wms/backend/wms/inventory/page.js.map +7 -0
  132. package/dist/modules/wms/backend/wms/inventory/page.meta.js +18 -0
  133. package/dist/modules/wms/backend/wms/inventory/page.meta.js.map +7 -0
  134. package/dist/modules/wms/backend/wms/location/[id]/page.js +9 -0
  135. package/dist/modules/wms/backend/wms/location/[id]/page.js.map +7 -0
  136. package/dist/modules/wms/backend/wms/location/[id]/page.meta.js +18 -0
  137. package/dist/modules/wms/backend/wms/location/[id]/page.meta.js.map +7 -0
  138. package/dist/modules/wms/backend/wms/locations/page.js +10 -0
  139. package/dist/modules/wms/backend/wms/locations/page.js.map +7 -0
  140. package/dist/modules/wms/backend/wms/locations/page.meta.js +18 -0
  141. package/dist/modules/wms/backend/wms/locations/page.meta.js.map +7 -0
  142. package/dist/modules/wms/backend/wms/lot/[id]/page.js +9 -0
  143. package/dist/modules/wms/backend/wms/lot/[id]/page.js.map +7 -0
  144. package/dist/modules/wms/backend/wms/lot/[id]/page.meta.js +18 -0
  145. package/dist/modules/wms/backend/wms/lot/[id]/page.meta.js.map +7 -0
  146. package/dist/modules/wms/backend/wms/lots/page.js +9 -0
  147. package/dist/modules/wms/backend/wms/lots/page.js.map +7 -0
  148. package/dist/modules/wms/backend/wms/lots/page.meta.js +18 -0
  149. package/dist/modules/wms/backend/wms/lots/page.meta.js.map +7 -0
  150. package/dist/modules/wms/backend/wms/movements/page.js +22 -0
  151. package/dist/modules/wms/backend/wms/movements/page.js.map +7 -0
  152. package/dist/modules/wms/backend/wms/movements/page.meta.js +18 -0
  153. package/dist/modules/wms/backend/wms/movements/page.meta.js.map +7 -0
  154. package/dist/modules/wms/backend/wms/reservations/page.js +23 -0
  155. package/dist/modules/wms/backend/wms/reservations/page.js.map +7 -0
  156. package/dist/modules/wms/backend/wms/reservations/page.meta.js +18 -0
  157. package/dist/modules/wms/backend/wms/reservations/page.meta.js.map +7 -0
  158. package/dist/modules/wms/backend/wms/sku/[id]/page.js +9 -0
  159. package/dist/modules/wms/backend/wms/sku/[id]/page.js.map +7 -0
  160. package/dist/modules/wms/backend/wms/sku/[id]/page.meta.js +18 -0
  161. package/dist/modules/wms/backend/wms/sku/[id]/page.meta.js.map +7 -0
  162. package/dist/modules/wms/backend/wms/warehouses/page.js +10 -0
  163. package/dist/modules/wms/backend/wms/warehouses/page.js.map +7 -0
  164. package/dist/modules/wms/backend/wms/warehouses/page.meta.js +18 -0
  165. package/dist/modules/wms/backend/wms/warehouses/page.meta.js.map +7 -0
  166. package/dist/modules/wms/backend/wms/zones/page.js +10 -0
  167. package/dist/modules/wms/backend/wms/zones/page.js.map +7 -0
  168. package/dist/modules/wms/backend/wms/zones/page.meta.js +18 -0
  169. package/dist/modules/wms/backend/wms/zones/page.meta.js.map +7 -0
  170. package/dist/modules/wms/ce.js +80 -0
  171. package/dist/modules/wms/ce.js.map +7 -0
  172. package/dist/modules/wms/cli.js +83 -0
  173. package/dist/modules/wms/cli.js.map +7 -0
  174. package/dist/modules/wms/commands/configuration.js +1519 -0
  175. package/dist/modules/wms/commands/configuration.js.map +7 -0
  176. package/dist/modules/wms/commands/index.js +4 -0
  177. package/dist/modules/wms/commands/index.js.map +7 -0
  178. package/dist/modules/wms/commands/inventory-actions.js +1539 -0
  179. package/dist/modules/wms/commands/inventory-actions.js.map +7 -0
  180. package/dist/modules/wms/commands/sales-order-assignment.js +277 -0
  181. package/dist/modules/wms/commands/sales-order-assignment.js.map +7 -0
  182. package/dist/modules/wms/commands/shared.js +82 -0
  183. package/dist/modules/wms/commands/shared.js.map +7 -0
  184. package/dist/modules/wms/components/backend/AdjustInventoryDialog.js +755 -0
  185. package/dist/modules/wms/components/backend/AdjustInventoryDialog.js.map +7 -0
  186. package/dist/modules/wms/components/backend/ChangeLotStatusDialog.js +269 -0
  187. package/dist/modules/wms/components/backend/ChangeLotStatusDialog.js.map +7 -0
  188. package/dist/modules/wms/components/backend/CycleCountWizardDialog.js +1783 -0
  189. package/dist/modules/wms/components/backend/CycleCountWizardDialog.js.map +7 -0
  190. package/dist/modules/wms/components/backend/ImportInventoryDialog.js +766 -0
  191. package/dist/modules/wms/components/backend/ImportInventoryDialog.js.map +7 -0
  192. package/dist/modules/wms/components/backend/InventoryOperationsSection.js +119 -0
  193. package/dist/modules/wms/components/backend/InventoryOperationsSection.js.map +7 -0
  194. package/dist/modules/wms/components/backend/LocationEditDialog.js +140 -0
  195. package/dist/modules/wms/components/backend/LocationEditDialog.js.map +7 -0
  196. package/dist/modules/wms/components/backend/MoveInventoryDialog.js +791 -0
  197. package/dist/modules/wms/components/backend/MoveInventoryDialog.js.map +7 -0
  198. package/dist/modules/wms/components/backend/ReceiveInventoryDialog.js +656 -0
  199. package/dist/modules/wms/components/backend/ReceiveInventoryDialog.js.map +7 -0
  200. package/dist/modules/wms/components/backend/ReleaseReservationDialog.js +344 -0
  201. package/dist/modules/wms/components/backend/ReleaseReservationDialog.js.map +7 -0
  202. package/dist/modules/wms/components/backend/ReserveInventoryDialog.js +475 -0
  203. package/dist/modules/wms/components/backend/ReserveInventoryDialog.js.map +7 -0
  204. package/dist/modules/wms/components/backend/WmsConfigurationPage.js +1112 -0
  205. package/dist/modules/wms/components/backend/WmsConfigurationPage.js.map +7 -0
  206. package/dist/modules/wms/components/backend/WmsInventoryConsolePage.js +908 -0
  207. package/dist/modules/wms/components/backend/WmsInventoryConsolePage.js.map +7 -0
  208. package/dist/modules/wms/components/backend/WmsLocationDetailPage.js +1015 -0
  209. package/dist/modules/wms/components/backend/WmsLocationDetailPage.js.map +7 -0
  210. package/dist/modules/wms/components/backend/WmsLotDetailPage.js +1049 -0
  211. package/dist/modules/wms/components/backend/WmsLotDetailPage.js.map +7 -0
  212. package/dist/modules/wms/components/backend/WmsLotsListPage.js +297 -0
  213. package/dist/modules/wms/components/backend/WmsLotsListPage.js.map +7 -0
  214. package/dist/modules/wms/components/backend/WmsOperationalDashboardPage.js +757 -0
  215. package/dist/modules/wms/components/backend/WmsOperationalDashboardPage.js.map +7 -0
  216. package/dist/modules/wms/components/backend/WmsSkuDetailPage.js +975 -0
  217. package/dist/modules/wms/components/backend/WmsSkuDetailPage.js.map +7 -0
  218. package/dist/modules/wms/components/backend/inventoryMutationLoaders.js +710 -0
  219. package/dist/modules/wms/components/backend/inventoryMutationLoaders.js.map +7 -0
  220. package/dist/modules/wms/components/backend/useWmsInventoryMutationAccess.js +79 -0
  221. package/dist/modules/wms/components/backend/useWmsInventoryMutationAccess.js.map +7 -0
  222. package/dist/modules/wms/components/backend/useWmsInventoryScopeFromSearchParams.js +24 -0
  223. package/dist/modules/wms/components/backend/useWmsInventoryScopeFromSearchParams.js.map +7 -0
  224. package/dist/modules/wms/components/backend/wmsLookupLoaders.js +55 -0
  225. package/dist/modules/wms/components/backend/wmsLookupLoaders.js.map +7 -0
  226. package/dist/modules/wms/data/enrichers.js +501 -0
  227. package/dist/modules/wms/data/enrichers.js.map +7 -0
  228. package/dist/modules/wms/data/entities.js +467 -0
  229. package/dist/modules/wms/data/entities.js.map +7 -0
  230. package/dist/modules/wms/data/validators.js +399 -0
  231. package/dist/modules/wms/data/validators.js.map +7 -0
  232. package/dist/modules/wms/di.js +27 -0
  233. package/dist/modules/wms/di.js.map +7 -0
  234. package/dist/modules/wms/events.js +42 -0
  235. package/dist/modules/wms/events.js.map +7 -0
  236. package/dist/modules/wms/index.js +17 -0
  237. package/dist/modules/wms/index.js.map +7 -0
  238. package/dist/modules/wms/lib/catalogInventoryProfileIntent.js +54 -0
  239. package/dist/modules/wms/lib/catalogInventoryProfileIntent.js.map +7 -0
  240. package/dist/modules/wms/lib/catalogInventoryProfileSync.js +144 -0
  241. package/dist/modules/wms/lib/catalogInventoryProfileSync.js.map +7 -0
  242. package/dist/modules/wms/lib/expiry.js +29 -0
  243. package/dist/modules/wms/lib/expiry.js.map +7 -0
  244. package/dist/modules/wms/lib/flashMutationError.js +51 -0
  245. package/dist/modules/wms/lib/flashMutationError.js.map +7 -0
  246. package/dist/modules/wms/lib/inventoryDisplayUi.js +188 -0
  247. package/dist/modules/wms/lib/inventoryDisplayUi.js.map +7 -0
  248. package/dist/modules/wms/lib/inventoryIdempotency.js +37 -0
  249. package/dist/modules/wms/lib/inventoryIdempotency.js.map +7 -0
  250. package/dist/modules/wms/lib/inventoryImportCsv.js +173 -0
  251. package/dist/modules/wms/lib/inventoryImportCsv.js.map +7 -0
  252. package/dist/modules/wms/lib/inventoryImportService.js +468 -0
  253. package/dist/modules/wms/lib/inventoryImportService.js.map +7 -0
  254. package/dist/modules/wms/lib/inventoryMutationUi.js +25 -0
  255. package/dist/modules/wms/lib/inventoryMutationUi.js.map +7 -0
  256. package/dist/modules/wms/lib/inventoryPolicy.js +61 -0
  257. package/dist/modules/wms/lib/inventoryPolicy.js.map +7 -0
  258. package/dist/modules/wms/lib/inventoryReconciliation.js +193 -0
  259. package/dist/modules/wms/lib/inventoryReconciliation.js.map +7 -0
  260. package/dist/modules/wms/lib/inventoryTrackingValidation.js +13 -0
  261. package/dist/modules/wms/lib/inventoryTrackingValidation.js.map +7 -0
  262. package/dist/modules/wms/lib/loadOperationalDashboard.js +580 -0
  263. package/dist/modules/wms/lib/loadOperationalDashboard.js.map +7 -0
  264. package/dist/modules/wms/lib/lowStockBalanceFilter.js +49 -0
  265. package/dist/modules/wms/lib/lowStockBalanceFilter.js.map +7 -0
  266. package/dist/modules/wms/lib/primaryWarehousePolicy.js +32 -0
  267. package/dist/modules/wms/lib/primaryWarehousePolicy.js.map +7 -0
  268. package/dist/modules/wms/lib/roleFeatures.js +30 -0
  269. package/dist/modules/wms/lib/roleFeatures.js.map +7 -0
  270. package/dist/modules/wms/lib/salesOrderInventoryAutomation.js +291 -0
  271. package/dist/modules/wms/lib/salesOrderInventoryAutomation.js.map +7 -0
  272. package/dist/modules/wms/lib/salesOrderWarehouseAssignment.js +54 -0
  273. package/dist/modules/wms/lib/salesOrderWarehouseAssignment.js.map +7 -0
  274. package/dist/modules/wms/lib/wmsIntegrationToggles.js +71 -0
  275. package/dist/modules/wms/lib/wmsIntegrationToggles.js.map +7 -0
  276. package/dist/modules/wms/migrations/Migration20260428110546.js +78 -0
  277. package/dist/modules/wms/migrations/Migration20260428110546.js.map +7 -0
  278. package/dist/modules/wms/migrations/Migration20260527120000.js +15 -0
  279. package/dist/modules/wms/migrations/Migration20260527120000.js.map +7 -0
  280. package/dist/modules/wms/migrations/Migration20260527140000.js +37 -0
  281. package/dist/modules/wms/migrations/Migration20260527140000.js.map +7 -0
  282. package/dist/modules/wms/migrations/Migration20260613120000.js +23 -0
  283. package/dist/modules/wms/migrations/Migration20260613120000.js.map +7 -0
  284. package/dist/modules/wms/migrations/Migration20260616090000.js +13 -0
  285. package/dist/modules/wms/migrations/Migration20260616090000.js.map +7 -0
  286. package/dist/modules/wms/migrations/Migration20260707180000.js +15 -0
  287. package/dist/modules/wms/migrations/Migration20260707180000.js.map +7 -0
  288. package/dist/modules/wms/notifications.client.js +18 -0
  289. package/dist/modules/wms/notifications.client.js.map +7 -0
  290. package/dist/modules/wms/notifications.js +53 -0
  291. package/dist/modules/wms/notifications.js.map +7 -0
  292. package/dist/modules/wms/search.js +196 -0
  293. package/dist/modules/wms/search.js.map +7 -0
  294. package/dist/modules/wms/setup.js +30 -0
  295. package/dist/modules/wms/setup.js.map +7 -0
  296. package/dist/modules/wms/subscribers/low-stock-notification.js +40 -0
  297. package/dist/modules/wms/subscribers/low-stock-notification.js.map +7 -0
  298. package/dist/modules/wms/subscribers/reservation-shortfall-notification.js +69 -0
  299. package/dist/modules/wms/subscribers/reservation-shortfall-notification.js.map +7 -0
  300. package/dist/modules/wms/subscribers/sales-order-cancelled-release.js +14 -0
  301. package/dist/modules/wms/subscribers/sales-order-cancelled-release.js.map +7 -0
  302. package/dist/modules/wms/subscribers/sales-order-confirmed-reserve.js +14 -0
  303. package/dist/modules/wms/subscribers/sales-order-confirmed-reserve.js.map +7 -0
  304. package/dist/modules/wms/translations.js +10 -0
  305. package/dist/modules/wms/translations.js.map +7 -0
  306. package/dist/modules/wms/vector.js +9 -0
  307. package/dist/modules/wms/vector.js.map +7 -0
  308. package/dist/modules/wms/widgets/injection/catalog-inventory-profile/widget.js +200 -0
  309. package/dist/modules/wms/widgets/injection/catalog-inventory-profile/widget.js.map +7 -0
  310. package/dist/modules/wms/widgets/injection/order-items-stock-column/widget.js +104 -0
  311. package/dist/modules/wms/widgets/injection/order-items-stock-column/widget.js.map +7 -0
  312. package/dist/modules/wms/widgets/injection/sales-order-stock-context/widget.client.js +389 -0
  313. package/dist/modules/wms/widgets/injection/sales-order-stock-context/widget.client.js.map +7 -0
  314. package/dist/modules/wms/widgets/injection/sales-order-stock-context/widget.js +16 -0
  315. package/dist/modules/wms/widgets/injection/sales-order-stock-context/widget.js.map +7 -0
  316. package/dist/modules/wms/widgets/injection-table.js +36 -0
  317. package/dist/modules/wms/widgets/injection-table.js.map +7 -0
  318. package/dist/modules/wms/widgets/notifications/WmsLowStockRenderer.js +150 -0
  319. package/dist/modules/wms/widgets/notifications/WmsLowStockRenderer.js.map +7 -0
  320. package/dist/modules/wms/widgets/notifications/WmsReservationShortfallRenderer.js +186 -0
  321. package/dist/modules/wms/widgets/notifications/WmsReservationShortfallRenderer.js.map +7 -0
  322. package/dist/modules/wms/widgets/notifications/index.js +7 -0
  323. package/dist/modules/wms/widgets/notifications/index.js.map +7 -0
  324. package/generated/entities/inventory_balance/index.ts +9 -0
  325. package/generated/entities/inventory_lot/index.ts +8 -0
  326. package/generated/entities/inventory_movement/index.ts +16 -0
  327. package/generated/entities/inventory_reservation/index.ts +10 -0
  328. package/generated/entities/product_inventory_profile/index.ts +9 -0
  329. package/generated/entities/sales_order_warehouse_assignment/index.ts +4 -0
  330. package/generated/entities/warehouse/index.ts +11 -0
  331. package/generated/entities/warehouse_location/index.ts +9 -0
  332. package/generated/entities/warehouse_zone/index.ts +4 -0
  333. package/generated/entities.ids.generated.ts +12 -0
  334. package/generated/entity-fields-registry.ts +98 -0
  335. package/package.json +7 -7
  336. package/src/modules/auth/backend/roles/page.tsx +13 -4
  337. package/src/modules/auth/i18n/de.json +2 -0
  338. package/src/modules/auth/i18n/en.json +2 -0
  339. package/src/modules/auth/i18n/es.json +2 -0
  340. package/src/modules/auth/i18n/pl.json +2 -0
  341. package/src/modules/auth/lib/backendChrome.tsx +1 -0
  342. package/src/modules/auth/services/rbacService.ts +25 -21
  343. package/src/modules/catalog/api/products/route.ts +3 -0
  344. package/src/modules/catalog/api/variants/route.ts +3 -0
  345. package/src/modules/customer_accounts/migrations/Migration20260724120000_customer_accounts.ts +96 -0
  346. package/src/modules/directory/api/organizations/route.ts +1 -1
  347. package/src/modules/entities/api/definitions.ts +2 -0
  348. package/src/modules/entities/lib/helpers.ts +1 -0
  349. package/src/modules/query_index/lib/engine.ts +18 -6
  350. package/src/modules/sales/api/documents/factory.ts +1 -0
  351. package/src/modules/sales/api/quotes/accept/route.ts +9 -2
  352. package/src/modules/sales/api/quotes/public/[token]/route.ts +2 -1
  353. package/src/modules/sales/commands/documents.ts +71 -0
  354. package/src/modules/sales/components/documents/ItemsSection.tsx +58 -0
  355. package/src/modules/sales/events.ts +2 -0
  356. package/src/modules/sales/lib/publicQuoteTenantScope.ts +49 -0
  357. package/src/modules/sales/widgets/injection/order-items-context.ts +15 -0
  358. package/src/modules/wms/acl.ts +14 -0
  359. package/src/modules/wms/analytics.ts +9 -0
  360. package/src/modules/wms/api/dashboard/operational/route.ts +83 -0
  361. package/src/modules/wms/api/interceptors.ts +108 -0
  362. package/src/modules/wms/api/inventory/adjust/route.ts +50 -0
  363. package/src/modules/wms/api/inventory/allocate/route.ts +50 -0
  364. package/src/modules/wms/api/inventory/balances/route.ts +178 -0
  365. package/src/modules/wms/api/inventory/cycle-count/route.ts +51 -0
  366. package/src/modules/wms/api/inventory/helpers.ts +128 -0
  367. package/src/modules/wms/api/inventory/import/apply/route.ts +47 -0
  368. package/src/modules/wms/api/inventory/import/helpers.ts +223 -0
  369. package/src/modules/wms/api/inventory/import/template/route.ts +31 -0
  370. package/src/modules/wms/api/inventory/import/validate/route.ts +56 -0
  371. package/src/modules/wms/api/inventory/move/route.ts +50 -0
  372. package/src/modules/wms/api/inventory/movements/route.ts +164 -0
  373. package/src/modules/wms/api/inventory/receive/route.ts +49 -0
  374. package/src/modules/wms/api/inventory/release/route.ts +43 -0
  375. package/src/modules/wms/api/inventory/reservations/route.ts +128 -0
  376. package/src/modules/wms/api/inventory/reserve/route.ts +59 -0
  377. package/src/modules/wms/api/inventory-profiles/route.ts +177 -0
  378. package/src/modules/wms/api/listEnrichers.ts +312 -0
  379. package/src/modules/wms/api/listSearch.ts +121 -0
  380. package/src/modules/wms/api/locations/route.ts +171 -0
  381. package/src/modules/wms/api/lots/route.ts +205 -0
  382. package/src/modules/wms/api/openapi.ts +28 -0
  383. package/src/modules/wms/api/sales-orders/[salesOrderId]/re-run-reservation/route.ts +58 -0
  384. package/src/modules/wms/api/sales-orders/[salesOrderId]/warehouse-assignment/route.ts +306 -0
  385. package/src/modules/wms/api/sales-orders/assign-warehouse/route.ts +57 -0
  386. package/src/modules/wms/api/sales-orders/unassign-warehouse/route.ts +43 -0
  387. package/src/modules/wms/api/warehouses/route.ts +181 -0
  388. package/src/modules/wms/api/zones/route.ts +158 -0
  389. package/src/modules/wms/backend/config/wms/page.meta.ts +14 -0
  390. package/src/modules/wms/backend/config/wms/page.tsx +5 -0
  391. package/src/modules/wms/backend/page.meta.ts +12 -0
  392. package/src/modules/wms/backend/page.tsx +5 -0
  393. package/src/modules/wms/backend/wms/inventory/page.meta.ts +14 -0
  394. package/src/modules/wms/backend/wms/inventory/page.tsx +5 -0
  395. package/src/modules/wms/backend/wms/location/[id]/page.meta.ts +14 -0
  396. package/src/modules/wms/backend/wms/location/[id]/page.tsx +5 -0
  397. package/src/modules/wms/backend/wms/locations/page.meta.ts +14 -0
  398. package/src/modules/wms/backend/wms/locations/page.tsx +12 -0
  399. package/src/modules/wms/backend/wms/lot/[id]/page.meta.ts +14 -0
  400. package/src/modules/wms/backend/wms/lot/[id]/page.tsx +5 -0
  401. package/src/modules/wms/backend/wms/lots/page.meta.ts +14 -0
  402. package/src/modules/wms/backend/wms/lots/page.tsx +5 -0
  403. package/src/modules/wms/backend/wms/movements/page.meta.ts +14 -0
  404. package/src/modules/wms/backend/wms/movements/page.tsx +23 -0
  405. package/src/modules/wms/backend/wms/reservations/page.meta.ts +14 -0
  406. package/src/modules/wms/backend/wms/reservations/page.tsx +24 -0
  407. package/src/modules/wms/backend/wms/sku/[id]/page.meta.ts +14 -0
  408. package/src/modules/wms/backend/wms/sku/[id]/page.tsx +5 -0
  409. package/src/modules/wms/backend/wms/warehouses/page.meta.ts +14 -0
  410. package/src/modules/wms/backend/wms/warehouses/page.tsx +12 -0
  411. package/src/modules/wms/backend/wms/zones/page.meta.ts +14 -0
  412. package/src/modules/wms/backend/wms/zones/page.tsx +12 -0
  413. package/src/modules/wms/ce.ts +76 -0
  414. package/src/modules/wms/cli.ts +96 -0
  415. package/src/modules/wms/commands/configuration.ts +1844 -0
  416. package/src/modules/wms/commands/index.ts +3 -0
  417. package/src/modules/wms/commands/inventory-actions.ts +1866 -0
  418. package/src/modules/wms/commands/sales-order-assignment.ts +346 -0
  419. package/src/modules/wms/commands/shared.ts +90 -0
  420. package/src/modules/wms/components/backend/AdjustInventoryDialog.tsx +820 -0
  421. package/src/modules/wms/components/backend/ChangeLotStatusDialog.tsx +312 -0
  422. package/src/modules/wms/components/backend/CycleCountWizardDialog.tsx +2007 -0
  423. package/src/modules/wms/components/backend/ImportInventoryDialog.tsx +1018 -0
  424. package/src/modules/wms/components/backend/InventoryOperationsSection.tsx +159 -0
  425. package/src/modules/wms/components/backend/LocationEditDialog.tsx +185 -0
  426. package/src/modules/wms/components/backend/MoveInventoryDialog.tsx +907 -0
  427. package/src/modules/wms/components/backend/ReceiveInventoryDialog.tsx +695 -0
  428. package/src/modules/wms/components/backend/ReleaseReservationDialog.tsx +424 -0
  429. package/src/modules/wms/components/backend/ReserveInventoryDialog.tsx +511 -0
  430. package/src/modules/wms/components/backend/WmsConfigurationPage.tsx +1340 -0
  431. package/src/modules/wms/components/backend/WmsInventoryConsolePage.tsx +1132 -0
  432. package/src/modules/wms/components/backend/WmsLocationDetailPage.tsx +1310 -0
  433. package/src/modules/wms/components/backend/WmsLotDetailPage.tsx +1355 -0
  434. package/src/modules/wms/components/backend/WmsLotsListPage.tsx +359 -0
  435. package/src/modules/wms/components/backend/WmsOperationalDashboardPage.tsx +958 -0
  436. package/src/modules/wms/components/backend/WmsSkuDetailPage.tsx +1245 -0
  437. package/src/modules/wms/components/backend/inventoryMutationLoaders.ts +960 -0
  438. package/src/modules/wms/components/backend/useWmsInventoryMutationAccess.ts +89 -0
  439. package/src/modules/wms/components/backend/useWmsInventoryScopeFromSearchParams.ts +32 -0
  440. package/src/modules/wms/components/backend/wmsLookupLoaders.ts +78 -0
  441. package/src/modules/wms/data/enrichers.ts +718 -0
  442. package/src/modules/wms/data/entities.ts +436 -0
  443. package/src/modules/wms/data/validators.ts +463 -0
  444. package/src/modules/wms/di.ts +25 -0
  445. package/src/modules/wms/events.ts +41 -0
  446. package/src/modules/wms/i18n/de.json +1051 -0
  447. package/src/modules/wms/i18n/en.json +1051 -0
  448. package/src/modules/wms/i18n/es.json +1051 -0
  449. package/src/modules/wms/i18n/pl.json +1051 -0
  450. package/src/modules/wms/index.ts +15 -0
  451. package/src/modules/wms/lib/catalogInventoryProfileIntent.ts +65 -0
  452. package/src/modules/wms/lib/catalogInventoryProfileSync.ts +199 -0
  453. package/src/modules/wms/lib/expiry.ts +30 -0
  454. package/src/modules/wms/lib/flashMutationError.ts +74 -0
  455. package/src/modules/wms/lib/inventoryDisplayUi.ts +249 -0
  456. package/src/modules/wms/lib/inventoryIdempotency.ts +54 -0
  457. package/src/modules/wms/lib/inventoryImportCsv.ts +212 -0
  458. package/src/modules/wms/lib/inventoryImportService.ts +629 -0
  459. package/src/modules/wms/lib/inventoryMutationUi.ts +21 -0
  460. package/src/modules/wms/lib/inventoryPolicy.ts +111 -0
  461. package/src/modules/wms/lib/inventoryReconciliation.ts +263 -0
  462. package/src/modules/wms/lib/inventoryTrackingValidation.ts +23 -0
  463. package/src/modules/wms/lib/loadOperationalDashboard.ts +776 -0
  464. package/src/modules/wms/lib/lowStockBalanceFilter.ts +74 -0
  465. package/src/modules/wms/lib/primaryWarehousePolicy.ts +46 -0
  466. package/src/modules/wms/lib/roleFeatures.ts +32 -0
  467. package/src/modules/wms/lib/salesOrderInventoryAutomation.ts +390 -0
  468. package/src/modules/wms/lib/salesOrderWarehouseAssignment.ts +78 -0
  469. package/src/modules/wms/lib/wmsIntegrationToggles.ts +81 -0
  470. package/src/modules/wms/migrations/.snapshot-open-mercato.json +2880 -0
  471. package/src/modules/wms/migrations/Migration20260428110546.ts +91 -0
  472. package/src/modules/wms/migrations/Migration20260527120000.ts +15 -0
  473. package/src/modules/wms/migrations/Migration20260527140000.ts +35 -0
  474. package/src/modules/wms/migrations/Migration20260613120000.ts +21 -0
  475. package/src/modules/wms/migrations/Migration20260616090000.ts +11 -0
  476. package/src/modules/wms/migrations/Migration20260707180000.ts +13 -0
  477. package/src/modules/wms/notifications.client.ts +18 -0
  478. package/src/modules/wms/notifications.ts +51 -0
  479. package/src/modules/wms/search.ts +230 -0
  480. package/src/modules/wms/setup.ts +29 -0
  481. package/src/modules/wms/subscribers/low-stock-notification.ts +54 -0
  482. package/src/modules/wms/subscribers/reservation-shortfall-notification.ts +98 -0
  483. package/src/modules/wms/subscribers/sales-order-cancelled-release.ts +21 -0
  484. package/src/modules/wms/subscribers/sales-order-confirmed-reserve.ts +21 -0
  485. package/src/modules/wms/translations.ts +6 -0
  486. package/src/modules/wms/vector.ts +7 -0
  487. package/src/modules/wms/widgets/injection/catalog-inventory-profile/widget.ts +238 -0
  488. package/src/modules/wms/widgets/injection/order-items-stock-column/widget.tsx +153 -0
  489. package/src/modules/wms/widgets/injection/sales-order-stock-context/widget.client.tsx +476 -0
  490. package/src/modules/wms/widgets/injection/sales-order-stock-context/widget.ts +15 -0
  491. package/src/modules/wms/widgets/injection-table.ts +34 -0
  492. package/src/modules/wms/widgets/notifications/WmsLowStockRenderer.tsx +153 -0
  493. package/src/modules/wms/widgets/notifications/WmsReservationShortfallRenderer.tsx +194 -0
  494. package/src/modules/wms/widgets/notifications/index.ts +2 -0
@@ -0,0 +1,1519 @@
1
+ import { registerCommand } from "@open-mercato/shared/lib/commands";
2
+ import { withAtomicFlush } from "@open-mercato/shared/lib/commands/flush";
3
+ import { CrudHttpError, isUniqueViolation } from "@open-mercato/shared/lib/crud/errors";
4
+ import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
5
+ import { findOneWithDecryption, findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
6
+ import { extractUndoPayload } from "@open-mercato/shared/lib/commands/undo";
7
+ import {
8
+ InventoryLot,
9
+ ProductInventoryProfile,
10
+ Warehouse,
11
+ WarehouseLocation,
12
+ WarehouseZone
13
+ } from "../data/entities.js";
14
+ import {
15
+ inventoryLotCreateSchema,
16
+ inventoryLotUpdateSchema,
17
+ productInventoryProfileCreateSchema,
18
+ productInventoryProfileUpdateSchema,
19
+ warehouseCreateSchema,
20
+ warehouseLocationCreateSchema,
21
+ warehouseLocationUpdateSchema,
22
+ warehouseUpdateSchema,
23
+ warehouseZoneCreateSchema,
24
+ warehouseZoneUpdateSchema
25
+ } from "../data/validators.js";
26
+ import {
27
+ ensureOrganizationScope,
28
+ ensureTenantScope,
29
+ normalizeOptionalString,
30
+ requireId,
31
+ toNumericString
32
+ } from "./shared.js";
33
+ import { emitWmsEvent } from "../events.js";
34
+ function resolveScope(ctx, fallback) {
35
+ return {
36
+ tenantId: fallback?.tenantId ?? ctx.auth?.tenantId ?? null,
37
+ organizationId: fallback?.organizationId ?? ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null
38
+ };
39
+ }
40
+ function resolveEm(ctx) {
41
+ return ctx.container.resolve("em").fork();
42
+ }
43
+ function toJsonValue(value) {
44
+ if (value === void 0) return void 0;
45
+ return value ?? null;
46
+ }
47
+ async function buildCrudLog(ctx, input, resultId, actionKey, fallbackLabel, resourceKind) {
48
+ const { translate } = await resolveTranslations();
49
+ return {
50
+ actionLabel: translate(actionKey, fallbackLabel),
51
+ resourceKind,
52
+ resourceId: resultId ?? input?.id ?? null,
53
+ tenantId: input?.tenantId ?? ctx.auth?.tenantId ?? null,
54
+ organizationId: input?.organizationId ?? ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null
55
+ };
56
+ }
57
+ async function loadWarehouse(em, ctx, id) {
58
+ const warehouse = await findOneWithDecryption(em, Warehouse, { id, deletedAt: null }, void 0, resolveScope(ctx));
59
+ if (!warehouse) throw new CrudHttpError(404, { error: "Warehouse not found." });
60
+ ensureTenantScope(ctx, warehouse.tenantId);
61
+ ensureOrganizationScope(ctx, warehouse.organizationId);
62
+ return warehouse;
63
+ }
64
+ async function loadZone(em, ctx, id) {
65
+ const zone = await findOneWithDecryption(em, WarehouseZone, { id, deletedAt: null }, void 0, resolveScope(ctx));
66
+ if (!zone) throw new CrudHttpError(404, { error: "Warehouse zone not found." });
67
+ ensureTenantScope(ctx, zone.tenantId);
68
+ ensureOrganizationScope(ctx, zone.organizationId);
69
+ return zone;
70
+ }
71
+ async function loadLocation(em, ctx, id) {
72
+ const location = await findOneWithDecryption(em, WarehouseLocation, { id, deletedAt: null }, void 0, resolveScope(ctx));
73
+ if (!location) throw new CrudHttpError(404, { error: "Warehouse location not found." });
74
+ ensureTenantScope(ctx, location.tenantId);
75
+ ensureOrganizationScope(ctx, location.organizationId);
76
+ return location;
77
+ }
78
+ async function loadProfile(em, ctx, id) {
79
+ const profile = await findOneWithDecryption(em, ProductInventoryProfile, { id, deletedAt: null }, void 0, resolveScope(ctx));
80
+ if (!profile) throw new CrudHttpError(404, { error: "Inventory profile not found." });
81
+ ensureTenantScope(ctx, profile.tenantId);
82
+ ensureOrganizationScope(ctx, profile.organizationId);
83
+ return profile;
84
+ }
85
+ async function loadLot(em, ctx, id) {
86
+ const lot = await findOneWithDecryption(em, InventoryLot, { id, deletedAt: null }, void 0, resolveScope(ctx));
87
+ if (!lot) throw new CrudHttpError(404, { error: "Inventory lot not found." });
88
+ ensureTenantScope(ctx, lot.tenantId);
89
+ ensureOrganizationScope(ctx, lot.organizationId);
90
+ return lot;
91
+ }
92
+ function isoOrNull(value) {
93
+ if (!value) return null;
94
+ if (value instanceof Date) return value.toISOString();
95
+ if (typeof value === "string") return value;
96
+ return null;
97
+ }
98
+ function dateOrNull(value) {
99
+ if (!value) return null;
100
+ if (value instanceof Date) return value;
101
+ if (typeof value === "string") return new Date(value);
102
+ return null;
103
+ }
104
+ function snapshotWarehouse(record) {
105
+ return {
106
+ id: record.id,
107
+ organizationId: record.organizationId,
108
+ tenantId: record.tenantId,
109
+ name: record.name,
110
+ code: record.code,
111
+ isActive: !!record.isActive,
112
+ isPrimary: !!record.isPrimary,
113
+ addressLine1: record.addressLine1 ?? null,
114
+ city: record.city ?? null,
115
+ postalCode: record.postalCode ?? null,
116
+ country: record.country ?? null,
117
+ timezone: record.timezone ?? null,
118
+ metadata: record.metadata ?? null,
119
+ createdAt: record.createdAt.toISOString(),
120
+ updatedAt: record.updatedAt.toISOString()
121
+ };
122
+ }
123
+ async function loadWarehouseSnapshot(em, ctx, id) {
124
+ const record = await findOneWithDecryption(em, Warehouse, { id }, void 0, resolveScope(ctx));
125
+ return record ? snapshotWarehouse(record) : null;
126
+ }
127
+ function snapshotWarehouseZone(record) {
128
+ const warehouseId = typeof record.warehouse === "string" ? record.warehouse : record.warehouse.id;
129
+ return {
130
+ id: record.id,
131
+ organizationId: record.organizationId,
132
+ tenantId: record.tenantId,
133
+ warehouseId,
134
+ code: record.code,
135
+ name: record.name,
136
+ priority: record.priority ?? 0,
137
+ metadata: record.metadata ?? null,
138
+ createdAt: record.createdAt.toISOString(),
139
+ updatedAt: record.updatedAt.toISOString()
140
+ };
141
+ }
142
+ async function loadWarehouseZoneSnapshot(em, ctx, id) {
143
+ const record = await findOneWithDecryption(em, WarehouseZone, { id }, void 0, resolveScope(ctx));
144
+ return record ? snapshotWarehouseZone(record) : null;
145
+ }
146
+ function snapshotWarehouseLocation(record) {
147
+ const warehouseId = typeof record.warehouse === "string" ? record.warehouse : record.warehouse.id;
148
+ let parentId = null;
149
+ if (record.parent) {
150
+ parentId = typeof record.parent === "string" ? record.parent : record.parent.id;
151
+ }
152
+ return {
153
+ id: record.id,
154
+ organizationId: record.organizationId,
155
+ tenantId: record.tenantId,
156
+ warehouseId,
157
+ parentId,
158
+ code: record.code,
159
+ type: record.type,
160
+ isActive: !!record.isActive,
161
+ capacityUnits: record.capacityUnits ?? null,
162
+ capacityWeight: record.capacityWeight ?? null,
163
+ constraints: record.constraints ?? null,
164
+ metadata: record.metadata ?? null,
165
+ createdAt: record.createdAt.toISOString(),
166
+ updatedAt: record.updatedAt.toISOString()
167
+ };
168
+ }
169
+ async function loadWarehouseLocationSnapshot(em, ctx, id) {
170
+ const record = await findOneWithDecryption(em, WarehouseLocation, { id }, void 0, resolveScope(ctx));
171
+ return record ? snapshotWarehouseLocation(record) : null;
172
+ }
173
+ function snapshotInventoryProfile(record) {
174
+ return {
175
+ id: record.id,
176
+ organizationId: record.organizationId,
177
+ tenantId: record.tenantId,
178
+ catalogProductId: record.catalogProductId,
179
+ catalogVariantId: record.catalogVariantId ?? null,
180
+ defaultUom: record.defaultUom,
181
+ trackLot: !!record.trackLot,
182
+ trackSerial: !!record.trackSerial,
183
+ trackExpiration: !!record.trackExpiration,
184
+ defaultStrategy: record.defaultStrategy,
185
+ reorderPoint: record.reorderPoint,
186
+ safetyStock: record.safetyStock,
187
+ metadata: record.metadata ?? null,
188
+ createdAt: record.createdAt.toISOString(),
189
+ updatedAt: record.updatedAt.toISOString()
190
+ };
191
+ }
192
+ async function loadInventoryProfileSnapshot(em, ctx, id) {
193
+ const record = await findOneWithDecryption(em, ProductInventoryProfile, { id }, void 0, resolveScope(ctx));
194
+ return record ? snapshotInventoryProfile(record) : null;
195
+ }
196
+ function snapshotInventoryLot(record) {
197
+ return {
198
+ id: record.id,
199
+ organizationId: record.organizationId,
200
+ tenantId: record.tenantId,
201
+ catalogVariantId: record.catalogVariantId,
202
+ sku: record.sku,
203
+ lotNumber: record.lotNumber,
204
+ batchNumber: record.batchNumber ?? null,
205
+ manufacturedAt: isoOrNull(record.manufacturedAt ?? null),
206
+ bestBeforeAt: isoOrNull(record.bestBeforeAt ?? null),
207
+ expiresAt: isoOrNull(record.expiresAt ?? null),
208
+ status: record.status,
209
+ metadata: record.metadata ?? null,
210
+ createdAt: record.createdAt.toISOString(),
211
+ updatedAt: record.updatedAt.toISOString()
212
+ };
213
+ }
214
+ async function loadInventoryLotSnapshot(em, ctx, id) {
215
+ const record = await findOneWithDecryption(em, InventoryLot, { id }, void 0, resolveScope(ctx));
216
+ return record ? snapshotInventoryLot(record) : null;
217
+ }
218
+ async function ensureWarehouseCodeUnique(em, tenantId, organizationId, code, excludeId) {
219
+ const existing = await findOneWithDecryption(
220
+ em,
221
+ Warehouse,
222
+ { tenantId, organizationId, code, deletedAt: null },
223
+ void 0,
224
+ { tenantId, organizationId }
225
+ );
226
+ if (existing && existing.id !== excludeId) {
227
+ throw new CrudHttpError(409, { error: "Warehouse code already exists in this organization." });
228
+ }
229
+ }
230
+ async function loadDemotedPrimarySnapshots(em, organizationId, tenantId, warehouseId) {
231
+ const siblings = await findWithDecryption(
232
+ em,
233
+ Warehouse,
234
+ {
235
+ organizationId,
236
+ id: { $ne: warehouseId },
237
+ isPrimary: true,
238
+ deletedAt: null
239
+ },
240
+ void 0,
241
+ { tenantId, organizationId }
242
+ );
243
+ return siblings.map((record) => ({ id: record.id, isPrimary: true }));
244
+ }
245
+ async function enforcePrimaryWarehouse(em, organizationId, tenantId, warehouseId) {
246
+ const demotedPrimariesBefore = await loadDemotedPrimarySnapshots(em, organizationId, tenantId, warehouseId);
247
+ if (demotedPrimariesBefore.length === 0) return [];
248
+ await em.nativeUpdate(
249
+ Warehouse,
250
+ {
251
+ organizationId,
252
+ id: { $ne: warehouseId },
253
+ isPrimary: true,
254
+ deletedAt: null
255
+ },
256
+ { isPrimary: false }
257
+ );
258
+ return demotedPrimariesBefore;
259
+ }
260
+ async function rejectInactivePrimaryWarehouse(ctx) {
261
+ const { translate } = await resolveTranslations();
262
+ const message = translate(
263
+ "wms.validation.warehouse.inactivePrimary",
264
+ "Inactive warehouses cannot be marked as primary."
265
+ );
266
+ throw new CrudHttpError(422, { error: message, fieldErrors: { isPrimary: message } });
267
+ }
268
+ const WMS_WAREHOUSE_PRIMARY_UNIQUE_CONSTRAINT = "wms_warehouses_org_primary_unique_idx";
269
+ async function rejectPrimaryWarehouseConflict() {
270
+ const { translate } = await resolveTranslations();
271
+ const message = translate(
272
+ "wms.validation.warehouse.primaryConflict",
273
+ "Another warehouse was just set as primary for this organization. Please retry."
274
+ );
275
+ throw new CrudHttpError(409, { error: message, fieldErrors: { isPrimary: message } });
276
+ }
277
+ async function restoreDemotedPrimaryWarehouses(em, ctx, demotedPrimariesBefore) {
278
+ if (!demotedPrimariesBefore?.length) return;
279
+ for (const snapshot of demotedPrimariesBefore) {
280
+ const record = await findOneWithDecryption(em, Warehouse, { id: snapshot.id }, void 0, resolveScope(ctx));
281
+ if (!record) continue;
282
+ ensureTenantScope(ctx, record.tenantId);
283
+ ensureOrganizationScope(ctx, record.organizationId);
284
+ record.isPrimary = snapshot.isPrimary;
285
+ }
286
+ }
287
+ async function ensureZoneCodeUnique(em, warehouseId, tenantId, organizationId, code, excludeId) {
288
+ const existing = await findOneWithDecryption(
289
+ em,
290
+ WarehouseZone,
291
+ { warehouse: warehouseId, tenantId, organizationId, code, deletedAt: null },
292
+ void 0,
293
+ { tenantId, organizationId }
294
+ );
295
+ if (existing && existing.id !== excludeId) {
296
+ throw new CrudHttpError(409, { error: "Zone code already exists in this warehouse." });
297
+ }
298
+ }
299
+ async function ensureLocationCodeUnique(em, warehouseId, tenantId, organizationId, code, excludeId) {
300
+ const existing = await findOneWithDecryption(
301
+ em,
302
+ WarehouseLocation,
303
+ { warehouse: warehouseId, tenantId, organizationId, code, deletedAt: null },
304
+ void 0,
305
+ { tenantId, organizationId }
306
+ );
307
+ if (existing && existing.id !== excludeId) {
308
+ throw new CrudHttpError(409, { error: "Location code already exists in this warehouse." });
309
+ }
310
+ }
311
+ async function ensureProfileUniqueness(em, input, excludeId) {
312
+ const where = input.catalogVariantId ? {
313
+ tenantId: input.tenantId,
314
+ organizationId: input.organizationId,
315
+ catalogVariantId: input.catalogVariantId,
316
+ deletedAt: null
317
+ } : {
318
+ tenantId: input.tenantId,
319
+ organizationId: input.organizationId,
320
+ catalogProductId: input.catalogProductId,
321
+ catalogVariantId: null,
322
+ deletedAt: null
323
+ };
324
+ const existing = await findOneWithDecryption(
325
+ em,
326
+ ProductInventoryProfile,
327
+ where,
328
+ void 0,
329
+ { tenantId: input.tenantId, organizationId: input.organizationId }
330
+ );
331
+ if (existing && existing.id !== excludeId) {
332
+ throw new CrudHttpError(409, { error: "Inventory profile already exists for this product scope." });
333
+ }
334
+ }
335
+ async function ensureLotUniqueness(em, input, excludeId) {
336
+ const existing = await findOneWithDecryption(
337
+ em,
338
+ InventoryLot,
339
+ {
340
+ tenantId: input.tenantId,
341
+ organizationId: input.organizationId,
342
+ catalogVariantId: input.catalogVariantId,
343
+ lotNumber: input.lotNumber,
344
+ deletedAt: null
345
+ },
346
+ void 0,
347
+ { tenantId: input.tenantId, organizationId: input.organizationId }
348
+ );
349
+ if (existing && existing.id !== excludeId) {
350
+ throw new CrudHttpError(409, { error: "Lot number already exists for this variant." });
351
+ }
352
+ }
353
+ async function resolveParentLocation(em, ctx, warehouseId, parentId) {
354
+ if (!parentId) return null;
355
+ const parent = await loadLocation(em, ctx, parentId);
356
+ const parentWarehouseId = typeof parent.warehouse === "string" ? parent.warehouse : parent.warehouse.id;
357
+ if (parentWarehouseId !== warehouseId) {
358
+ throw new CrudHttpError(422, { error: "Parent location must belong to the same warehouse." });
359
+ }
360
+ return parent;
361
+ }
362
+ const createWarehouseCommand = {
363
+ id: "wms.warehouses.create",
364
+ async execute(rawInput, ctx) {
365
+ const parsed = warehouseCreateSchema.parse(rawInput ?? {});
366
+ ensureTenantScope(ctx, parsed.tenantId);
367
+ ensureOrganizationScope(ctx, parsed.organizationId);
368
+ const em = resolveEm(ctx);
369
+ await ensureWarehouseCodeUnique(em, parsed.tenantId, parsed.organizationId, parsed.code);
370
+ const isActive = parsed.isActive ?? true;
371
+ const isPrimary = parsed.isPrimary ?? false;
372
+ if (isPrimary && !isActive) {
373
+ await rejectInactivePrimaryWarehouse(ctx);
374
+ }
375
+ const warehouse = em.create(Warehouse, {
376
+ organizationId: parsed.organizationId,
377
+ tenantId: parsed.tenantId,
378
+ name: parsed.name,
379
+ code: parsed.code,
380
+ isActive,
381
+ // Always insert as non-primary first: inserting with isPrimary=true would
382
+ // race the `wms_warehouses_org_primary_unique_idx` partial unique index
383
+ // against an existing sibling primary before that sibling gets demoted
384
+ // below (Postgres checks partial unique indexes per-statement, not at
385
+ // commit time).
386
+ isPrimary: false,
387
+ addressLine1: normalizeOptionalString(parsed.addressLine1),
388
+ city: normalizeOptionalString(parsed.city),
389
+ postalCode: normalizeOptionalString(parsed.postalCode),
390
+ country: normalizeOptionalString(parsed.country),
391
+ timezone: normalizeOptionalString(parsed.timezone),
392
+ metadata: toJsonValue(parsed.metadata)
393
+ });
394
+ let demotedPrimariesBefore = [];
395
+ try {
396
+ await withAtomicFlush(
397
+ em,
398
+ [
399
+ () => {
400
+ em.persist(warehouse);
401
+ },
402
+ async () => {
403
+ if (!isPrimary) return;
404
+ demotedPrimariesBefore = await enforcePrimaryWarehouse(
405
+ em,
406
+ warehouse.organizationId,
407
+ warehouse.tenantId,
408
+ warehouse.id
409
+ );
410
+ },
411
+ () => {
412
+ if (isPrimary) warehouse.isPrimary = true;
413
+ }
414
+ ],
415
+ { transaction: true, label: "wms.warehouses.create" }
416
+ );
417
+ } catch (err) {
418
+ if (isUniqueViolation(err, WMS_WAREHOUSE_PRIMARY_UNIQUE_CONSTRAINT)) {
419
+ await rejectPrimaryWarehouseConflict();
420
+ }
421
+ throw err;
422
+ }
423
+ void emitWmsEvent("wms.warehouse.created", {
424
+ id: warehouse.id,
425
+ warehouseId: warehouse.id,
426
+ tenantId: warehouse.tenantId,
427
+ organizationId: warehouse.organizationId
428
+ }).catch(() => void 0);
429
+ return {
430
+ warehouseId: warehouse.id,
431
+ ...demotedPrimariesBefore.length > 0 ? { demotedPrimariesBefore } : {}
432
+ };
433
+ },
434
+ captureAfter: async (_input, result, ctx) => {
435
+ const em = resolveEm(ctx);
436
+ return loadWarehouseSnapshot(em, ctx, result.warehouseId);
437
+ },
438
+ buildLog: async ({ input, result, ctx, snapshots }) => {
439
+ const base = await buildCrudLog(ctx, input, result?.warehouseId ?? null, "wms.audit.warehouse.create", "Create warehouse", "wms.warehouse");
440
+ const after = snapshots?.after;
441
+ const demotedPrimariesBefore = result?.demotedPrimariesBefore;
442
+ return {
443
+ ...base,
444
+ snapshotAfter: after,
445
+ payload: {
446
+ undo: {
447
+ after,
448
+ ...demotedPrimariesBefore?.length ? { demotedPrimariesBefore } : {}
449
+ }
450
+ }
451
+ };
452
+ },
453
+ undo: async ({ logEntry, ctx }) => {
454
+ const payload = extractUndoPayload(logEntry);
455
+ const after = payload?.after;
456
+ if (!after) return;
457
+ const em = resolveEm(ctx);
458
+ const record = await findOneWithDecryption(
459
+ em,
460
+ Warehouse,
461
+ { id: after.id },
462
+ void 0,
463
+ resolveScope(ctx, { tenantId: after.tenantId, organizationId: after.organizationId })
464
+ );
465
+ if (!record) return;
466
+ ensureTenantScope(ctx, record.tenantId);
467
+ ensureOrganizationScope(ctx, record.organizationId);
468
+ record.deletedAt = /* @__PURE__ */ new Date();
469
+ await em.flush();
470
+ if (payload.demotedPrimariesBefore?.length) {
471
+ await restoreDemotedPrimaryWarehouses(em, ctx, payload.demotedPrimariesBefore);
472
+ await em.flush();
473
+ }
474
+ }
475
+ };
476
+ const updateWarehouseCommand = {
477
+ id: "wms.warehouses.update",
478
+ prepare: async (input, ctx) => {
479
+ const id = requireId(input?.id, "Warehouse");
480
+ const em = resolveEm(ctx);
481
+ const before = await loadWarehouseSnapshot(em, ctx, id);
482
+ return before ? { before } : {};
483
+ },
484
+ async execute(rawInput, ctx) {
485
+ const parsed = warehouseUpdateSchema.parse(rawInput ?? {});
486
+ const em = resolveEm(ctx);
487
+ const warehouse = await loadWarehouse(em, ctx, parsed.id);
488
+ if (parsed.code !== void 0 && parsed.code !== warehouse.code) {
489
+ await ensureWarehouseCodeUnique(em, warehouse.tenantId, warehouse.organizationId, parsed.code, warehouse.id);
490
+ }
491
+ let nextIsActive = warehouse.isActive;
492
+ let nextIsPrimary = warehouse.isPrimary;
493
+ if (parsed.isActive !== void 0) nextIsActive = parsed.isActive;
494
+ if (parsed.isPrimary !== void 0) nextIsPrimary = parsed.isPrimary;
495
+ if (parsed.isActive === false && nextIsPrimary) {
496
+ nextIsPrimary = false;
497
+ } else if (nextIsPrimary && !nextIsActive) {
498
+ await rejectInactivePrimaryWarehouse(ctx);
499
+ }
500
+ let demotedPrimariesBefore = [];
501
+ try {
502
+ await withAtomicFlush(
503
+ em,
504
+ [
505
+ () => {
506
+ if (parsed.code !== void 0) warehouse.code = parsed.code;
507
+ if (parsed.name !== void 0) warehouse.name = parsed.name;
508
+ if (parsed.addressLine1 !== void 0) warehouse.addressLine1 = normalizeOptionalString(parsed.addressLine1);
509
+ if (parsed.city !== void 0) warehouse.city = normalizeOptionalString(parsed.city);
510
+ if (parsed.postalCode !== void 0) warehouse.postalCode = normalizeOptionalString(parsed.postalCode);
511
+ if (parsed.country !== void 0) warehouse.country = normalizeOptionalString(parsed.country);
512
+ if (parsed.timezone !== void 0) warehouse.timezone = normalizeOptionalString(parsed.timezone);
513
+ if (parsed.metadata !== void 0) warehouse.metadata = toJsonValue(parsed.metadata);
514
+ warehouse.isActive = nextIsActive;
515
+ warehouse.isPrimary = false;
516
+ },
517
+ async () => {
518
+ if (!nextIsPrimary) return;
519
+ demotedPrimariesBefore = await enforcePrimaryWarehouse(
520
+ em,
521
+ warehouse.organizationId,
522
+ warehouse.tenantId,
523
+ warehouse.id
524
+ );
525
+ },
526
+ () => {
527
+ warehouse.isPrimary = nextIsPrimary;
528
+ }
529
+ ],
530
+ { transaction: true, label: "wms.warehouses.update" }
531
+ );
532
+ } catch (err) {
533
+ if (isUniqueViolation(err, WMS_WAREHOUSE_PRIMARY_UNIQUE_CONSTRAINT)) {
534
+ await rejectPrimaryWarehouseConflict();
535
+ }
536
+ throw err;
537
+ }
538
+ void emitWmsEvent("wms.warehouse.updated", {
539
+ id: warehouse.id,
540
+ warehouseId: warehouse.id,
541
+ tenantId: warehouse.tenantId,
542
+ organizationId: warehouse.organizationId
543
+ }).catch(() => void 0);
544
+ return {
545
+ warehouseId: warehouse.id,
546
+ ...demotedPrimariesBefore.length > 0 ? { demotedPrimariesBefore } : {}
547
+ };
548
+ },
549
+ captureAfter: async (_input, result, ctx) => {
550
+ const em = resolveEm(ctx);
551
+ return loadWarehouseSnapshot(em, ctx, result.warehouseId);
552
+ },
553
+ buildLog: async ({ input, result, ctx, snapshots }) => {
554
+ const base = await buildCrudLog(ctx, input, result?.warehouseId ?? null, "wms.audit.warehouse.update", "Update warehouse", "wms.warehouse");
555
+ const before = snapshots?.before;
556
+ const after = snapshots?.after;
557
+ const demotedPrimariesBefore = result?.demotedPrimariesBefore;
558
+ return {
559
+ ...base,
560
+ snapshotBefore: before,
561
+ snapshotAfter: after,
562
+ payload: {
563
+ undo: {
564
+ before,
565
+ after,
566
+ ...demotedPrimariesBefore?.length ? { demotedPrimariesBefore } : {}
567
+ }
568
+ }
569
+ };
570
+ },
571
+ undo: async ({ logEntry, ctx }) => {
572
+ const payload = extractUndoPayload(logEntry);
573
+ const before = payload?.before;
574
+ if (!before) return;
575
+ const em = resolveEm(ctx);
576
+ let record = await findOneWithDecryption(
577
+ em,
578
+ Warehouse,
579
+ { id: before.id },
580
+ void 0,
581
+ resolveScope(ctx, { tenantId: before.tenantId, organizationId: before.organizationId })
582
+ );
583
+ if (!record) {
584
+ record = em.create(Warehouse, {
585
+ id: before.id,
586
+ organizationId: before.organizationId,
587
+ tenantId: before.tenantId,
588
+ name: before.name,
589
+ code: before.code,
590
+ isActive: before.isActive,
591
+ isPrimary: before.isPrimary,
592
+ addressLine1: before.addressLine1,
593
+ city: before.city,
594
+ postalCode: before.postalCode,
595
+ country: before.country,
596
+ timezone: before.timezone,
597
+ metadata: before.metadata,
598
+ createdAt: new Date(before.createdAt),
599
+ updatedAt: new Date(before.updatedAt)
600
+ });
601
+ em.persist(record);
602
+ } else {
603
+ ensureTenantScope(ctx, before.tenantId);
604
+ ensureOrganizationScope(ctx, before.organizationId);
605
+ record.name = before.name;
606
+ record.code = before.code;
607
+ record.isActive = before.isActive;
608
+ record.isPrimary = before.isPrimary;
609
+ record.addressLine1 = before.addressLine1;
610
+ record.city = before.city;
611
+ record.postalCode = before.postalCode;
612
+ record.country = before.country;
613
+ record.timezone = before.timezone;
614
+ record.metadata = before.metadata;
615
+ record.deletedAt = null;
616
+ }
617
+ await em.flush();
618
+ if (payload.demotedPrimariesBefore?.length) {
619
+ await restoreDemotedPrimaryWarehouses(em, ctx, payload.demotedPrimariesBefore);
620
+ await em.flush();
621
+ }
622
+ }
623
+ };
624
+ const deleteWarehouseCommand = {
625
+ id: "wms.warehouses.delete",
626
+ prepare: async (input, ctx) => {
627
+ const id = requireId(input?.id, "Warehouse");
628
+ const em = resolveEm(ctx);
629
+ const before = await loadWarehouseSnapshot(em, ctx, id);
630
+ return before ? { before } : {};
631
+ },
632
+ async execute(input, ctx) {
633
+ const warehouseId = requireId(input?.id, "Warehouse");
634
+ const em = resolveEm(ctx);
635
+ const warehouse = await loadWarehouse(em, ctx, warehouseId);
636
+ warehouse.deletedAt = /* @__PURE__ */ new Date();
637
+ await em.flush();
638
+ return { warehouseId: warehouse.id };
639
+ },
640
+ buildLog: async ({ input, result, ctx, snapshots }) => {
641
+ const base = await buildCrudLog(ctx, input, result?.warehouseId ?? null, "wms.audit.warehouse.delete", "Delete warehouse", "wms.warehouse");
642
+ const before = snapshots?.before;
643
+ return { ...base, snapshotBefore: before, payload: { undo: { before } } };
644
+ },
645
+ undo: async ({ logEntry, ctx }) => {
646
+ const payload = extractUndoPayload(logEntry);
647
+ const before = payload?.before;
648
+ if (!before) return;
649
+ const em = resolveEm(ctx);
650
+ let record = await findOneWithDecryption(
651
+ em,
652
+ Warehouse,
653
+ { id: before.id },
654
+ void 0,
655
+ resolveScope(ctx, { tenantId: before.tenantId, organizationId: before.organizationId })
656
+ );
657
+ if (!record) {
658
+ record = em.create(Warehouse, {
659
+ id: before.id,
660
+ organizationId: before.organizationId,
661
+ tenantId: before.tenantId,
662
+ name: before.name,
663
+ code: before.code,
664
+ isActive: before.isActive,
665
+ isPrimary: before.isPrimary,
666
+ addressLine1: before.addressLine1,
667
+ city: before.city,
668
+ postalCode: before.postalCode,
669
+ country: before.country,
670
+ timezone: before.timezone,
671
+ metadata: before.metadata,
672
+ createdAt: new Date(before.createdAt),
673
+ updatedAt: new Date(before.updatedAt)
674
+ });
675
+ em.persist(record);
676
+ } else {
677
+ ensureTenantScope(ctx, before.tenantId);
678
+ ensureOrganizationScope(ctx, before.organizationId);
679
+ record.deletedAt = null;
680
+ record.name = before.name;
681
+ record.code = before.code;
682
+ record.isActive = before.isActive;
683
+ record.isPrimary = before.isPrimary;
684
+ record.addressLine1 = before.addressLine1;
685
+ record.city = before.city;
686
+ record.postalCode = before.postalCode;
687
+ record.country = before.country;
688
+ record.timezone = before.timezone;
689
+ record.metadata = before.metadata;
690
+ }
691
+ await em.flush();
692
+ }
693
+ };
694
+ async function restoreZoneFromSnapshot(em, before) {
695
+ const scope = { tenantId: before.tenantId, organizationId: before.organizationId };
696
+ const warehouseRef = await findOneWithDecryption(em, Warehouse, { id: before.warehouseId }, void 0, scope);
697
+ if (!warehouseRef) {
698
+ throw new CrudHttpError(409, { error: "Cannot undo zone: parent warehouse no longer exists." });
699
+ }
700
+ let record = await findOneWithDecryption(em, WarehouseZone, { id: before.id }, void 0, scope);
701
+ if (!record) {
702
+ record = em.create(WarehouseZone, {
703
+ id: before.id,
704
+ organizationId: before.organizationId,
705
+ tenantId: before.tenantId,
706
+ warehouse: warehouseRef,
707
+ code: before.code,
708
+ name: before.name,
709
+ priority: before.priority,
710
+ metadata: before.metadata,
711
+ createdAt: new Date(before.createdAt),
712
+ updatedAt: new Date(before.updatedAt)
713
+ });
714
+ em.persist(record);
715
+ } else {
716
+ record.warehouse = warehouseRef;
717
+ record.code = before.code;
718
+ record.name = before.name;
719
+ record.priority = before.priority;
720
+ record.metadata = before.metadata;
721
+ record.deletedAt = null;
722
+ }
723
+ }
724
+ const createWarehouseZoneCommand = {
725
+ id: "wms.zones.create",
726
+ async execute(rawInput, ctx) {
727
+ const parsed = warehouseZoneCreateSchema.parse(rawInput ?? {});
728
+ ensureTenantScope(ctx, parsed.tenantId);
729
+ ensureOrganizationScope(ctx, parsed.organizationId);
730
+ const em = resolveEm(ctx);
731
+ const warehouse = await loadWarehouse(em, ctx, parsed.warehouseId);
732
+ await ensureZoneCodeUnique(em, warehouse.id, warehouse.tenantId, warehouse.organizationId, parsed.code);
733
+ const zone = em.create(WarehouseZone, {
734
+ organizationId: parsed.organizationId,
735
+ tenantId: parsed.tenantId,
736
+ warehouse,
737
+ code: parsed.code,
738
+ name: parsed.name,
739
+ priority: parsed.priority ?? 0,
740
+ metadata: toJsonValue(parsed.metadata)
741
+ });
742
+ await em.persist(zone).flush();
743
+ void emitWmsEvent("wms.zone.created", {
744
+ id: zone.id,
745
+ zoneId: zone.id,
746
+ warehouseId: typeof zone.warehouse === "string" ? zone.warehouse : zone.warehouse.id,
747
+ tenantId: zone.tenantId,
748
+ organizationId: zone.organizationId
749
+ }).catch(() => void 0);
750
+ return { zoneId: zone.id };
751
+ },
752
+ captureAfter: async (_input, result, ctx) => {
753
+ const em = resolveEm(ctx);
754
+ return loadWarehouseZoneSnapshot(em, ctx, result.zoneId);
755
+ },
756
+ buildLog: async ({ input, result, ctx, snapshots }) => {
757
+ const base = await buildCrudLog(ctx, input, result?.zoneId ?? null, "wms.audit.zone.create", "Create warehouse zone", "wms.zone");
758
+ const after = snapshots?.after;
759
+ return { ...base, snapshotAfter: after, payload: { undo: { after } } };
760
+ },
761
+ undo: async ({ logEntry, ctx }) => {
762
+ const payload = extractUndoPayload(logEntry);
763
+ const after = payload?.after;
764
+ if (!after) return;
765
+ const em = resolveEm(ctx);
766
+ const record = await findOneWithDecryption(
767
+ em,
768
+ WarehouseZone,
769
+ { id: after.id },
770
+ void 0,
771
+ resolveScope(ctx, { tenantId: after.tenantId, organizationId: after.organizationId })
772
+ );
773
+ if (!record) return;
774
+ ensureTenantScope(ctx, record.tenantId);
775
+ ensureOrganizationScope(ctx, record.organizationId);
776
+ record.deletedAt = /* @__PURE__ */ new Date();
777
+ await em.flush();
778
+ }
779
+ };
780
+ const updateWarehouseZoneCommand = {
781
+ id: "wms.zones.update",
782
+ prepare: async (input, ctx) => {
783
+ const id = requireId(input?.id, "Zone");
784
+ const em = resolveEm(ctx);
785
+ const before = await loadWarehouseZoneSnapshot(em, ctx, id);
786
+ return before ? { before } : {};
787
+ },
788
+ async execute(rawInput, ctx) {
789
+ const parsed = warehouseZoneUpdateSchema.parse(rawInput ?? {});
790
+ const em = resolveEm(ctx);
791
+ const zone = await loadZone(em, ctx, parsed.id);
792
+ if (parsed.warehouseId !== void 0) {
793
+ const warehouse = await loadWarehouse(em, ctx, parsed.warehouseId);
794
+ zone.warehouse = warehouse;
795
+ }
796
+ const warehouseId = typeof zone.warehouse === "string" ? zone.warehouse : zone.warehouse.id;
797
+ if (parsed.code !== void 0 && parsed.code !== zone.code) {
798
+ await ensureZoneCodeUnique(em, warehouseId, zone.tenantId, zone.organizationId, parsed.code, zone.id);
799
+ zone.code = parsed.code;
800
+ }
801
+ if (parsed.name !== void 0) zone.name = parsed.name;
802
+ if (parsed.priority !== void 0) zone.priority = parsed.priority;
803
+ if (parsed.metadata !== void 0) zone.metadata = toJsonValue(parsed.metadata);
804
+ await em.flush();
805
+ void emitWmsEvent("wms.zone.updated", {
806
+ id: zone.id,
807
+ zoneId: zone.id,
808
+ warehouseId: typeof zone.warehouse === "string" ? zone.warehouse : zone.warehouse.id,
809
+ tenantId: zone.tenantId,
810
+ organizationId: zone.organizationId
811
+ }).catch(() => void 0);
812
+ return { zoneId: zone.id };
813
+ },
814
+ captureAfter: async (_input, result, ctx) => {
815
+ const em = resolveEm(ctx);
816
+ return loadWarehouseZoneSnapshot(em, ctx, result.zoneId);
817
+ },
818
+ buildLog: async ({ input, result, ctx, snapshots }) => {
819
+ const base = await buildCrudLog(ctx, input, result?.zoneId ?? null, "wms.audit.zone.update", "Update warehouse zone", "wms.zone");
820
+ const before = snapshots?.before;
821
+ const after = snapshots?.after;
822
+ return { ...base, snapshotBefore: before, snapshotAfter: after, payload: { undo: { before, after } } };
823
+ },
824
+ undo: async ({ logEntry, ctx }) => {
825
+ const payload = extractUndoPayload(logEntry);
826
+ const before = payload?.before;
827
+ if (!before) return;
828
+ const em = resolveEm(ctx);
829
+ ensureTenantScope(ctx, before.tenantId);
830
+ ensureOrganizationScope(ctx, before.organizationId);
831
+ await restoreZoneFromSnapshot(em, before);
832
+ await em.flush();
833
+ }
834
+ };
835
+ const deleteWarehouseZoneCommand = {
836
+ id: "wms.zones.delete",
837
+ prepare: async (input, ctx) => {
838
+ const id = requireId(input?.id, "Zone");
839
+ const em = resolveEm(ctx);
840
+ const before = await loadWarehouseZoneSnapshot(em, ctx, id);
841
+ return before ? { before } : {};
842
+ },
843
+ async execute(input, ctx) {
844
+ const zoneId = requireId(input?.id, "Zone");
845
+ const em = resolveEm(ctx);
846
+ const zone = await loadZone(em, ctx, zoneId);
847
+ zone.deletedAt = /* @__PURE__ */ new Date();
848
+ await em.flush();
849
+ return { zoneId: zone.id };
850
+ },
851
+ buildLog: async ({ input, result, ctx, snapshots }) => {
852
+ const base = await buildCrudLog(ctx, input, result?.zoneId ?? null, "wms.audit.zone.delete", "Delete warehouse zone", "wms.zone");
853
+ const before = snapshots?.before;
854
+ return { ...base, snapshotBefore: before, payload: { undo: { before } } };
855
+ },
856
+ undo: async ({ logEntry, ctx }) => {
857
+ const payload = extractUndoPayload(logEntry);
858
+ const before = payload?.before;
859
+ if (!before) return;
860
+ const em = resolveEm(ctx);
861
+ ensureTenantScope(ctx, before.tenantId);
862
+ ensureOrganizationScope(ctx, before.organizationId);
863
+ await restoreZoneFromSnapshot(em, before);
864
+ await em.flush();
865
+ }
866
+ };
867
+ async function restoreLocationFromSnapshot(em, before) {
868
+ const scope = { tenantId: before.tenantId, organizationId: before.organizationId };
869
+ const warehouseRef = await findOneWithDecryption(em, Warehouse, { id: before.warehouseId }, void 0, scope);
870
+ if (!warehouseRef) {
871
+ throw new CrudHttpError(409, { error: "Cannot undo location: parent warehouse no longer exists." });
872
+ }
873
+ let parentRef = null;
874
+ if (before.parentId) {
875
+ parentRef = await findOneWithDecryption(em, WarehouseLocation, { id: before.parentId }, void 0, scope);
876
+ }
877
+ let record = await findOneWithDecryption(em, WarehouseLocation, { id: before.id }, void 0, scope);
878
+ if (!record) {
879
+ record = em.create(WarehouseLocation, {
880
+ id: before.id,
881
+ organizationId: before.organizationId,
882
+ tenantId: before.tenantId,
883
+ warehouse: warehouseRef,
884
+ parent: parentRef,
885
+ code: before.code,
886
+ type: before.type,
887
+ isActive: before.isActive,
888
+ capacityUnits: before.capacityUnits,
889
+ capacityWeight: before.capacityWeight,
890
+ constraints: before.constraints,
891
+ metadata: before.metadata,
892
+ createdAt: new Date(before.createdAt),
893
+ updatedAt: new Date(before.updatedAt)
894
+ });
895
+ em.persist(record);
896
+ } else {
897
+ record.warehouse = warehouseRef;
898
+ record.parent = parentRef;
899
+ record.code = before.code;
900
+ record.type = before.type;
901
+ record.isActive = before.isActive;
902
+ record.capacityUnits = before.capacityUnits;
903
+ record.capacityWeight = before.capacityWeight;
904
+ record.constraints = before.constraints;
905
+ record.metadata = before.metadata;
906
+ record.deletedAt = null;
907
+ }
908
+ }
909
+ const createWarehouseLocationCommand = {
910
+ id: "wms.locations.create",
911
+ async execute(rawInput, ctx) {
912
+ const parsed = warehouseLocationCreateSchema.parse(rawInput ?? {});
913
+ ensureTenantScope(ctx, parsed.tenantId);
914
+ ensureOrganizationScope(ctx, parsed.organizationId);
915
+ const em = resolveEm(ctx);
916
+ const warehouse = await loadWarehouse(em, ctx, parsed.warehouseId);
917
+ await ensureLocationCodeUnique(em, warehouse.id, warehouse.tenantId, warehouse.organizationId, parsed.code);
918
+ const parent = await resolveParentLocation(em, ctx, warehouse.id, parsed.parentId ?? null);
919
+ const location = em.create(WarehouseLocation, {
920
+ organizationId: parsed.organizationId,
921
+ tenantId: parsed.tenantId,
922
+ warehouse,
923
+ code: parsed.code,
924
+ type: parsed.type,
925
+ parent,
926
+ isActive: parsed.isActive ?? true,
927
+ capacityUnits: parsed.capacityUnits !== void 0 ? toNumericString(parsed.capacityUnits) : null,
928
+ capacityWeight: parsed.capacityWeight !== void 0 ? toNumericString(parsed.capacityWeight) : null,
929
+ constraints: toJsonValue(parsed.constraints),
930
+ metadata: toJsonValue(parsed.metadata)
931
+ });
932
+ await em.persist(location).flush();
933
+ void emitWmsEvent("wms.location.created", {
934
+ id: location.id,
935
+ locationId: location.id,
936
+ warehouseId: typeof location.warehouse === "string" ? location.warehouse : location.warehouse.id,
937
+ tenantId: location.tenantId,
938
+ organizationId: location.organizationId
939
+ }).catch(() => void 0);
940
+ return { locationId: location.id };
941
+ },
942
+ captureAfter: async (_input, result, ctx) => {
943
+ const em = resolveEm(ctx);
944
+ return loadWarehouseLocationSnapshot(em, ctx, result.locationId);
945
+ },
946
+ buildLog: async ({ input, result, ctx, snapshots }) => {
947
+ const base = await buildCrudLog(ctx, input, result?.locationId ?? null, "wms.audit.location.create", "Create warehouse location", "wms.location");
948
+ const after = snapshots?.after;
949
+ return { ...base, snapshotAfter: after, payload: { undo: { after } } };
950
+ },
951
+ undo: async ({ logEntry, ctx }) => {
952
+ const payload = extractUndoPayload(logEntry);
953
+ const after = payload?.after;
954
+ if (!after) return;
955
+ const em = resolveEm(ctx);
956
+ const record = await findOneWithDecryption(
957
+ em,
958
+ WarehouseLocation,
959
+ { id: after.id },
960
+ void 0,
961
+ resolveScope(ctx, { tenantId: after.tenantId, organizationId: after.organizationId })
962
+ );
963
+ if (!record) return;
964
+ ensureTenantScope(ctx, record.tenantId);
965
+ ensureOrganizationScope(ctx, record.organizationId);
966
+ record.deletedAt = /* @__PURE__ */ new Date();
967
+ await em.flush();
968
+ }
969
+ };
970
+ const updateWarehouseLocationCommand = {
971
+ id: "wms.locations.update",
972
+ prepare: async (input, ctx) => {
973
+ const id = requireId(input?.id, "Location");
974
+ const em = resolveEm(ctx);
975
+ const before = await loadWarehouseLocationSnapshot(em, ctx, id);
976
+ return before ? { before } : {};
977
+ },
978
+ async execute(rawInput, ctx) {
979
+ const parsed = warehouseLocationUpdateSchema.parse(rawInput ?? {});
980
+ const em = resolveEm(ctx);
981
+ const location = await loadLocation(em, ctx, parsed.id);
982
+ if (parsed.warehouseId !== void 0) {
983
+ const warehouse = await loadWarehouse(em, ctx, parsed.warehouseId);
984
+ location.warehouse = warehouse;
985
+ }
986
+ const warehouseId = typeof location.warehouse === "string" ? location.warehouse : location.warehouse.id;
987
+ if (parsed.parentId !== void 0) {
988
+ location.parent = await resolveParentLocation(em, ctx, warehouseId, parsed.parentId);
989
+ }
990
+ if (parsed.code !== void 0 && parsed.code !== location.code) {
991
+ await ensureLocationCodeUnique(em, warehouseId, location.tenantId, location.organizationId, parsed.code, location.id);
992
+ location.code = parsed.code;
993
+ }
994
+ if (parsed.type !== void 0) location.type = parsed.type;
995
+ if (parsed.isActive !== void 0) location.isActive = parsed.isActive;
996
+ if (parsed.capacityUnits !== void 0) location.capacityUnits = toNumericString(parsed.capacityUnits);
997
+ if (parsed.capacityWeight !== void 0) location.capacityWeight = toNumericString(parsed.capacityWeight);
998
+ if (parsed.constraints !== void 0) location.constraints = toJsonValue(parsed.constraints);
999
+ if (parsed.metadata !== void 0) location.metadata = toJsonValue(parsed.metadata);
1000
+ await em.flush();
1001
+ void emitWmsEvent("wms.location.updated", {
1002
+ id: location.id,
1003
+ locationId: location.id,
1004
+ warehouseId: typeof location.warehouse === "string" ? location.warehouse : location.warehouse.id,
1005
+ tenantId: location.tenantId,
1006
+ organizationId: location.organizationId
1007
+ }).catch(() => void 0);
1008
+ return { locationId: location.id };
1009
+ },
1010
+ captureAfter: async (_input, result, ctx) => {
1011
+ const em = resolveEm(ctx);
1012
+ return loadWarehouseLocationSnapshot(em, ctx, result.locationId);
1013
+ },
1014
+ buildLog: async ({ input, result, ctx, snapshots }) => {
1015
+ const base = await buildCrudLog(ctx, input, result?.locationId ?? null, "wms.audit.location.update", "Update warehouse location", "wms.location");
1016
+ const before = snapshots?.before;
1017
+ const after = snapshots?.after;
1018
+ return { ...base, snapshotBefore: before, snapshotAfter: after, payload: { undo: { before, after } } };
1019
+ },
1020
+ undo: async ({ logEntry, ctx }) => {
1021
+ const payload = extractUndoPayload(logEntry);
1022
+ const before = payload?.before;
1023
+ if (!before) return;
1024
+ const em = resolveEm(ctx);
1025
+ ensureTenantScope(ctx, before.tenantId);
1026
+ ensureOrganizationScope(ctx, before.organizationId);
1027
+ await restoreLocationFromSnapshot(em, before);
1028
+ await em.flush();
1029
+ }
1030
+ };
1031
+ const deleteWarehouseLocationCommand = {
1032
+ id: "wms.locations.delete",
1033
+ prepare: async (input, ctx) => {
1034
+ const id = requireId(input?.id, "Location");
1035
+ const em = resolveEm(ctx);
1036
+ const before = await loadWarehouseLocationSnapshot(em, ctx, id);
1037
+ return before ? { before } : {};
1038
+ },
1039
+ async execute(input, ctx) {
1040
+ const locationId = requireId(input?.id, "Location");
1041
+ const em = resolveEm(ctx);
1042
+ const location = await loadLocation(em, ctx, locationId);
1043
+ location.deletedAt = /* @__PURE__ */ new Date();
1044
+ await em.flush();
1045
+ return { locationId: location.id };
1046
+ },
1047
+ buildLog: async ({ input, result, ctx, snapshots }) => {
1048
+ const base = await buildCrudLog(ctx, input, result?.locationId ?? null, "wms.audit.location.delete", "Delete warehouse location", "wms.location");
1049
+ const before = snapshots?.before;
1050
+ return { ...base, snapshotBefore: before, payload: { undo: { before } } };
1051
+ },
1052
+ undo: async ({ logEntry, ctx }) => {
1053
+ const payload = extractUndoPayload(logEntry);
1054
+ const before = payload?.before;
1055
+ if (!before) return;
1056
+ const em = resolveEm(ctx);
1057
+ ensureTenantScope(ctx, before.tenantId);
1058
+ ensureOrganizationScope(ctx, before.organizationId);
1059
+ await restoreLocationFromSnapshot(em, before);
1060
+ await em.flush();
1061
+ }
1062
+ };
1063
+ function restoreInventoryProfileFromSnapshot(em, before, record) {
1064
+ record.catalogProductId = before.catalogProductId;
1065
+ record.catalogVariantId = before.catalogVariantId;
1066
+ record.defaultUom = before.defaultUom;
1067
+ record.trackLot = before.trackLot;
1068
+ record.trackSerial = before.trackSerial;
1069
+ record.trackExpiration = before.trackExpiration;
1070
+ record.defaultStrategy = before.defaultStrategy;
1071
+ record.reorderPoint = before.reorderPoint;
1072
+ record.safetyStock = before.safetyStock;
1073
+ record.metadata = before.metadata;
1074
+ record.deletedAt = null;
1075
+ }
1076
+ const createProductInventoryProfileCommand = {
1077
+ id: "wms.inventoryProfiles.create",
1078
+ async execute(rawInput, ctx) {
1079
+ const parsed = productInventoryProfileCreateSchema.parse(rawInput ?? {});
1080
+ ensureTenantScope(ctx, parsed.tenantId);
1081
+ ensureOrganizationScope(ctx, parsed.organizationId);
1082
+ const em = resolveEm(ctx);
1083
+ await ensureProfileUniqueness(em, parsed);
1084
+ const profile = em.create(ProductInventoryProfile, {
1085
+ organizationId: parsed.organizationId,
1086
+ tenantId: parsed.tenantId,
1087
+ catalogProductId: parsed.catalogProductId,
1088
+ catalogVariantId: parsed.catalogVariantId ?? null,
1089
+ defaultUom: parsed.defaultUom,
1090
+ trackLot: parsed.trackLot ?? false,
1091
+ trackSerial: parsed.trackSerial ?? false,
1092
+ trackExpiration: parsed.trackExpiration ?? false,
1093
+ defaultStrategy: parsed.defaultStrategy,
1094
+ reorderPoint: toNumericString(parsed.reorderPoint),
1095
+ safetyStock: toNumericString(parsed.safetyStock),
1096
+ metadata: toJsonValue(parsed.metadata)
1097
+ });
1098
+ await em.persist(profile).flush();
1099
+ void emitWmsEvent("wms.inventory_profile.created", {
1100
+ id: profile.id,
1101
+ profileId: profile.id,
1102
+ catalogProductId: profile.catalogProductId,
1103
+ catalogVariantId: profile.catalogVariantId ?? null,
1104
+ tenantId: profile.tenantId,
1105
+ organizationId: profile.organizationId
1106
+ }).catch(() => void 0);
1107
+ return { profileId: profile.id };
1108
+ },
1109
+ captureAfter: async (_input, result, ctx) => {
1110
+ const em = resolveEm(ctx);
1111
+ return loadInventoryProfileSnapshot(em, ctx, result.profileId);
1112
+ },
1113
+ buildLog: async ({ input, result, ctx, snapshots }) => {
1114
+ const base = await buildCrudLog(ctx, input, result?.profileId ?? null, "wms.audit.inventoryProfile.create", "Create inventory profile", "wms.inventoryProfile");
1115
+ const after = snapshots?.after;
1116
+ return { ...base, snapshotAfter: after, payload: { undo: { after } } };
1117
+ },
1118
+ undo: async ({ logEntry, ctx }) => {
1119
+ const payload = extractUndoPayload(logEntry);
1120
+ const after = payload?.after;
1121
+ if (!after) return;
1122
+ const em = resolveEm(ctx);
1123
+ const record = await findOneWithDecryption(
1124
+ em,
1125
+ ProductInventoryProfile,
1126
+ { id: after.id },
1127
+ void 0,
1128
+ resolveScope(ctx, { tenantId: after.tenantId, organizationId: after.organizationId })
1129
+ );
1130
+ if (!record) return;
1131
+ ensureTenantScope(ctx, record.tenantId);
1132
+ ensureOrganizationScope(ctx, record.organizationId);
1133
+ record.deletedAt = /* @__PURE__ */ new Date();
1134
+ await em.flush();
1135
+ }
1136
+ };
1137
+ const updateProductInventoryProfileCommand = {
1138
+ id: "wms.inventoryProfiles.update",
1139
+ prepare: async (input, ctx) => {
1140
+ const id = requireId(input?.id, "Inventory profile");
1141
+ const em = resolveEm(ctx);
1142
+ const before = await loadInventoryProfileSnapshot(em, ctx, id);
1143
+ return before ? { before } : {};
1144
+ },
1145
+ async execute(rawInput, ctx) {
1146
+ const parsed = productInventoryProfileUpdateSchema.parse(rawInput ?? {});
1147
+ const em = resolveEm(ctx);
1148
+ const profile = await loadProfile(em, ctx, parsed.id);
1149
+ const next = {
1150
+ tenantId: profile.tenantId,
1151
+ organizationId: profile.organizationId,
1152
+ catalogProductId: parsed.catalogProductId ?? profile.catalogProductId,
1153
+ catalogVariantId: parsed.catalogVariantId !== void 0 ? parsed.catalogVariantId ?? null : profile.catalogVariantId ?? null
1154
+ };
1155
+ if (parsed.catalogProductId !== void 0 || parsed.catalogVariantId !== void 0) {
1156
+ await ensureProfileUniqueness(em, next, profile.id);
1157
+ }
1158
+ if (parsed.catalogProductId !== void 0) profile.catalogProductId = parsed.catalogProductId;
1159
+ if (parsed.catalogVariantId !== void 0) profile.catalogVariantId = parsed.catalogVariantId ?? null;
1160
+ if (parsed.defaultUom !== void 0) profile.defaultUom = parsed.defaultUom;
1161
+ if (parsed.trackLot !== void 0) profile.trackLot = parsed.trackLot;
1162
+ if (parsed.trackSerial !== void 0) profile.trackSerial = parsed.trackSerial;
1163
+ if (parsed.trackExpiration !== void 0) profile.trackExpiration = parsed.trackExpiration;
1164
+ if (parsed.defaultStrategy !== void 0) profile.defaultStrategy = parsed.defaultStrategy;
1165
+ if (parsed.reorderPoint !== void 0) profile.reorderPoint = toNumericString(parsed.reorderPoint);
1166
+ if (parsed.safetyStock !== void 0) profile.safetyStock = toNumericString(parsed.safetyStock);
1167
+ if (parsed.metadata !== void 0) profile.metadata = toJsonValue(parsed.metadata);
1168
+ await em.flush();
1169
+ void emitWmsEvent("wms.inventory_profile.updated", {
1170
+ id: profile.id,
1171
+ profileId: profile.id,
1172
+ catalogProductId: profile.catalogProductId,
1173
+ catalogVariantId: profile.catalogVariantId ?? null,
1174
+ tenantId: profile.tenantId,
1175
+ organizationId: profile.organizationId
1176
+ }).catch(() => void 0);
1177
+ return { profileId: profile.id };
1178
+ },
1179
+ captureAfter: async (_input, result, ctx) => {
1180
+ const em = resolveEm(ctx);
1181
+ return loadInventoryProfileSnapshot(em, ctx, result.profileId);
1182
+ },
1183
+ buildLog: async ({ input, result, ctx, snapshots }) => {
1184
+ const base = await buildCrudLog(ctx, input, result?.profileId ?? null, "wms.audit.inventoryProfile.update", "Update inventory profile", "wms.inventoryProfile");
1185
+ const before = snapshots?.before;
1186
+ const after = snapshots?.after;
1187
+ return { ...base, snapshotBefore: before, snapshotAfter: after, payload: { undo: { before, after } } };
1188
+ },
1189
+ undo: async ({ logEntry, ctx }) => {
1190
+ const payload = extractUndoPayload(logEntry);
1191
+ const before = payload?.before;
1192
+ if (!before) return;
1193
+ const em = resolveEm(ctx);
1194
+ let record = await findOneWithDecryption(
1195
+ em,
1196
+ ProductInventoryProfile,
1197
+ { id: before.id },
1198
+ void 0,
1199
+ resolveScope(ctx, { tenantId: before.tenantId, organizationId: before.organizationId })
1200
+ );
1201
+ if (!record) {
1202
+ record = em.create(ProductInventoryProfile, {
1203
+ id: before.id,
1204
+ organizationId: before.organizationId,
1205
+ tenantId: before.tenantId,
1206
+ catalogProductId: before.catalogProductId,
1207
+ catalogVariantId: before.catalogVariantId,
1208
+ defaultUom: before.defaultUom,
1209
+ trackLot: before.trackLot,
1210
+ trackSerial: before.trackSerial,
1211
+ trackExpiration: before.trackExpiration,
1212
+ defaultStrategy: before.defaultStrategy,
1213
+ reorderPoint: before.reorderPoint,
1214
+ safetyStock: before.safetyStock,
1215
+ metadata: before.metadata,
1216
+ createdAt: new Date(before.createdAt),
1217
+ updatedAt: new Date(before.updatedAt)
1218
+ });
1219
+ em.persist(record);
1220
+ } else {
1221
+ ensureTenantScope(ctx, before.tenantId);
1222
+ ensureOrganizationScope(ctx, before.organizationId);
1223
+ restoreInventoryProfileFromSnapshot(em, before, record);
1224
+ }
1225
+ await em.flush();
1226
+ }
1227
+ };
1228
+ const deleteProductInventoryProfileCommand = {
1229
+ id: "wms.inventoryProfiles.delete",
1230
+ prepare: async (input, ctx) => {
1231
+ const id = requireId(input?.id, "Inventory profile");
1232
+ const em = resolveEm(ctx);
1233
+ const before = await loadInventoryProfileSnapshot(em, ctx, id);
1234
+ return before ? { before } : {};
1235
+ },
1236
+ async execute(input, ctx) {
1237
+ const profileId = requireId(input?.id, "Inventory profile");
1238
+ const em = resolveEm(ctx);
1239
+ const profile = await loadProfile(em, ctx, profileId);
1240
+ profile.deletedAt = /* @__PURE__ */ new Date();
1241
+ await em.flush();
1242
+ return { profileId: profile.id };
1243
+ },
1244
+ buildLog: async ({ input, result, ctx, snapshots }) => {
1245
+ const base = await buildCrudLog(ctx, input, result?.profileId ?? null, "wms.audit.inventoryProfile.delete", "Delete inventory profile", "wms.inventoryProfile");
1246
+ const before = snapshots?.before;
1247
+ return { ...base, snapshotBefore: before, payload: { undo: { before } } };
1248
+ },
1249
+ undo: async ({ logEntry, ctx }) => {
1250
+ const payload = extractUndoPayload(logEntry);
1251
+ const before = payload?.before;
1252
+ if (!before) return;
1253
+ const em = resolveEm(ctx);
1254
+ let record = await findOneWithDecryption(
1255
+ em,
1256
+ ProductInventoryProfile,
1257
+ { id: before.id },
1258
+ void 0,
1259
+ resolveScope(ctx, { tenantId: before.tenantId, organizationId: before.organizationId })
1260
+ );
1261
+ if (!record) {
1262
+ record = em.create(ProductInventoryProfile, {
1263
+ id: before.id,
1264
+ organizationId: before.organizationId,
1265
+ tenantId: before.tenantId,
1266
+ catalogProductId: before.catalogProductId,
1267
+ catalogVariantId: before.catalogVariantId,
1268
+ defaultUom: before.defaultUom,
1269
+ trackLot: before.trackLot,
1270
+ trackSerial: before.trackSerial,
1271
+ trackExpiration: before.trackExpiration,
1272
+ defaultStrategy: before.defaultStrategy,
1273
+ reorderPoint: before.reorderPoint,
1274
+ safetyStock: before.safetyStock,
1275
+ metadata: before.metadata,
1276
+ createdAt: new Date(before.createdAt),
1277
+ updatedAt: new Date(before.updatedAt)
1278
+ });
1279
+ em.persist(record);
1280
+ } else {
1281
+ ensureTenantScope(ctx, before.tenantId);
1282
+ ensureOrganizationScope(ctx, before.organizationId);
1283
+ restoreInventoryProfileFromSnapshot(em, before, record);
1284
+ }
1285
+ await em.flush();
1286
+ }
1287
+ };
1288
+ function restoreInventoryLotFromSnapshot(_em, before, record) {
1289
+ record.catalogVariantId = before.catalogVariantId;
1290
+ record.sku = before.sku;
1291
+ record.lotNumber = before.lotNumber;
1292
+ record.batchNumber = before.batchNumber;
1293
+ record.manufacturedAt = dateOrNull(before.manufacturedAt);
1294
+ record.bestBeforeAt = dateOrNull(before.bestBeforeAt);
1295
+ record.expiresAt = dateOrNull(before.expiresAt);
1296
+ record.status = before.status;
1297
+ record.metadata = before.metadata;
1298
+ record.deletedAt = null;
1299
+ }
1300
+ const createInventoryLotCommand = {
1301
+ id: "wms.lots.create",
1302
+ async execute(rawInput, ctx) {
1303
+ const parsed = inventoryLotCreateSchema.parse(rawInput ?? {});
1304
+ ensureTenantScope(ctx, parsed.tenantId);
1305
+ ensureOrganizationScope(ctx, parsed.organizationId);
1306
+ const em = resolveEm(ctx);
1307
+ await ensureLotUniqueness(em, parsed);
1308
+ const lot = em.create(InventoryLot, {
1309
+ organizationId: parsed.organizationId,
1310
+ tenantId: parsed.tenantId,
1311
+ catalogVariantId: parsed.catalogVariantId,
1312
+ sku: parsed.sku,
1313
+ lotNumber: parsed.lotNumber,
1314
+ batchNumber: normalizeOptionalString(parsed.batchNumber),
1315
+ manufacturedAt: parsed.manufacturedAt ?? null,
1316
+ bestBeforeAt: parsed.bestBeforeAt ?? null,
1317
+ expiresAt: parsed.expiresAt ?? null,
1318
+ status: parsed.status ?? "available",
1319
+ metadata: toJsonValue(parsed.metadata)
1320
+ });
1321
+ await em.persist(lot).flush();
1322
+ return { lotId: lot.id };
1323
+ },
1324
+ captureAfter: async (_input, result, ctx) => {
1325
+ const em = resolveEm(ctx);
1326
+ return loadInventoryLotSnapshot(em, ctx, result.lotId);
1327
+ },
1328
+ buildLog: async ({ input, result, ctx, snapshots }) => {
1329
+ const base = await buildCrudLog(ctx, input, result?.lotId ?? null, "wms.audit.lot.create", "Create inventory lot", "wms.inventoryLot");
1330
+ const after = snapshots?.after;
1331
+ return { ...base, snapshotAfter: after, payload: { undo: { after } } };
1332
+ },
1333
+ undo: async ({ logEntry, ctx }) => {
1334
+ const payload = extractUndoPayload(logEntry);
1335
+ const after = payload?.after;
1336
+ if (!after) return;
1337
+ const em = resolveEm(ctx);
1338
+ const record = await findOneWithDecryption(
1339
+ em,
1340
+ InventoryLot,
1341
+ { id: after.id },
1342
+ void 0,
1343
+ resolveScope(ctx, { tenantId: after.tenantId, organizationId: after.organizationId })
1344
+ );
1345
+ if (!record) return;
1346
+ ensureTenantScope(ctx, record.tenantId);
1347
+ ensureOrganizationScope(ctx, record.organizationId);
1348
+ record.deletedAt = /* @__PURE__ */ new Date();
1349
+ await em.flush();
1350
+ }
1351
+ };
1352
+ const updateInventoryLotCommand = {
1353
+ id: "wms.lots.update",
1354
+ prepare: async (input, ctx) => {
1355
+ const id = requireId(input?.id, "Inventory lot");
1356
+ const em = resolveEm(ctx);
1357
+ const before = await loadInventoryLotSnapshot(em, ctx, id);
1358
+ return before ? { before } : {};
1359
+ },
1360
+ async execute(rawInput, ctx) {
1361
+ const parsed = inventoryLotUpdateSchema.parse(rawInput ?? {});
1362
+ const em = resolveEm(ctx);
1363
+ const lot = await loadLot(em, ctx, parsed.id);
1364
+ const nextVariantId = parsed.catalogVariantId ?? lot.catalogVariantId;
1365
+ const nextLotNumber = parsed.lotNumber ?? lot.lotNumber;
1366
+ if (parsed.catalogVariantId !== void 0 || parsed.lotNumber !== void 0) {
1367
+ await ensureLotUniqueness(
1368
+ em,
1369
+ {
1370
+ tenantId: lot.tenantId,
1371
+ organizationId: lot.organizationId,
1372
+ catalogVariantId: nextVariantId,
1373
+ lotNumber: nextLotNumber
1374
+ },
1375
+ lot.id
1376
+ );
1377
+ }
1378
+ if (parsed.catalogVariantId !== void 0) lot.catalogVariantId = parsed.catalogVariantId;
1379
+ if (parsed.sku !== void 0) lot.sku = parsed.sku;
1380
+ if (parsed.lotNumber !== void 0) lot.lotNumber = parsed.lotNumber;
1381
+ if (parsed.batchNumber !== void 0) lot.batchNumber = normalizeOptionalString(parsed.batchNumber);
1382
+ if (parsed.manufacturedAt !== void 0) lot.manufacturedAt = parsed.manufacturedAt ?? null;
1383
+ if (parsed.bestBeforeAt !== void 0) lot.bestBeforeAt = parsed.bestBeforeAt ?? null;
1384
+ if (parsed.expiresAt !== void 0) lot.expiresAt = parsed.expiresAt ?? null;
1385
+ if (parsed.status !== void 0) lot.status = parsed.status;
1386
+ if (parsed.metadata !== void 0) lot.metadata = toJsonValue(parsed.metadata);
1387
+ if (parsed.notes !== void 0) {
1388
+ const existing = lot.metadata && typeof lot.metadata === "object" && !Array.isArray(lot.metadata) ? { ...lot.metadata } : {};
1389
+ const trimmedNotes = parsed.notes.trim();
1390
+ if (trimmedNotes) existing.notes = trimmedNotes;
1391
+ else delete existing.notes;
1392
+ lot.metadata = toJsonValue(existing);
1393
+ }
1394
+ await em.flush();
1395
+ return { lotId: lot.id };
1396
+ },
1397
+ captureAfter: async (_input, result, ctx) => {
1398
+ const em = resolveEm(ctx);
1399
+ return loadInventoryLotSnapshot(em, ctx, result.lotId);
1400
+ },
1401
+ buildLog: async ({ input, result, ctx, snapshots }) => {
1402
+ const base = await buildCrudLog(ctx, input, result?.lotId ?? null, "wms.audit.lot.update", "Update inventory lot", "wms.inventoryLot");
1403
+ const before = snapshots?.before;
1404
+ const after = snapshots?.after;
1405
+ return { ...base, snapshotBefore: before, snapshotAfter: after, payload: { undo: { before, after } } };
1406
+ },
1407
+ undo: async ({ logEntry, ctx }) => {
1408
+ const payload = extractUndoPayload(logEntry);
1409
+ const before = payload?.before;
1410
+ if (!before) return;
1411
+ const em = resolveEm(ctx);
1412
+ let record = await findOneWithDecryption(
1413
+ em,
1414
+ InventoryLot,
1415
+ { id: before.id },
1416
+ void 0,
1417
+ resolveScope(ctx, { tenantId: before.tenantId, organizationId: before.organizationId })
1418
+ );
1419
+ if (!record) {
1420
+ record = em.create(InventoryLot, {
1421
+ id: before.id,
1422
+ organizationId: before.organizationId,
1423
+ tenantId: before.tenantId,
1424
+ catalogVariantId: before.catalogVariantId,
1425
+ sku: before.sku,
1426
+ lotNumber: before.lotNumber,
1427
+ batchNumber: before.batchNumber,
1428
+ manufacturedAt: dateOrNull(before.manufacturedAt),
1429
+ bestBeforeAt: dateOrNull(before.bestBeforeAt),
1430
+ expiresAt: dateOrNull(before.expiresAt),
1431
+ status: before.status,
1432
+ metadata: before.metadata,
1433
+ createdAt: new Date(before.createdAt),
1434
+ updatedAt: new Date(before.updatedAt)
1435
+ });
1436
+ em.persist(record);
1437
+ } else {
1438
+ ensureTenantScope(ctx, before.tenantId);
1439
+ ensureOrganizationScope(ctx, before.organizationId);
1440
+ restoreInventoryLotFromSnapshot(em, before, record);
1441
+ }
1442
+ await em.flush();
1443
+ }
1444
+ };
1445
+ const deleteInventoryLotCommand = {
1446
+ id: "wms.lots.delete",
1447
+ prepare: async (input, ctx) => {
1448
+ const id = requireId(input?.id, "Inventory lot");
1449
+ const em = resolveEm(ctx);
1450
+ const before = await loadInventoryLotSnapshot(em, ctx, id);
1451
+ return before ? { before } : {};
1452
+ },
1453
+ async execute(input, ctx) {
1454
+ const lotId = requireId(input?.id, "Inventory lot");
1455
+ const em = resolveEm(ctx);
1456
+ const lot = await loadLot(em, ctx, lotId);
1457
+ lot.deletedAt = /* @__PURE__ */ new Date();
1458
+ await em.flush();
1459
+ return { lotId: lot.id };
1460
+ },
1461
+ buildLog: async ({ input, result, ctx, snapshots }) => {
1462
+ const base = await buildCrudLog(ctx, input, result?.lotId ?? null, "wms.audit.lot.delete", "Delete inventory lot", "wms.inventoryLot");
1463
+ const before = snapshots?.before;
1464
+ return { ...base, snapshotBefore: before, payload: { undo: { before } } };
1465
+ },
1466
+ undo: async ({ logEntry, ctx }) => {
1467
+ const payload = extractUndoPayload(logEntry);
1468
+ const before = payload?.before;
1469
+ if (!before) return;
1470
+ const em = resolveEm(ctx);
1471
+ let record = await findOneWithDecryption(
1472
+ em,
1473
+ InventoryLot,
1474
+ { id: before.id },
1475
+ void 0,
1476
+ resolveScope(ctx, { tenantId: before.tenantId, organizationId: before.organizationId })
1477
+ );
1478
+ if (!record) {
1479
+ record = em.create(InventoryLot, {
1480
+ id: before.id,
1481
+ organizationId: before.organizationId,
1482
+ tenantId: before.tenantId,
1483
+ catalogVariantId: before.catalogVariantId,
1484
+ sku: before.sku,
1485
+ lotNumber: before.lotNumber,
1486
+ batchNumber: before.batchNumber,
1487
+ manufacturedAt: dateOrNull(before.manufacturedAt),
1488
+ bestBeforeAt: dateOrNull(before.bestBeforeAt),
1489
+ expiresAt: dateOrNull(before.expiresAt),
1490
+ status: before.status,
1491
+ metadata: before.metadata,
1492
+ createdAt: new Date(before.createdAt),
1493
+ updatedAt: new Date(before.updatedAt)
1494
+ });
1495
+ em.persist(record);
1496
+ } else {
1497
+ ensureTenantScope(ctx, before.tenantId);
1498
+ ensureOrganizationScope(ctx, before.organizationId);
1499
+ restoreInventoryLotFromSnapshot(em, before, record);
1500
+ }
1501
+ await em.flush();
1502
+ }
1503
+ };
1504
+ registerCommand(createWarehouseCommand);
1505
+ registerCommand(updateWarehouseCommand);
1506
+ registerCommand(deleteWarehouseCommand);
1507
+ registerCommand(createWarehouseZoneCommand);
1508
+ registerCommand(updateWarehouseZoneCommand);
1509
+ registerCommand(deleteWarehouseZoneCommand);
1510
+ registerCommand(createWarehouseLocationCommand);
1511
+ registerCommand(updateWarehouseLocationCommand);
1512
+ registerCommand(deleteWarehouseLocationCommand);
1513
+ registerCommand(createProductInventoryProfileCommand);
1514
+ registerCommand(updateProductInventoryProfileCommand);
1515
+ registerCommand(deleteProductInventoryProfileCommand);
1516
+ registerCommand(createInventoryLotCommand);
1517
+ registerCommand(updateInventoryLotCommand);
1518
+ registerCommand(deleteInventoryLotCommand);
1519
+ //# sourceMappingURL=configuration.js.map