@things-factory/warehouse-base 5.0.14 → 6.0.0-alpha.3

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 (142) hide show
  1. package/dist-server/controllers/ecommerce/ecommerce-controller.js +1 -1
  2. package/dist-server/controllers/ecommerce/ecommerce-controller.js.map +1 -1
  3. package/dist-server/controllers/ecommerce/sellercraft-controller.js +64 -60
  4. package/dist-server/controllers/ecommerce/sellercraft-controller.js.map +1 -1
  5. package/dist-server/controllers/warehouse-controller.js +7 -5
  6. package/dist-server/controllers/warehouse-controller.js.map +1 -1
  7. package/dist-server/service/inventory/inventory-mutation.js +6 -6
  8. package/dist-server/service/inventory/inventory-mutation.js.map +1 -1
  9. package/dist-server/service/inventory/inventory-query.js +217 -195
  10. package/dist-server/service/inventory/inventory-query.js.map +1 -1
  11. package/dist-server/service/inventory/inventory-types.js +14 -15
  12. package/dist-server/service/inventory/inventory-types.js.map +1 -1
  13. package/dist-server/service/inventory/inventory.js +62 -42
  14. package/dist-server/service/inventory/inventory.js.map +1 -1
  15. package/dist-server/service/inventory-change/inventory-change-mutation.js +252 -216
  16. package/dist-server/service/inventory-change/inventory-change-mutation.js.map +1 -1
  17. package/dist-server/service/inventory-change/inventory-change-query.js +6 -7
  18. package/dist-server/service/inventory-change/inventory-change-query.js.map +1 -1
  19. package/dist-server/service/inventory-change/inventory-change.js +7 -7
  20. package/dist-server/service/inventory-change/inventory-change.js.map +1 -1
  21. package/dist-server/service/inventory-history/inventory-history-mutation.js +1 -1
  22. package/dist-server/service/inventory-history/inventory-history-mutation.js.map +1 -1
  23. package/dist-server/service/inventory-history/inventory-history-query.js +89 -99
  24. package/dist-server/service/inventory-history/inventory-history-query.js.map +1 -1
  25. package/dist-server/service/inventory-history/inventory-history-types.js +3 -3
  26. package/dist-server/service/inventory-history/inventory-history-types.js.map +1 -1
  27. package/dist-server/service/inventory-history/inventory-history.js +7 -7
  28. package/dist-server/service/inventory-history/inventory-history.js.map +1 -1
  29. package/dist-server/service/inventory-item/inventory-item-mutation.js +10 -10
  30. package/dist-server/service/inventory-item/inventory-item-mutation.js.map +1 -1
  31. package/dist-server/service/inventory-item/inventory-item-query.js +18 -17
  32. package/dist-server/service/inventory-item/inventory-item-query.js.map +1 -1
  33. package/dist-server/service/inventory-item/inventory-item-type.js +4 -5
  34. package/dist-server/service/inventory-item/inventory-item-type.js.map +1 -1
  35. package/dist-server/service/inventory-item/inventory-item.js +5 -5
  36. package/dist-server/service/inventory-item/inventory-item.js.map +1 -1
  37. package/dist-server/service/inventory-item-change/inventory-item-change-mutation.js +4 -4
  38. package/dist-server/service/inventory-item-change/inventory-item-change-mutation.js.map +1 -1
  39. package/dist-server/service/inventory-item-change/inventory-item-change-query.js +7 -9
  40. package/dist-server/service/inventory-item-change/inventory-item-change-query.js.map +1 -1
  41. package/dist-server/service/inventory-item-change/inventory-item-change-type.js +6 -7
  42. package/dist-server/service/inventory-item-change/inventory-item-change-type.js.map +1 -1
  43. package/dist-server/service/inventory-item-change/inventory-item-change.js +3 -4
  44. package/dist-server/service/inventory-item-change/inventory-item-change.js.map +1 -1
  45. package/dist-server/service/inventory-product/inventory-product-mutation.js +4 -4
  46. package/dist-server/service/inventory-product/inventory-product-mutation.js.map +1 -1
  47. package/dist-server/service/inventory-product/inventory-product-query.js +7 -9
  48. package/dist-server/service/inventory-product/inventory-product-query.js.map +1 -1
  49. package/dist-server/service/inventory-product/inventory-product-type.js +1 -2
  50. package/dist-server/service/inventory-product/inventory-product-type.js.map +1 -1
  51. package/dist-server/service/inventory-product/inventory-product.js +5 -5
  52. package/dist-server/service/inventory-product/inventory-product.js.map +1 -1
  53. package/dist-server/service/location/location-mutation.js +5 -5
  54. package/dist-server/service/location/location-mutation.js.map +1 -1
  55. package/dist-server/service/location/location-query.js +17 -19
  56. package/dist-server/service/location/location-query.js.map +1 -1
  57. package/dist-server/service/location/location-types.js +3 -3
  58. package/dist-server/service/location/location-types.js.map +1 -1
  59. package/dist-server/service/location/location.js +14 -14
  60. package/dist-server/service/location/location.js.map +1 -1
  61. package/dist-server/service/movement/movement-mutation.js +6 -5
  62. package/dist-server/service/movement/movement-mutation.js.map +1 -1
  63. package/dist-server/service/movement/movement-query.js +15 -15
  64. package/dist-server/service/movement/movement-query.js.map +1 -1
  65. package/dist-server/service/movement/movement-types.js +4 -5
  66. package/dist-server/service/movement/movement-types.js.map +1 -1
  67. package/dist-server/service/movement/movement.js +5 -5
  68. package/dist-server/service/movement/movement.js.map +1 -1
  69. package/dist-server/service/pallet/pallet-mutation.js +5 -5
  70. package/dist-server/service/pallet/pallet-mutation.js.map +1 -1
  71. package/dist-server/service/pallet/pallet-query.js +9 -11
  72. package/dist-server/service/pallet/pallet-query.js.map +1 -1
  73. package/dist-server/service/pallet/pallet-types.js +5 -6
  74. package/dist-server/service/pallet/pallet-types.js.map +1 -1
  75. package/dist-server/service/pallet/pallet.js +6 -6
  76. package/dist-server/service/pallet/pallet.js.map +1 -1
  77. package/dist-server/service/pallet-count/pallet-count-mutation.js +1 -1
  78. package/dist-server/service/pallet-count/pallet-count-mutation.js.map +1 -1
  79. package/dist-server/service/pallet-count/pallet-count-query.js +6 -8
  80. package/dist-server/service/pallet-count/pallet-count-query.js.map +1 -1
  81. package/dist-server/service/pallet-count/pallet-count.js +3 -4
  82. package/dist-server/service/pallet-count/pallet-count.js.map +1 -1
  83. package/dist-server/service/pallet-history/pallet-history-mutation.js +1 -1
  84. package/dist-server/service/pallet-history/pallet-history-mutation.js.map +1 -1
  85. package/dist-server/service/pallet-history/pallet-history-query.js +6 -8
  86. package/dist-server/service/pallet-history/pallet-history-query.js.map +1 -1
  87. package/dist-server/service/pallet-history/pallet-history.js +6 -6
  88. package/dist-server/service/pallet-history/pallet-history.js.map +1 -1
  89. package/dist-server/service/reduced-inventory-history/reduced-inventory-history.js +1 -1
  90. package/dist-server/service/reduced-inventory-history/reduced-inventory-history.js.map +1 -1
  91. package/dist-server/service/tote/tote-mutation.js +4 -4
  92. package/dist-server/service/tote/tote-mutation.js.map +1 -1
  93. package/dist-server/service/tote/tote-query.js +12 -13
  94. package/dist-server/service/tote/tote-query.js.map +1 -1
  95. package/dist-server/service/tote/tote-types.js +2 -3
  96. package/dist-server/service/tote/tote-types.js.map +1 -1
  97. package/dist-server/service/tote/tote.js +5 -5
  98. package/dist-server/service/tote/tote.js.map +1 -1
  99. package/dist-server/service/warehouse/warehouse-mutation.js +1 -1
  100. package/dist-server/service/warehouse/warehouse-mutation.js.map +1 -1
  101. package/dist-server/service/warehouse/warehouse-query.js +9 -10
  102. package/dist-server/service/warehouse/warehouse-query.js.map +1 -1
  103. package/dist-server/service/warehouse/warehouse.js +12 -12
  104. package/dist-server/service/warehouse/warehouse.js.map +1 -1
  105. package/dist-server/tsconfig.tsbuildinfo +1 -1
  106. package/dist-server/utils/inventory-util.js +16 -16
  107. package/dist-server/utils/inventory-util.js.map +1 -1
  108. package/package.json +8 -8
  109. package/server/controllers/ecommerce/ecommerce-controller.ts +1 -1
  110. package/server/controllers/ecommerce/sellercraft-controller.ts +75 -65
  111. package/server/controllers/warehouse-controller.ts +9 -4
  112. package/server/service/inventory/inventory-mutation.ts +28 -24
  113. package/server/service/inventory/inventory-query.ts +269 -304
  114. package/server/service/inventory/inventory.ts +50 -35
  115. package/server/service/inventory-change/inventory-change-mutation.ts +305 -256
  116. package/server/service/inventory-change/inventory-change-query.ts +9 -9
  117. package/server/service/inventory-history/inventory-history-mutation.ts +12 -15
  118. package/server/service/inventory-history/inventory-history-query.ts +158 -133
  119. package/server/service/inventory-item/inventory-item-mutation.ts +20 -17
  120. package/server/service/inventory-item/inventory-item-query.ts +28 -18
  121. package/server/service/inventory-item-change/inventory-item-change-mutation.ts +12 -9
  122. package/server/service/inventory-item-change/inventory-item-change-query.ts +10 -8
  123. package/server/service/inventory-product/inventory-product-mutation.ts +16 -12
  124. package/server/service/inventory-product/inventory-product-query.ts +7 -8
  125. package/server/service/location/location-mutation.ts +24 -24
  126. package/server/service/location/location-query.ts +27 -23
  127. package/server/service/location/location.ts +11 -10
  128. package/server/service/movement/movement-mutation.ts +7 -8
  129. package/server/service/movement/movement-query.ts +18 -16
  130. package/server/service/pallet/pallet-mutation.ts +18 -20
  131. package/server/service/pallet/pallet-query.ts +13 -14
  132. package/server/service/pallet-count/pallet-count-mutation.ts +20 -17
  133. package/server/service/pallet-count/pallet-count-query.ts +6 -7
  134. package/server/service/pallet-history/pallet-history-mutation.ts +15 -15
  135. package/server/service/pallet-history/pallet-history-query.ts +7 -8
  136. package/server/service/reduced-inventory-history/reduced-inventory-history.ts +1 -1
  137. package/server/service/tote/tote-mutation.ts +23 -22
  138. package/server/service/tote/tote-query.ts +18 -14
  139. package/server/service/warehouse/warehouse-mutation.ts +21 -18
  140. package/server/service/warehouse/warehouse-query.ts +11 -11
  141. package/server/service/warehouse/warehouse.ts +7 -7
  142. package/server/utils/inventory-util.ts +10 -10
@@ -4,12 +4,10 @@ exports.approveInventoryChanges = exports.InventoryChangeMutation = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const type_graphql_1 = require("type-graphql");
6
6
  const typeorm_1 = require("typeorm");
7
- const auth_base_1 = require("@things-factory/auth-base");
8
7
  const biz_base_1 = require("@things-factory/biz-base");
9
8
  const id_rule_base_1 = require("@things-factory/id-rule-base");
10
9
  const integration_marketplace_1 = require("@things-factory/integration-marketplace");
11
10
  const integration_sellercraft_1 = require("@things-factory/integration-sellercraft");
12
- const notification_1 = require("@things-factory/notification");
13
11
  const product_base_1 = require("@things-factory/product-base");
14
12
  const setting_base_1 = require("@things-factory/setting-base");
15
13
  const constants_1 = require("../../constants");
@@ -53,7 +51,7 @@ let InventoryChangeMutation = class InventoryChangeMutation {
53
51
  if (_updateRecords.length > 0) {
54
52
  for (let i = 0; i < _updateRecords.length; i++) {
55
53
  const newRecord = _updateRecords[i];
56
- const inventoryChange = await inventoryChangeRepo.findOne(newRecord.id);
54
+ const inventoryChange = await inventoryChangeRepo.findOneBy({ id: newRecord.id });
57
55
  const result = await inventoryChangeRepo.save(Object.assign(Object.assign(Object.assign({}, inventoryChange), newRecord), { updater: user }));
58
56
  results.push(Object.assign(Object.assign({}, result), { cuFlag: 'M' }));
59
57
  }
@@ -74,216 +72,241 @@ let InventoryChangeMutation = class InventoryChangeMutation {
74
72
  return true;
75
73
  }
76
74
  async submitInventoryChanges(patches, context) {
77
- var _a, _b, _c, _d, _e, _f, _g, _h;
75
+ var _a, _b, _c, _d, _e, _f;
78
76
  const { domain, user, tx } = context.state;
79
- const _createRecords = [];
80
- const _updateRecords = [];
81
- for (let i = 0; i < patches.length; i++) {
82
- let foundExistingPallet = await tx.getRepository(inventory_1.Inventory).findOne({
83
- where: { domain, palletId: (_a = patches[i]) === null || _a === void 0 ? void 0 : _a.palletId }
84
- });
85
- if (!foundExistingPallet) {
86
- foundExistingPallet = await tx.getRepository(inventory_1.Inventory).findOne({
87
- where: { id: (_b = patches[i]) === null || _b === void 0 ? void 0 : _b.id }
88
- });
89
- }
90
- const invLockedQty = (foundExistingPallet === null || foundExistingPallet === void 0 ? void 0 : foundExistingPallet.lockedQty) == null ? 0 : foundExistingPallet === null || foundExistingPallet === void 0 ? void 0 : foundExistingPallet.lockedQty;
91
- if (((_c = patches[i]) === null || _c === void 0 ? void 0 : _c.qty) < invLockedQty) {
92
- throw new Error('Adjusted qty value should not be lower than released qty, kindly contact our support if you have difficulties');
93
- }
94
- if (foundExistingPallet) {
95
- delete patches[i].serialNumbers;
96
- _updateRecords.push(patches[i]);
97
- }
98
- else {
99
- _createRecords.push(patches[i]);
100
- }
101
- }
102
- const inventoryChangeRepo = tx.getRepository(inventory_change_1.InventoryChange);
103
- const inventoryItemChangeRepo = tx.getRepository(inventory_item_change_1.InventoryItemChange);
104
- if (_createRecords.length > 0) {
105
- for (let i = 0; i < _createRecords.length; i++) {
106
- const newRecord = _createRecords[i];
107
- var location = await tx.getRepository(location_1.Location).findOne({
108
- where: { id: newRecord.location.id },
109
- relations: ['warehouse']
110
- });
111
- newRecord.location = location;
112
- newRecord.zone = location.zone;
113
- newRecord.warehouse = location.warehouse;
114
- newRecord.bizplace = await tx.getRepository(biz_base_1.Bizplace).findOne(newRecord.bizplace.id);
115
- var product = await tx.getRepository(product_base_1.Product).findOne(newRecord.product.id);
116
- newRecord.product = product;
117
- newRecord.status = 'PENDING';
118
- newRecord.transactionType = 'NEW';
119
- newRecord.expirationDate =
120
- (((newRecord === null || newRecord === void 0 ? void 0 : newRecord.expirationDate) && new Date(newRecord.expirationDate).getFullYear()) || 0) < 2000
121
- ? null
122
- : newRecord.expirationDate;
123
- const newInventoryChange = await inventoryChangeRepo.save(Object.assign(Object.assign({}, newRecord), { id: undefined, name: utils_1.InventoryNoGenerator.inventoryName(), domain: domain, creator: user, updater: user }));
124
- if (newRecord === null || newRecord === void 0 ? void 0 : newRecord.serialNumbers) {
125
- let serialNumbers = newRecord.serialNumbers;
126
- let newSerialNumbers = serialNumbers.split(',');
127
- let inventoryItemChanges = [];
128
- for (let i = 0; i < newSerialNumbers.length; i++) {
129
- let newSerialNumber = newSerialNumbers[i];
130
- let inventoryItemChange = {
131
- serialNumber: newSerialNumber,
132
- type: constants_1.INVENTORY_ITEM_CHANGE_TYPE.NEW
133
- };
134
- inventoryItemChanges.push(inventoryItemChange);
135
- }
136
- newRecord.inventoryItemChangesJson = JSON.stringify(inventoryItemChanges);
137
- }
138
- // create new inventory items
139
- if (newRecord === null || newRecord === void 0 ? void 0 : newRecord.inventoryItemChangesJson) {
140
- const inventoryItemChanges = JSON.parse(newRecord.inventoryItemChangesJson);
141
- if (product === null || product === void 0 ? void 0 : product.isRequireSerialNumberScanningInbound) {
142
- if ((inventoryItemChanges === null || inventoryItemChanges === void 0 ? void 0 : inventoryItemChanges.length) != (newRecord === null || newRecord === void 0 ? void 0 : newRecord.qty)) {
143
- throw new Error('Serial Numbers not tally with quantity');
144
- }
77
+ try {
78
+ const _createRecords = [];
79
+ const _updateRecords = [];
80
+ for (let i = 0; i < patches.length; i++) {
81
+ let foundExistingPallet = await tx
82
+ .getRepository(inventory_1.Inventory)
83
+ .createQueryBuilder('iv')
84
+ .where('iv.domain_id = :domain_id', { domain_id: domain.id })
85
+ .andWhere('(iv.pallet_id = :pallet_id or iv.id = :id)', {
86
+ pallet_id: (_a = patches[i]) === null || _a === void 0 ? void 0 : _a.palletId,
87
+ id: (_b = patches[i]) === null || _b === void 0 ? void 0 : _b.id
88
+ })
89
+ .getOne();
90
+ //remove any unchanged records
91
+ for (const [key, value] of Object.entries(patches[i])) {
92
+ if (foundExistingPallet && key != 'id' && key != 'cuFlag' && value == foundExistingPallet[key]) {
93
+ delete patches[i][key];
145
94
  }
146
- let newInventoryItemChanges = [];
147
- inventoryItemChanges.map(inventoryItemChange => {
148
- let newInventoryItemChange = new inventory_item_change_1.InventoryItemChange();
149
- newInventoryItemChange.name = utils_1.InventoryNoGenerator.inventoryName();
150
- newInventoryItemChange.serialNumber = inventoryItemChange.serialNumber;
151
- newInventoryItemChange.type = inventoryItemChange.type;
152
- newInventoryItemChange.status = newInventoryChange.status;
153
- newInventoryItemChange.inventoryItem =
154
- (newInventoryItemChange === null || newInventoryItemChange === void 0 ? void 0 : newInventoryItemChange.type) == constants_1.INVENTORY_ITEM_CHANGE_TYPE.NEW
155
- ? null
156
- : newInventoryItemChange.inventoryItem;
157
- newInventoryItemChange.inventoryChange = newInventoryChange;
158
- newInventoryItemChange.domain = domain;
159
- newInventoryItemChange.creator = user;
160
- newInventoryItemChange.updater = user;
161
- newInventoryItemChanges.push(newInventoryItemChange);
162
- });
163
- await inventoryItemChangeRepo.save(newInventoryItemChanges);
164
95
  }
165
- }
166
- }
167
- if (_updateRecords.length > 0) {
168
- for (let i = 0; i < _updateRecords.length; i++) {
169
- const updateRecord = _updateRecords[i];
170
- let existingRecord = await tx.getRepository(inventory_1.Inventory).findOne({
171
- where: { domain, palletId: updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.palletId },
172
- relations: ['location', 'warehouse', 'product', 'bizplace']
173
- });
174
- if (!existingRecord) {
175
- existingRecord = await tx.getRepository(inventory_1.Inventory).findOne({
176
- where: { domain, id: updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.id },
177
- relations: ['location', 'warehouse', 'product', 'bizplace']
178
- });
96
+ // skip process when no processable data found
97
+ if (Object.keys(patches[i]).filter(x => x != 'id' && x != 'cuFlag').length == 0) {
98
+ continue;
179
99
  }
180
- if (!!((_d = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.location) === null || _d === void 0 ? void 0 : _d.id)) {
181
- var location = await tx.getRepository(location_1.Location).findOne({
182
- where: { id: updateRecord.location.id },
183
- relations: ['warehouse']
184
- });
185
- updateRecord.location = location;
186
- updateRecord.zone = location.zone;
187
- updateRecord.warehouse = location.warehouse;
100
+ const invLockedQty = (foundExistingPallet === null || foundExistingPallet === void 0 ? void 0 : foundExistingPallet.lockedQty) == null ? 0 : foundExistingPallet === null || foundExistingPallet === void 0 ? void 0 : foundExistingPallet.lockedQty;
101
+ if (((_c = patches[i]) === null || _c === void 0 ? void 0 : _c.qty) < invLockedQty) {
102
+ throw new Error('Adjusted qty value should not be lower than released qty, kindly contact our support for assistance');
188
103
  }
189
- if (!!((_e = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.bizplace) === null || _e === void 0 ? void 0 : _e.id))
190
- updateRecord.bizplace = await tx.getRepository(biz_base_1.Bizplace).findOne(updateRecord.bizplace.id);
191
- let product;
192
- if (!!((_f = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.product) === null || _f === void 0 ? void 0 : _f.id)) {
193
- product = await tx.getRepository(product_base_1.Product).findOne(updateRecord.product.id);
194
- updateRecord.product = product;
104
+ if (foundExistingPallet) {
105
+ delete patches[i].serialNumbers;
106
+ _updateRecords.push(patches[i]);
195
107
  }
196
108
  else {
197
- product = existingRecord.product;
109
+ _createRecords.push(patches[i]);
198
110
  }
199
- updateRecord.transactionType == 'MISSING'
200
- ? (updateRecord.transactionType = 'MISSING')
201
- : (updateRecord.transactionType = 'CHANGES');
202
- updateRecord.status = 'PENDING';
203
- updateRecord.expirationDate =
204
- (((updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.expirationDate) && new Date(updateRecord.expirationDate).getFullYear()) || 0) < 2000
205
- ? null
206
- : updateRecord.expirationDate;
207
- const updatedInventoryChange = await inventoryChangeRepo.save(Object.assign(Object.assign({ palletId: existingRecord.palletId }, updateRecord), { id: undefined, name: utils_1.InventoryNoGenerator.inventoryName(), inventory: existingRecord, domain: domain, creator: user, updater: user }));
208
- // create new inventory items
209
- if (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.inventoryItemChangesJson) {
210
- const originalInventoryItems = await tx.getRepository(inventory_item_1.InventoryItem).find({
211
- where: { domain, inventory: existingRecord }
111
+ }
112
+ const inventoryChangeRepo = tx.getRepository(inventory_change_1.InventoryChange);
113
+ const inventoryItemChangeRepo = tx.getRepository(inventory_item_change_1.InventoryItemChange);
114
+ if (_createRecords.length > 0) {
115
+ for (let i = 0; i < _createRecords.length; i++) {
116
+ const newRecord = _createRecords[i];
117
+ var location = await tx.getRepository(location_1.Location).findOne({
118
+ where: { id: newRecord.location.id },
119
+ relations: ['warehouse']
212
120
  });
213
- const inventoryItemChanges = JSON.parse(updateRecord.inventoryItemChangesJson);
214
- if (product === null || product === void 0 ? void 0 : product.isRequireSerialNumberScanningInbound) {
215
- const combinedInventoryItems = [...originalInventoryItems, ...inventoryItemChanges];
216
- let qty = (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.qty) ? updateRecord.qty : existingRecord.qty;
217
- if ((combinedInventoryItems === null || combinedInventoryItems === void 0 ? void 0 : combinedInventoryItems.length) != qty) {
218
- throw new Error('Serial Numbers not tally with quantity');
121
+ newRecord.location = location;
122
+ newRecord.zone = location.zone;
123
+ newRecord.warehouse = location.warehouse;
124
+ newRecord.bizplace = await tx.getRepository(biz_base_1.Bizplace).findOneBy({ id: newRecord.bizplace.id });
125
+ var product = await tx.getRepository(product_base_1.Product).findOne(newRecord.product.id);
126
+ newRecord.product = product;
127
+ newRecord.status = 'PENDING';
128
+ newRecord.transactionType = 'NEW';
129
+ newRecord.expirationDate =
130
+ (((newRecord === null || newRecord === void 0 ? void 0 : newRecord.expirationDate) && new Date(newRecord.expirationDate).getFullYear()) || 0) < 2000
131
+ ? null
132
+ : newRecord.expirationDate;
133
+ const newInventoryChange = await inventoryChangeRepo.save(Object.assign(Object.assign({}, newRecord), { id: undefined, name: utils_1.InventoryNoGenerator.inventoryName(), domain: domain, creator: user, updater: user }));
134
+ if (newRecord === null || newRecord === void 0 ? void 0 : newRecord.serialNumbers) {
135
+ let serialNumbers = newRecord.serialNumbers;
136
+ let newSerialNumbers = serialNumbers.split(',');
137
+ let inventoryItemChanges = [];
138
+ for (let i = 0; i < newSerialNumbers.length; i++) {
139
+ let newSerialNumber = newSerialNumbers[i];
140
+ let inventoryItemChange = {
141
+ serialNumber: newSerialNumber,
142
+ type: constants_1.INVENTORY_ITEM_CHANGE_TYPE.NEW
143
+ };
144
+ inventoryItemChanges.push(inventoryItemChange);
219
145
  }
146
+ newRecord.inventoryItemChangesJson = JSON.stringify(inventoryItemChanges);
147
+ }
148
+ // create new inventory items
149
+ if (newRecord === null || newRecord === void 0 ? void 0 : newRecord.inventoryItemChangesJson) {
150
+ const inventoryItemChanges = JSON.parse(newRecord.inventoryItemChangesJson);
151
+ if (product === null || product === void 0 ? void 0 : product.isRequireSerialNumberScanningInbound) {
152
+ if ((inventoryItemChanges === null || inventoryItemChanges === void 0 ? void 0 : inventoryItemChanges.length) != (newRecord === null || newRecord === void 0 ? void 0 : newRecord.qty)) {
153
+ throw new Error('Serial Numbers not tally with quantity');
154
+ }
155
+ }
156
+ let newInventoryItemChanges = [];
157
+ inventoryItemChanges.map(inventoryItemChange => {
158
+ let newInventoryItemChange = new inventory_item_change_1.InventoryItemChange();
159
+ newInventoryItemChange.name = utils_1.InventoryNoGenerator.inventoryName();
160
+ newInventoryItemChange.serialNumber = inventoryItemChange.serialNumber;
161
+ newInventoryItemChange.type = inventoryItemChange.type;
162
+ newInventoryItemChange.status = newInventoryChange.status;
163
+ newInventoryItemChange.inventoryItem =
164
+ (newInventoryItemChange === null || newInventoryItemChange === void 0 ? void 0 : newInventoryItemChange.type) == constants_1.INVENTORY_ITEM_CHANGE_TYPE.NEW
165
+ ? null
166
+ : newInventoryItemChange.inventoryItem;
167
+ newInventoryItemChange.inventoryChange = newInventoryChange;
168
+ newInventoryItemChange.domain = domain;
169
+ newInventoryItemChange.creator = user;
170
+ newInventoryItemChange.updater = user;
171
+ newInventoryItemChanges.push(newInventoryItemChange);
172
+ });
173
+ await inventoryItemChangeRepo.save(newInventoryItemChanges);
220
174
  }
221
- let newInventoryItemChanges = [];
222
- inventoryItemChanges.map(inventoryItemChange => {
223
- let newInventoryItemChange = new inventory_item_change_1.InventoryItemChange();
224
- newInventoryItemChange.name = utils_1.InventoryNoGenerator.inventoryName();
225
- newInventoryItemChange.serialNumber = inventoryItemChange.serialNumber;
226
- newInventoryItemChange.type = inventoryItemChange.type;
227
- newInventoryItemChange.status = updatedInventoryChange.status;
228
- newInventoryItemChange.inventory = updatedInventoryChange.inventory;
229
- newInventoryItemChange.inventoryItem =
230
- (newInventoryItemChange === null || newInventoryItemChange === void 0 ? void 0 : newInventoryItemChange.type) == constants_1.INVENTORY_ITEM_CHANGE_TYPE.NEW
231
- ? null
232
- : newInventoryItemChange.inventoryItem;
233
- newInventoryItemChange.inventoryChange = updatedInventoryChange;
234
- newInventoryItemChange.domain = domain;
235
- newInventoryItemChange.creator = user;
236
- newInventoryItemChange.updater = user;
237
- newInventoryItemChanges.push(newInventoryItemChange);
238
- });
239
- await inventoryItemChangeRepo.save(newInventoryItemChanges);
240
175
  }
241
176
  }
242
- }
243
- const notificationApprover = await tx
244
- .getRepository(auth_base_1.Privilege)
245
- .createQueryBuilder('p')
246
- .select('u.*')
247
- .addSelect('r.*')
248
- .innerJoin('p.roles', 'r')
249
- .innerJoin('r.users', 'u')
250
- .where('p.name = :name', { name: 'mutation' })
251
- .andWhere('p.category = :category', { category: 'inventory' })
252
- .andWhere('r.domain_id = :domainId', { domainId: domain.id })
253
- .groupBy('u.id')
254
- .addGroupBy('r.id')
255
- .getRawMany();
256
- if (_updateRecords.some(res => res.transactionType == 'MISSING')) {
257
- if ((notificationApprover === null || notificationApprover === void 0 ? void 0 : notificationApprover.length) && ((_g = context.header) === null || _g === void 0 ? void 0 : _g.referer)) {
258
- const receivers = notificationApprover.map(user => user.id);
259
- const msg = {
260
- title: `Missing stock identified. Review this transaction in Inventory Adjustment Approval.`,
261
- body: ``,
262
- url: context.header.referer,
263
- data: { url: context.header.referer }
264
- };
265
- await (0, notification_1.sendNotification)({
266
- receivers,
267
- message: Object.assign({}, msg)
268
- });
177
+ if (_updateRecords.length > 0) {
178
+ for (let i = 0; i < _updateRecords.length; i++) {
179
+ const updateRecord = _updateRecords[i];
180
+ let existingRecord = await tx.getRepository(inventory_1.Inventory).findOne({
181
+ where: { domain, palletId: updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.palletId },
182
+ relations: ['location', 'warehouse', 'product', 'bizplace']
183
+ });
184
+ if (!existingRecord) {
185
+ existingRecord = await tx.getRepository(inventory_1.Inventory).findOne({
186
+ where: { domain, id: updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.id },
187
+ relations: ['location', 'warehouse', 'product', 'bizplace']
188
+ });
189
+ }
190
+ if (!!((_d = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.location) === null || _d === void 0 ? void 0 : _d.id)) {
191
+ var location = await tx.getRepository(location_1.Location).findOne({
192
+ where: { id: updateRecord.location.id },
193
+ relations: ['warehouse']
194
+ });
195
+ updateRecord.location = location;
196
+ updateRecord.zone = location.zone;
197
+ updateRecord.warehouse = location.warehouse;
198
+ }
199
+ if (!!((_e = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.bizplace) === null || _e === void 0 ? void 0 : _e.id))
200
+ updateRecord.bizplace = await tx.getRepository(biz_base_1.Bizplace).findOne(updateRecord.bizplace.id);
201
+ let product;
202
+ if (!!((_f = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.product) === null || _f === void 0 ? void 0 : _f.id)) {
203
+ product = await tx.getRepository(product_base_1.Product).findOne(updateRecord.product.id);
204
+ updateRecord.product = product;
205
+ }
206
+ else {
207
+ product = existingRecord.product;
208
+ }
209
+ updateRecord.transactionType == 'MISSING'
210
+ ? (updateRecord.transactionType = 'MISSING')
211
+ : (updateRecord.transactionType = 'CHANGES');
212
+ updateRecord.status = 'PENDING';
213
+ updateRecord.expirationDate =
214
+ (((updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.expirationDate) && new Date(updateRecord.expirationDate).getFullYear()) || 0) < 2000
215
+ ? null
216
+ : updateRecord.expirationDate;
217
+ const updatedInventoryChange = await inventoryChangeRepo.save(Object.assign(Object.assign({ palletId: existingRecord.palletId }, updateRecord), { id: undefined, name: utils_1.InventoryNoGenerator.inventoryName(), inventory: existingRecord, domain: domain, creator: user, updater: user }));
218
+ // create new inventory items
219
+ if (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.inventoryItemChangesJson) {
220
+ const originalInventoryItems = await tx.getRepository(inventory_item_1.InventoryItem).find({
221
+ where: { domain: { id: domain.id }, inventory: { id: existingRecord.id }, status: (0, typeorm_1.Not)('DELETED') }
222
+ });
223
+ const inventoryItemChanges = JSON.parse(updateRecord.inventoryItemChangesJson);
224
+ if (product === null || product === void 0 ? void 0 : product.isRequireSerialNumberScanningInbound) {
225
+ // const combinedInventoryItems: any[] = [...originalInventoryItems, ...inventoryItemChanges]
226
+ let qty = (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.qty) ? updateRecord.qty : existingRecord.qty;
227
+ if (originalInventoryItems.length == 0) {
228
+ if (inventoryItemChanges.length !== qty) {
229
+ throw new Error('Serial Numbers not tally with quantity');
230
+ }
231
+ }
232
+ else {
233
+ if ((originalInventoryItems === null || originalInventoryItems === void 0 ? void 0 : originalInventoryItems.length) - inventoryItemChanges.length !== qty) {
234
+ throw new Error('Serial Numbers not tally with quantity');
235
+ }
236
+ }
237
+ }
238
+ let newInventoryItemChanges = [];
239
+ inventoryItemChanges.map(inventoryItemChange => {
240
+ let newInventoryItemChange = new inventory_item_change_1.InventoryItemChange();
241
+ newInventoryItemChange.name = utils_1.InventoryNoGenerator.inventoryName();
242
+ newInventoryItemChange.serialNumber = inventoryItemChange.serialNumber;
243
+ newInventoryItemChange.type = inventoryItemChange.type;
244
+ newInventoryItemChange.status = updatedInventoryChange.status;
245
+ newInventoryItemChange.inventory = updatedInventoryChange.inventory;
246
+ newInventoryItemChange.inventoryItem =
247
+ (newInventoryItemChange === null || newInventoryItemChange === void 0 ? void 0 : newInventoryItemChange.type) == constants_1.INVENTORY_ITEM_CHANGE_TYPE.NEW
248
+ ? null
249
+ : inventoryItemChange.inventoryItem;
250
+ newInventoryItemChange.inventoryChange = updatedInventoryChange;
251
+ newInventoryItemChange.domain = domain;
252
+ newInventoryItemChange.creator = user;
253
+ newInventoryItemChange.updater = user;
254
+ newInventoryItemChanges.push(newInventoryItemChange);
255
+ });
256
+ await inventoryItemChangeRepo.save(newInventoryItemChanges);
257
+ }
258
+ }
269
259
  }
260
+ /**
261
+ * @notes Temporary off sendNotification due to suspect of causing wms down
262
+ */
263
+ // const notificationApprover: any = await tx
264
+ // .getRepository(Privilege)
265
+ // .createQueryBuilder('p')
266
+ // .select('u.*')
267
+ // .addSelect('r.*')
268
+ // .innerJoin('p.roles', 'r')
269
+ // .innerJoin('r.users', 'u')
270
+ // .where('p.name = :name', { name: 'mutation' })
271
+ // .andWhere('p.category = :category', { category: 'inventory' })
272
+ // .andWhere('r.domain_id = :domainId', { domainId: domain.id })
273
+ // .groupBy('u.id')
274
+ // .addGroupBy('r.id')
275
+ // .getRawMany()
276
+ // if (_updateRecords.some(res => res.transactionType == 'MISSING')) {
277
+ // if (notificationApprover?.length && context.header?.referer) {
278
+ // const receivers: any[] = notificationApprover.map(user => user.id)
279
+ // const msg = {
280
+ // title: `Missing stock identified. Review this transaction in Inventory Adjustment Approval.`,
281
+ // body: ``,
282
+ // url: context.header.referer,
283
+ // data: { url: context.header.referer }
284
+ // }
285
+ // await sendNotification({
286
+ // receivers,
287
+ // message: { ...msg }
288
+ // })
289
+ // }
290
+ // } else {
291
+ // if (notificationApprover?.length && context.header?.referer) {
292
+ // const receivers: any[] = notificationApprover.map(user => user.id)
293
+ // const msg = {
294
+ // title: `There is an inventory adjustment pending for approval, kindly review within 1 hour to avoid any operation conflict.`,
295
+ // body: ``,
296
+ // url: context.header.referer,
297
+ // data: { url: context.header.referer }
298
+ // }
299
+ // await sendNotification({
300
+ // receivers,
301
+ // message: { ...msg }
302
+ // })
303
+ // }
304
+ // }
305
+ return true;
270
306
  }
271
- else {
272
- if ((notificationApprover === null || notificationApprover === void 0 ? void 0 : notificationApprover.length) && ((_h = context.header) === null || _h === void 0 ? void 0 : _h.referer)) {
273
- const receivers = notificationApprover.map(user => user.id);
274
- const msg = {
275
- title: `There is an inventory adjustment pending for approval, kindly review within 1 hour to avoid any operation conflict.`,
276
- body: ``,
277
- url: context.header.referer,
278
- data: { url: context.header.referer }
279
- };
280
- await (0, notification_1.sendNotification)({
281
- receivers,
282
- message: Object.assign({}, msg)
283
- });
284
- }
307
+ catch (error) {
308
+ throw error;
285
309
  }
286
- return true;
287
310
  }
288
311
  async approveInventoryChanges(patches, context) {
289
312
  const inventoryChangeList = await approveInventoryChanges(patches, context);
@@ -416,7 +439,7 @@ function clean(obj) {
416
439
  async function upsertInventoryItems(context, inventoryChange, approvalStatus, tx) {
417
440
  const { domain, user } = context.state;
418
441
  let inventoryItemChanges = await tx.getRepository(inventory_item_change_1.InventoryItemChange).find({
419
- where: { domain, inventoryChange: inventoryChange, status: 'PENDING' },
442
+ where: { domain: { id: domain.id }, inventoryChange: inventoryChange, status: 'PENDING' },
420
443
  relations: ['domain', 'inventoryItem', 'inventory', 'inventory.product', 'inventoryChange']
421
444
  });
422
445
  await Promise.all(inventoryItemChanges.map(async (inventoryItemChange) => {
@@ -455,7 +478,7 @@ async function upsertInventoryItems(context, inventoryChange, approvalStatus, tx
455
478
  }));
456
479
  }
457
480
  async function approveInventoryChanges(patches, context) {
458
- var _a, _b, _c, _d;
481
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
459
482
  const { domain, user, tx } = context.state;
460
483
  // Get Selected Inventory Change Data
461
484
  const _inventoryChanges = await tx.getRepository(inventory_change_1.InventoryChange).find({
@@ -534,7 +557,7 @@ async function approveInventoryChanges(patches, context) {
534
557
  transactionType = 'ADJUSTMENT';
535
558
  // Check and set current location status
536
559
  let currentLocationInventoryCount = await tx.getRepository(inventory_1.Inventory).count({
537
- where: { location: inventory.location, status: 'STORED', id: (0, typeorm_1.Not)(inventory.id) }
560
+ where: { location: { id: inventory.location.id }, status: 'STORED', id: (0, typeorm_1.Not)(inventory.id) }
538
561
  });
539
562
  if (currentLocationInventoryCount == 0) {
540
563
  let currentLocation = await tx.getRepository(location_1.Location).findOne({
@@ -617,7 +640,7 @@ async function approveInventoryChanges(patches, context) {
617
640
  else {
618
641
  let latestLocationInventoryCount = await tx.getRepository(inventory_1.Inventory).count({
619
642
  where: {
620
- location: newRecord.location ? newRecord.location.id : inventory.location.id,
643
+ location: { id: newRecord.location ? newRecord.location.id : inventory.location.id },
621
644
  status: 'STORED',
622
645
  id: (0, typeorm_1.Not)(inventory.id)
623
646
  }
@@ -631,18 +654,31 @@ async function approveInventoryChanges(patches, context) {
631
654
  }
632
655
  await upsertInventoryItems(context, _inventoryChanges[i], 'APPROVED', tx);
633
656
  const companyDomain = (_a = inventory.bizplace) === null || _a === void 0 ? void 0 : _a.company.domain;
634
- const sellercraft = await tx
635
- .getRepository(integration_sellercraft_1.Sellercraft)
636
- .findOne({ where: { domain: customerDomain, status: integration_sellercraft_1.SellercraftStatus.ACTIVE }, relations: ['domain'] });
637
- if (sellercraft) {
638
- const sellercraftCtrl = new controllers_1.SellercraftController(tx, domain, user);
639
- await sellercraftCtrl.updateSellercraftStock(sellercraft, inventory);
640
- if ((newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) || (newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace)) {
641
- if (!(newRecord === null || newRecord === void 0 ? void 0 : newRecord.product))
642
- newRecord.product = inventory.product;
643
- await sellercraftCtrl.updateSellercraftStock(sellercraft, newRecord);
657
+ const sellercraftCtrl = new controllers_1.SellercraftController(tx, domain, user);
658
+ if (((_b = newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) === null || _b === void 0 ? void 0 : _b.id) && inventory.bizplace.id != ((_c = newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) === null || _c === void 0 ? void 0 : _c.id)) {
659
+ const prevBizSC = await tx.getRepository(integration_sellercraft_1.Sellercraft).findOne({
660
+ where: {
661
+ domain: { id: (_d = inventory.bizplace) === null || _d === void 0 ? void 0 : _d.domain.id },
662
+ status: integration_sellercraft_1.SellercraftStatus.ACTIVE
663
+ },
664
+ relations: ['domain']
665
+ });
666
+ if (prevBizSC) {
667
+ await sellercraftCtrl.updateSellercraftStock(prevBizSC, Object.assign(Object.assign({}, inventory), { bizplace: inventory.bizplace }));
644
668
  }
645
669
  }
670
+ const currentBizSC = await tx.getRepository(integration_sellercraft_1.Sellercraft).findOne({
671
+ where: {
672
+ domain: ((_e = newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) === null || _e === void 0 ? void 0 : _e.domain) || ((_f = inventory === null || inventory === void 0 ? void 0 : inventory.bizplace) === null || _f === void 0 ? void 0 : _f.domain),
673
+ status: integration_sellercraft_1.SellercraftStatus.ACTIVE
674
+ },
675
+ relations: ['domain']
676
+ });
677
+ if (currentBizSC) {
678
+ if (((_g = newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) === null || _g === void 0 ? void 0 : _g.id) && ((_h = newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) === null || _h === void 0 ? void 0 : _h.id) != ((_j = inventory === null || inventory === void 0 ? void 0 : inventory.product) === null || _j === void 0 ? void 0 : _j.id))
679
+ await sellercraftCtrl.updateSellercraftStock(currentBizSC, Object.assign(Object.assign({}, inventory), { bizplace: (newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) || (inventory === null || inventory === void 0 ? void 0 : inventory.bizplace), product: inventory.product }));
680
+ await sellercraftCtrl.updateSellercraftStock(currentBizSC, Object.assign(Object.assign({}, inventory), { bizplace: (newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) || (inventory === null || inventory === void 0 ? void 0 : inventory.bizplace), product: (newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) || (inventory === null || inventory === void 0 ? void 0 : inventory.product) }));
681
+ }
646
682
  // check for any existing active marketplace connection, update marketplace selling qty
647
683
  const marketplaceStores = await tx.getRepository(integration_marketplace_1.MarketplaceStore).find({
648
684
  where: { domain: companyDomain, status: 'ACTIVE', isAutoUpdateStockQty: true },
@@ -657,7 +693,7 @@ async function approveInventoryChanges(patches, context) {
657
693
  }
658
694
  // Adding Inventory
659
695
  else {
660
- const total = await tx.getRepository(inventory_1.Inventory).count({
696
+ const total = await tx.getRepository(inventory_1.Inventory).countBy({
661
697
  createdAt: (0, typeorm_1.MoreThan)(new Date(year, month, date))
662
698
  });
663
699
  const yy = String(year).substr(String(year).length - 2);
@@ -665,7 +701,7 @@ async function approveInventoryChanges(patches, context) {
665
701
  const dd = String(date).padStart(2, '0');
666
702
  const dateStr = yy + mm + dd;
667
703
  let palletId = newRecord.palletId;
668
- if (!((_b = newRecord.palletId) === null || _b === void 0 ? void 0 : _b.trim())) {
704
+ if (!((_k = newRecord.palletId) === null || _k === void 0 ? void 0 : _k.trim())) {
669
705
  palletId = await (0, id_rule_base_1.generateId)({
670
706
  domain: domain,
671
707
  type: 'adjustment_pallet_id',
@@ -688,10 +724,10 @@ async function approveInventoryChanges(patches, context) {
688
724
  newRecord.name = palletId;
689
725
  newRecord.palletId = palletId;
690
726
  const warehouseCartonSetting = await tx.getRepository(setting_base_1.Setting).findOne({
691
- where: { domain, category: 'id-rule', name: 'enable-carton-label' }
727
+ where: { domain: { id: domain.id }, category: 'id-rule', name: 'enable-carton-label' }
692
728
  });
693
729
  const partnerCartonSetting = await tx.getRepository(setting_base_1.PartnerSetting).findOne({
694
- where: { setting: warehouseCartonSetting, domain, partnerDomain: (_c = newRecord.bizplace) === null || _c === void 0 ? void 0 : _c.domain }
730
+ where: { setting: warehouseCartonSetting, domain, partnerDomain: (_l = newRecord.bizplace) === null || _l === void 0 ? void 0 : _l.domain }
695
731
  });
696
732
  if (partnerCartonSetting === null || partnerCartonSetting === void 0 ? void 0 : partnerCartonSetting.value) {
697
733
  let cartonId = await (0, id_rule_base_1.generateId)({
@@ -716,7 +752,7 @@ async function approveInventoryChanges(patches, context) {
716
752
  where: { domain: newRecord.bizplace.domain, status: integration_sellercraft_1.SellercraftStatus.ACTIVE },
717
753
  relations: ['domain']
718
754
  });
719
- const companyDomain = (_d = newRecord.bizplace) === null || _d === void 0 ? void 0 : _d.company.domain;
755
+ const companyDomain = (_m = newRecord.bizplace) === null || _m === void 0 ? void 0 : _m.company.domain;
720
756
  if (sellercraft) {
721
757
  const sellercraftCtrl = new controllers_1.SellercraftController(tx, domain, user);
722
758
  await sellercraftCtrl.updateSellercraftStock(sellercraft, newRecord);