@7365admin1/module-hygiene 4.0.0 → 4.2.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.
- package/CHANGELOG.md +12 -0
- package/dist/index.d.ts +72 -5
- package/dist/index.js +819 -222
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +752 -152
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -32,12 +32,14 @@ var src_exports = {};
|
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
MArea: () => MArea,
|
|
34
34
|
MAreaChecklist: () => MAreaChecklist,
|
|
35
|
+
MCheckOutItem: () => MCheckOutItem,
|
|
35
36
|
MParentChecklist: () => MParentChecklist,
|
|
36
37
|
MRequestItem: () => MRequestItem,
|
|
37
38
|
MScheduleTask: () => MScheduleTask,
|
|
38
39
|
MStock: () => MStock,
|
|
39
40
|
MSupply: () => MSupply,
|
|
40
41
|
MUnit: () => MUnit,
|
|
42
|
+
allowedCheckOutItemStatus: () => allowedCheckOutItemStatus,
|
|
41
43
|
allowedChecklistStatus: () => allowedChecklistStatus,
|
|
42
44
|
allowedPeriods: () => allowedPeriods,
|
|
43
45
|
allowedRequestItemStatus: () => allowedRequestItemStatus,
|
|
@@ -45,6 +47,7 @@ __export(src_exports, {
|
|
|
45
47
|
allowedTypes: () => allowedTypes,
|
|
46
48
|
areaChecklistSchema: () => areaChecklistSchema,
|
|
47
49
|
areaSchema: () => areaSchema,
|
|
50
|
+
checkOutItemSchema: () => checkOutItemSchema,
|
|
48
51
|
parentChecklistSchema: () => parentChecklistSchema,
|
|
49
52
|
requestItemSchema: () => requestItemSchema,
|
|
50
53
|
scheduleTaskSchema: () => scheduleTaskSchema,
|
|
@@ -57,6 +60,9 @@ __export(src_exports, {
|
|
|
57
60
|
useAreaController: () => useAreaController,
|
|
58
61
|
useAreaRepo: () => useAreaRepo,
|
|
59
62
|
useAreaService: () => useAreaService,
|
|
63
|
+
useCheckOutItemController: () => useCheckOutItemController,
|
|
64
|
+
useCheckOutItemRepository: () => useCheckOutItemRepository,
|
|
65
|
+
useCheckOutItemService: () => useCheckOutItemService,
|
|
60
66
|
useHygieneDashboardController: () => useHygieneDashboardController,
|
|
61
67
|
useHygieneDashboardRepository: () => useHygieneDashboardRepository,
|
|
62
68
|
useParentChecklistController: () => useParentChecklistController,
|
|
@@ -2312,7 +2318,8 @@ var areaChecklistSchema = import_joi8.default.object({
|
|
|
2312
2318
|
}).required()
|
|
2313
2319
|
).min(1).required()
|
|
2314
2320
|
}).required()
|
|
2315
|
-
).optional().default([])
|
|
2321
|
+
).optional().default([]),
|
|
2322
|
+
createdBy: import_joi8.default.string().hex().required()
|
|
2316
2323
|
});
|
|
2317
2324
|
function MAreaChecklist(value) {
|
|
2318
2325
|
const { error } = areaChecklistSchema.validate(value);
|
|
@@ -2349,6 +2356,13 @@ function MAreaChecklist(value) {
|
|
|
2349
2356
|
};
|
|
2350
2357
|
});
|
|
2351
2358
|
}
|
|
2359
|
+
if (value.createdBy) {
|
|
2360
|
+
try {
|
|
2361
|
+
value.createdBy = new import_mongodb8.ObjectId(value.createdBy);
|
|
2362
|
+
} catch (error2) {
|
|
2363
|
+
throw new import_node_server_utils14.BadRequestError("Invalid createdBy ID format.");
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2352
2366
|
return {
|
|
2353
2367
|
schedule: value.schedule,
|
|
2354
2368
|
area: value.area,
|
|
@@ -2356,6 +2370,7 @@ function MAreaChecklist(value) {
|
|
|
2356
2370
|
type: value.type,
|
|
2357
2371
|
checklist: value.checklist || [],
|
|
2358
2372
|
status: "ready",
|
|
2373
|
+
createdBy: value.createdBy,
|
|
2359
2374
|
createdAt: /* @__PURE__ */ new Date(),
|
|
2360
2375
|
completedAt: "",
|
|
2361
2376
|
updatedAt: ""
|
|
@@ -2486,6 +2501,32 @@ function useAreaChecklistRepo() {
|
|
|
2486
2501
|
try {
|
|
2487
2502
|
const pipeline = [
|
|
2488
2503
|
{ $match: query },
|
|
2504
|
+
{
|
|
2505
|
+
$lookup: {
|
|
2506
|
+
from: "users",
|
|
2507
|
+
let: { createdById: "$createdBy" },
|
|
2508
|
+
pipeline: [
|
|
2509
|
+
{
|
|
2510
|
+
$match: {
|
|
2511
|
+
$expr: {
|
|
2512
|
+
$and: [
|
|
2513
|
+
{ $ne: ["$$createdById", ""] },
|
|
2514
|
+
{ $eq: ["$_id", "$$createdById"] }
|
|
2515
|
+
]
|
|
2516
|
+
}
|
|
2517
|
+
}
|
|
2518
|
+
},
|
|
2519
|
+
{ $project: { name: 1 } }
|
|
2520
|
+
],
|
|
2521
|
+
as: "createdByDoc"
|
|
2522
|
+
}
|
|
2523
|
+
},
|
|
2524
|
+
{
|
|
2525
|
+
$unwind: {
|
|
2526
|
+
path: "$createdByDoc",
|
|
2527
|
+
preserveNullAndEmptyArrays: true
|
|
2528
|
+
}
|
|
2529
|
+
},
|
|
2489
2530
|
{
|
|
2490
2531
|
$project: {
|
|
2491
2532
|
name: 1,
|
|
@@ -2517,7 +2558,8 @@ function useAreaChecklistRepo() {
|
|
|
2517
2558
|
},
|
|
2518
2559
|
else: 0
|
|
2519
2560
|
}
|
|
2520
|
-
}
|
|
2561
|
+
},
|
|
2562
|
+
createdByName: "$createdByDoc.name"
|
|
2521
2563
|
}
|
|
2522
2564
|
},
|
|
2523
2565
|
{ $sort: { _id: -1 } },
|
|
@@ -3150,7 +3192,8 @@ function useAreaChecklistService() {
|
|
|
3150
3192
|
unit: unit.unit.toString(),
|
|
3151
3193
|
name: unit.name
|
|
3152
3194
|
}))
|
|
3153
|
-
}))
|
|
3195
|
+
})),
|
|
3196
|
+
createdBy: value.createdBy
|
|
3154
3197
|
};
|
|
3155
3198
|
const insertedId = await _createAreaChecklist(
|
|
3156
3199
|
checklistData,
|
|
@@ -3285,13 +3328,20 @@ function useAreaChecklistController() {
|
|
|
3285
3328
|
completeAreaChecklistUnits: _completeAreaChecklistUnits
|
|
3286
3329
|
} = useAreaChecklistService();
|
|
3287
3330
|
async function createAreaChecklist(req, res, next) {
|
|
3331
|
+
const cookies = req.headers.cookie ? req.headers.cookie.split(";").map((cookie) => cookie.trim().split("=")).reduce(
|
|
3332
|
+
(acc, [key, value2]) => ({ ...acc, [key]: value2 }),
|
|
3333
|
+
{}
|
|
3334
|
+
) : {};
|
|
3335
|
+
const createdBy = cookies["user"] || "";
|
|
3288
3336
|
const payload = {
|
|
3289
3337
|
site: req.params.site,
|
|
3290
|
-
schedule: req.params.schedule
|
|
3338
|
+
schedule: req.params.schedule,
|
|
3339
|
+
createdBy
|
|
3291
3340
|
};
|
|
3292
3341
|
const validation = import_joi9.default.object({
|
|
3293
3342
|
site: import_joi9.default.string().hex().required(),
|
|
3294
|
-
schedule: import_joi9.default.string().hex().required()
|
|
3343
|
+
schedule: import_joi9.default.string().hex().required(),
|
|
3344
|
+
createdBy: import_joi9.default.string().hex().required()
|
|
3295
3345
|
});
|
|
3296
3346
|
const { error, value } = validation.validate(payload);
|
|
3297
3347
|
if (error) {
|
|
@@ -4079,10 +4129,15 @@ var import_node_server_utils23 = require("@7365admin1/node-server-utils");
|
|
|
4079
4129
|
function useStockService() {
|
|
4080
4130
|
const { createStock: _createStock } = useStockRepository();
|
|
4081
4131
|
const { getSupplyById, updateSupply } = useSupplyRepository();
|
|
4082
|
-
async function createStock(value, out = false) {
|
|
4083
|
-
|
|
4132
|
+
async function createStock(value, out = false, session) {
|
|
4133
|
+
let ownSession = false;
|
|
4134
|
+
if (!session) {
|
|
4135
|
+
session = import_node_server_utils23.useAtlas.getClient()?.startSession();
|
|
4136
|
+
ownSession = true;
|
|
4137
|
+
}
|
|
4084
4138
|
try {
|
|
4085
|
-
|
|
4139
|
+
if (ownSession)
|
|
4140
|
+
session?.startTransaction();
|
|
4086
4141
|
const { qty, ...stockData } = value;
|
|
4087
4142
|
const supply = await getSupplyById(value.supply, session);
|
|
4088
4143
|
if (!supply || supply.qty === void 0) {
|
|
@@ -4104,13 +4159,16 @@ function useStockService() {
|
|
|
4104
4159
|
},
|
|
4105
4160
|
session
|
|
4106
4161
|
);
|
|
4107
|
-
|
|
4162
|
+
if (ownSession)
|
|
4163
|
+
await session?.commitTransaction();
|
|
4108
4164
|
return createdStock;
|
|
4109
4165
|
} catch (error) {
|
|
4110
|
-
|
|
4166
|
+
if (ownSession)
|
|
4167
|
+
await session?.abortTransaction();
|
|
4111
4168
|
throw error;
|
|
4112
4169
|
} finally {
|
|
4113
|
-
|
|
4170
|
+
if (ownSession)
|
|
4171
|
+
await session?.endSession();
|
|
4114
4172
|
}
|
|
4115
4173
|
}
|
|
4116
4174
|
return { createStock };
|
|
@@ -4790,28 +4848,24 @@ function useRequestItemController() {
|
|
|
4790
4848
|
};
|
|
4791
4849
|
}
|
|
4792
4850
|
|
|
4793
|
-
// src/models/hygiene-
|
|
4794
|
-
var import_node_server_utils29 = require("@7365admin1/node-server-utils");
|
|
4851
|
+
// src/models/hygiene-checkout-item.model.ts
|
|
4795
4852
|
var import_joi16 = __toESM(require("joi"));
|
|
4796
4853
|
var import_mongodb16 = require("mongodb");
|
|
4797
|
-
var
|
|
4854
|
+
var import_node_server_utils29 = require("@7365admin1/node-server-utils");
|
|
4855
|
+
var allowedCheckOutItemStatus = ["pending", "completed"];
|
|
4856
|
+
var checkOutItemSchema = import_joi16.default.object({
|
|
4798
4857
|
site: import_joi16.default.string().hex().required(),
|
|
4799
|
-
|
|
4800
|
-
|
|
4801
|
-
|
|
4802
|
-
|
|
4803
|
-
|
|
4804
|
-
|
|
4805
|
-
import_joi16.default.object({
|
|
4806
|
-
name: import_joi16.default.string().required(),
|
|
4807
|
-
value: import_joi16.default.any().required()
|
|
4808
|
-
})
|
|
4809
|
-
).required()
|
|
4858
|
+
supply: import_joi16.default.string().hex().required(),
|
|
4859
|
+
supplyName: import_joi16.default.string().required(),
|
|
4860
|
+
qty: import_joi16.default.number().min(0).required(),
|
|
4861
|
+
attachment: import_joi16.default.string().optional().allow("", null),
|
|
4862
|
+
createdBy: import_joi16.default.string().hex().required(),
|
|
4863
|
+
createdByName: import_joi16.default.string().required()
|
|
4810
4864
|
});
|
|
4811
|
-
function
|
|
4812
|
-
const { error } =
|
|
4865
|
+
function MCheckOutItem(value) {
|
|
4866
|
+
const { error } = checkOutItemSchema.validate(value);
|
|
4813
4867
|
if (error) {
|
|
4814
|
-
import_node_server_utils29.logger.info(`Hygiene
|
|
4868
|
+
import_node_server_utils29.logger.info(`Hygiene Check Out Item Model: ${error.message}`);
|
|
4815
4869
|
throw new import_node_server_utils29.BadRequestError(error.message);
|
|
4816
4870
|
}
|
|
4817
4871
|
if (value.site) {
|
|
@@ -4821,68 +4875,64 @@ function MScheduleTask(value) {
|
|
|
4821
4875
|
throw new import_node_server_utils29.BadRequestError("Invalid site ID format.");
|
|
4822
4876
|
}
|
|
4823
4877
|
}
|
|
4824
|
-
if (value.
|
|
4825
|
-
|
|
4826
|
-
|
|
4827
|
-
|
|
4828
|
-
|
|
4829
|
-
|
|
4830
|
-
};
|
|
4831
|
-
} catch (error2) {
|
|
4832
|
-
throw new import_node_server_utils29.BadRequestError(`Invalid area value format: ${area.name}`);
|
|
4833
|
-
}
|
|
4834
|
-
});
|
|
4878
|
+
if (value.supply) {
|
|
4879
|
+
try {
|
|
4880
|
+
value.supply = new import_mongodb16.ObjectId(value.supply);
|
|
4881
|
+
} catch (error2) {
|
|
4882
|
+
throw new import_node_server_utils29.BadRequestError("Invalid supply ID format.");
|
|
4883
|
+
}
|
|
4835
4884
|
}
|
|
4836
4885
|
return {
|
|
4837
4886
|
site: value.site,
|
|
4838
|
-
|
|
4839
|
-
|
|
4840
|
-
|
|
4841
|
-
|
|
4842
|
-
|
|
4843
|
-
|
|
4844
|
-
status: "
|
|
4845
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
4887
|
+
supply: value.supply,
|
|
4888
|
+
supplyName: value.supplyName,
|
|
4889
|
+
qty: value.qty,
|
|
4890
|
+
attachment: "",
|
|
4891
|
+
createdBy: value.createdBy,
|
|
4892
|
+
createdByName: value.createdByName,
|
|
4893
|
+
status: "pending",
|
|
4894
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4846
4895
|
updatedAt: "",
|
|
4847
4896
|
deletedAt: ""
|
|
4848
4897
|
};
|
|
4849
4898
|
}
|
|
4850
4899
|
|
|
4851
|
-
// src/repositories/hygiene-
|
|
4900
|
+
// src/repositories/hygiene-checkout-item.repository.ts
|
|
4852
4901
|
var import_mongodb17 = require("mongodb");
|
|
4853
4902
|
var import_node_server_utils30 = require("@7365admin1/node-server-utils");
|
|
4854
|
-
function
|
|
4903
|
+
function useCheckOutItemRepository() {
|
|
4855
4904
|
const db = import_node_server_utils30.useAtlas.getDb();
|
|
4856
4905
|
if (!db) {
|
|
4857
4906
|
throw new import_node_server_utils30.InternalServerError("Unable to connect to server.");
|
|
4858
4907
|
}
|
|
4859
|
-
const namespace_collection = "site.
|
|
4908
|
+
const namespace_collection = "site.supply.checkouts";
|
|
4860
4909
|
const collection = db.collection(namespace_collection);
|
|
4861
4910
|
const { delNamespace, setCache, getCache } = (0, import_node_server_utils30.useCache)(namespace_collection);
|
|
4862
4911
|
async function createIndex() {
|
|
4863
4912
|
try {
|
|
4864
4913
|
await collection.createIndexes([
|
|
4865
4914
|
{ key: { site: 1 } },
|
|
4915
|
+
{ key: { supply: 1 } },
|
|
4866
4916
|
{ key: { status: 1 } }
|
|
4867
4917
|
]);
|
|
4868
4918
|
} catch (error) {
|
|
4869
4919
|
throw new import_node_server_utils30.InternalServerError(
|
|
4870
|
-
"Failed to create index on hygiene
|
|
4920
|
+
"Failed to create index on hygiene check out item."
|
|
4871
4921
|
);
|
|
4872
4922
|
}
|
|
4873
4923
|
}
|
|
4874
4924
|
async function createTextIndex() {
|
|
4875
4925
|
try {
|
|
4876
|
-
await collection.createIndex({
|
|
4926
|
+
await collection.createIndex({ supplyName: "text" });
|
|
4877
4927
|
} catch (error) {
|
|
4878
4928
|
throw new import_node_server_utils30.InternalServerError(
|
|
4879
|
-
"Failed to create text index on hygiene
|
|
4929
|
+
"Failed to create text index on hygiene supply."
|
|
4880
4930
|
);
|
|
4881
4931
|
}
|
|
4882
4932
|
}
|
|
4883
|
-
async function
|
|
4933
|
+
async function createCheckOutItem(value, session) {
|
|
4884
4934
|
try {
|
|
4885
|
-
value =
|
|
4935
|
+
value = MCheckOutItem(value);
|
|
4886
4936
|
const res = await collection.insertOne(value, { session });
|
|
4887
4937
|
delNamespace().then(() => {
|
|
4888
4938
|
import_node_server_utils30.logger.info(`Cache cleared for namespace: ${namespace_collection}`);
|
|
@@ -4897,7 +4947,7 @@ function useScheduleTaskRepository() {
|
|
|
4897
4947
|
throw error;
|
|
4898
4948
|
}
|
|
4899
4949
|
}
|
|
4900
|
-
async function
|
|
4950
|
+
async function getCheckOutItems({
|
|
4901
4951
|
page = 1,
|
|
4902
4952
|
limit = 10,
|
|
4903
4953
|
search = "",
|
|
@@ -4919,7 +4969,7 @@ function useScheduleTaskRepository() {
|
|
|
4919
4969
|
throw new import_node_server_utils30.BadRequestError("Invalid site ID format.");
|
|
4920
4970
|
}
|
|
4921
4971
|
if (search) {
|
|
4922
|
-
query.$
|
|
4972
|
+
query.$text = { $search: search };
|
|
4923
4973
|
cacheOptions.search = search;
|
|
4924
4974
|
}
|
|
4925
4975
|
const cacheKey = (0, import_node_server_utils30.makeCacheKey)(namespace_collection, cacheOptions);
|
|
@@ -4932,77 +4982,65 @@ function useScheduleTaskRepository() {
|
|
|
4932
4982
|
const items = await collection.aggregate([
|
|
4933
4983
|
{ $match: query },
|
|
4934
4984
|
{
|
|
4935
|
-
$
|
|
4936
|
-
|
|
4937
|
-
|
|
4938
|
-
|
|
4985
|
+
$lookup: {
|
|
4986
|
+
from: "site.supplies",
|
|
4987
|
+
let: { supplyId: "$supply" },
|
|
4988
|
+
pipeline: [
|
|
4989
|
+
{
|
|
4990
|
+
$match: {
|
|
4991
|
+
$expr: {
|
|
4992
|
+
$and: [
|
|
4993
|
+
{ $ne: ["$$supplyId", ""] },
|
|
4994
|
+
{ $eq: ["$_id", "$$supplyId"] }
|
|
4995
|
+
]
|
|
4996
|
+
}
|
|
4997
|
+
}
|
|
4998
|
+
},
|
|
4999
|
+
{ $project: { qty: 1 } }
|
|
5000
|
+
],
|
|
5001
|
+
as: "supplyDoc"
|
|
5002
|
+
}
|
|
5003
|
+
},
|
|
5004
|
+
{
|
|
5005
|
+
$unwind: {
|
|
5006
|
+
path: "$supplyDoc",
|
|
5007
|
+
preserveNullAndEmptyArrays: true
|
|
5008
|
+
}
|
|
5009
|
+
},
|
|
5010
|
+
{
|
|
5011
|
+
$lookup: {
|
|
5012
|
+
from: "users",
|
|
5013
|
+
let: { createdById: "$createdBy" },
|
|
5014
|
+
pipeline: [
|
|
5015
|
+
{
|
|
5016
|
+
$match: {
|
|
5017
|
+
$expr: {
|
|
5018
|
+
$and: [
|
|
5019
|
+
{ $ne: ["$$createdById", ""] },
|
|
5020
|
+
{ $eq: ["$_id", "$$createdById"] }
|
|
5021
|
+
]
|
|
5022
|
+
}
|
|
5023
|
+
}
|
|
5024
|
+
},
|
|
5025
|
+
{ $project: { name: 1 } }
|
|
5026
|
+
],
|
|
5027
|
+
as: "createdByDoc"
|
|
5028
|
+
}
|
|
5029
|
+
},
|
|
5030
|
+
{
|
|
5031
|
+
$unwind: {
|
|
5032
|
+
path: "$createdByDoc",
|
|
5033
|
+
preserveNullAndEmptyArrays: true
|
|
4939
5034
|
}
|
|
4940
5035
|
},
|
|
4941
|
-
{ $sort: { _id: -1 } },
|
|
4942
|
-
{ $skip: page * limit },
|
|
4943
|
-
{ $limit: limit }
|
|
4944
|
-
]).toArray();
|
|
4945
|
-
const length = await collection.countDocuments(query);
|
|
4946
|
-
const data = (0, import_node_server_utils30.paginate)(items, page, limit, length);
|
|
4947
|
-
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
4948
|
-
import_node_server_utils30.logger.info(`Cache set for key: ${cacheKey}`);
|
|
4949
|
-
}).catch((err) => {
|
|
4950
|
-
import_node_server_utils30.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
4951
|
-
});
|
|
4952
|
-
return data;
|
|
4953
|
-
} catch (error) {
|
|
4954
|
-
throw error;
|
|
4955
|
-
}
|
|
4956
|
-
}
|
|
4957
|
-
async function getAllScheduleTask() {
|
|
4958
|
-
const query = {
|
|
4959
|
-
status: { $ne: "deleted" }
|
|
4960
|
-
};
|
|
4961
|
-
try {
|
|
4962
|
-
const items = await collection.aggregate([{ $match: query }, { $sort: { _id: -1 } }]).toArray();
|
|
4963
|
-
return items;
|
|
4964
|
-
} catch (error) {
|
|
4965
|
-
throw error;
|
|
4966
|
-
}
|
|
4967
|
-
}
|
|
4968
|
-
async function getTasksForScheduleTask({
|
|
4969
|
-
page = 1,
|
|
4970
|
-
limit = 10,
|
|
4971
|
-
search = "",
|
|
4972
|
-
site
|
|
4973
|
-
}) {
|
|
4974
|
-
page = page > 0 ? page - 1 : 0;
|
|
4975
|
-
const query = {
|
|
4976
|
-
status: { $ne: "deleted" }
|
|
4977
|
-
};
|
|
4978
|
-
const cacheOptions = {
|
|
4979
|
-
page,
|
|
4980
|
-
limit
|
|
4981
|
-
};
|
|
4982
|
-
try {
|
|
4983
|
-
site = new import_mongodb17.ObjectId(site);
|
|
4984
|
-
query.site = site;
|
|
4985
|
-
cacheOptions.site = site.toString();
|
|
4986
|
-
} catch (error) {
|
|
4987
|
-
throw new import_node_server_utils30.BadRequestError("Invalid site ID format.");
|
|
4988
|
-
}
|
|
4989
|
-
if (search) {
|
|
4990
|
-
query.$or = [{ name: { $regex: search, $options: "i" } }];
|
|
4991
|
-
cacheOptions.search = search;
|
|
4992
|
-
}
|
|
4993
|
-
const cacheKey = (0, import_node_server_utils30.makeCacheKey)(namespace_collection, cacheOptions);
|
|
4994
|
-
const cachedData = await getCache(cacheKey);
|
|
4995
|
-
if (cachedData) {
|
|
4996
|
-
import_node_server_utils30.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
4997
|
-
return cachedData;
|
|
4998
|
-
}
|
|
4999
|
-
try {
|
|
5000
|
-
const items = await collection.aggregate([
|
|
5001
|
-
{ $match: query },
|
|
5002
5036
|
{
|
|
5003
5037
|
$project: {
|
|
5038
|
+
supplyName: 1,
|
|
5039
|
+
supplyQty: "$supplyDoc.qty",
|
|
5040
|
+
checkOutByName: "$createdByDoc.name",
|
|
5041
|
+
checkOutQty: "$qty",
|
|
5004
5042
|
createdAt: 1,
|
|
5005
|
-
|
|
5043
|
+
status: 1
|
|
5006
5044
|
}
|
|
5007
5045
|
},
|
|
5008
5046
|
{ $sort: { _id: -1 } },
|
|
@@ -5021,16 +5059,13 @@ function useScheduleTaskRepository() {
|
|
|
5021
5059
|
throw error;
|
|
5022
5060
|
}
|
|
5023
5061
|
}
|
|
5024
|
-
async function
|
|
5062
|
+
async function getCheckOutItemById(_id, session) {
|
|
5025
5063
|
try {
|
|
5026
5064
|
_id = new import_mongodb17.ObjectId(_id);
|
|
5027
5065
|
} catch (error) {
|
|
5028
|
-
throw new import_node_server_utils30.BadRequestError("Invalid
|
|
5066
|
+
throw new import_node_server_utils30.BadRequestError("Invalid check out item ID format.");
|
|
5029
5067
|
}
|
|
5030
|
-
const query = {
|
|
5031
|
-
_id,
|
|
5032
|
-
status: { $ne: "deleted" }
|
|
5033
|
-
};
|
|
5068
|
+
const query = { _id };
|
|
5034
5069
|
const cacheKey = (0, import_node_server_utils30.makeCacheKey)(namespace_collection, {
|
|
5035
5070
|
_id: _id.toString()
|
|
5036
5071
|
});
|
|
@@ -5046,21 +5081,33 @@ function useScheduleTaskRepository() {
|
|
|
5046
5081
|
try {
|
|
5047
5082
|
const data = await collection.aggregate([
|
|
5048
5083
|
{ $match: query },
|
|
5084
|
+
{
|
|
5085
|
+
$lookup: {
|
|
5086
|
+
from: "site.supply.items",
|
|
5087
|
+
localField: "supply",
|
|
5088
|
+
foreignField: "_id",
|
|
5089
|
+
as: "supplyDetails"
|
|
5090
|
+
}
|
|
5091
|
+
},
|
|
5092
|
+
{
|
|
5093
|
+
$unwind: {
|
|
5094
|
+
path: "$supplyDetails",
|
|
5095
|
+
preserveNullAndEmptyArrays: true
|
|
5096
|
+
}
|
|
5097
|
+
},
|
|
5049
5098
|
{
|
|
5050
5099
|
$project: {
|
|
5051
|
-
|
|
5052
|
-
|
|
5053
|
-
|
|
5054
|
-
|
|
5055
|
-
description: 1,
|
|
5056
|
-
areas: 1,
|
|
5100
|
+
site: 1,
|
|
5101
|
+
supply: 1,
|
|
5102
|
+
supplyName: 1,
|
|
5103
|
+
qty: 1,
|
|
5057
5104
|
status: 1,
|
|
5058
|
-
|
|
5105
|
+
unitOfMeasurement: "$supplyDetails.unitOfMeasurement"
|
|
5059
5106
|
}
|
|
5060
5107
|
}
|
|
5061
5108
|
]).toArray();
|
|
5062
5109
|
if (!data || data.length === 0) {
|
|
5063
|
-
throw new import_node_server_utils30.NotFoundError("
|
|
5110
|
+
throw new import_node_server_utils30.NotFoundError("Check out item not found.");
|
|
5064
5111
|
}
|
|
5065
5112
|
setCache(cacheKey, data[0], 15 * 60).then(() => {
|
|
5066
5113
|
import_node_server_utils30.logger.info(`Cache set for key: ${cacheKey}`);
|
|
@@ -5072,21 +5119,559 @@ function useScheduleTaskRepository() {
|
|
|
5072
5119
|
throw error;
|
|
5073
5120
|
}
|
|
5074
5121
|
}
|
|
5075
|
-
|
|
5122
|
+
return {
|
|
5123
|
+
createIndex,
|
|
5124
|
+
createTextIndex,
|
|
5125
|
+
createCheckOutItem,
|
|
5126
|
+
getCheckOutItems,
|
|
5127
|
+
getCheckOutItemById
|
|
5128
|
+
};
|
|
5129
|
+
}
|
|
5130
|
+
|
|
5131
|
+
// src/services/hygiene-checkout-item.service.ts
|
|
5132
|
+
var import_core2 = require("@7365admin1/core");
|
|
5133
|
+
var import_node_server_utils31 = require("@7365admin1/node-server-utils");
|
|
5134
|
+
function useCheckOutItemService() {
|
|
5135
|
+
const {
|
|
5136
|
+
createCheckOutItem: _createCheckOutItem,
|
|
5137
|
+
getCheckOutItemById: _getCheckOutItemById
|
|
5138
|
+
} = useCheckOutItemRepository();
|
|
5139
|
+
const { getSupplyById } = useSupplyRepository();
|
|
5140
|
+
const { getUserById } = (0, import_core2.useUserRepo)();
|
|
5141
|
+
const { createStock } = useStockService();
|
|
5142
|
+
async function createCheckOutItem(value) {
|
|
5143
|
+
const session = import_node_server_utils31.useAtlas.getClient()?.startSession();
|
|
5076
5144
|
try {
|
|
5077
|
-
|
|
5145
|
+
session?.startTransaction();
|
|
5146
|
+
const supplyData = await getSupplyById(value.supply);
|
|
5147
|
+
const createdByData = await getUserById(value.createdBy);
|
|
5148
|
+
const checkOutItemId = await _createCheckOutItem(
|
|
5149
|
+
{
|
|
5150
|
+
...value,
|
|
5151
|
+
supplyName: supplyData?.name || "",
|
|
5152
|
+
createdByName: createdByData?.name || ""
|
|
5153
|
+
},
|
|
5154
|
+
session
|
|
5155
|
+
);
|
|
5156
|
+
const checkOutItem = await _getCheckOutItemById(checkOutItemId, session);
|
|
5157
|
+
if (!checkOutItem) {
|
|
5158
|
+
throw new import_node_server_utils31.BadRequestError("Failed to create check out item.");
|
|
5159
|
+
}
|
|
5160
|
+
const createdStocks = await createStock(
|
|
5161
|
+
{
|
|
5162
|
+
site: checkOutItem.site.toString(),
|
|
5163
|
+
supply: checkOutItem.supply.toString(),
|
|
5164
|
+
qty: checkOutItem.qty
|
|
5165
|
+
},
|
|
5166
|
+
true,
|
|
5167
|
+
session
|
|
5168
|
+
);
|
|
5169
|
+
await session?.commitTransaction();
|
|
5170
|
+
return createdStocks;
|
|
5078
5171
|
} catch (error) {
|
|
5079
|
-
|
|
5172
|
+
await session?.abortTransaction();
|
|
5173
|
+
throw error;
|
|
5174
|
+
} finally {
|
|
5175
|
+
await session?.endSession();
|
|
5176
|
+
}
|
|
5177
|
+
}
|
|
5178
|
+
async function createCheckOutItemByBatch(value) {
|
|
5179
|
+
const session = import_node_server_utils31.useAtlas.getClient()?.startSession();
|
|
5180
|
+
try {
|
|
5181
|
+
session?.startTransaction();
|
|
5182
|
+
const { site, createdBy, items } = value;
|
|
5183
|
+
const createdByData = await getUserById(createdBy);
|
|
5184
|
+
const createdCheckOutItemIds = [];
|
|
5185
|
+
for (const item of items) {
|
|
5186
|
+
const supplyData = await getSupplyById(item.supply, session);
|
|
5187
|
+
const createdId = await _createCheckOutItem(
|
|
5188
|
+
{
|
|
5189
|
+
site,
|
|
5190
|
+
supply: item.supply,
|
|
5191
|
+
supplyName: supplyData?.name || "",
|
|
5192
|
+
qty: item.qty,
|
|
5193
|
+
attachment: item.attachment || "",
|
|
5194
|
+
createdBy,
|
|
5195
|
+
createdByName: createdByData?.name || ""
|
|
5196
|
+
},
|
|
5197
|
+
session
|
|
5198
|
+
);
|
|
5199
|
+
await createStock(
|
|
5200
|
+
{
|
|
5201
|
+
site,
|
|
5202
|
+
supply: item.supply,
|
|
5203
|
+
qty: item.qty
|
|
5204
|
+
},
|
|
5205
|
+
true,
|
|
5206
|
+
session
|
|
5207
|
+
);
|
|
5208
|
+
createdCheckOutItemIds.push(createdId);
|
|
5209
|
+
}
|
|
5210
|
+
await session?.commitTransaction();
|
|
5211
|
+
return createdCheckOutItemIds;
|
|
5212
|
+
} catch (error) {
|
|
5213
|
+
await session?.abortTransaction();
|
|
5214
|
+
throw error;
|
|
5215
|
+
} finally {
|
|
5216
|
+
await session?.endSession();
|
|
5217
|
+
}
|
|
5218
|
+
}
|
|
5219
|
+
return {
|
|
5220
|
+
createCheckOutItem,
|
|
5221
|
+
createCheckOutItemByBatch
|
|
5222
|
+
};
|
|
5223
|
+
}
|
|
5224
|
+
|
|
5225
|
+
// src/controllers/hygiene-checkout-item.controller.ts
|
|
5226
|
+
var import_joi17 = __toESM(require("joi"));
|
|
5227
|
+
var import_node_server_utils32 = require("@7365admin1/node-server-utils");
|
|
5228
|
+
function useCheckOutItemController() {
|
|
5229
|
+
const {
|
|
5230
|
+
getCheckOutItems: _getCheckOutItems,
|
|
5231
|
+
getCheckOutItemById: _getCheckOutItemById
|
|
5232
|
+
} = useCheckOutItemRepository();
|
|
5233
|
+
const {
|
|
5234
|
+
createCheckOutItem: _createCheckOutItem,
|
|
5235
|
+
createCheckOutItemByBatch: _createCheckOutItemByBatch
|
|
5236
|
+
} = useCheckOutItemService();
|
|
5237
|
+
async function createCheckOutItem(req, res, next) {
|
|
5238
|
+
const cookies = req.headers.cookie ? req.headers.cookie.split(";").map((cookie) => cookie.trim().split("=")).reduce(
|
|
5239
|
+
(acc, [key, value]) => ({ ...acc, [key]: value }),
|
|
5240
|
+
{}
|
|
5241
|
+
) : {};
|
|
5242
|
+
const createdBy = cookies["user"] || "";
|
|
5243
|
+
const payload = {
|
|
5244
|
+
...req.body,
|
|
5245
|
+
...req.params,
|
|
5246
|
+
createdBy
|
|
5247
|
+
};
|
|
5248
|
+
const validation = import_joi17.default.object({
|
|
5249
|
+
site: import_joi17.default.string().hex().required(),
|
|
5250
|
+
supply: import_joi17.default.string().hex().required(),
|
|
5251
|
+
qty: import_joi17.default.number().min(0).required(),
|
|
5252
|
+
attachment: import_joi17.default.string().optional().allow("", null),
|
|
5253
|
+
createdBy: import_joi17.default.string().hex().required()
|
|
5254
|
+
});
|
|
5255
|
+
const { error } = validation.validate(payload);
|
|
5256
|
+
if (error) {
|
|
5257
|
+
import_node_server_utils32.logger.log({ level: "error", message: error.message });
|
|
5258
|
+
next(new import_node_server_utils32.BadRequestError(error.message));
|
|
5259
|
+
return;
|
|
5260
|
+
}
|
|
5261
|
+
try {
|
|
5262
|
+
const id = await _createCheckOutItem(payload);
|
|
5263
|
+
res.status(201).json({ message: "Check out item created successfully.", id });
|
|
5264
|
+
return;
|
|
5265
|
+
} catch (error2) {
|
|
5266
|
+
import_node_server_utils32.logger.log({ level: "error", message: error2.message });
|
|
5267
|
+
next(error2);
|
|
5268
|
+
return;
|
|
5269
|
+
}
|
|
5270
|
+
}
|
|
5271
|
+
async function createCheckOutItemByBatch(req, res, next) {
|
|
5272
|
+
const cookies = req.headers.cookie ? req.headers.cookie.split(";").map((cookie) => cookie.trim().split("=")).reduce(
|
|
5273
|
+
(acc, [key, value]) => ({ ...acc, [key]: value }),
|
|
5274
|
+
{}
|
|
5275
|
+
) : {};
|
|
5276
|
+
const createdBy = cookies["user"] || "";
|
|
5277
|
+
const payload = {
|
|
5278
|
+
...req.body,
|
|
5279
|
+
...req.params,
|
|
5280
|
+
createdBy
|
|
5281
|
+
};
|
|
5282
|
+
const validation = import_joi17.default.object({
|
|
5283
|
+
site: import_joi17.default.string().hex().required(),
|
|
5284
|
+
createdBy: import_joi17.default.string().hex().required(),
|
|
5285
|
+
items: import_joi17.default.array().items(
|
|
5286
|
+
import_joi17.default.object({
|
|
5287
|
+
supply: import_joi17.default.string().hex().required(),
|
|
5288
|
+
qty: import_joi17.default.number().min(0).required(),
|
|
5289
|
+
attachment: import_joi17.default.string().optional().allow("", null)
|
|
5290
|
+
})
|
|
5291
|
+
).min(1).required()
|
|
5292
|
+
});
|
|
5293
|
+
const { error } = validation.validate(payload);
|
|
5294
|
+
if (error) {
|
|
5295
|
+
import_node_server_utils32.logger.log({ level: "error", message: error.message });
|
|
5296
|
+
next(new import_node_server_utils32.BadRequestError(error.message));
|
|
5297
|
+
return;
|
|
5298
|
+
}
|
|
5299
|
+
try {
|
|
5300
|
+
await _createCheckOutItemByBatch(payload);
|
|
5301
|
+
res.status(201).json({ message: "Check out items created successfully." });
|
|
5302
|
+
return;
|
|
5303
|
+
} catch (error2) {
|
|
5304
|
+
import_node_server_utils32.logger.log({ level: "error", message: error2.message });
|
|
5305
|
+
next(error2);
|
|
5306
|
+
return;
|
|
5307
|
+
}
|
|
5308
|
+
}
|
|
5309
|
+
async function getCheckOutItems(req, res, next) {
|
|
5310
|
+
const query = { ...req.query, ...req.params };
|
|
5311
|
+
const validation = import_joi17.default.object({
|
|
5312
|
+
page: import_joi17.default.number().min(1).optional().allow("", null),
|
|
5313
|
+
limit: import_joi17.default.number().min(1).optional().allow("", null),
|
|
5314
|
+
search: import_joi17.default.string().optional().allow("", null),
|
|
5315
|
+
site: import_joi17.default.string().hex().required()
|
|
5316
|
+
});
|
|
5317
|
+
const { error } = validation.validate(query);
|
|
5318
|
+
if (error) {
|
|
5319
|
+
import_node_server_utils32.logger.log({ level: "error", message: error.message });
|
|
5320
|
+
next(new import_node_server_utils32.BadRequestError(error.message));
|
|
5321
|
+
return;
|
|
5322
|
+
}
|
|
5323
|
+
const page = parseInt(req.query.page) ?? 1;
|
|
5324
|
+
const limit = parseInt(req.query.limit) ?? 10;
|
|
5325
|
+
const search = req.query.search ?? "";
|
|
5326
|
+
const site = req.params.site ?? "";
|
|
5327
|
+
try {
|
|
5328
|
+
const data = await _getCheckOutItems({
|
|
5329
|
+
page,
|
|
5330
|
+
limit,
|
|
5331
|
+
search,
|
|
5332
|
+
site
|
|
5333
|
+
});
|
|
5334
|
+
res.json(data);
|
|
5335
|
+
return;
|
|
5336
|
+
} catch (error2) {
|
|
5337
|
+
import_node_server_utils32.logger.log({ level: "error", message: error2.message });
|
|
5338
|
+
next(error2);
|
|
5339
|
+
return;
|
|
5340
|
+
}
|
|
5341
|
+
}
|
|
5342
|
+
async function getCheckOutItemById(req, res, next) {
|
|
5343
|
+
const validation = import_joi17.default.string().hex().required();
|
|
5344
|
+
const _id = req.params.id;
|
|
5345
|
+
const { error, value } = validation.validate(_id);
|
|
5346
|
+
if (error) {
|
|
5347
|
+
import_node_server_utils32.logger.log({ level: "error", message: error.message });
|
|
5348
|
+
next(new import_node_server_utils32.BadRequestError(error.message));
|
|
5349
|
+
return;
|
|
5350
|
+
}
|
|
5351
|
+
try {
|
|
5352
|
+
const data = await _getCheckOutItemById(value);
|
|
5353
|
+
res.json(data);
|
|
5354
|
+
return;
|
|
5355
|
+
} catch (error2) {
|
|
5356
|
+
import_node_server_utils32.logger.log({ level: "error", message: error2.message });
|
|
5357
|
+
next(error2);
|
|
5358
|
+
return;
|
|
5359
|
+
}
|
|
5360
|
+
}
|
|
5361
|
+
return {
|
|
5362
|
+
createCheckOutItem,
|
|
5363
|
+
createCheckOutItemByBatch,
|
|
5364
|
+
getCheckOutItems,
|
|
5365
|
+
getCheckOutItemById
|
|
5366
|
+
};
|
|
5367
|
+
}
|
|
5368
|
+
|
|
5369
|
+
// src/models/hygiene-schedule-task.model.ts
|
|
5370
|
+
var import_node_server_utils33 = require("@7365admin1/node-server-utils");
|
|
5371
|
+
var import_joi18 = __toESM(require("joi"));
|
|
5372
|
+
var import_mongodb18 = require("mongodb");
|
|
5373
|
+
var scheduleTaskSchema = import_joi18.default.object({
|
|
5374
|
+
site: import_joi18.default.string().hex().required(),
|
|
5375
|
+
title: import_joi18.default.string().required(),
|
|
5376
|
+
time: import_joi18.default.string().pattern(/^([0-1]\d|2[0-3]):([0-5]\d)$/).required(),
|
|
5377
|
+
startDate: import_joi18.default.string().pattern(/^\d{4}-\d{2}-\d{2}$/).required(),
|
|
5378
|
+
endDate: import_joi18.default.string().pattern(/^\d{4}-\d{2}-\d{2}$/).optional().allow("", null),
|
|
5379
|
+
description: import_joi18.default.string().optional().allow("", null),
|
|
5380
|
+
areas: import_joi18.default.array().min(1).items(
|
|
5381
|
+
import_joi18.default.object({
|
|
5382
|
+
name: import_joi18.default.string().required(),
|
|
5383
|
+
value: import_joi18.default.any().required()
|
|
5384
|
+
})
|
|
5385
|
+
).required(),
|
|
5386
|
+
createdBy: import_joi18.default.string().hex().required()
|
|
5387
|
+
});
|
|
5388
|
+
function MScheduleTask(value) {
|
|
5389
|
+
const { error } = scheduleTaskSchema.validate(value);
|
|
5390
|
+
if (error) {
|
|
5391
|
+
import_node_server_utils33.logger.info(`Hygiene Schedule Task Model: ${error.message}`);
|
|
5392
|
+
throw new import_node_server_utils33.BadRequestError(error.message);
|
|
5393
|
+
}
|
|
5394
|
+
if (value.site) {
|
|
5395
|
+
try {
|
|
5396
|
+
value.site = new import_mongodb18.ObjectId(value.site);
|
|
5397
|
+
} catch (error2) {
|
|
5398
|
+
throw new import_node_server_utils33.BadRequestError("Invalid site ID format.");
|
|
5399
|
+
}
|
|
5400
|
+
}
|
|
5401
|
+
if (value.areas && Array.isArray(value.areas)) {
|
|
5402
|
+
value.areas = value.areas.map((area) => {
|
|
5403
|
+
try {
|
|
5404
|
+
return {
|
|
5405
|
+
name: area.name,
|
|
5406
|
+
value: new import_mongodb18.ObjectId(area.value.toString())
|
|
5407
|
+
};
|
|
5408
|
+
} catch (error2) {
|
|
5409
|
+
throw new import_node_server_utils33.BadRequestError(`Invalid area value format: ${area.name}`);
|
|
5410
|
+
}
|
|
5411
|
+
});
|
|
5412
|
+
}
|
|
5413
|
+
if (value.createdBy) {
|
|
5414
|
+
try {
|
|
5415
|
+
value.createdBy = new import_mongodb18.ObjectId(value.createdBy);
|
|
5416
|
+
} catch (error2) {
|
|
5417
|
+
throw new import_node_server_utils33.BadRequestError("Invalid createdBy ID format.");
|
|
5418
|
+
}
|
|
5419
|
+
}
|
|
5420
|
+
return {
|
|
5421
|
+
site: value.site,
|
|
5422
|
+
title: value.title,
|
|
5423
|
+
time: value.time,
|
|
5424
|
+
startDate: value.startDate,
|
|
5425
|
+
endDate: value.endDate,
|
|
5426
|
+
description: value.description,
|
|
5427
|
+
areas: value.areas,
|
|
5428
|
+
status: "active",
|
|
5429
|
+
createdBy: value.createdBy,
|
|
5430
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
5431
|
+
updatedAt: "",
|
|
5432
|
+
deletedAt: ""
|
|
5433
|
+
};
|
|
5434
|
+
}
|
|
5435
|
+
|
|
5436
|
+
// src/repositories/hygiene-schedule-task.repository.ts
|
|
5437
|
+
var import_mongodb19 = require("mongodb");
|
|
5438
|
+
var import_node_server_utils34 = require("@7365admin1/node-server-utils");
|
|
5439
|
+
function useScheduleTaskRepository() {
|
|
5440
|
+
const db = import_node_server_utils34.useAtlas.getDb();
|
|
5441
|
+
if (!db) {
|
|
5442
|
+
throw new import_node_server_utils34.InternalServerError("Unable to connect to server.");
|
|
5443
|
+
}
|
|
5444
|
+
const namespace_collection = "site.schedule-tasks";
|
|
5445
|
+
const collection = db.collection(namespace_collection);
|
|
5446
|
+
const { delNamespace, setCache, getCache } = (0, import_node_server_utils34.useCache)(namespace_collection);
|
|
5447
|
+
async function createIndex() {
|
|
5448
|
+
try {
|
|
5449
|
+
await collection.createIndexes([
|
|
5450
|
+
{ key: { site: 1 } },
|
|
5451
|
+
{ key: { status: 1 } }
|
|
5452
|
+
]);
|
|
5453
|
+
} catch (error) {
|
|
5454
|
+
throw new import_node_server_utils34.InternalServerError(
|
|
5455
|
+
"Failed to create index on hygiene schedule task."
|
|
5456
|
+
);
|
|
5457
|
+
}
|
|
5458
|
+
}
|
|
5459
|
+
async function createTextIndex() {
|
|
5460
|
+
try {
|
|
5461
|
+
await collection.createIndex({ title: "text", description: "text" });
|
|
5462
|
+
} catch (error) {
|
|
5463
|
+
throw new import_node_server_utils34.InternalServerError(
|
|
5464
|
+
"Failed to create text index on hygiene schedule task."
|
|
5465
|
+
);
|
|
5466
|
+
}
|
|
5467
|
+
}
|
|
5468
|
+
async function createScheduleTask(value, session) {
|
|
5469
|
+
try {
|
|
5470
|
+
value = MScheduleTask(value);
|
|
5471
|
+
const res = await collection.insertOne(value, { session });
|
|
5472
|
+
delNamespace().then(() => {
|
|
5473
|
+
import_node_server_utils34.logger.info(`Cache cleared for namespace: ${namespace_collection}`);
|
|
5474
|
+
}).catch((err) => {
|
|
5475
|
+
import_node_server_utils34.logger.error(
|
|
5476
|
+
`Failed to clear cache for namespace: ${namespace_collection}`,
|
|
5477
|
+
err
|
|
5478
|
+
);
|
|
5479
|
+
});
|
|
5480
|
+
return res.insertedId;
|
|
5481
|
+
} catch (error) {
|
|
5482
|
+
throw error;
|
|
5483
|
+
}
|
|
5484
|
+
}
|
|
5485
|
+
async function getScheduleTasks({
|
|
5486
|
+
page = 1,
|
|
5487
|
+
limit = 10,
|
|
5488
|
+
search = "",
|
|
5489
|
+
site
|
|
5490
|
+
}) {
|
|
5491
|
+
page = page > 0 ? page - 1 : 0;
|
|
5492
|
+
const query = {
|
|
5493
|
+
status: { $ne: "deleted" }
|
|
5494
|
+
};
|
|
5495
|
+
const cacheOptions = {
|
|
5496
|
+
page,
|
|
5497
|
+
limit
|
|
5498
|
+
};
|
|
5499
|
+
try {
|
|
5500
|
+
site = new import_mongodb19.ObjectId(site);
|
|
5501
|
+
query.site = site;
|
|
5502
|
+
cacheOptions.site = site.toString();
|
|
5503
|
+
} catch (error) {
|
|
5504
|
+
throw new import_node_server_utils34.BadRequestError("Invalid site ID format.");
|
|
5505
|
+
}
|
|
5506
|
+
if (search) {
|
|
5507
|
+
query.$or = [{ name: { $regex: search, $options: "i" } }];
|
|
5508
|
+
cacheOptions.search = search;
|
|
5509
|
+
}
|
|
5510
|
+
const cacheKey = (0, import_node_server_utils34.makeCacheKey)(namespace_collection, cacheOptions);
|
|
5511
|
+
const cachedData = await getCache(cacheKey);
|
|
5512
|
+
if (cachedData) {
|
|
5513
|
+
import_node_server_utils34.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
5514
|
+
return cachedData;
|
|
5515
|
+
}
|
|
5516
|
+
try {
|
|
5517
|
+
const items = await collection.aggregate([
|
|
5518
|
+
{ $match: query },
|
|
5519
|
+
{
|
|
5520
|
+
$project: {
|
|
5521
|
+
title: 1,
|
|
5522
|
+
areas: 1,
|
|
5523
|
+
status: 1
|
|
5524
|
+
}
|
|
5525
|
+
},
|
|
5526
|
+
{ $sort: { _id: -1 } },
|
|
5527
|
+
{ $skip: page * limit },
|
|
5528
|
+
{ $limit: limit }
|
|
5529
|
+
]).toArray();
|
|
5530
|
+
const length = await collection.countDocuments(query);
|
|
5531
|
+
const data = (0, import_node_server_utils34.paginate)(items, page, limit, length);
|
|
5532
|
+
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
5533
|
+
import_node_server_utils34.logger.info(`Cache set for key: ${cacheKey}`);
|
|
5534
|
+
}).catch((err) => {
|
|
5535
|
+
import_node_server_utils34.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
5536
|
+
});
|
|
5537
|
+
return data;
|
|
5538
|
+
} catch (error) {
|
|
5539
|
+
throw error;
|
|
5540
|
+
}
|
|
5541
|
+
}
|
|
5542
|
+
async function getAllScheduleTask() {
|
|
5543
|
+
const query = {
|
|
5544
|
+
status: { $ne: "deleted" }
|
|
5545
|
+
};
|
|
5546
|
+
try {
|
|
5547
|
+
const items = await collection.aggregate([{ $match: query }, { $sort: { _id: -1 } }]).toArray();
|
|
5548
|
+
return items;
|
|
5549
|
+
} catch (error) {
|
|
5550
|
+
throw error;
|
|
5551
|
+
}
|
|
5552
|
+
}
|
|
5553
|
+
async function getTasksForScheduleTask({
|
|
5554
|
+
page = 1,
|
|
5555
|
+
limit = 10,
|
|
5556
|
+
search = "",
|
|
5557
|
+
site
|
|
5558
|
+
}) {
|
|
5559
|
+
page = page > 0 ? page - 1 : 0;
|
|
5560
|
+
const query = {
|
|
5561
|
+
status: { $ne: "deleted" }
|
|
5562
|
+
};
|
|
5563
|
+
const cacheOptions = {
|
|
5564
|
+
page,
|
|
5565
|
+
limit
|
|
5566
|
+
};
|
|
5567
|
+
try {
|
|
5568
|
+
site = new import_mongodb19.ObjectId(site);
|
|
5569
|
+
query.site = site;
|
|
5570
|
+
cacheOptions.site = site.toString();
|
|
5571
|
+
} catch (error) {
|
|
5572
|
+
throw new import_node_server_utils34.BadRequestError("Invalid site ID format.");
|
|
5573
|
+
}
|
|
5574
|
+
if (search) {
|
|
5575
|
+
query.$or = [{ name: { $regex: search, $options: "i" } }];
|
|
5576
|
+
cacheOptions.search = search;
|
|
5577
|
+
}
|
|
5578
|
+
const cacheKey = (0, import_node_server_utils34.makeCacheKey)(namespace_collection, cacheOptions);
|
|
5579
|
+
const cachedData = await getCache(cacheKey);
|
|
5580
|
+
if (cachedData) {
|
|
5581
|
+
import_node_server_utils34.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
5582
|
+
return cachedData;
|
|
5583
|
+
}
|
|
5584
|
+
try {
|
|
5585
|
+
const items = await collection.aggregate([
|
|
5586
|
+
{ $match: query },
|
|
5587
|
+
{
|
|
5588
|
+
$project: {
|
|
5589
|
+
createdAt: 1,
|
|
5590
|
+
title: 1
|
|
5591
|
+
}
|
|
5592
|
+
},
|
|
5593
|
+
{ $sort: { _id: -1 } },
|
|
5594
|
+
{ $skip: page * limit },
|
|
5595
|
+
{ $limit: limit }
|
|
5596
|
+
]).toArray();
|
|
5597
|
+
const length = await collection.countDocuments(query);
|
|
5598
|
+
const data = (0, import_node_server_utils34.paginate)(items, page, limit, length);
|
|
5599
|
+
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
5600
|
+
import_node_server_utils34.logger.info(`Cache set for key: ${cacheKey}`);
|
|
5601
|
+
}).catch((err) => {
|
|
5602
|
+
import_node_server_utils34.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
5603
|
+
});
|
|
5604
|
+
return data;
|
|
5605
|
+
} catch (error) {
|
|
5606
|
+
throw error;
|
|
5607
|
+
}
|
|
5608
|
+
}
|
|
5609
|
+
async function getScheduleTaskById(_id, session) {
|
|
5610
|
+
try {
|
|
5611
|
+
_id = new import_mongodb19.ObjectId(_id);
|
|
5612
|
+
} catch (error) {
|
|
5613
|
+
throw new import_node_server_utils34.BadRequestError("Invalid schedule task ID format.");
|
|
5614
|
+
}
|
|
5615
|
+
const query = {
|
|
5616
|
+
_id,
|
|
5617
|
+
status: { $ne: "deleted" }
|
|
5618
|
+
};
|
|
5619
|
+
const cacheKey = (0, import_node_server_utils34.makeCacheKey)(namespace_collection, {
|
|
5620
|
+
_id: _id.toString()
|
|
5621
|
+
});
|
|
5622
|
+
if (!session) {
|
|
5623
|
+
const cachedData = await getCache(cacheKey);
|
|
5624
|
+
if (cachedData) {
|
|
5625
|
+
import_node_server_utils34.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
5626
|
+
return cachedData;
|
|
5627
|
+
}
|
|
5628
|
+
} else {
|
|
5629
|
+
import_node_server_utils34.logger.info(`Skipping cache during transaction for key: ${cacheKey}`);
|
|
5630
|
+
}
|
|
5631
|
+
try {
|
|
5632
|
+
const data = await collection.aggregate([
|
|
5633
|
+
{ $match: query },
|
|
5634
|
+
{
|
|
5635
|
+
$project: {
|
|
5636
|
+
title: 1,
|
|
5637
|
+
time: 1,
|
|
5638
|
+
startDate: 1,
|
|
5639
|
+
endDate: 1,
|
|
5640
|
+
description: 1,
|
|
5641
|
+
areas: 1,
|
|
5642
|
+
status: 1,
|
|
5643
|
+
createdAt: 1
|
|
5644
|
+
}
|
|
5645
|
+
}
|
|
5646
|
+
]).toArray();
|
|
5647
|
+
if (!data || data.length === 0) {
|
|
5648
|
+
throw new import_node_server_utils34.NotFoundError("Schedule task not found.");
|
|
5649
|
+
}
|
|
5650
|
+
setCache(cacheKey, data[0], 15 * 60).then(() => {
|
|
5651
|
+
import_node_server_utils34.logger.info(`Cache set for key: ${cacheKey}`);
|
|
5652
|
+
}).catch((err) => {
|
|
5653
|
+
import_node_server_utils34.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
5654
|
+
});
|
|
5655
|
+
return data[0];
|
|
5656
|
+
} catch (error) {
|
|
5657
|
+
throw error;
|
|
5658
|
+
}
|
|
5659
|
+
}
|
|
5660
|
+
async function updateScheduleTask(_id, value, session) {
|
|
5661
|
+
try {
|
|
5662
|
+
_id = new import_mongodb19.ObjectId(_id);
|
|
5663
|
+
} catch (error) {
|
|
5664
|
+
throw new import_node_server_utils34.BadRequestError("Invalid schedule task ID format.");
|
|
5080
5665
|
}
|
|
5081
5666
|
if (value.areas && Array.isArray(value.areas)) {
|
|
5082
5667
|
value.areas = value.areas.map((area) => {
|
|
5083
5668
|
try {
|
|
5084
5669
|
return {
|
|
5085
5670
|
name: area.name,
|
|
5086
|
-
value: new
|
|
5671
|
+
value: new import_mongodb19.ObjectId(area.value.toString())
|
|
5087
5672
|
};
|
|
5088
5673
|
} catch (error) {
|
|
5089
|
-
throw new
|
|
5674
|
+
throw new import_node_server_utils34.BadRequestError(`Invalid area value format: ${area.name}`);
|
|
5090
5675
|
}
|
|
5091
5676
|
});
|
|
5092
5677
|
}
|
|
@@ -5098,14 +5683,14 @@ function useScheduleTaskRepository() {
|
|
|
5098
5683
|
{ session }
|
|
5099
5684
|
);
|
|
5100
5685
|
if (res.modifiedCount === 0) {
|
|
5101
|
-
throw new
|
|
5686
|
+
throw new import_node_server_utils34.InternalServerError(
|
|
5102
5687
|
"Unable to update hygiene schedule task."
|
|
5103
5688
|
);
|
|
5104
5689
|
}
|
|
5105
5690
|
delNamespace().then(() => {
|
|
5106
|
-
|
|
5691
|
+
import_node_server_utils34.logger.info(`Cache cleared for namespace: ${namespace_collection}`);
|
|
5107
5692
|
}).catch((err) => {
|
|
5108
|
-
|
|
5693
|
+
import_node_server_utils34.logger.error(
|
|
5109
5694
|
`Failed to clear cache for namespace: ${namespace_collection}`,
|
|
5110
5695
|
err
|
|
5111
5696
|
);
|
|
@@ -5128,10 +5713,10 @@ function useScheduleTaskRepository() {
|
|
|
5128
5713
|
}
|
|
5129
5714
|
|
|
5130
5715
|
// src/services/hygiene-schedule-task.service.ts
|
|
5131
|
-
var
|
|
5716
|
+
var import_node_server_utils35 = require("@7365admin1/node-server-utils");
|
|
5132
5717
|
function useScheduleTaskService() {
|
|
5133
5718
|
const { createParentChecklist } = useParentChecklistRepo();
|
|
5134
|
-
const { getAllScheduleTask
|
|
5719
|
+
const { getAllScheduleTask } = useScheduleTaskRepository();
|
|
5135
5720
|
const {
|
|
5136
5721
|
createAreaChecklist,
|
|
5137
5722
|
getAreaChecklistByAreaAndSchedule,
|
|
@@ -5151,13 +5736,13 @@ function useScheduleTaskService() {
|
|
|
5151
5736
|
const currentDateString = now.toLocaleDateString("en-US", {
|
|
5152
5737
|
timeZone: "Asia/Singapore"
|
|
5153
5738
|
});
|
|
5154
|
-
|
|
5739
|
+
import_node_server_utils35.logger.info(
|
|
5155
5740
|
`Checking schedule ${schedule._id}: Current time ${currentHour}:${currentMinute}, Current date ${currentDateString}, Schedule time ${schedule.time}, Start date ${schedule.startDate}, End date ${schedule.endDate}`
|
|
5156
5741
|
);
|
|
5157
5742
|
const startDate = /* @__PURE__ */ new Date(schedule.startDate + "T00:00:00");
|
|
5158
5743
|
const currentDateOnly = /* @__PURE__ */ new Date(currentDateString + "T00:00:00");
|
|
5159
5744
|
if (currentDateOnly < startDate) {
|
|
5160
|
-
|
|
5745
|
+
import_node_server_utils35.logger.info(
|
|
5161
5746
|
`Schedule ${schedule._id}: Current date ${currentDateString} is before start date ${schedule.startDate}`
|
|
5162
5747
|
);
|
|
5163
5748
|
return false;
|
|
@@ -5165,7 +5750,7 @@ function useScheduleTaskService() {
|
|
|
5165
5750
|
if (schedule.endDate) {
|
|
5166
5751
|
const endDate = /* @__PURE__ */ new Date(schedule.endDate + "T00:00:00");
|
|
5167
5752
|
if (currentDateOnly > endDate) {
|
|
5168
|
-
|
|
5753
|
+
import_node_server_utils35.logger.info(
|
|
5169
5754
|
`Schedule ${schedule._id}: Current date ${currentDateString} is after end date ${schedule.endDate}`
|
|
5170
5755
|
);
|
|
5171
5756
|
return false;
|
|
@@ -5174,17 +5759,17 @@ function useScheduleTaskService() {
|
|
|
5174
5759
|
const [scheduleHour, scheduleMinute] = schedule.time.split(":").map(Number);
|
|
5175
5760
|
const timeMatches = currentHour === scheduleHour && currentMinute === scheduleMinute;
|
|
5176
5761
|
if (!timeMatches) {
|
|
5177
|
-
|
|
5762
|
+
import_node_server_utils35.logger.info(
|
|
5178
5763
|
`Schedule ${schedule._id}: Time does not match. Current: ${currentHour}:${currentMinute}, Expected: ${scheduleHour}:${scheduleMinute}`
|
|
5179
5764
|
);
|
|
5180
5765
|
return false;
|
|
5181
5766
|
}
|
|
5182
|
-
|
|
5767
|
+
import_node_server_utils35.logger.info(
|
|
5183
5768
|
`Schedule ${schedule._id}: All conditions matched - Date is within range and time matches`
|
|
5184
5769
|
);
|
|
5185
5770
|
return true;
|
|
5186
5771
|
} catch (error) {
|
|
5187
|
-
|
|
5772
|
+
import_node_server_utils35.logger.error(
|
|
5188
5773
|
`Error checking schedule conditions for ${schedule._id}:`,
|
|
5189
5774
|
error
|
|
5190
5775
|
);
|
|
@@ -5193,40 +5778,40 @@ function useScheduleTaskService() {
|
|
|
5193
5778
|
}
|
|
5194
5779
|
async function processScheduledTasks(currentDate) {
|
|
5195
5780
|
try {
|
|
5196
|
-
|
|
5781
|
+
import_node_server_utils35.logger.info("Starting scheduled task processing...");
|
|
5197
5782
|
const scheduleTasks = await getAllScheduleTask();
|
|
5198
5783
|
if (!scheduleTasks || scheduleTasks.length === 0) {
|
|
5199
|
-
|
|
5784
|
+
import_node_server_utils35.logger.info("No schedule tasks found to process");
|
|
5200
5785
|
return { processed: 0, validated: 0 };
|
|
5201
5786
|
}
|
|
5202
|
-
|
|
5787
|
+
import_node_server_utils35.logger.info(`Found ${scheduleTasks.length} schedule tasks to check`);
|
|
5203
5788
|
let processedCount = 0;
|
|
5204
5789
|
let validatedCount = 0;
|
|
5205
5790
|
const validatedTasks = [];
|
|
5206
5791
|
for (const scheduleTask of scheduleTasks) {
|
|
5207
5792
|
try {
|
|
5208
|
-
|
|
5793
|
+
import_node_server_utils35.logger.info(
|
|
5209
5794
|
`Checking schedule ${scheduleTask._id} - ${scheduleTask.title}: time=${scheduleTask.time}, startDate=${scheduleTask.startDate}, endDate=${scheduleTask.endDate}`
|
|
5210
5795
|
);
|
|
5211
5796
|
const shouldRun = checkScheduleConditions(scheduleTask, currentDate);
|
|
5212
5797
|
if (!shouldRun) {
|
|
5213
|
-
|
|
5798
|
+
import_node_server_utils35.logger.info(
|
|
5214
5799
|
`Schedule ${scheduleTask._id} conditions not met, skipping`
|
|
5215
5800
|
);
|
|
5216
5801
|
continue;
|
|
5217
5802
|
}
|
|
5218
|
-
|
|
5803
|
+
import_node_server_utils35.logger.info(
|
|
5219
5804
|
`Schedule ${scheduleTask._id} conditions validated, creating area checklists`
|
|
5220
5805
|
);
|
|
5221
5806
|
if (!scheduleTask._id) {
|
|
5222
|
-
|
|
5807
|
+
import_node_server_utils35.logger.warn(`Schedule ${scheduleTask.title} has no _id, skipping`);
|
|
5223
5808
|
continue;
|
|
5224
5809
|
}
|
|
5225
5810
|
if (!scheduleTask.site) {
|
|
5226
|
-
|
|
5811
|
+
import_node_server_utils35.logger.warn(`Schedule ${scheduleTask._id} has no site, skipping`);
|
|
5227
5812
|
continue;
|
|
5228
5813
|
}
|
|
5229
|
-
|
|
5814
|
+
import_node_server_utils35.logger.info(
|
|
5230
5815
|
`Getting or creating parent checklist for schedule ${scheduleTask._id} in site ${scheduleTask.site}`
|
|
5231
5816
|
);
|
|
5232
5817
|
const parentChecklistIds = await createParentChecklist({
|
|
@@ -5234,7 +5819,7 @@ function useScheduleTaskService() {
|
|
|
5234
5819
|
createdAt: /* @__PURE__ */ new Date()
|
|
5235
5820
|
});
|
|
5236
5821
|
const parentChecklistId = Array.isArray(parentChecklistIds) ? parentChecklistIds[0] : parentChecklistIds;
|
|
5237
|
-
|
|
5822
|
+
import_node_server_utils35.logger.info(
|
|
5238
5823
|
`Using parent checklist ${parentChecklistId}, now creating/updating area checklists`
|
|
5239
5824
|
);
|
|
5240
5825
|
for (const area of scheduleTask.areas) {
|
|
@@ -5247,14 +5832,14 @@ function useScheduleTaskService() {
|
|
|
5247
5832
|
unit: unit.unit.toString(),
|
|
5248
5833
|
name: unit.name
|
|
5249
5834
|
}));
|
|
5250
|
-
|
|
5835
|
+
import_node_server_utils35.logger.info(
|
|
5251
5836
|
`Area ${area.name} (${areaId}): Using units from area details: ${JSON.stringify(
|
|
5252
5837
|
units
|
|
5253
5838
|
)}`
|
|
5254
5839
|
);
|
|
5255
5840
|
}
|
|
5256
5841
|
if (units.length === 0) {
|
|
5257
|
-
|
|
5842
|
+
import_node_server_utils35.logger.warn(
|
|
5258
5843
|
`Area ${area.name} (${areaId}): No units found, skipping area.`
|
|
5259
5844
|
);
|
|
5260
5845
|
continue;
|
|
@@ -5265,11 +5850,11 @@ function useScheduleTaskService() {
|
|
|
5265
5850
|
parentChecklistId.toString(),
|
|
5266
5851
|
areaId
|
|
5267
5852
|
);
|
|
5268
|
-
|
|
5853
|
+
import_node_server_utils35.logger.info(
|
|
5269
5854
|
`Area ${area.name} (${areaId}): Existing area checklist found: ${existingAreaChecklist ? "Yes" : "No"}`
|
|
5270
5855
|
);
|
|
5271
5856
|
if (existingAreaChecklist) {
|
|
5272
|
-
|
|
5857
|
+
import_node_server_utils35.logger.info(
|
|
5273
5858
|
`Area ${area.name} (${areaId}): Existing checklist content: ${JSON.stringify(
|
|
5274
5859
|
existingAreaChecklist.checklist
|
|
5275
5860
|
)}`
|
|
@@ -5277,7 +5862,7 @@ function useScheduleTaskService() {
|
|
|
5277
5862
|
}
|
|
5278
5863
|
} catch (error) {
|
|
5279
5864
|
existingAreaChecklist = null;
|
|
5280
|
-
|
|
5865
|
+
import_node_server_utils35.logger.info(
|
|
5281
5866
|
`Area ${area.name} (${areaId}): No existing area checklist found (exception).`
|
|
5282
5867
|
);
|
|
5283
5868
|
}
|
|
@@ -5291,7 +5876,7 @@ function useScheduleTaskService() {
|
|
|
5291
5876
|
...existingAreaChecklist.checklist || [],
|
|
5292
5877
|
newSet
|
|
5293
5878
|
];
|
|
5294
|
-
|
|
5879
|
+
import_node_server_utils35.logger.info(
|
|
5295
5880
|
`Area ${area.name} (${areaId}): Appending new set ${newSet.set} to checklist. Updated checklist: ${JSON.stringify(
|
|
5296
5881
|
updatedChecklist
|
|
5297
5882
|
)}`
|
|
@@ -5299,7 +5884,7 @@ function useScheduleTaskService() {
|
|
|
5299
5884
|
await updateAreaChecklist(existingAreaChecklist._id, {
|
|
5300
5885
|
checklist: updatedChecklist
|
|
5301
5886
|
});
|
|
5302
|
-
|
|
5887
|
+
import_node_server_utils35.logger.info(
|
|
5303
5888
|
`Appended set ${newSet.set} to area checklist for area ${area.name}`
|
|
5304
5889
|
);
|
|
5305
5890
|
try {
|
|
@@ -5307,13 +5892,13 @@ function useScheduleTaskService() {
|
|
|
5307
5892
|
parentChecklistId.toString(),
|
|
5308
5893
|
areaId
|
|
5309
5894
|
);
|
|
5310
|
-
|
|
5895
|
+
import_node_server_utils35.logger.info(
|
|
5311
5896
|
`Area ${area.name} (${areaId}): Checklist after update: ${JSON.stringify(
|
|
5312
5897
|
verifyChecklist.checklist
|
|
5313
5898
|
)}`
|
|
5314
5899
|
);
|
|
5315
5900
|
} catch (verifyError) {
|
|
5316
|
-
|
|
5901
|
+
import_node_server_utils35.logger.warn(
|
|
5317
5902
|
`Area ${area.name} (${areaId}): Error verifying checklist after update:`,
|
|
5318
5903
|
verifyError
|
|
5319
5904
|
);
|
|
@@ -5329,52 +5914,53 @@ function useScheduleTaskService() {
|
|
|
5329
5914
|
set: 1,
|
|
5330
5915
|
units
|
|
5331
5916
|
}
|
|
5332
|
-
]
|
|
5917
|
+
],
|
|
5918
|
+
createdBy: scheduleTask.createdBy
|
|
5333
5919
|
};
|
|
5334
|
-
|
|
5920
|
+
import_node_server_utils35.logger.info(
|
|
5335
5921
|
`Area ${area.name} (${areaId}): Creating new area checklist with data: ${JSON.stringify(
|
|
5336
5922
|
checklistData
|
|
5337
5923
|
)}`
|
|
5338
5924
|
);
|
|
5339
5925
|
await createAreaChecklist(checklistData);
|
|
5340
|
-
|
|
5926
|
+
import_node_server_utils35.logger.info(`Created new area checklist for area ${area.name}`);
|
|
5341
5927
|
try {
|
|
5342
5928
|
const verifyChecklist = await getAreaChecklistByAreaAndSchedule(
|
|
5343
5929
|
parentChecklistId.toString(),
|
|
5344
5930
|
areaId
|
|
5345
5931
|
);
|
|
5346
|
-
|
|
5932
|
+
import_node_server_utils35.logger.info(
|
|
5347
5933
|
`Area ${area.name} (${areaId}): Checklist after creation: ${JSON.stringify(
|
|
5348
5934
|
verifyChecklist.checklist
|
|
5349
5935
|
)}`
|
|
5350
5936
|
);
|
|
5351
5937
|
} catch (verifyError) {
|
|
5352
|
-
|
|
5938
|
+
import_node_server_utils35.logger.warn(
|
|
5353
5939
|
`Area ${area.name} (${areaId}): Error verifying checklist after creation:`,
|
|
5354
5940
|
verifyError
|
|
5355
5941
|
);
|
|
5356
5942
|
}
|
|
5357
5943
|
}
|
|
5358
5944
|
} catch (error) {
|
|
5359
|
-
|
|
5945
|
+
import_node_server_utils35.logger.error(`Error processing area ${area.name}:`, error);
|
|
5360
5946
|
continue;
|
|
5361
5947
|
}
|
|
5362
5948
|
}
|
|
5363
5949
|
processedCount++;
|
|
5364
5950
|
validatedCount++;
|
|
5365
5951
|
validatedTasks.push(scheduleTask);
|
|
5366
|
-
|
|
5952
|
+
import_node_server_utils35.logger.info(
|
|
5367
5953
|
`Successfully processed schedule ${scheduleTask._id}, created/updated area checklists for all areas.`
|
|
5368
5954
|
);
|
|
5369
5955
|
} catch (error) {
|
|
5370
|
-
|
|
5956
|
+
import_node_server_utils35.logger.error(
|
|
5371
5957
|
`Error processing schedule task ${scheduleTask._id}:`,
|
|
5372
5958
|
error
|
|
5373
5959
|
);
|
|
5374
5960
|
continue;
|
|
5375
5961
|
}
|
|
5376
5962
|
}
|
|
5377
|
-
|
|
5963
|
+
import_node_server_utils35.logger.info(
|
|
5378
5964
|
`Scheduled task processing completed. Processed: ${processedCount}, Validated: ${validatedCount} tasks`
|
|
5379
5965
|
);
|
|
5380
5966
|
return {
|
|
@@ -5383,7 +5969,7 @@ function useScheduleTaskService() {
|
|
|
5383
5969
|
tasks: validatedTasks
|
|
5384
5970
|
};
|
|
5385
5971
|
} catch (error) {
|
|
5386
|
-
|
|
5972
|
+
import_node_server_utils35.logger.error("Error processing scheduled tasks:", error);
|
|
5387
5973
|
throw error;
|
|
5388
5974
|
}
|
|
5389
5975
|
}
|
|
@@ -5391,8 +5977,8 @@ function useScheduleTaskService() {
|
|
|
5391
5977
|
}
|
|
5392
5978
|
|
|
5393
5979
|
// src/controllers/hygiene-schedule-task.controller.ts
|
|
5394
|
-
var
|
|
5395
|
-
var
|
|
5980
|
+
var import_joi19 = __toESM(require("joi"));
|
|
5981
|
+
var import_node_server_utils36 = require("@7365admin1/node-server-utils");
|
|
5396
5982
|
function useScheduleTaskController() {
|
|
5397
5983
|
const {
|
|
5398
5984
|
createScheduleTask: _createScheduleTask,
|
|
@@ -5402,11 +5988,16 @@ function useScheduleTaskController() {
|
|
|
5402
5988
|
updateScheduleTask: _updateScheduleTask
|
|
5403
5989
|
} = useScheduleTaskRepository();
|
|
5404
5990
|
async function createScheduleTask(req, res, next) {
|
|
5405
|
-
const
|
|
5991
|
+
const cookies = req.headers.cookie ? req.headers.cookie.split(";").map((cookie) => cookie.trim().split("=")).reduce(
|
|
5992
|
+
(acc, [key, value]) => ({ ...acc, [key]: value }),
|
|
5993
|
+
{}
|
|
5994
|
+
) : {};
|
|
5995
|
+
const createdBy = cookies["user"] || "";
|
|
5996
|
+
const payload = { ...req.body, ...req.params, createdBy };
|
|
5406
5997
|
const { error } = scheduleTaskSchema.validate(payload);
|
|
5407
5998
|
if (error) {
|
|
5408
|
-
|
|
5409
|
-
next(new
|
|
5999
|
+
import_node_server_utils36.logger.log({ level: "error", message: error.message });
|
|
6000
|
+
next(new import_node_server_utils36.BadRequestError(error.message));
|
|
5410
6001
|
return;
|
|
5411
6002
|
}
|
|
5412
6003
|
try {
|
|
@@ -5414,23 +6005,23 @@ function useScheduleTaskController() {
|
|
|
5414
6005
|
res.status(201).json({ message: "Schedule task created successfully.", id });
|
|
5415
6006
|
return;
|
|
5416
6007
|
} catch (error2) {
|
|
5417
|
-
|
|
6008
|
+
import_node_server_utils36.logger.log({ level: "error", message: error2.message });
|
|
5418
6009
|
next(error2);
|
|
5419
6010
|
return;
|
|
5420
6011
|
}
|
|
5421
6012
|
}
|
|
5422
6013
|
async function getScheduleTasks(req, res, next) {
|
|
5423
6014
|
const query = { ...req.query, ...req.params };
|
|
5424
|
-
const validation =
|
|
5425
|
-
page:
|
|
5426
|
-
limit:
|
|
5427
|
-
search:
|
|
5428
|
-
site:
|
|
6015
|
+
const validation = import_joi19.default.object({
|
|
6016
|
+
page: import_joi19.default.number().min(1).optional().allow("", null),
|
|
6017
|
+
limit: import_joi19.default.number().min(1).optional().allow("", null),
|
|
6018
|
+
search: import_joi19.default.string().optional().allow("", null),
|
|
6019
|
+
site: import_joi19.default.string().hex().required()
|
|
5429
6020
|
});
|
|
5430
6021
|
const { error } = validation.validate(query);
|
|
5431
6022
|
if (error) {
|
|
5432
|
-
|
|
5433
|
-
next(new
|
|
6023
|
+
import_node_server_utils36.logger.log({ level: "error", message: error.message });
|
|
6024
|
+
next(new import_node_server_utils36.BadRequestError(error.message));
|
|
5434
6025
|
return;
|
|
5435
6026
|
}
|
|
5436
6027
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -5447,23 +6038,23 @@ function useScheduleTaskController() {
|
|
|
5447
6038
|
res.json(data);
|
|
5448
6039
|
return;
|
|
5449
6040
|
} catch (error2) {
|
|
5450
|
-
|
|
6041
|
+
import_node_server_utils36.logger.log({ level: "error", message: error2.message });
|
|
5451
6042
|
next(error2);
|
|
5452
6043
|
return;
|
|
5453
6044
|
}
|
|
5454
6045
|
}
|
|
5455
6046
|
async function getTasksForScheduleTask(req, res, next) {
|
|
5456
6047
|
const query = { ...req.query, ...req.params };
|
|
5457
|
-
const validation =
|
|
5458
|
-
page:
|
|
5459
|
-
limit:
|
|
5460
|
-
search:
|
|
5461
|
-
site:
|
|
6048
|
+
const validation = import_joi19.default.object({
|
|
6049
|
+
page: import_joi19.default.number().min(1).optional().allow("", null),
|
|
6050
|
+
limit: import_joi19.default.number().min(1).optional().allow("", null),
|
|
6051
|
+
search: import_joi19.default.string().optional().allow("", null),
|
|
6052
|
+
site: import_joi19.default.string().hex().required()
|
|
5462
6053
|
});
|
|
5463
6054
|
const { error } = validation.validate(query);
|
|
5464
6055
|
if (error) {
|
|
5465
|
-
|
|
5466
|
-
next(new
|
|
6056
|
+
import_node_server_utils36.logger.log({ level: "error", message: error.message });
|
|
6057
|
+
next(new import_node_server_utils36.BadRequestError(error.message));
|
|
5467
6058
|
return;
|
|
5468
6059
|
}
|
|
5469
6060
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -5480,18 +6071,18 @@ function useScheduleTaskController() {
|
|
|
5480
6071
|
res.json(data);
|
|
5481
6072
|
return;
|
|
5482
6073
|
} catch (error2) {
|
|
5483
|
-
|
|
6074
|
+
import_node_server_utils36.logger.log({ level: "error", message: error2.message });
|
|
5484
6075
|
next(error2);
|
|
5485
6076
|
return;
|
|
5486
6077
|
}
|
|
5487
6078
|
}
|
|
5488
6079
|
async function getScheduleTaskById(req, res, next) {
|
|
5489
|
-
const validation =
|
|
6080
|
+
const validation = import_joi19.default.string().hex().required();
|
|
5490
6081
|
const _id = req.params.id;
|
|
5491
6082
|
const { error, value } = validation.validate(_id);
|
|
5492
6083
|
if (error) {
|
|
5493
|
-
|
|
5494
|
-
next(new
|
|
6084
|
+
import_node_server_utils36.logger.log({ level: "error", message: error.message });
|
|
6085
|
+
next(new import_node_server_utils36.BadRequestError(error.message));
|
|
5495
6086
|
return;
|
|
5496
6087
|
}
|
|
5497
6088
|
try {
|
|
@@ -5499,31 +6090,31 @@ function useScheduleTaskController() {
|
|
|
5499
6090
|
res.json(data);
|
|
5500
6091
|
return;
|
|
5501
6092
|
} catch (error2) {
|
|
5502
|
-
|
|
6093
|
+
import_node_server_utils36.logger.log({ level: "error", message: error2.message });
|
|
5503
6094
|
next(error2);
|
|
5504
6095
|
return;
|
|
5505
6096
|
}
|
|
5506
6097
|
}
|
|
5507
6098
|
async function updateScheduleTask(req, res, next) {
|
|
5508
6099
|
const payload = { id: req.params.id, ...req.body };
|
|
5509
|
-
const validation =
|
|
5510
|
-
id:
|
|
5511
|
-
title:
|
|
5512
|
-
time:
|
|
5513
|
-
startDate:
|
|
5514
|
-
endDate:
|
|
5515
|
-
description:
|
|
5516
|
-
areas:
|
|
5517
|
-
|
|
5518
|
-
name:
|
|
5519
|
-
value:
|
|
6100
|
+
const validation = import_joi19.default.object({
|
|
6101
|
+
id: import_joi19.default.string().hex().required(),
|
|
6102
|
+
title: import_joi19.default.string().optional().allow("", null),
|
|
6103
|
+
time: import_joi19.default.string().pattern(/^([0-1]\d|2[0-3]):([0-5]\d)$/).optional().allow("", null),
|
|
6104
|
+
startDate: import_joi19.default.string().pattern(/^\d{4}-\d{2}-\d{2}$/).optional().allow("", null),
|
|
6105
|
+
endDate: import_joi19.default.string().pattern(/^\d{4}-\d{2}-\d{2}$/).optional().allow("", null),
|
|
6106
|
+
description: import_joi19.default.string().optional().allow("", null),
|
|
6107
|
+
areas: import_joi19.default.array().min(1).items(
|
|
6108
|
+
import_joi19.default.object({
|
|
6109
|
+
name: import_joi19.default.string().required(),
|
|
6110
|
+
value: import_joi19.default.any().required()
|
|
5520
6111
|
})
|
|
5521
6112
|
).optional()
|
|
5522
6113
|
});
|
|
5523
6114
|
const { error } = validation.validate(payload);
|
|
5524
6115
|
if (error) {
|
|
5525
|
-
|
|
5526
|
-
next(new
|
|
6116
|
+
import_node_server_utils36.logger.log({ level: "error", message: error.message });
|
|
6117
|
+
next(new import_node_server_utils36.BadRequestError(error.message));
|
|
5527
6118
|
return;
|
|
5528
6119
|
}
|
|
5529
6120
|
try {
|
|
@@ -5532,7 +6123,7 @@ function useScheduleTaskController() {
|
|
|
5532
6123
|
res.json({ message: "Schedule task updated successfully." });
|
|
5533
6124
|
return;
|
|
5534
6125
|
} catch (error2) {
|
|
5535
|
-
|
|
6126
|
+
import_node_server_utils36.logger.log({ level: "error", message: error2.message });
|
|
5536
6127
|
next(error2);
|
|
5537
6128
|
return;
|
|
5538
6129
|
}
|
|
@@ -5549,12 +6140,14 @@ function useScheduleTaskController() {
|
|
|
5549
6140
|
0 && (module.exports = {
|
|
5550
6141
|
MArea,
|
|
5551
6142
|
MAreaChecklist,
|
|
6143
|
+
MCheckOutItem,
|
|
5552
6144
|
MParentChecklist,
|
|
5553
6145
|
MRequestItem,
|
|
5554
6146
|
MScheduleTask,
|
|
5555
6147
|
MStock,
|
|
5556
6148
|
MSupply,
|
|
5557
6149
|
MUnit,
|
|
6150
|
+
allowedCheckOutItemStatus,
|
|
5558
6151
|
allowedChecklistStatus,
|
|
5559
6152
|
allowedPeriods,
|
|
5560
6153
|
allowedRequestItemStatus,
|
|
@@ -5562,6 +6155,7 @@ function useScheduleTaskController() {
|
|
|
5562
6155
|
allowedTypes,
|
|
5563
6156
|
areaChecklistSchema,
|
|
5564
6157
|
areaSchema,
|
|
6158
|
+
checkOutItemSchema,
|
|
5565
6159
|
parentChecklistSchema,
|
|
5566
6160
|
requestItemSchema,
|
|
5567
6161
|
scheduleTaskSchema,
|
|
@@ -5574,6 +6168,9 @@ function useScheduleTaskController() {
|
|
|
5574
6168
|
useAreaController,
|
|
5575
6169
|
useAreaRepo,
|
|
5576
6170
|
useAreaService,
|
|
6171
|
+
useCheckOutItemController,
|
|
6172
|
+
useCheckOutItemRepository,
|
|
6173
|
+
useCheckOutItemService,
|
|
5577
6174
|
useHygieneDashboardController,
|
|
5578
6175
|
useHygieneDashboardRepository,
|
|
5579
6176
|
useParentChecklistController,
|