@7365admin1/module-hygiene 4.28.1 → 4.28.2-staging.11
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.
- package/.changeset/hygiene-checkout-approval.md +5 -0
- package/.changeset/hygiene-checkout-stock-return.md +5 -0
- package/dist/index.d.ts +59 -2
- package/dist/index.js +176 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +171 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/test/hygiene-checkout-decision.util.test.mjs +168 -0
package/dist/index.mjs
CHANGED
|
@@ -5871,6 +5871,30 @@ function useStockController() {
|
|
|
5871
5871
|
};
|
|
5872
5872
|
}
|
|
5873
5873
|
|
|
5874
|
+
// src/utils/hygiene-checkout-decision.util.ts
|
|
5875
|
+
var CHECKOUT_DECISIONS = ["approve", "disapprove"];
|
|
5876
|
+
function isCheckOutDecision(value) {
|
|
5877
|
+
return CHECKOUT_DECISIONS.includes(value);
|
|
5878
|
+
}
|
|
5879
|
+
var CHECKOUT_DECIDABLE_STATUS = "pending";
|
|
5880
|
+
function buildCheckOutDecision(decision, remarks, now = (/* @__PURE__ */ new Date()).toISOString()) {
|
|
5881
|
+
const approve = decision === "approve";
|
|
5882
|
+
return {
|
|
5883
|
+
approve,
|
|
5884
|
+
reject: !approve,
|
|
5885
|
+
status: "completed",
|
|
5886
|
+
remarks: remarks ?? "",
|
|
5887
|
+
updatedAt: now
|
|
5888
|
+
};
|
|
5889
|
+
}
|
|
5890
|
+
async function applyCheckOutDecision(decision, decide, returnStock) {
|
|
5891
|
+
const decided = await decide();
|
|
5892
|
+
if (decided > 0 && decision === "disapprove") {
|
|
5893
|
+
await returnStock();
|
|
5894
|
+
}
|
|
5895
|
+
return decided;
|
|
5896
|
+
}
|
|
5897
|
+
|
|
5874
5898
|
// src/models/hygiene-checkout-item.model.ts
|
|
5875
5899
|
import Joi14 from "joi";
|
|
5876
5900
|
import { ObjectId as ObjectId16 } from "mongodb";
|
|
@@ -5919,6 +5943,10 @@ function MCheckOutItem(value) {
|
|
|
5919
5943
|
createdBy: value.createdBy,
|
|
5920
5944
|
createdByName: value.createdByName,
|
|
5921
5945
|
status: "pending",
|
|
5946
|
+
approve: false,
|
|
5947
|
+
reject: false,
|
|
5948
|
+
remarks: "",
|
|
5949
|
+
completedBy: "",
|
|
5922
5950
|
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
5923
5951
|
updatedAt: "",
|
|
5924
5952
|
deletedAt: ""
|
|
@@ -6058,7 +6086,10 @@ function useCheckOutItemRepository() {
|
|
|
6058
6086
|
profile: "$createdByDoc.profile",
|
|
6059
6087
|
checkOutQty: "$qty",
|
|
6060
6088
|
createdAt: 1,
|
|
6061
|
-
status: 1
|
|
6089
|
+
status: 1,
|
|
6090
|
+
approve: 1,
|
|
6091
|
+
reject: 1,
|
|
6092
|
+
remarks: 1
|
|
6062
6093
|
}
|
|
6063
6094
|
},
|
|
6064
6095
|
{ $sort: { _id: -1 } },
|
|
@@ -6124,7 +6155,10 @@ function useCheckOutItemRepository() {
|
|
|
6124
6155
|
status: 1,
|
|
6125
6156
|
unitOfMeasurement: "$supplyDetails.unitOfMeasurement",
|
|
6126
6157
|
stockQty: "$supplyQty",
|
|
6127
|
-
attachment: 1
|
|
6158
|
+
attachment: 1,
|
|
6159
|
+
approve: 1,
|
|
6160
|
+
reject: 1,
|
|
6161
|
+
remarks: 1
|
|
6128
6162
|
}
|
|
6129
6163
|
}
|
|
6130
6164
|
],
|
|
@@ -6175,13 +6209,55 @@ function useCheckOutItemRepository() {
|
|
|
6175
6209
|
throw error;
|
|
6176
6210
|
}
|
|
6177
6211
|
}
|
|
6212
|
+
async function decideCheckOutItem(_id, value, session) {
|
|
6213
|
+
try {
|
|
6214
|
+
_id = new ObjectId17(_id);
|
|
6215
|
+
} catch (error) {
|
|
6216
|
+
throw new BadRequestError25("Invalid check out item ID format.");
|
|
6217
|
+
}
|
|
6218
|
+
const updateValue = buildCheckOutDecision(
|
|
6219
|
+
value.approve === true ? "approve" : "disapprove",
|
|
6220
|
+
value.remarks
|
|
6221
|
+
);
|
|
6222
|
+
if (value.completedBy) {
|
|
6223
|
+
try {
|
|
6224
|
+
updateValue.completedBy = new ObjectId17(value.completedBy);
|
|
6225
|
+
} catch (error) {
|
|
6226
|
+
throw new BadRequestError25("Invalid user ID format.");
|
|
6227
|
+
}
|
|
6228
|
+
}
|
|
6229
|
+
try {
|
|
6230
|
+
const res = await collection.updateOne(
|
|
6231
|
+
{ _id, status: CHECKOUT_DECIDABLE_STATUS },
|
|
6232
|
+
{ $set: updateValue },
|
|
6233
|
+
{ session }
|
|
6234
|
+
);
|
|
6235
|
+
if (res.matchedCount === 0) {
|
|
6236
|
+
throw new NotFoundError6(
|
|
6237
|
+
"Check out item not found or is no longer pending approval."
|
|
6238
|
+
);
|
|
6239
|
+
}
|
|
6240
|
+
delNamespace().then(() => {
|
|
6241
|
+
logger28.info(`Cache cleared for namespace: ${namespace_collection}`);
|
|
6242
|
+
}).catch((err) => {
|
|
6243
|
+
logger28.error(
|
|
6244
|
+
`Failed to clear cache for namespace: ${namespace_collection}`,
|
|
6245
|
+
err
|
|
6246
|
+
);
|
|
6247
|
+
});
|
|
6248
|
+
return res.modifiedCount;
|
|
6249
|
+
} catch (error) {
|
|
6250
|
+
throw error;
|
|
6251
|
+
}
|
|
6252
|
+
}
|
|
6178
6253
|
return {
|
|
6179
6254
|
createIndex,
|
|
6180
6255
|
createTextIndex,
|
|
6181
6256
|
createCheckOutItem,
|
|
6182
6257
|
getCheckOutItems,
|
|
6183
6258
|
getCheckOutItemById,
|
|
6184
|
-
completeCheckOutItem
|
|
6259
|
+
completeCheckOutItem,
|
|
6260
|
+
decideCheckOutItem
|
|
6185
6261
|
};
|
|
6186
6262
|
}
|
|
6187
6263
|
|
|
@@ -6192,7 +6268,8 @@ function useCheckOutItemService() {
|
|
|
6192
6268
|
const {
|
|
6193
6269
|
createCheckOutItem: _createCheckOutItem,
|
|
6194
6270
|
getCheckOutItemById: _getCheckOutItemById,
|
|
6195
|
-
completeCheckOutItem
|
|
6271
|
+
completeCheckOutItem,
|
|
6272
|
+
decideCheckOutItem: _decideCheckOutItem
|
|
6196
6273
|
} = useCheckOutItemRepository();
|
|
6197
6274
|
const { getSupplyById } = useSupplyRepository();
|
|
6198
6275
|
const { getUserById } = useUserRepo2();
|
|
@@ -6283,9 +6360,41 @@ function useCheckOutItemService() {
|
|
|
6283
6360
|
await session?.endSession();
|
|
6284
6361
|
}
|
|
6285
6362
|
}
|
|
6363
|
+
async function decideCheckOutItem(_id, value) {
|
|
6364
|
+
const session = useAtlas13.getClient()?.startSession();
|
|
6365
|
+
try {
|
|
6366
|
+
session?.startTransaction();
|
|
6367
|
+
const checkOutItem = await _getCheckOutItemById(_id, session);
|
|
6368
|
+
if (!checkOutItem) {
|
|
6369
|
+
throw new BadRequestError26("Check out item not found.");
|
|
6370
|
+
}
|
|
6371
|
+
const decided = await applyCheckOutDecision(
|
|
6372
|
+
value.approve === true ? "approve" : "disapprove",
|
|
6373
|
+
() => _decideCheckOutItem(_id, value, session),
|
|
6374
|
+
() => createStock(
|
|
6375
|
+
{
|
|
6376
|
+
site: checkOutItem.site.toString(),
|
|
6377
|
+
serviceType: checkOutItem.serviceType,
|
|
6378
|
+
supply: checkOutItem.supply.toString(),
|
|
6379
|
+
qty: checkOutItem.qty
|
|
6380
|
+
},
|
|
6381
|
+
false,
|
|
6382
|
+
session
|
|
6383
|
+
)
|
|
6384
|
+
);
|
|
6385
|
+
await session?.commitTransaction();
|
|
6386
|
+
return decided;
|
|
6387
|
+
} catch (error) {
|
|
6388
|
+
await session?.abortTransaction();
|
|
6389
|
+
throw error;
|
|
6390
|
+
} finally {
|
|
6391
|
+
await session?.endSession();
|
|
6392
|
+
}
|
|
6393
|
+
}
|
|
6286
6394
|
return {
|
|
6287
6395
|
createCheckOutItem,
|
|
6288
|
-
createCheckOutItemByBatch
|
|
6396
|
+
createCheckOutItemByBatch,
|
|
6397
|
+
decideCheckOutItem
|
|
6289
6398
|
};
|
|
6290
6399
|
}
|
|
6291
6400
|
|
|
@@ -6300,7 +6409,8 @@ function useCheckOutItemController() {
|
|
|
6300
6409
|
} = useCheckOutItemRepository();
|
|
6301
6410
|
const {
|
|
6302
6411
|
createCheckOutItem: _createCheckOutItem,
|
|
6303
|
-
createCheckOutItemByBatch: _createCheckOutItemByBatch
|
|
6412
|
+
createCheckOutItemByBatch: _createCheckOutItemByBatch,
|
|
6413
|
+
decideCheckOutItem: _decideCheckOutItem
|
|
6304
6414
|
} = useCheckOutItemService();
|
|
6305
6415
|
async function createCheckOutItem(req, res, next) {
|
|
6306
6416
|
const cookies = req.headers.cookie ? req.headers.cookie.split(";").map((cookie) => cookie.trim().split("=")).reduce(
|
|
@@ -6431,11 +6541,60 @@ function useCheckOutItemController() {
|
|
|
6431
6541
|
return;
|
|
6432
6542
|
}
|
|
6433
6543
|
}
|
|
6544
|
+
async function decideCheckOutItem(req, res, next) {
|
|
6545
|
+
const cookies = req.headers.cookie ? req.headers.cookie.split(";").map((cookie) => cookie.trim().split("=")).reduce(
|
|
6546
|
+
(acc, [key, value]) => ({ ...acc, [key]: value }),
|
|
6547
|
+
{}
|
|
6548
|
+
) : {};
|
|
6549
|
+
const completedBy = cookies["user"] || "";
|
|
6550
|
+
const decisionMap = {
|
|
6551
|
+
approve: { approve: true, reject: false },
|
|
6552
|
+
disapprove: { approve: false, reject: true }
|
|
6553
|
+
};
|
|
6554
|
+
const decision = req.params.decision;
|
|
6555
|
+
const decisionValues = decisionMap[decision] || {
|
|
6556
|
+
approve: void 0,
|
|
6557
|
+
reject: void 0
|
|
6558
|
+
};
|
|
6559
|
+
const payload = {
|
|
6560
|
+
...req.params,
|
|
6561
|
+
...req.body,
|
|
6562
|
+
...decisionValues,
|
|
6563
|
+
completedBy
|
|
6564
|
+
};
|
|
6565
|
+
const validation = Joi15.object({
|
|
6566
|
+
id: Joi15.string().hex().required(),
|
|
6567
|
+
decision: Joi15.string().valid("approve", "disapprove").required(),
|
|
6568
|
+
approve: Joi15.boolean().required(),
|
|
6569
|
+
reject: Joi15.boolean().required(),
|
|
6570
|
+
remarks: Joi15.string().optional().allow("", null),
|
|
6571
|
+
completedBy: Joi15.string().hex().required()
|
|
6572
|
+
});
|
|
6573
|
+
const { error } = validation.validate(payload);
|
|
6574
|
+
if (error) {
|
|
6575
|
+
logger29.log({ level: "error", message: error.message });
|
|
6576
|
+
next(new BadRequestError27(error.message));
|
|
6577
|
+
return;
|
|
6578
|
+
}
|
|
6579
|
+
try {
|
|
6580
|
+
const { id, decision: _d, ...value } = payload;
|
|
6581
|
+
await _decideCheckOutItem(id, value);
|
|
6582
|
+
res.json({
|
|
6583
|
+
message: `Check out item ${decision === "approve" ? "approved" : "disapproved"} successfully.`
|
|
6584
|
+
});
|
|
6585
|
+
return;
|
|
6586
|
+
} catch (error2) {
|
|
6587
|
+
logger29.log({ level: "error", message: error2.message });
|
|
6588
|
+
next(error2);
|
|
6589
|
+
return;
|
|
6590
|
+
}
|
|
6591
|
+
}
|
|
6434
6592
|
return {
|
|
6435
6593
|
createCheckOutItem,
|
|
6436
6594
|
createCheckOutItemByBatch,
|
|
6437
6595
|
getCheckOutItems,
|
|
6438
|
-
getCheckOutItemById
|
|
6596
|
+
getCheckOutItemById,
|
|
6597
|
+
decideCheckOutItem
|
|
6439
6598
|
};
|
|
6440
6599
|
}
|
|
6441
6600
|
|
|
@@ -7484,6 +7643,8 @@ function useQRController() {
|
|
|
7484
7643
|
};
|
|
7485
7644
|
}
|
|
7486
7645
|
export {
|
|
7646
|
+
CHECKOUT_DECIDABLE_STATUS,
|
|
7647
|
+
CHECKOUT_DECISIONS,
|
|
7487
7648
|
MArea,
|
|
7488
7649
|
MAreaChecklist,
|
|
7489
7650
|
MCheckOutItem,
|
|
@@ -7497,9 +7658,12 @@ export {
|
|
|
7497
7658
|
allowedPeriods,
|
|
7498
7659
|
allowedStatus,
|
|
7499
7660
|
allowedTypes,
|
|
7661
|
+
applyCheckOutDecision,
|
|
7500
7662
|
areaChecklistSchema,
|
|
7501
7663
|
areaSchema,
|
|
7664
|
+
buildCheckOutDecision,
|
|
7502
7665
|
checkOutItemSchema,
|
|
7666
|
+
isCheckOutDecision,
|
|
7503
7667
|
parentChecklistSchema,
|
|
7504
7668
|
scheduleTaskSchema,
|
|
7505
7669
|
stockSchema,
|