@7365admin1/module-hygiene 4.13.0 → 4.14.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/dist/index.js CHANGED
@@ -832,13 +832,10 @@ function useAreaRepo() {
832
832
  }
833
833
  async function getAreasForChecklist(site, serviceType) {
834
834
  const query = {
835
- status: { $ne: "deleted" }
835
+ status: { $ne: "deleted" },
836
+ serviceType
836
837
  };
837
- const cacheOptions = {};
838
- if (serviceType) {
839
- query.serviceType = serviceType;
840
- cacheOptions.serviceType = serviceType;
841
- }
838
+ const cacheOptions = { serviceType };
842
839
  try {
843
840
  site = new import_mongodb3.ObjectId(site);
844
841
  query.site = site;
@@ -2268,6 +2265,7 @@ function useParentChecklistRepo() {
2268
2265
  async function createIndex() {
2269
2266
  try {
2270
2267
  await collection.createIndexes([
2268
+ { key: { serviceType: 1 } },
2271
2269
  { key: { createdAt: 1 } },
2272
2270
  { key: { site: 1 } },
2273
2271
  { key: { status: 1 } }
@@ -2285,44 +2283,63 @@ function useParentChecklistRepo() {
2285
2283
  startOfDay.setUTCHours(0, 0, 0, 0);
2286
2284
  const endOfDay = new Date(currentDate);
2287
2285
  endOfDay.setUTCHours(23, 59, 59, 999);
2288
- const existingQuery = {
2289
- createdAt: {
2290
- $gte: startOfDay,
2291
- $lte: endOfDay
2292
- }
2293
- };
2286
+ const allServiceTypes = Object.values(ServiceType);
2287
+ const dateStr = currentDate.toISOString().split("T")[0];
2294
2288
  if (value.site) {
2289
+ let siteObjectId;
2295
2290
  try {
2296
- existingQuery.site = new import_mongodb7.ObjectId(value.site);
2297
- import_node_server_utils14.logger.info(
2298
- `createParentChecklist: Looking for existing checklist with query: ${JSON.stringify(
2299
- { ...existingQuery, site: value.site }
2300
- )}`
2301
- );
2291
+ siteObjectId = new import_mongodb7.ObjectId(value.site);
2302
2292
  } catch (error) {
2303
2293
  throw new import_node_server_utils14.BadRequestError("Invalid site ID format.");
2304
2294
  }
2305
- } else {
2306
- import_node_server_utils14.logger.info(
2307
- `createParentChecklist: Looking for existing checklist with query (no site filter): ${JSON.stringify(
2308
- existingQuery
2309
- )}`
2310
- );
2311
- }
2312
- const existingChecklist = await collection.findOne(existingQuery);
2313
- if (existingChecklist) {
2314
- const dateStr2 = currentDate.toISOString().split("T")[0];
2315
- import_node_server_utils14.logger.info(
2316
- `Parent checklist already exists for ${value.site ? `site ${value.site} on` : ""} today: ${dateStr2}. Found checklist: ${JSON.stringify({
2317
- _id: existingChecklist._id,
2318
- site: existingChecklist.site
2319
- })}`
2295
+ if (value.serviceType) {
2296
+ const existingChecklist = await collection.findOne({
2297
+ createdAt: { $gte: startOfDay, $lte: endOfDay },
2298
+ site: siteObjectId,
2299
+ serviceType: value.serviceType
2300
+ });
2301
+ if (existingChecklist) {
2302
+ import_node_server_utils14.logger.info(
2303
+ `Parent checklist already exists for site ${value.site} / ${value.serviceType} on ${dateStr}. _id: ${existingChecklist._id}`
2304
+ );
2305
+ return existingChecklist._id;
2306
+ }
2307
+ const doc = MParentChecklist({
2308
+ site: value.site,
2309
+ createdAt: currentDate,
2310
+ serviceType: value.serviceType
2311
+ });
2312
+ const result3 = await collection.insertOne(doc, { session });
2313
+ delNamespace().catch((err) => {
2314
+ import_node_server_utils14.logger.error(
2315
+ `Failed to clear cache for namespace: ${namespace_collection}`,
2316
+ err
2317
+ );
2318
+ });
2319
+ import_node_server_utils14.logger.info(
2320
+ `Created parent checklist for site ${value.site} / ${value.serviceType} for today: ${dateStr}`
2321
+ );
2322
+ return result3.insertedId;
2323
+ }
2324
+ const existingServiceTypes = await collection.distinct("serviceType", {
2325
+ createdAt: { $gte: startOfDay, $lte: endOfDay },
2326
+ site: siteObjectId,
2327
+ serviceType: { $exists: true, $ne: null }
2328
+ });
2329
+ const missingServiceTypes = allServiceTypes.filter(
2330
+ (st) => !existingServiceTypes.includes(st)
2320
2331
  );
2321
- return existingChecklist._id;
2322
- }
2323
- const allServiceTypes = Object.values(ServiceType);
2324
- if (value.site) {
2325
- const checklistDocs2 = allServiceTypes.map(
2332
+ if (missingServiceTypes.length === 0) {
2333
+ import_node_server_utils14.logger.info(
2334
+ `All serviceType checklists already exist for site ${value.site} on ${dateStr}`
2335
+ );
2336
+ const first = await collection.findOne({
2337
+ createdAt: { $gte: startOfDay, $lte: endOfDay },
2338
+ site: siteObjectId
2339
+ });
2340
+ return first._id;
2341
+ }
2342
+ const checklistDocs2 = missingServiceTypes.map(
2326
2343
  (serviceType) => MParentChecklist({
2327
2344
  site: value.site,
2328
2345
  createdAt: currentDate,
@@ -2330,17 +2347,14 @@ function useParentChecklistRepo() {
2330
2347
  })
2331
2348
  );
2332
2349
  const result2 = await collection.insertMany(checklistDocs2, { session });
2333
- delNamespace().then(() => {
2334
- import_node_server_utils14.logger.info(`Cache cleared for namespace: ${namespace_collection}`);
2335
- }).catch((err) => {
2350
+ delNamespace().catch((err) => {
2336
2351
  import_node_server_utils14.logger.error(
2337
2352
  `Failed to clear cache for namespace: ${namespace_collection}`,
2338
2353
  err
2339
2354
  );
2340
2355
  });
2341
- const dateStr2 = currentDate.toISOString().split("T")[0];
2342
2356
  import_node_server_utils14.logger.info(
2343
- `Created ${checklistDocs2.length} parent checklists for site ${value.site} for today: ${dateStr2}`
2357
+ `Created ${checklistDocs2.length} parent checklists for site ${value.site} for today: ${dateStr}`
2344
2358
  );
2345
2359
  return Object.values(result2.insertedIds);
2346
2360
  }
@@ -2353,18 +2367,41 @@ function useParentChecklistRepo() {
2353
2367
  import_node_server_utils14.logger.warn("No sites found for creating parent checklist");
2354
2368
  throw new import_node_server_utils14.BadRequestError("No sites available for checklist creation");
2355
2369
  }
2370
+ const existingPairs = await collection.find(
2371
+ {
2372
+ createdAt: { $gte: startOfDay, $lte: endOfDay },
2373
+ serviceType: { $exists: true, $ne: null }
2374
+ },
2375
+ { projection: { site: 1, serviceType: 1 } }
2376
+ ).toArray();
2377
+ const existingSet = new Set(
2378
+ existingPairs.map(
2379
+ (doc) => `${doc.site?.toString()}|${doc.serviceType}`
2380
+ )
2381
+ );
2356
2382
  const checklistDocs = [];
2357
2383
  for (const site of siteIds) {
2358
2384
  for (const serviceType of allServiceTypes) {
2359
- checklistDocs.push(
2360
- MParentChecklist({
2361
- site,
2362
- createdAt: currentDate,
2363
- serviceType
2364
- })
2365
- );
2385
+ if (!existingSet.has(`${site}|${serviceType}`)) {
2386
+ checklistDocs.push(
2387
+ MParentChecklist({
2388
+ site,
2389
+ createdAt: currentDate,
2390
+ serviceType
2391
+ })
2392
+ );
2393
+ }
2366
2394
  }
2367
2395
  }
2396
+ if (checklistDocs.length === 0) {
2397
+ import_node_server_utils14.logger.info(
2398
+ `All site+serviceType checklists already exist for today: ${dateStr}`
2399
+ );
2400
+ return [];
2401
+ }
2402
+ import_node_server_utils14.logger.info(
2403
+ `createParentChecklist: Creating ${checklistDocs.length} missing site+serviceType checklist(s) for today: ${dateStr}`
2404
+ );
2368
2405
  const result = await collection.insertMany(checklistDocs, { session });
2369
2406
  delNamespace().then(() => {
2370
2407
  import_node_server_utils14.logger.info(`Cache cleared for namespace: ${namespace_collection}`);
@@ -2374,7 +2411,6 @@ function useParentChecklistRepo() {
2374
2411
  err
2375
2412
  );
2376
2413
  });
2377
- const dateStr = currentDate.toISOString().split("T")[0];
2378
2414
  import_node_server_utils14.logger.info(
2379
2415
  `Created ${Object.keys(result.insertedIds).length} parent checklists for today: ${dateStr}`
2380
2416
  );
@@ -2482,6 +2518,23 @@ function useParentChecklistRepo() {
2482
2518
  throw error;
2483
2519
  }
2484
2520
  }
2521
+ async function getParentChecklistById(_id) {
2522
+ try {
2523
+ _id = new import_mongodb7.ObjectId(_id);
2524
+ } catch (error) {
2525
+ throw new import_node_server_utils14.BadRequestError("Invalid parent checklist ID format.");
2526
+ }
2527
+ try {
2528
+ const data = await collection.findOne({ _id });
2529
+ if (!data) {
2530
+ throw new import_node_server_utils14.BadRequestError("Parent checklist not found.");
2531
+ }
2532
+ return data;
2533
+ } catch (error) {
2534
+ import_node_server_utils14.logger.error("Failed to get parent checklist by ID", error);
2535
+ throw error;
2536
+ }
2537
+ }
2485
2538
  async function updateParentChecklistStatuses(_id, status, session) {
2486
2539
  try {
2487
2540
  _id = new import_mongodb7.ObjectId(_id);
@@ -2596,13 +2649,38 @@ function useParentChecklistRepo() {
2596
2649
  throw error;
2597
2650
  }
2598
2651
  }
2652
+ async function getTodayParentChecklistsForAreaGen() {
2653
+ const now = /* @__PURE__ */ new Date();
2654
+ const start = new Date(now);
2655
+ start.setUTCHours(0, 0, 0, 0);
2656
+ const end = new Date(now);
2657
+ end.setUTCHours(23, 59, 59, 999);
2658
+ try {
2659
+ const items = await collection.find(
2660
+ {
2661
+ createdAt: { $gte: start, $lte: end },
2662
+ serviceType: { $exists: true, $ne: null }
2663
+ },
2664
+ { projection: { _id: 1, site: 1, serviceType: 1 } }
2665
+ ).toArray();
2666
+ return items;
2667
+ } catch (error) {
2668
+ import_node_server_utils14.logger.error(
2669
+ "Failed to get today's parent checklists for area generation",
2670
+ error
2671
+ );
2672
+ throw error;
2673
+ }
2674
+ }
2599
2675
  return {
2600
2676
  createIndex,
2601
2677
  createParentChecklist,
2602
2678
  getAllParentChecklist,
2679
+ getParentChecklistById,
2603
2680
  updateParentChecklistStatuses,
2604
2681
  closeExpiredParentChecklists,
2605
- getTodayParentChecklists
2682
+ getTodayParentChecklists,
2683
+ getTodayParentChecklistsForAreaGen
2606
2684
  };
2607
2685
  }
2608
2686
 
@@ -2690,6 +2768,7 @@ var import_node_server_utils16 = require("@7365admin1/node-server-utils");
2690
2768
  var allowedChecklistStatus = ["open", "completed", "closed"];
2691
2769
  var areaChecklistSchema = import_joi8.default.object({
2692
2770
  schedule: import_joi8.default.string().hex().required(),
2771
+ serviceType: import_joi8.default.string().valid(...Object.values(ServiceType)).optional(),
2693
2772
  area: import_joi8.default.string().hex().required(),
2694
2773
  name: import_joi8.default.string().required(),
2695
2774
  type: import_joi8.default.string().valid(...allowedTypes).required(),
@@ -2756,6 +2835,7 @@ function MAreaChecklist(value) {
2756
2835
  }
2757
2836
  return {
2758
2837
  schedule: value.schedule,
2838
+ serviceType: value.serviceType,
2759
2839
  area: value.area,
2760
2840
  name: value.name,
2761
2841
  type: value.type,
@@ -2865,7 +2945,8 @@ function useAreaChecklistRepo() {
2865
2945
  search = "",
2866
2946
  type = "all",
2867
2947
  status = "all",
2868
- schedule
2948
+ schedule,
2949
+ serviceType
2869
2950
  }, session) {
2870
2951
  page = page > 0 ? page - 1 : 0;
2871
2952
  const query = {};
@@ -2879,6 +2960,10 @@ function useAreaChecklistRepo() {
2879
2960
  } catch (error) {
2880
2961
  throw new import_node_server_utils17.BadRequestError("Invalid parent checklist ID format.");
2881
2962
  }
2963
+ if (serviceType) {
2964
+ query.serviceType = serviceType;
2965
+ cacheOptions.serviceType = serviceType;
2966
+ }
2882
2967
  if (type && type !== "all") {
2883
2968
  query.type = type;
2884
2969
  cacheOptions.type = type;
@@ -3274,6 +3359,7 @@ function useAreaChecklistRepo() {
3274
3359
  page = 1,
3275
3360
  limit = 10,
3276
3361
  search = "",
3362
+ serviceType,
3277
3363
  _id
3278
3364
  }, session) {
3279
3365
  page = page > 0 ? page - 1 : 0;
@@ -3288,6 +3374,10 @@ function useAreaChecklistRepo() {
3288
3374
  } catch (error) {
3289
3375
  throw new import_node_server_utils17.BadRequestError("Invalid area checklist ID format.");
3290
3376
  }
3377
+ if (serviceType) {
3378
+ query.serviceType = serviceType;
3379
+ cacheOptions.serviceType = serviceType;
3380
+ }
3291
3381
  if (search) {
3292
3382
  query.$text = { $search: search };
3293
3383
  cacheOptions.search = search;
@@ -3511,7 +3601,12 @@ function useAreaChecklistRepo() {
3511
3601
  throw error;
3512
3602
  }
3513
3603
  }
3514
- async function getAreaChecklistByAreaAndSchedule(schedule, area, session) {
3604
+ async function getAreaChecklistByAreaAndSchedule({
3605
+ schedule,
3606
+ serviceType,
3607
+ area,
3608
+ session
3609
+ }) {
3515
3610
  try {
3516
3611
  schedule = new import_mongodb9.ObjectId(schedule);
3517
3612
  area = new import_mongodb9.ObjectId(area);
@@ -3519,7 +3614,10 @@ function useAreaChecklistRepo() {
3519
3614
  throw new import_node_server_utils17.BadRequestError("Invalid area checklist ID format.");
3520
3615
  }
3521
3616
  try {
3522
- const data = await collection.findOne({ schedule, area }, { session });
3617
+ const data = await collection.findOne(
3618
+ { schedule, serviceType, area },
3619
+ { session }
3620
+ );
3523
3621
  if (!data) {
3524
3622
  throw new import_node_server_utils17.BadRequestError("Area checklist not found.");
3525
3623
  }
@@ -3728,7 +3826,7 @@ function useAreaChecklistRepo() {
3728
3826
  throw error;
3729
3827
  }
3730
3828
  }
3731
- async function pushScheduleTaskSets(scheduleId, areaId, scheduleTaskId, newSets) {
3829
+ async function pushScheduleTaskSets(scheduleId, serviceType, areaId, scheduleTaskId, newSets) {
3732
3830
  try {
3733
3831
  const schedule = new import_mongodb9.ObjectId(scheduleId);
3734
3832
  const area = new import_mongodb9.ObjectId(areaId);
@@ -3736,6 +3834,7 @@ function useAreaChecklistRepo() {
3736
3834
  const result = await collection.updateOne(
3737
3835
  {
3738
3836
  schedule,
3837
+ serviceType,
3739
3838
  area,
3740
3839
  checklist: { $not: { $elemMatch: { scheduleTaskId: taskId } } }
3741
3840
  },
@@ -3758,13 +3857,14 @@ function useAreaChecklistRepo() {
3758
3857
  throw error;
3759
3858
  }
3760
3859
  }
3761
- async function insertAutoGenSets(scheduleId, areaId, newSets) {
3860
+ async function insertAutoGenSets(scheduleId, serviceType, areaId, newSets) {
3762
3861
  try {
3763
3862
  const schedule = new import_mongodb9.ObjectId(scheduleId);
3764
3863
  const area = new import_mongodb9.ObjectId(areaId);
3765
3864
  const result = await collection.updateOne(
3766
3865
  {
3767
3866
  schedule,
3867
+ serviceType,
3768
3868
  area,
3769
3869
  checklist: {
3770
3870
  $not: { $elemMatch: { isScheduleTask: { $ne: true } } }
@@ -3910,14 +4010,25 @@ function useAreaChecklistService() {
3910
4010
  updateAreaChecklistStatus,
3911
4011
  insertAutoGenSets
3912
4012
  } = useAreaChecklistRepo();
3913
- const { updateParentChecklistStatuses } = useParentChecklistRepo();
4013
+ const { updateParentChecklistStatuses, getParentChecklistById } = useParentChecklistRepo();
3914
4014
  const { getUserById } = (0, import_core.useUserRepo)();
3915
4015
  async function createAreaChecklist(value) {
3916
4016
  const results = [];
3917
4017
  let totalChecklistsCreated = 0;
3918
4018
  try {
3919
4019
  const BATCH_SIZE = 10;
3920
- const areasResult = await getAreasForChecklist(value.site);
4020
+ const schedule = await getParentChecklistById(value.schedule.toString());
4021
+ const serviceType = schedule.serviceType;
4022
+ if (!serviceType) {
4023
+ import_node_server_utils18.logger.warn(
4024
+ `createAreaChecklist: Parent checklist ${value.schedule} has no serviceType, skipping area checklist generation`
4025
+ );
4026
+ return results;
4027
+ }
4028
+ const areasResult = await getAreasForChecklist(
4029
+ value.site,
4030
+ serviceType
4031
+ );
3921
4032
  const areas = areasResult || [];
3922
4033
  if (areas.length > 0) {
3923
4034
  for (let i = 0; i < areas.length; i += BATCH_SIZE) {
@@ -3931,10 +4042,11 @@ function useAreaChecklistService() {
3931
4042
  }
3932
4043
  let existing = null;
3933
4044
  try {
3934
- existing = await getAreaChecklistByAreaAndSchedule(
3935
- value.schedule.toString(),
3936
- area._id.toString()
3937
- );
4045
+ existing = await getAreaChecklistByAreaAndSchedule({
4046
+ schedule: value.schedule.toString(),
4047
+ serviceType,
4048
+ area: area._id.toString()
4049
+ });
3938
4050
  } catch (_) {
3939
4051
  }
3940
4052
  if (existing) {
@@ -3982,6 +4094,7 @@ function useAreaChecklistService() {
3982
4094
  );
3983
4095
  await insertAutoGenSets(
3984
4096
  value.schedule.toString(),
4097
+ serviceType,
3985
4098
  area._id.toString(),
3986
4099
  autoGenSets
3987
4100
  );
@@ -3990,6 +4103,7 @@ function useAreaChecklistService() {
3990
4103
  const setCount = Number(area.set) || 1;
3991
4104
  const checklistData = {
3992
4105
  schedule: value.schedule,
4106
+ serviceType,
3993
4107
  area: area._id.toString(),
3994
4108
  name: area.name,
3995
4109
  type: area.type,
@@ -4389,7 +4503,8 @@ function useAreaChecklistController() {
4389
4503
  search: import_joi9.default.string().optional().allow("", null),
4390
4504
  type: import_joi9.default.string().valid(...allowedTypes, "all").optional().allow("", null),
4391
4505
  status: import_joi9.default.string().valid(...allowedStatus, "all").optional().allow("", null),
4392
- schedule: import_joi9.default.string().hex().required()
4506
+ schedule: import_joi9.default.string().hex().required(),
4507
+ serviceType: import_joi9.default.string().valid(...Object.values(ServiceType)).required()
4393
4508
  });
4394
4509
  const { error } = validation.validate(query);
4395
4510
  if (error) {
@@ -4403,6 +4518,7 @@ function useAreaChecklistController() {
4403
4518
  const type = req.query.type ?? "all";
4404
4519
  const status = req.query.status ?? "all";
4405
4520
  const schedule = req.params.schedule ?? "";
4521
+ const serviceType = req.query.serviceType ?? "";
4406
4522
  try {
4407
4523
  const data = await _getAllAreaChecklist({
4408
4524
  page,
@@ -4410,7 +4526,8 @@ function useAreaChecklistController() {
4410
4526
  search,
4411
4527
  type,
4412
4528
  status,
4413
- schedule
4529
+ schedule,
4530
+ serviceType
4414
4531
  });
4415
4532
  res.json(data);
4416
4533
  return;
@@ -4487,6 +4604,7 @@ function useAreaChecklistController() {
4487
4604
  page: import_joi9.default.number().min(1).optional().allow("", null),
4488
4605
  limit: import_joi9.default.number().min(1).optional().allow("", null),
4489
4606
  search: import_joi9.default.string().optional().allow("", null),
4607
+ serviceType: import_joi9.default.string().valid(...Object.values(ServiceType)).required(),
4490
4608
  id: import_joi9.default.string().hex().required()
4491
4609
  });
4492
4610
  const { error } = validation.validate(query);
@@ -4498,12 +4616,14 @@ function useAreaChecklistController() {
4498
4616
  const page = parseInt(req.query.page) ?? 1;
4499
4617
  const limit = parseInt(req.query.limit) ?? 10;
4500
4618
  const search = req.query.search ?? "";
4619
+ const serviceType = req.query.serviceType ?? "";
4501
4620
  const _id = req.params.id ?? "";
4502
4621
  try {
4503
4622
  const data = await _getAreaChecklistUnits({
4504
4623
  page,
4505
4624
  limit,
4506
4625
  search,
4626
+ serviceType,
4507
4627
  _id
4508
4628
  });
4509
4629
  res.json(data);
@@ -4575,12 +4695,15 @@ function useAreaChecklistController() {
4575
4695
  try {
4576
4696
  const pdfBuffer = await _generateChecklistPdf(value);
4577
4697
  if (!pdfBuffer || pdfBuffer.length === 0) {
4578
- throw new import_node_server_utils20.InternalServerError("Generated checklist PDF is empty or invalid.");
4698
+ throw new import_node_server_utils20.InternalServerError(
4699
+ "Generated checklist PDF is empty or invalid."
4700
+ );
4579
4701
  }
4580
4702
  const date = /* @__PURE__ */ new Date();
4581
- const formattedDate = `${String(date.getDate()).padStart(2, "0")}_${String(
4582
- date.getMonth() + 1
4583
- ).padStart(2, "0")}_${date.getFullYear()}`;
4703
+ const formattedDate = `${String(date.getDate()).padStart(
4704
+ 2,
4705
+ "0"
4706
+ )}_${String(date.getMonth() + 1).padStart(2, "0")}_${date.getFullYear()}`;
4584
4707
  res.setHeader("Content-Type", "application/pdf");
4585
4708
  res.setHeader(
4586
4709
  "Content-Disposition",
@@ -5850,6 +5973,7 @@ var import_joi16 = __toESM(require("joi"));
5850
5973
  var import_mongodb18 = require("mongodb");
5851
5974
  var scheduleTaskSchema = import_joi16.default.object({
5852
5975
  site: import_joi16.default.string().hex().required(),
5976
+ serviceType: import_joi16.default.string().valid(...Object.values(ServiceType)).required(),
5853
5977
  title: import_joi16.default.string().required(),
5854
5978
  time: import_joi16.default.string().pattern(/^([0-1]\d|2[0-3]):([0-5]\d)$/).required(),
5855
5979
  dates: import_joi16.default.array().min(1).items(
@@ -5898,6 +6022,7 @@ function MScheduleTask(value) {
5898
6022
  }
5899
6023
  return {
5900
6024
  site: value.site,
6025
+ serviceType: value.serviceType,
5901
6026
  title: value.title,
5902
6027
  time: value.time,
5903
6028
  dates: value.dates,
@@ -5926,6 +6051,7 @@ function useScheduleTaskRepository() {
5926
6051
  try {
5927
6052
  await collection.createIndexes([
5928
6053
  { key: { site: 1 } },
6054
+ { key: { serviceType: 1 } },
5929
6055
  { key: { status: 1 } }
5930
6056
  ]);
5931
6057
  } catch (error) {
@@ -5964,15 +6090,18 @@ function useScheduleTaskRepository() {
5964
6090
  page = 1,
5965
6091
  limit = 10,
5966
6092
  search = "",
5967
- site
6093
+ site,
6094
+ serviceType
5968
6095
  }) {
5969
6096
  page = page > 0 ? page - 1 : 0;
5970
6097
  const query = {
5971
- status: { $ne: "deleted" }
6098
+ status: { $ne: "deleted" },
6099
+ serviceType
5972
6100
  };
5973
6101
  const cacheOptions = {
5974
6102
  page,
5975
- limit
6103
+ limit,
6104
+ serviceType
5976
6105
  };
5977
6106
  try {
5978
6107
  site = new import_mongodb19.ObjectId(site);
@@ -6028,62 +6157,6 @@ function useScheduleTaskRepository() {
6028
6157
  throw error;
6029
6158
  }
6030
6159
  }
6031
- async function getTasksForScheduleTask({
6032
- page = 1,
6033
- limit = 10,
6034
- search = "",
6035
- site
6036
- }) {
6037
- page = page > 0 ? page - 1 : 0;
6038
- const query = {
6039
- status: { $ne: "deleted" }
6040
- };
6041
- const cacheOptions = {
6042
- page,
6043
- limit
6044
- };
6045
- try {
6046
- site = new import_mongodb19.ObjectId(site);
6047
- query.site = site;
6048
- cacheOptions.site = site.toString();
6049
- } catch (error) {
6050
- throw new import_node_server_utils33.BadRequestError("Invalid site ID format.");
6051
- }
6052
- if (search) {
6053
- query.$or = [{ name: { $regex: search, $options: "i" } }];
6054
- cacheOptions.search = search;
6055
- }
6056
- const cacheKey = (0, import_node_server_utils33.makeCacheKey)(namespace_collection, cacheOptions);
6057
- const cachedData = await getCache(cacheKey);
6058
- if (cachedData) {
6059
- import_node_server_utils33.logger.info(`Cache hit for key: ${cacheKey}`);
6060
- return cachedData;
6061
- }
6062
- try {
6063
- const items = await collection.aggregate([
6064
- { $match: query },
6065
- {
6066
- $project: {
6067
- createdAt: 1,
6068
- title: 1
6069
- }
6070
- },
6071
- { $sort: { _id: -1 } },
6072
- { $skip: page * limit },
6073
- { $limit: limit }
6074
- ]).toArray();
6075
- const length = await collection.countDocuments(query);
6076
- const data = (0, import_node_server_utils33.paginate)(items, page, limit, length);
6077
- setCache(cacheKey, data, 15 * 60).then(() => {
6078
- import_node_server_utils33.logger.info(`Cache set for key: ${cacheKey}`);
6079
- }).catch((err) => {
6080
- import_node_server_utils33.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
6081
- });
6082
- return data;
6083
- } catch (error) {
6084
- throw error;
6085
- }
6086
- }
6087
6160
  async function getScheduleTaskById(_id, session) {
6088
6161
  try {
6089
6162
  _id = new import_mongodb19.ObjectId(_id);
@@ -6183,7 +6256,6 @@ function useScheduleTaskRepository() {
6183
6256
  createScheduleTask,
6184
6257
  getScheduleTasks,
6185
6258
  getAllScheduleTask,
6186
- getTasksForScheduleTask,
6187
6259
  getScheduleTaskById,
6188
6260
  updateScheduleTask
6189
6261
  };
@@ -6201,7 +6273,7 @@ function useScheduleTaskService() {
6201
6273
  pushScheduleTaskSets
6202
6274
  } = useAreaChecklistRepo();
6203
6275
  const { getAreaById } = useAreaRepo();
6204
- function checkScheduleConditions(schedule, currentDate = /* @__PURE__ */ new Date()) {
6276
+ function checkScheduleConditions(schedule, serviceType, currentDate = /* @__PURE__ */ new Date()) {
6205
6277
  try {
6206
6278
  const now = currentDate;
6207
6279
  const timeString = now.toLocaleTimeString("en-US", {
@@ -6292,12 +6364,17 @@ function useScheduleTaskService() {
6292
6364
  const validatedTasks = [];
6293
6365
  for (const scheduleTask of scheduleTasks) {
6294
6366
  try {
6367
+ const serviceType = scheduleTask.serviceType;
6295
6368
  import_node_server_utils34.logger.info(
6296
6369
  `Checking schedule ${scheduleTask._id} - ${scheduleTask.title}: time=${scheduleTask.time}, dates=${JSON.stringify(
6297
6370
  scheduleTask.dates
6298
6371
  )}`
6299
6372
  );
6300
- const shouldRun = checkScheduleConditions(scheduleTask, currentDate);
6373
+ const shouldRun = checkScheduleConditions(
6374
+ scheduleTask,
6375
+ serviceType,
6376
+ currentDate
6377
+ );
6301
6378
  if (!shouldRun) {
6302
6379
  import_node_server_utils34.logger.info(
6303
6380
  `Schedule ${scheduleTask._id} conditions not met, skipping`
@@ -6320,6 +6397,7 @@ function useScheduleTaskService() {
6320
6397
  );
6321
6398
  const parentChecklistIds = await createParentChecklist({
6322
6399
  site: scheduleTask.site.toString(),
6400
+ serviceType,
6323
6401
  createdAt: /* @__PURE__ */ new Date()
6324
6402
  });
6325
6403
  const parentChecklistId = Array.isArray(parentChecklistIds) ? parentChecklistIds[0] : parentChecklistIds;
@@ -6357,8 +6435,11 @@ function useScheduleTaskService() {
6357
6435
  let existingAreaChecklist;
6358
6436
  try {
6359
6437
  existingAreaChecklist = await getAreaChecklistByAreaAndSchedule(
6360
- parentChecklistId.toString(),
6361
- areaId
6438
+ {
6439
+ schedule: parentChecklistId.toString(),
6440
+ serviceType,
6441
+ area: areaId
6442
+ }
6362
6443
  );
6363
6444
  import_node_server_utils34.logger.info(
6364
6445
  `Area ${area.name} (${areaId}): Existing area checklist found: ${existingAreaChecklist ? "Yes" : "No"}`
@@ -6395,6 +6476,7 @@ function useScheduleTaskService() {
6395
6476
  );
6396
6477
  const modified = await pushScheduleTaskSets(
6397
6478
  parentChecklistId.toString(),
6479
+ serviceType,
6398
6480
  areaId,
6399
6481
  scheduleTask._id.toString(),
6400
6482
  newSets
@@ -6422,6 +6504,7 @@ function useScheduleTaskService() {
6422
6504
  );
6423
6505
  const checklistData = {
6424
6506
  schedule: parentChecklistId.toString(),
6507
+ serviceType,
6425
6508
  area: areaId,
6426
6509
  name: area.name,
6427
6510
  type: areaDetails.type || "common",
@@ -6476,7 +6559,6 @@ function useScheduleTaskController() {
6476
6559
  const {
6477
6560
  createScheduleTask: _createScheduleTask,
6478
6561
  getScheduleTasks: _getScheduleTasks,
6479
- getTasksForScheduleTask: _getTasksForScheduleTask,
6480
6562
  getScheduleTaskById: _getScheduleTaskById,
6481
6563
  updateScheduleTask: _updateScheduleTask
6482
6564
  } = useScheduleTaskRepository();
@@ -6509,7 +6591,8 @@ function useScheduleTaskController() {
6509
6591
  page: import_joi17.default.number().min(1).optional().allow("", null),
6510
6592
  limit: import_joi17.default.number().min(1).optional().allow("", null),
6511
6593
  search: import_joi17.default.string().optional().allow("", null),
6512
- site: import_joi17.default.string().hex().required()
6594
+ site: import_joi17.default.string().hex().required(),
6595
+ serviceType: import_joi17.default.string().valid(...Object.values(ServiceType)).required()
6513
6596
  });
6514
6597
  const { error } = validation.validate(query);
6515
6598
  if (error) {
@@ -6521,45 +6604,14 @@ function useScheduleTaskController() {
6521
6604
  const limit = parseInt(req.query.limit) ?? 10;
6522
6605
  const search = req.query.search ?? "";
6523
6606
  const site = req.params.site ?? "";
6607
+ const serviceType = req.query.serviceType ?? "";
6524
6608
  try {
6525
6609
  const data = await _getScheduleTasks({
6526
6610
  page,
6527
6611
  limit,
6528
6612
  search,
6529
- site
6530
- });
6531
- res.json(data);
6532
- return;
6533
- } catch (error2) {
6534
- import_node_server_utils35.logger.log({ level: "error", message: error2.message });
6535
- next(error2);
6536
- return;
6537
- }
6538
- }
6539
- async function getTasksForScheduleTask(req, res, next) {
6540
- const query = { ...req.query, ...req.params };
6541
- const validation = import_joi17.default.object({
6542
- page: import_joi17.default.number().min(1).optional().allow("", null),
6543
- limit: import_joi17.default.number().min(1).optional().allow("", null),
6544
- search: import_joi17.default.string().optional().allow("", null),
6545
- site: import_joi17.default.string().hex().required()
6546
- });
6547
- const { error } = validation.validate(query);
6548
- if (error) {
6549
- import_node_server_utils35.logger.log({ level: "error", message: error.message });
6550
- next(new import_node_server_utils35.BadRequestError(error.message));
6551
- return;
6552
- }
6553
- const page = parseInt(req.query.page) ?? 1;
6554
- const limit = parseInt(req.query.limit) ?? 10;
6555
- const search = req.query.search ?? "";
6556
- const site = req.params.site ?? "";
6557
- try {
6558
- const data = await _getTasksForScheduleTask({
6559
- page,
6560
- limit,
6561
- search,
6562
- site
6613
+ site,
6614
+ serviceType
6563
6615
  });
6564
6616
  res.json(data);
6565
6617
  return;
@@ -6625,7 +6677,6 @@ function useScheduleTaskController() {
6625
6677
  return {
6626
6678
  createScheduleTask,
6627
6679
  getScheduleTasks,
6628
- getTasksForScheduleTask,
6629
6680
  getScheduleTaskById,
6630
6681
  updateScheduleTask
6631
6682
  };