@rytass/wms-module-core 0.4.0 → 0.6.0

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 (80) hide show
  1. package/README.md +69 -7
  2. package/docs/api-reference.md +58 -10
  3. package/docs/custom-entities.md +105 -68
  4. package/index.cjs.js +1263 -151
  5. package/index.js +10 -0
  6. package/lib/constants/index.d.ts +2 -0
  7. package/lib/constants/inventory-count-status.d.ts +19 -0
  8. package/lib/constants/inventory-count-status.js +22 -0
  9. package/lib/constants/operations.d.ts +34 -0
  10. package/lib/constants/operations.js +66 -0
  11. package/lib/constants/stock-status.d.ts +2 -0
  12. package/lib/constants/stock-status.js +4 -0
  13. package/lib/core.module.js +91 -30
  14. package/lib/events/index.d.ts +1 -0
  15. package/lib/events/inventory-count-lock.subscriber.d.ts +38 -0
  16. package/lib/events/inventory-count-lock.subscriber.js +98 -0
  17. package/lib/events/stock-change.subscriber.js +6 -0
  18. package/lib/events/wms-event.types.d.ts +2 -2
  19. package/lib/models/extensions/allocate-inventory-order/allocate-inventory-order-item.entity.js +1 -0
  20. package/lib/models/extensions/hold-inventory-order/hold-inventory-order-item.entity.js +1 -0
  21. package/lib/models/extensions/index.d.ts +3 -0
  22. package/lib/models/extensions/inventory-count-order/inventory-count-order-change.entity.d.ts +20 -0
  23. package/lib/models/extensions/inventory-count-order/inventory-count-order-change.entity.js +50 -0
  24. package/lib/models/extensions/inventory-count-order/inventory-count-order-item.entity.d.ts +39 -0
  25. package/lib/models/extensions/inventory-count-order/inventory-count-order-item.entity.js +123 -0
  26. package/lib/models/extensions/inventory-count-order/inventory-count-order.entity.d.ts +50 -0
  27. package/lib/models/extensions/inventory-count-order/inventory-count-order.entity.js +155 -0
  28. package/lib/models/extensions/merge-batch-order/merge-batch-order-item.entity.js +1 -0
  29. package/lib/models/extensions/quality-inspection-order/quality-inspection-order-item.entity.js +1 -0
  30. package/lib/models/extensions/reclassify-inventory-order/reclassify-inventory-order-item.entity.js +1 -0
  31. package/lib/models/extensions/scrape-order/scrape-order-item.entity.js +1 -0
  32. package/lib/models/extensions/split-batch-order/split-batch-order-item.entity.js +1 -0
  33. package/lib/models/extensions/transfer-customer-order/transfer-customer-order-item.entity.js +1 -0
  34. package/lib/models/extensions/transfer-order/transfer-order-item.entity.js +1 -0
  35. package/lib/models/extensions/transfer-vendor-order/transfer-vendor-order-item.entity.js +1 -0
  36. package/lib/models/index.d.ts +1 -0
  37. package/lib/models/metadata/index.d.ts +1 -0
  38. package/lib/models/metadata/order.entity.d.ts +9 -0
  39. package/lib/models/metadata/order.entity.js +5 -0
  40. package/lib/models/models.module.js +22 -8
  41. package/lib/models/wms-entities.d.ts +45 -0
  42. package/lib/models/wms-entities.js +82 -0
  43. package/lib/services/allocate-inventory-order.service.d.ts +3 -1
  44. package/lib/services/allocate-inventory-order.service.js +13 -6
  45. package/lib/services/batch.service.js +8 -3
  46. package/lib/services/hold-inventory-order.service.d.ts +3 -1
  47. package/lib/services/hold-inventory-order.service.js +13 -6
  48. package/lib/services/index.d.ts +1 -0
  49. package/lib/services/inventory-count-order.service.d.ts +143 -0
  50. package/lib/services/inventory-count-order.service.js +521 -0
  51. package/lib/services/loader.service.js +5 -0
  52. package/lib/services/location.service.js +5 -0
  53. package/lib/services/material.service.d.ts +1 -15
  54. package/lib/services/material.service.js +5 -20
  55. package/lib/services/merge-batch-order.service.d.ts +3 -1
  56. package/lib/services/merge-batch-order.service.js +16 -9
  57. package/lib/services/quality-inspection-order.service.d.ts +3 -1
  58. package/lib/services/quality-inspection-order.service.js +15 -8
  59. package/lib/services/receive-inventory-order.service.d.ts +3 -1
  60. package/lib/services/receive-inventory-order.service.js +18 -11
  61. package/lib/services/reclassify-inventory-order.service.d.ts +3 -1
  62. package/lib/services/reclassify-inventory-order.service.js +16 -9
  63. package/lib/services/scrape.service.d.ts +3 -1
  64. package/lib/services/scrape.service.js +12 -5
  65. package/lib/services/ship-inventory-order.service.d.ts +3 -1
  66. package/lib/services/ship-inventory-order.service.js +21 -14
  67. package/lib/services/split-batch-order.service.d.ts +3 -1
  68. package/lib/services/split-batch-order.service.js +16 -9
  69. package/lib/services/stock.service.d.ts +3 -1
  70. package/lib/services/stock.service.js +17 -10
  71. package/lib/services/transfer-customer-order.service.d.ts +3 -1
  72. package/lib/services/transfer-customer-order.service.js +15 -8
  73. package/lib/services/transfer-order.service.d.ts +3 -1
  74. package/lib/services/transfer-order.service.js +14 -7
  75. package/lib/services/transfer-vendor-order.service.d.ts +3 -1
  76. package/lib/services/transfer-vendor-order.service.js +15 -8
  77. package/lib/services/unit.service.js +5 -0
  78. package/lib/services/warehouse-map.service.d.ts +14 -2
  79. package/lib/services/warehouse-map.service.js +21 -9
  80. package/package.json +2 -2
package/index.js CHANGED
@@ -7,9 +7,11 @@ export { LoaderUnitEntity, LoaderUnitEntityClass } from './lib/models/metadata/l
7
7
  export { LocationEntity, LocationEntityClass, WarehouseMapEntity } from './lib/models/metadata/location.entity.js';
8
8
  export { MaterialEntity, MaterialEntityClass } from './lib/models/metadata/material.entity.js';
9
9
  export { MaterialUnitEntity, MaterialUnitEntityClass } from './lib/models/metadata/material-unit.entity.js';
10
+ export { OrderEntity } from './lib/models/metadata/order.entity.js';
10
11
  export { SaleOrderEntity, SaleOrderEntityClass } from './lib/models/metadata/sale-order.entity.js';
11
12
  export { StockEntity, StockEntityClass } from './lib/models/metadata/stock.entity.js';
12
13
  export { UnitEntity, UnitEntityClass } from './lib/models/metadata/unit.entity.js';
14
+ export { WmsEntities } from './lib/models/wms-entities.js';
13
15
  export { ReceiveInventoryOrderItemEntity, ReceiveInventoryOrderItemEntityClass } from './lib/models/extensions/receive-inventory-order/receive-inventory-order-item.entity.js';
14
16
  export { ReceiveInventoryOrderEntity, ReceiveInventoryOrderEntityClass } from './lib/models/extensions/receive-inventory-order/receive-inventory-order.entity.js';
15
17
  export { ReceiveInventoryOrderChangeEntity, ReceiveInventoryOrderChangeEntityClass } from './lib/models/extensions/receive-inventory-order/receive-inventory-order-change.entity.js';
@@ -47,6 +49,9 @@ export { TransferCustomerOrderChangeEntity, TransferCustomerOrderChangeEntityCla
47
49
  export { ScrapeOrderEntity, ScrapeOrderEntityClass } from './lib/models/extensions/scrape-order/scrape-order.entity.js';
48
50
  export { ScrapeOrderItemEntity, ScrapeOrderItemEntityClass } from './lib/models/extensions/scrape-order/scrape-order-item.entity.js';
49
51
  export { ScrapeOrderChangeEntity, ScrapeOrderChangeEntityClass } from './lib/models/extensions/scrape-order/scrape-order-change.entity.js';
52
+ export { InventoryCountOrderEntity, InventoryCountOrderEntityClass, InventoryCountOrderLocationEntity, InventoryCountOrderLocationEntityClass } from './lib/models/extensions/inventory-count-order/inventory-count-order.entity.js';
53
+ export { InventoryCountOrderItemEntity, InventoryCountOrderItemEntityClass } from './lib/models/extensions/inventory-count-order/inventory-count-order-item.entity.js';
54
+ export { InventoryCountOrderChangeEntity, InventoryCountOrderChangeEntityClass } from './lib/models/extensions/inventory-count-order/inventory-count-order-change.entity.js';
50
55
  export { StockService } from './lib/services/stock.service.js';
51
56
  export { LoaderService } from './lib/services/loader.service.js';
52
57
  export { MaterialService } from './lib/services/material.service.js';
@@ -65,14 +70,19 @@ export { TransferVendorOrderService } from './lib/services/transfer-vendor-order
65
70
  export { QualityInspectionOrderService } from './lib/services/quality-inspection-order.service.js';
66
71
  export { TransferCustomerOrderService } from './lib/services/transfer-customer-order.service.js';
67
72
  export { SaleOrderService } from './lib/services/sale-order.service.js';
73
+ export { InventoryCountOrderService } from './lib/services/inventory-count-order.service.js';
68
74
  export { ScrapeService } from './lib/services/scrape.service.js';
69
75
  export { WarehouseMapDataService } from './lib/services/warehouse-map.service.js';
70
76
  export { HelperService } from './lib/services/helper.service.js';
71
77
  export { WMS_CORE_MODULE_OPTIONS } from './lib/typings/core-module-options.js';
72
78
  export { ReceiveInventoryOrderStatus } from './lib/typings/find-receive-inventory-order.input.js';
73
79
  export { ShipInventoryOrderStatus } from './lib/typings/find-ship-inventory-order.input.js';
80
+ export { InventoryCountStatus } from './lib/constants/inventory-count-status.js';
81
+ export { WMS_OPERATION_NAMES } from './lib/constants/operations.js';
74
82
  export { ChangeType, StockStatus } from './lib/constants/stock-status.js';
75
83
  export { WMS_ACTOR_EXTRACTOR, WMS_EVENT_EMITTER_BRIDGE, WMS_EVENT_OPTIONS, WMS_STOCK_ENRICH } from './lib/events/wms-event.tokens.js';
76
84
  export { WmsEventCollector } from './lib/events/wms-event.collector.js';
77
85
  export { WmsEventDispatcher } from './lib/events/wms-event.dispatcher.js';
78
86
  export { collectToScope, runInDispatchScope } from './lib/events/dispatch-scope.js';
87
+ export { InventoryCountLockSubscriber } from './lib/events/inventory-count-lock.subscriber.js';
88
+ export { OrderEntity as OrderEntityClass } from '@rytass/wms-base-nestjs-module';
@@ -1 +1,3 @@
1
+ export * from './inventory-count-status';
2
+ export * from './operations';
1
3
  export * from './stock-status';
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The lifecycle of an inventory count order, matching the four states the
3
+ * design shows on the list page.
4
+ *
5
+ * Only `COUNTING` accepts counted quantities, and only `COUNTING` locks the
6
+ * scoped locations; `COMPLETED` is reached exclusively by settling the variance,
7
+ * so an order that was counted but never settled stays `COUNTING` for as long as
8
+ * it takes.
9
+ */
10
+ export declare enum InventoryCountStatus {
11
+ /** 尚未執行 — created, scope fixed, nothing counted, nothing locked. */
12
+ PENDING = "PENDING",
13
+ /** 盤點中 — book quantities snapshotted and the scoped locations locked. */
14
+ COUNTING = "COUNTING",
15
+ /** 已完成 — the variance was settled and stock adjusted. */
16
+ COMPLETED = "COMPLETED",
17
+ /** 已取消 — abandoned from either PENDING or COUNTING; nothing adjusted. */
18
+ CANCELLED = "CANCELLED"
19
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * The lifecycle of an inventory count order, matching the four states the
3
+ * design shows on the list page.
4
+ *
5
+ * Only `COUNTING` accepts counted quantities, and only `COUNTING` locks the
6
+ * scoped locations; `COMPLETED` is reached exclusively by settling the variance,
7
+ * so an order that was counted but never settled stays `COUNTING` for as long as
8
+ * it takes.
9
+ */
10
+ var InventoryCountStatus;
11
+ (function (InventoryCountStatus) {
12
+ /** 尚未執行 — created, scope fixed, nothing counted, nothing locked. */
13
+ InventoryCountStatus["PENDING"] = "PENDING";
14
+ /** 盤點中 — book quantities snapshotted and the scoped locations locked. */
15
+ InventoryCountStatus["COUNTING"] = "COUNTING";
16
+ /** 已完成 — the variance was settled and stock adjusted. */
17
+ InventoryCountStatus["COMPLETED"] = "COMPLETED";
18
+ /** 已取消 — abandoned from either PENDING or COUNTING; nothing adjusted. */
19
+ InventoryCountStatus["CANCELLED"] = "CANCELLED";
20
+ })(InventoryCountStatus || (InventoryCountStatus = {}));
21
+
22
+ export { InventoryCountStatus };
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @file operations.ts
3
+ * The canonical list of every root operation this package exposes, named the
4
+ * same way `@WmsOperation` declares it: `` `${resource}:${action}` ``.
5
+ *
6
+ * One name, three uses
7
+ * --------------------
8
+ * 1. The `requirement` a consumer's `authChecker` receives.
9
+ * 2. The key for `enabledOperations` / `disabledOperations` — the deployment
10
+ * switch that decides whether a resolver is registered in the schema at all.
11
+ * 3. The value `@rytass/wms-module-react`'s `INVENTORY_ACTION_OPERATIONS` maps
12
+ * each UI action to, so a host can derive its frontend and backend switches
13
+ * from one constant.
14
+ *
15
+ * These strings are internal metadata — they never appear in the GraphQL schema
16
+ * — so refining one is not a GraphQL breaking change, but it *is* a breaking
17
+ * change for consumers keying off it. Treat the union as public API.
18
+ *
19
+ * **Invariant:** this union must list exactly the requirements declared by
20
+ * `@WmsOperation` across `libs/graphql`, no more and no less.
21
+ * `libs/graphql/src/lib/operation-names.invariant.spec.ts` fails otherwise, so a
22
+ * new resolver that forgets to add its name here cannot merge.
23
+ *
24
+ * Note that a name is **not** unique per schema field: several queries share one
25
+ * requirement (`loader:read` covers 6 fields, `stock:read` 5, `material:read` and
26
+ * `loader:create` 3 each, and 7 more cover 2). Disabling a name disables every
27
+ * field carrying it — use `disabledFields` when you need finer granularity.
28
+ */
29
+ export type WmsOperationName = 'allocate-inventory-order:create' | 'allocate-inventory-order:deallocate' | 'allocate-inventory-order:read' | 'allocate-inventory-order:update' | 'batch:merge' | 'batch:read' | 'batch:split' | 'hold-inventory-order:create' | 'hold-inventory-order:read' | 'hold-inventory-order:release' | 'hold-inventory-order:update' | 'inventory-count-order:add-item' | 'inventory-count-order:cancel' | 'inventory-count-order:count' | 'inventory-count-order:create' | 'inventory-count-order:read' | 'inventory-count-order:remove-item' | 'inventory-count-order:reset' | 'inventory-count-order:settle' | 'inventory-count-order:start' | 'inventory-count-order:update' | 'loader:create' | 'loader:read' | 'loader:update' | 'location:create' | 'location:read' | 'location:update' | 'material:create' | 'material:read' | 'material:update' | 'quality-inspection-order:inspect' | 'receive-inventory-order:create' | 'receive-inventory-order:read' | 'receive-inventory-order:receive' | 'receive-inventory-order:update' | 'reclassify-inventory-order:reclassify' | 'sale-order:read' | 'scrape-order:scrape' | 'ship-inventory-order:cancel' | 'ship-inventory-order:create' | 'ship-inventory-order:read' | 'ship-inventory-order:ship' | 'ship-inventory-order:update' | 'stock:read' | 'transfer-customer-order:create' | 'transfer-customer-order:return' | 'transfer-customer-order:update' | 'transfer-order:cancel' | 'transfer-order:create' | 'transfer-order:read' | 'transfer-order:transfer' | 'transfer-order:update' | 'transfer-vendor-order:create' | 'transfer-vendor-order:return' | 'transfer-vendor-order:update' | 'unit:read' | 'warehouse-map:read' | 'warehouse-map:update';
30
+ /**
31
+ * Runtime counterpart of {@link WmsOperationName}. Iterate it to build a UI, to
32
+ * diff against a host's own allow-list, or to assert the invariant above.
33
+ */
34
+ export declare const WMS_OPERATION_NAMES: readonly WmsOperationName[];
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Runtime counterpart of {@link WmsOperationName}. Iterate it to build a UI, to
3
+ * diff against a host's own allow-list, or to assert the invariant above.
4
+ */
5
+ const WMS_OPERATION_NAMES = [
6
+ 'allocate-inventory-order:create',
7
+ 'allocate-inventory-order:deallocate',
8
+ 'allocate-inventory-order:read',
9
+ 'allocate-inventory-order:update',
10
+ 'batch:merge',
11
+ 'batch:read',
12
+ 'batch:split',
13
+ 'hold-inventory-order:create',
14
+ 'hold-inventory-order:read',
15
+ 'hold-inventory-order:release',
16
+ 'hold-inventory-order:update',
17
+ 'inventory-count-order:add-item',
18
+ 'inventory-count-order:cancel',
19
+ 'inventory-count-order:count',
20
+ 'inventory-count-order:create',
21
+ 'inventory-count-order:read',
22
+ 'inventory-count-order:remove-item',
23
+ 'inventory-count-order:reset',
24
+ 'inventory-count-order:settle',
25
+ 'inventory-count-order:start',
26
+ 'inventory-count-order:update',
27
+ 'loader:create',
28
+ 'loader:read',
29
+ 'loader:update',
30
+ 'location:create',
31
+ 'location:read',
32
+ 'location:update',
33
+ 'material:create',
34
+ 'material:read',
35
+ 'material:update',
36
+ 'quality-inspection-order:inspect',
37
+ 'receive-inventory-order:create',
38
+ 'receive-inventory-order:read',
39
+ 'receive-inventory-order:receive',
40
+ 'receive-inventory-order:update',
41
+ 'reclassify-inventory-order:reclassify',
42
+ 'sale-order:read',
43
+ 'scrape-order:scrape',
44
+ 'ship-inventory-order:cancel',
45
+ 'ship-inventory-order:create',
46
+ 'ship-inventory-order:read',
47
+ 'ship-inventory-order:ship',
48
+ 'ship-inventory-order:update',
49
+ 'stock:read',
50
+ 'transfer-customer-order:create',
51
+ 'transfer-customer-order:return',
52
+ 'transfer-customer-order:update',
53
+ 'transfer-order:cancel',
54
+ 'transfer-order:create',
55
+ 'transfer-order:read',
56
+ 'transfer-order:transfer',
57
+ 'transfer-order:update',
58
+ 'transfer-vendor-order:create',
59
+ 'transfer-vendor-order:return',
60
+ 'transfer-vendor-order:update',
61
+ 'unit:read',
62
+ 'warehouse-map:read',
63
+ 'warehouse-map:update',
64
+ ];
65
+
66
+ export { WMS_OPERATION_NAMES };
@@ -26,5 +26,7 @@ export declare enum ChangeType {
26
26
  ALLOCATED = "ALLOCATED",
27
27
  DEALLOCATED = "DEALLOCATED",
28
28
  RECLASSIFIED = "RECLASSIFIED",
29
+ COUNT_GAINED = "COUNT_GAINED",
30
+ COUNT_LOST = "COUNT_LOST",
29
31
  SCRAPED = "SCRAPED"
30
32
  }
@@ -44,6 +44,10 @@ var ChangeType;
44
44
  ChangeType["DEALLOCATED"] = "DEALLOCATED";
45
45
  // reclassify-inventory-order-change.entity.ts
46
46
  ChangeType["RECLASSIFIED"] = "RECLASSIFIED";
47
+ // inventory-count-order-change.entity.ts, change > 0
48
+ ChangeType["COUNT_GAINED"] = "COUNT_GAINED";
49
+ // inventory-count-order-change.entity.ts, change < 0
50
+ ChangeType["COUNT_LOST"] = "COUNT_LOST";
47
51
  // scrape-order-change.entity.ts
48
52
  ChangeType["SCRAPED"] = "SCRAPED";
49
53
  })(ChangeType || (ChangeType = {}));
@@ -1,8 +1,14 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { Module } from '@nestjs/common';
3
- import { WMSBaseModule, PROVIDE_MATERIAL_ENTITY } from '@rytass/wms-base-nestjs-module';
3
+ import { PROVIDE_MATERIAL_ENTITY, PROVIDE_BATCH_ENTITY, PROVIDE_LOCATION_ENTITY, PROVIDE_STOCK_ENTITY, OrderEntity, PROVIDE_ORDER_ENTITY, PROVIDE_WAREHOUSE_MAP_ENTITY, WMSBaseModule } from '@rytass/wms-base-nestjs-module';
4
+ import { WarehouseMapEntity } from '@rytass/wms-base-nestjs-module/models/warehouse-map.entity';
4
5
  import { ModelsModule } from './models/models.module.js';
5
- import { MaterialEntity, MaterialEntityClass } from './models/metadata/material.entity.js';
6
+ import { WmsEntities } from './models/wms-entities.js';
7
+ import { BatchEntityClass, BatchEntity } from './models/metadata/batch.entity.js';
8
+ import { LocationEntityClass, LocationEntity, WarehouseMapEntity as WarehouseMapEntity$1 } from './models/metadata/location.entity.js';
9
+ import { MaterialEntityClass, MaterialEntity } from './models/metadata/material.entity.js';
10
+ import { OrderEntity as OrderEntity$1 } from './models/metadata/order.entity.js';
11
+ import { StockEntityClass, StockEntity } from './models/metadata/stock.entity.js';
6
12
  import { StockService } from './services/stock.service.js';
7
13
  import { LoaderService } from './services/loader.service.js';
8
14
  import { MaterialService } from './services/material.service.js';
@@ -21,43 +27,92 @@ import { TransferVendorOrderService } from './services/transfer-vendor-order.ser
21
27
  import { QualityInspectionOrderService } from './services/quality-inspection-order.service.js';
22
28
  import { TransferCustomerOrderService } from './services/transfer-customer-order.service.js';
23
29
  import { SaleOrderService } from './services/sale-order.service.js';
30
+ import { InventoryCountOrderService } from './services/inventory-count-order.service.js';
24
31
  import { ScrapeService } from './services/scrape.service.js';
25
32
  import { WarehouseMapDataService } from './services/warehouse-map.service.js';
26
33
  import { HelperService } from './services/helper.service.js';
27
34
  import { StockChangeSubscriber } from './events/stock-change.subscriber.js';
35
+ import { InventoryCountLockSubscriber } from './events/inventory-count-lock.subscriber.js';
28
36
  import { WMS_STOCK_ENRICH } from './events/wms-event.tokens.js';
29
37
  import { WMS_CORE_MODULE_OPTIONS } from './typings/core-module-options.js';
30
38
 
31
39
  var WmsModuleCoreModule_1;
32
40
  /**
33
- * Binds the `MaterialEntity` Symbol to the entity the consumer configured
34
- * through `WMSBaseModuleOptions.materialEntity`, falling back to the built-in
35
- * `MaterialEntityClass`.
41
+ * The entity Symbols a consumer can rebind, each paired with the base module's
42
+ * token carrying their replacement and the class it falls back to.
36
43
  *
37
- * This is the single seam that makes the option real above the base layer.
38
- * `PROVIDE_MATERIAL_ENTITY` is resolved by `WMSBaseModule` from its own options
39
- * — so it works identically for `forRoot` and for an async factory — and every
40
- * service, DataLoader and query in this package reads the entity through this
41
- * Symbol rather than importing the class. Rebinding here therefore swaps the
42
- * material entity everywhere at once, and cannot drift from the repository the
43
- * base module resolved: both are derived from the same option.
44
+ * These Symbols are how the whole package reaches an entity — no service,
45
+ * DataLoader or query imports the class directly — so rebinding one here swaps
46
+ * that entity everywhere at once. Resolution happens at injection time, from
47
+ * tokens `WMSBaseModule` derives from its own options, which means an override
48
+ * may arrive from `forRoot` or from an async options factory alike and cannot
49
+ * drift from the repository the base module resolved: both come from one option.
44
50
  */
45
- const MaterialEntityProvider = {
46
- provide: MaterialEntity,
51
+ const OVERRIDABLE_ENTITIES = [
52
+ {
53
+ option: 'materialEntity',
54
+ token: MaterialEntity,
55
+ provide: PROVIDE_MATERIAL_ENTITY,
56
+ fallback: MaterialEntityClass,
57
+ requires: 'description / baseUnit / isActive',
58
+ },
59
+ {
60
+ option: 'batchEntity',
61
+ token: BatchEntity,
62
+ provide: PROVIDE_BATCH_ENTITY,
63
+ fallback: BatchEntityClass,
64
+ },
65
+ {
66
+ option: 'locationEntity',
67
+ token: LocationEntity,
68
+ provide: PROVIDE_LOCATION_ENTITY,
69
+ fallback: LocationEntityClass,
70
+ requires: 'description / isActive',
71
+ },
72
+ {
73
+ option: 'stockEntity',
74
+ token: StockEntity,
75
+ provide: PROVIDE_STOCK_ENTITY,
76
+ fallback: StockEntityClass,
77
+ requires: 'loaderId / rollbackedAt',
78
+ },
79
+ {
80
+ option: 'orderEntity',
81
+ token: OrderEntity$1,
82
+ provide: PROVIDE_ORDER_ENTITY,
83
+ fallback: OrderEntity,
84
+ },
85
+ {
86
+ option: 'warehouseMapEntity',
87
+ token: WarehouseMapEntity$1,
88
+ provide: PROVIDE_WAREHOUSE_MAP_ENTITY,
89
+ fallback: WarehouseMapEntity,
90
+ },
91
+ ];
92
+ const entityProviders = OVERRIDABLE_ENTITIES.map(({ option, token, provide, fallback, requires }) => ({
93
+ provide: token,
47
94
  useFactory: (configured) => {
48
- if (!configured)
49
- return MaterialEntityClass;
50
- // `WMSBaseModuleOptions` only asks for a child of the base `MaterialEntity`,
51
- // but this package's services and its `MaterialDto` read `description`,
52
- // `baseUnit` and `isActive` — the columns `MaterialEntityClass` adds. A
53
- // sibling would type-check against the base contract and then fail as a
54
- // missing column at query time, which is a far worse error to debug.
55
- if (!(configured.prototype instanceof MaterialEntityClass))
56
- throw new Error(`WmsModuleCoreModule: materialEntity "${configured.name}" must extend MaterialEntityClass (a @ChildEntity() of it). @rytass/wms-module-core reads description / baseUnit / isActive from it, which the base MaterialEntity does not declare.`);
95
+ if (!configured || configured === fallback)
96
+ return fallback;
97
+ // `WMSBaseModuleOptions` only asks for a child of the *base* entity, but
98
+ // this package reads the columns its own subclass adds. TypeScript's
99
+ // structural typing happily accepts a sibling that never declares them,
100
+ // which then surfaces as a missing column mid-query — a much worse error
101
+ // to work backwards from than this one.
102
+ if (!(configured.prototype instanceof fallback))
103
+ throw new Error(`WmsModuleCoreModule: ${option} "${configured.name}" must extend ${fallback.name} (a @ChildEntity() of it)${requires
104
+ ? `. @rytass/wms-module-core reads ${requires} from it, which the base entity does not declare`
105
+ : ''}.`);
57
106
  return configured;
58
107
  },
59
- inject: [PROVIDE_MATERIAL_ENTITY],
60
- };
108
+ inject: [provide],
109
+ }));
110
+ /** Bound after {@link entityProviders}, since it reads every one of them. */
111
+ const resolvedEntityProviders = [...entityProviders, WmsEntities];
112
+ const exportedEntityTokens = [
113
+ ...OVERRIDABLE_ENTITIES.map(({ token }) => token),
114
+ WmsEntities,
115
+ ];
61
116
  const exportDependencies = [
62
117
  LoaderService,
63
118
  MaterialService,
@@ -78,6 +133,7 @@ const exportDependencies = [
78
133
  MergeBatchOrderService,
79
134
  ReclassifyInventoryOrderService,
80
135
  ScrapeService,
136
+ InventoryCountOrderService,
81
137
  QualityInspectionOrderService,
82
138
  WarehouseMapDataService,
83
139
  ];
@@ -90,15 +146,19 @@ let WmsModuleCoreModule = WmsModuleCoreModule_1 = class WmsModuleCoreModule {
90
146
  providers: [
91
147
  ...exportDependencies,
92
148
  StockChangeSubscriber,
93
- MaterialEntityProvider,
149
+ InventoryCountLockSubscriber,
150
+ ...resolvedEntityProviders,
94
151
  { provide: WMS_CORE_MODULE_OPTIONS, useValue: coreOptions },
95
- { provide: WMS_STOCK_ENRICH, useValue: !!coreOptions.enrichStockEvents },
152
+ {
153
+ provide: WMS_STOCK_ENRICH,
154
+ useValue: !!coreOptions.enrichStockEvents,
155
+ },
96
156
  ],
97
157
  exports: [
98
158
  ...exportDependencies,
99
159
  coreModule,
100
160
  ModelsModule,
101
- MaterialEntity,
161
+ ...exportedEntityTokens,
102
162
  WMS_CORE_MODULE_OPTIONS,
103
163
  ],
104
164
  };
@@ -112,7 +172,8 @@ let WmsModuleCoreModule = WmsModuleCoreModule_1 = class WmsModuleCoreModule {
112
172
  providers: [
113
173
  ...exportDependencies,
114
174
  StockChangeSubscriber,
115
- MaterialEntityProvider,
175
+ InventoryCountLockSubscriber,
176
+ ...resolvedEntityProviders,
116
177
  {
117
178
  provide: WMS_CORE_MODULE_OPTIONS,
118
179
  useValue: {
@@ -127,7 +188,7 @@ let WmsModuleCoreModule = WmsModuleCoreModule_1 = class WmsModuleCoreModule {
127
188
  ...exportDependencies,
128
189
  coreModule,
129
190
  ModelsModule,
130
- MaterialEntity,
191
+ ...exportedEntityTokens,
131
192
  WMS_CORE_MODULE_OPTIONS,
132
193
  ],
133
194
  };
@@ -3,3 +3,4 @@ export { WmsEventCollector } from './wms-event.collector';
3
3
  export { WmsEventDispatcher } from './wms-event.dispatcher';
4
4
  export { collectToScope, runInDispatchScope, type DispatchScopeResult, } from './dispatch-scope';
5
5
  export type { WmsActor, WmsActorExtractor, WmsAfterEventContext, WmsAfterListener, WmsAfterListenerMap, WmsBeforeHook, WmsBeforeHookContext, WmsBeforeHookMap, WmsDomainEvent, WmsEventEmitterBridge, WmsEventModuleOptions, WmsEventName, WmsEventPayloadMap, WmsOperationEventName, WmsOperationEventPayload, WmsStockEventName, WmsStockEventPayload, } from './wms-event.types';
6
+ export * from './inventory-count-lock.subscriber';
@@ -0,0 +1,38 @@
1
+ import { DataSource, type EntitySubscriberInterface, type InsertEvent } from 'typeorm';
2
+ /**
3
+ * Refuse stock movements in locations a running inventory count has locked.
4
+ *
5
+ * Starting a count snapshots the ledger for its scope. That snapshot is what the
6
+ * variance is measured against, so it has to stay true: if a transfer or a
7
+ * shipment moved stock in the meantime, settlement would post the difference
8
+ * between "what we counted" and "what the books said an hour ago" — silently
9
+ * writing off someone else's legitimate movement.
10
+ *
11
+ * Why a subscriber rather than a check in each service
12
+ * ---------------------------------------------------
13
+ * Every stock movement in this package, without exception, ends in a row on some
14
+ * `*_order_change` table — that is the same property `StockChangeSubscriber`
15
+ * relies on to collect `stock.*` events without touching a single service. Doing
16
+ * the lock check at the same choke point means:
17
+ *
18
+ * - the twelve domain services stay untouched;
19
+ * - any future path that writes a change row is covered automatically;
20
+ * - a consumer calling a service **directly**, bypassing the resolvers, is
21
+ * covered too — which is exactly the case that matters, because a host with
22
+ * its own inbound flow does precisely that.
23
+ *
24
+ * The check costs one indexed lookup per change row. That is deliberate: caching
25
+ * the locked set would open a window right after `start()` in which a movement
26
+ * slips through, and a lock with a window is not a lock.
27
+ */
28
+ export declare class InventoryCountLockSubscriber implements EntitySubscriberInterface {
29
+ constructor(dataSource: DataSource);
30
+ beforeInsert(event: InsertEvent<unknown>): Promise<void>;
31
+ /**
32
+ * The location the change ultimately touches, taken from the change's order
33
+ * item. `beforeInsert` may see the relation either already flattened to
34
+ * `itemId` or still as the item object, depending on how the caller built the
35
+ * row, so both are accepted.
36
+ */
37
+ private resolveLocationId;
38
+ }
@@ -0,0 +1,98 @@
1
+ import { __decorate, __param, __metadata } from 'tslib';
2
+ import { Injectable, ConflictException } from '@nestjs/common';
3
+ import { InjectDataSource } from '@nestjs/typeorm';
4
+ import { DataSource } from 'typeorm';
5
+
6
+ /** The count's own settlement writes here; it must never lock itself out. */
7
+ const INVENTORY_COUNT_CHANGE_TABLE = 'inventory_count_order_changes';
8
+ /**
9
+ * Refuse stock movements in locations a running inventory count has locked.
10
+ *
11
+ * Starting a count snapshots the ledger for its scope. That snapshot is what the
12
+ * variance is measured against, so it has to stay true: if a transfer or a
13
+ * shipment moved stock in the meantime, settlement would post the difference
14
+ * between "what we counted" and "what the books said an hour ago" — silently
15
+ * writing off someone else's legitimate movement.
16
+ *
17
+ * Why a subscriber rather than a check in each service
18
+ * ---------------------------------------------------
19
+ * Every stock movement in this package, without exception, ends in a row on some
20
+ * `*_order_change` table — that is the same property `StockChangeSubscriber`
21
+ * relies on to collect `stock.*` events without touching a single service. Doing
22
+ * the lock check at the same choke point means:
23
+ *
24
+ * - the twelve domain services stay untouched;
25
+ * - any future path that writes a change row is covered automatically;
26
+ * - a consumer calling a service **directly**, bypassing the resolvers, is
27
+ * covered too — which is exactly the case that matters, because a host with
28
+ * its own inbound flow does precisely that.
29
+ *
30
+ * The check costs one indexed lookup per change row. That is deliberate: caching
31
+ * the locked set would open a window right after `start()` in which a movement
32
+ * slips through, and a lock with a window is not a lock.
33
+ */
34
+ let InventoryCountLockSubscriber = class InventoryCountLockSubscriber {
35
+ constructor(dataSource) {
36
+ dataSource.subscribers.push(this);
37
+ }
38
+ async beforeInsert(event) {
39
+ const tableName = event.metadata.tableName;
40
+ if (tableName === INVENTORY_COUNT_CHANGE_TABLE ||
41
+ !tableName.endsWith('_order_changes')) {
42
+ return;
43
+ }
44
+ const locationId = await this.resolveLocationId(event);
45
+ if (!locationId) {
46
+ // split / merge / reclassify items carry no location. They still move
47
+ // stock, but only within a position the caller already named elsewhere;
48
+ // there is nothing to match against the scope here.
49
+ return;
50
+ }
51
+ const locks = await event.manager.query(`SELECT o."key"
52
+ FROM inventory_count_order_locations l
53
+ JOIN inventory_count_orders o ON o.id = l."orderId"
54
+ WHERE l."locationId" = $1
55
+ AND o.status = 'COUNTING'
56
+ LIMIT 1`, [locationId]);
57
+ if (!locks.length) {
58
+ return;
59
+ }
60
+ // Naming the order is the difference between an operator who can go and
61
+ // settle the count and one who just sees "denied".
62
+ throw new ConflictException(`Location ${locationId} is locked by inventory count order ${locks[0].key}; finish or cancel that count before moving this stock.`);
63
+ }
64
+ /**
65
+ * The location the change ultimately touches, taken from the change's order
66
+ * item. `beforeInsert` may see the relation either already flattened to
67
+ * `itemId` or still as the item object, depending on how the caller built the
68
+ * row, so both are accepted.
69
+ */
70
+ async resolveLocationId(event) {
71
+ const entity = event.entity;
72
+ if (!entity) {
73
+ return null;
74
+ }
75
+ if (entity.item && entity.item.locationId !== undefined) {
76
+ return entity.item.locationId;
77
+ }
78
+ const itemId = entity.itemId ?? entity.item?.id;
79
+ if (!itemId) {
80
+ return null;
81
+ }
82
+ const itemRelation = event.metadata.relations.find((relation) => relation.propertyName === 'item');
83
+ if (!itemRelation) {
84
+ return null;
85
+ }
86
+ const item = await event.manager
87
+ .getRepository(itemRelation.inverseEntityMetadata.target)
88
+ .findOne({ where: { id: itemId } });
89
+ return item?.locationId ?? null;
90
+ }
91
+ };
92
+ InventoryCountLockSubscriber = __decorate([
93
+ Injectable(),
94
+ __param(0, InjectDataSource()),
95
+ __metadata("design:paramtypes", [DataSource])
96
+ ], InventoryCountLockSubscriber);
97
+
98
+ export { InventoryCountLockSubscriber };
@@ -41,6 +41,10 @@ const TABLE_TO_CHANGE_TYPE = new Map([
41
41
  ['merge_batch_order_changes', () => ChangeType.MERGED],
42
42
  ['reclassify_inventory_order_changes', () => ChangeType.RECLASSIFIED],
43
43
  ['scrape_order_changes', () => ChangeType.SCRAPED],
44
+ [
45
+ 'inventory_count_order_changes',
46
+ (q) => (q > 0 ? ChangeType.COUNT_GAINED : ChangeType.COUNT_LOST),
47
+ ],
44
48
  ]);
45
49
  const CHANGE_TYPE_TO_EVENT = {
46
50
  [ChangeType.RECEIVED]: 'stock.received',
@@ -60,6 +64,8 @@ const CHANGE_TYPE_TO_EVENT = {
60
64
  [ChangeType.DEALLOCATED]: 'stock.deallocated',
61
65
  [ChangeType.RECLASSIFIED]: 'stock.reclassified',
62
66
  [ChangeType.SCRAPED]: 'stock.scraped',
67
+ [ChangeType.COUNT_GAINED]: 'stock.count-gained',
68
+ [ChangeType.COUNT_LOST]: 'stock.count-lost',
63
69
  };
64
70
  const QUALITY_INSPECTION_TABLE = 'quality_inspection_order_changes';
65
71
  /**
@@ -15,8 +15,8 @@ import type { ChangeType } from '../constants/stock-status';
15
15
  * names and payloads. Wiring is opt-in via `forRootAsync({ beforeHooks,
16
16
  * afterListeners })`; when nothing is configured the whole chain is inert.
17
17
  */
18
- export type WmsOperationEventName = 'receive-inventory-order.created' | 'receive-inventory-order.updated' | 'receive-inventory-order.received' | 'ship-inventory-order.created' | 'ship-inventory-order.updated' | 'ship-inventory-order.shipped' | 'ship-inventory-order.ship-cancelled' | 'transfer-order.created' | 'transfer-order.updated' | 'transfer-order.transferred' | 'transfer-order.transfer-cancelled' | 'hold-inventory-order.created' | 'hold-inventory-order.updated' | 'hold-inventory-order.released' | 'allocate-inventory-order.created' | 'allocate-inventory-order.updated' | 'allocate-inventory-order.deallocated' | 'transfer-vendor-order.created' | 'transfer-vendor-order.updated' | 'transfer-vendor-order.returned' | 'transfer-customer-order.created' | 'transfer-customer-order.updated' | 'transfer-customer-order.returned' | 'quality-inspection-order.inspected' | 'scrape-order.scraped' | 'reclassify-inventory-order.reclassified' | 'batch.merged' | 'batch.splitted' | 'material.created' | 'material.updated' | 'loader.created' | 'loader.batch-created' | 'loader.updated' | 'loader.type-created' | 'loader.type-updated' | 'location.created' | 'location.updated' | 'warehouse-map.updated';
19
- export type WmsStockEventName = 'stock.received' | 'stock.in-shipment' | 'stock.shipped' | 'stock.in-transfer' | 'stock.transferred' | 'stock.vendor-transferred' | 'stock.vendor-returned' | 'stock.held' | 'stock.released' | 'stock.customer-held' | 'stock.customer-returned' | 'stock.splitted' | 'stock.merged' | 'stock.allocated' | 'stock.deallocated' | 'stock.reclassified' | 'stock.scraped' | 'stock.inspection-resolved';
18
+ export type WmsOperationEventName = 'receive-inventory-order.created' | 'receive-inventory-order.updated' | 'receive-inventory-order.received' | 'ship-inventory-order.created' | 'ship-inventory-order.updated' | 'ship-inventory-order.shipped' | 'ship-inventory-order.ship-cancelled' | 'transfer-order.created' | 'transfer-order.updated' | 'transfer-order.transferred' | 'transfer-order.transfer-cancelled' | 'hold-inventory-order.created' | 'hold-inventory-order.updated' | 'hold-inventory-order.released' | 'allocate-inventory-order.created' | 'allocate-inventory-order.updated' | 'allocate-inventory-order.deallocated' | 'transfer-vendor-order.created' | 'transfer-vendor-order.updated' | 'transfer-vendor-order.returned' | 'transfer-customer-order.created' | 'transfer-customer-order.updated' | 'transfer-customer-order.returned' | 'quality-inspection-order.inspected' | 'scrape-order.scraped' | 'reclassify-inventory-order.reclassified' | 'batch.merged' | 'batch.splitted' | 'material.created' | 'material.updated' | 'loader.created' | 'loader.batch-created' | 'loader.updated' | 'loader.type-created' | 'loader.type-updated' | 'location.created' | 'location.updated' | 'warehouse-map.updated' | 'inventory-count-order.created' | 'inventory-count-order.updated' | 'inventory-count-order.started' | 'inventory-count-order.counted' | 'inventory-count-order.reset' | 'inventory-count-order.cancelled' | 'inventory-count-order.settled';
19
+ export type WmsStockEventName = 'stock.received' | 'stock.in-shipment' | 'stock.shipped' | 'stock.in-transfer' | 'stock.transferred' | 'stock.vendor-transferred' | 'stock.vendor-returned' | 'stock.held' | 'stock.released' | 'stock.customer-held' | 'stock.customer-returned' | 'stock.splitted' | 'stock.merged' | 'stock.allocated' | 'stock.deallocated' | 'stock.reclassified' | 'stock.scraped' | 'stock.inspection-resolved' | 'stock.count-gained' | 'stock.count-lost';
20
20
  export type WmsEventName = WmsOperationEventName | WmsStockEventName;
21
21
  /**
22
22
  * The authenticated identity that performed a mutation, in a host-neutral shape.
@@ -8,6 +8,7 @@ import '../../metadata/loader-unit.entity.js';
8
8
  import { LocationEntityClass } from '../../metadata/location.entity.js';
9
9
  import '../../metadata/material.entity.js';
10
10
  import '../../metadata/material-unit.entity.js';
11
+ import '@rytass/wms-base-nestjs-module';
11
12
  import '../../metadata/sale-order.entity.js';
12
13
  import '../../metadata/stock.entity.js';
13
14
  import '../../metadata/unit.entity.js';
@@ -8,6 +8,7 @@ import '../../metadata/loader-unit.entity.js';
8
8
  import { LocationEntityClass } from '../../metadata/location.entity.js';
9
9
  import '../../metadata/material.entity.js';
10
10
  import '../../metadata/material-unit.entity.js';
11
+ import '@rytass/wms-base-nestjs-module';
11
12
  import '../../metadata/sale-order.entity.js';
12
13
  import '../../metadata/stock.entity.js';
13
14
  import '../../metadata/unit.entity.js';
@@ -35,3 +35,6 @@ export * from './transfer-customer-order/transfer-customer-order-change.entity';
35
35
  export * from './scrape-order/scrape-order.entity';
36
36
  export * from './scrape-order/scrape-order-item.entity';
37
37
  export * from './scrape-order/scrape-order-change.entity';
38
+ export * from './inventory-count-order/inventory-count-order.entity';
39
+ export * from './inventory-count-order/inventory-count-order-item.entity';
40
+ export * from './inventory-count-order/inventory-count-order-change.entity';
@@ -0,0 +1,20 @@
1
+ import { OrderEntity } from '@rytass/wms-base-nestjs-module';
2
+ import { InventoryCountOrderItemEntityClass } from './inventory-count-order-item.entity';
3
+ export declare const InventoryCountOrderChangeEntity: unique symbol;
4
+ /**
5
+ * The stock movement a settled count line produced.
6
+ *
7
+ * `quantityChanged` carries the sign, exactly like every other `*_order_change`
8
+ * table: positive is a surplus (盤盈), negative a shortage (盤虧).
9
+ * `StockChangeSubscriber` reads the sign to pick between `stock.count-gained`
10
+ * and `stock.count-lost`, so nothing here has to emit events itself.
11
+ *
12
+ * Lines whose count matched the books produce no row at all.
13
+ */
14
+ export declare class InventoryCountOrderChangeEntityClass {
15
+ id: string;
16
+ itemId: string;
17
+ quantityChanged: number;
18
+ order: OrderEntity;
19
+ item: InventoryCountOrderItemEntityClass;
20
+ }