@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.mjs CHANGED
@@ -763,13 +763,10 @@ function useAreaRepo() {
763
763
  }
764
764
  async function getAreasForChecklist(site, serviceType) {
765
765
  const query = {
766
- status: { $ne: "deleted" }
766
+ status: { $ne: "deleted" },
767
+ serviceType
767
768
  };
768
- const cacheOptions = {};
769
- if (serviceType) {
770
- query.serviceType = serviceType;
771
- cacheOptions.serviceType = serviceType;
772
- }
769
+ const cacheOptions = { serviceType };
773
770
  try {
774
771
  site = new ObjectId3(site);
775
772
  query.site = site;
@@ -2224,6 +2221,7 @@ function useParentChecklistRepo() {
2224
2221
  async function createIndex() {
2225
2222
  try {
2226
2223
  await collection.createIndexes([
2224
+ { key: { serviceType: 1 } },
2227
2225
  { key: { createdAt: 1 } },
2228
2226
  { key: { site: 1 } },
2229
2227
  { key: { status: 1 } }
@@ -2241,44 +2239,63 @@ function useParentChecklistRepo() {
2241
2239
  startOfDay.setUTCHours(0, 0, 0, 0);
2242
2240
  const endOfDay = new Date(currentDate);
2243
2241
  endOfDay.setUTCHours(23, 59, 59, 999);
2244
- const existingQuery = {
2245
- createdAt: {
2246
- $gte: startOfDay,
2247
- $lte: endOfDay
2248
- }
2249
- };
2242
+ const allServiceTypes = Object.values(ServiceType);
2243
+ const dateStr = currentDate.toISOString().split("T")[0];
2250
2244
  if (value.site) {
2245
+ let siteObjectId;
2251
2246
  try {
2252
- existingQuery.site = new ObjectId7(value.site);
2253
- logger14.info(
2254
- `createParentChecklist: Looking for existing checklist with query: ${JSON.stringify(
2255
- { ...existingQuery, site: value.site }
2256
- )}`
2257
- );
2247
+ siteObjectId = new ObjectId7(value.site);
2258
2248
  } catch (error) {
2259
2249
  throw new BadRequestError12("Invalid site ID format.");
2260
2250
  }
2261
- } else {
2262
- logger14.info(
2263
- `createParentChecklist: Looking for existing checklist with query (no site filter): ${JSON.stringify(
2264
- existingQuery
2265
- )}`
2266
- );
2267
- }
2268
- const existingChecklist = await collection.findOne(existingQuery);
2269
- if (existingChecklist) {
2270
- const dateStr2 = currentDate.toISOString().split("T")[0];
2271
- logger14.info(
2272
- `Parent checklist already exists for ${value.site ? `site ${value.site} on` : ""} today: ${dateStr2}. Found checklist: ${JSON.stringify({
2273
- _id: existingChecklist._id,
2274
- site: existingChecklist.site
2275
- })}`
2251
+ if (value.serviceType) {
2252
+ const existingChecklist = await collection.findOne({
2253
+ createdAt: { $gte: startOfDay, $lte: endOfDay },
2254
+ site: siteObjectId,
2255
+ serviceType: value.serviceType
2256
+ });
2257
+ if (existingChecklist) {
2258
+ logger14.info(
2259
+ `Parent checklist already exists for site ${value.site} / ${value.serviceType} on ${dateStr}. _id: ${existingChecklist._id}`
2260
+ );
2261
+ return existingChecklist._id;
2262
+ }
2263
+ const doc = MParentChecklist({
2264
+ site: value.site,
2265
+ createdAt: currentDate,
2266
+ serviceType: value.serviceType
2267
+ });
2268
+ const result3 = await collection.insertOne(doc, { session });
2269
+ delNamespace().catch((err) => {
2270
+ logger14.error(
2271
+ `Failed to clear cache for namespace: ${namespace_collection}`,
2272
+ err
2273
+ );
2274
+ });
2275
+ logger14.info(
2276
+ `Created parent checklist for site ${value.site} / ${value.serviceType} for today: ${dateStr}`
2277
+ );
2278
+ return result3.insertedId;
2279
+ }
2280
+ const existingServiceTypes = await collection.distinct("serviceType", {
2281
+ createdAt: { $gte: startOfDay, $lte: endOfDay },
2282
+ site: siteObjectId,
2283
+ serviceType: { $exists: true, $ne: null }
2284
+ });
2285
+ const missingServiceTypes = allServiceTypes.filter(
2286
+ (st) => !existingServiceTypes.includes(st)
2276
2287
  );
2277
- return existingChecklist._id;
2278
- }
2279
- const allServiceTypes = Object.values(ServiceType);
2280
- if (value.site) {
2281
- const checklistDocs2 = allServiceTypes.map(
2288
+ if (missingServiceTypes.length === 0) {
2289
+ logger14.info(
2290
+ `All serviceType checklists already exist for site ${value.site} on ${dateStr}`
2291
+ );
2292
+ const first = await collection.findOne({
2293
+ createdAt: { $gte: startOfDay, $lte: endOfDay },
2294
+ site: siteObjectId
2295
+ });
2296
+ return first._id;
2297
+ }
2298
+ const checklistDocs2 = missingServiceTypes.map(
2282
2299
  (serviceType) => MParentChecklist({
2283
2300
  site: value.site,
2284
2301
  createdAt: currentDate,
@@ -2286,17 +2303,14 @@ function useParentChecklistRepo() {
2286
2303
  })
2287
2304
  );
2288
2305
  const result2 = await collection.insertMany(checklistDocs2, { session });
2289
- delNamespace().then(() => {
2290
- logger14.info(`Cache cleared for namespace: ${namespace_collection}`);
2291
- }).catch((err) => {
2306
+ delNamespace().catch((err) => {
2292
2307
  logger14.error(
2293
2308
  `Failed to clear cache for namespace: ${namespace_collection}`,
2294
2309
  err
2295
2310
  );
2296
2311
  });
2297
- const dateStr2 = currentDate.toISOString().split("T")[0];
2298
2312
  logger14.info(
2299
- `Created ${checklistDocs2.length} parent checklists for site ${value.site} for today: ${dateStr2}`
2313
+ `Created ${checklistDocs2.length} parent checklists for site ${value.site} for today: ${dateStr}`
2300
2314
  );
2301
2315
  return Object.values(result2.insertedIds);
2302
2316
  }
@@ -2309,18 +2323,41 @@ function useParentChecklistRepo() {
2309
2323
  logger14.warn("No sites found for creating parent checklist");
2310
2324
  throw new BadRequestError12("No sites available for checklist creation");
2311
2325
  }
2326
+ const existingPairs = await collection.find(
2327
+ {
2328
+ createdAt: { $gte: startOfDay, $lte: endOfDay },
2329
+ serviceType: { $exists: true, $ne: null }
2330
+ },
2331
+ { projection: { site: 1, serviceType: 1 } }
2332
+ ).toArray();
2333
+ const existingSet = new Set(
2334
+ existingPairs.map(
2335
+ (doc) => `${doc.site?.toString()}|${doc.serviceType}`
2336
+ )
2337
+ );
2312
2338
  const checklistDocs = [];
2313
2339
  for (const site of siteIds) {
2314
2340
  for (const serviceType of allServiceTypes) {
2315
- checklistDocs.push(
2316
- MParentChecklist({
2317
- site,
2318
- createdAt: currentDate,
2319
- serviceType
2320
- })
2321
- );
2341
+ if (!existingSet.has(`${site}|${serviceType}`)) {
2342
+ checklistDocs.push(
2343
+ MParentChecklist({
2344
+ site,
2345
+ createdAt: currentDate,
2346
+ serviceType
2347
+ })
2348
+ );
2349
+ }
2322
2350
  }
2323
2351
  }
2352
+ if (checklistDocs.length === 0) {
2353
+ logger14.info(
2354
+ `All site+serviceType checklists already exist for today: ${dateStr}`
2355
+ );
2356
+ return [];
2357
+ }
2358
+ logger14.info(
2359
+ `createParentChecklist: Creating ${checklistDocs.length} missing site+serviceType checklist(s) for today: ${dateStr}`
2360
+ );
2324
2361
  const result = await collection.insertMany(checklistDocs, { session });
2325
2362
  delNamespace().then(() => {
2326
2363
  logger14.info(`Cache cleared for namespace: ${namespace_collection}`);
@@ -2330,7 +2367,6 @@ function useParentChecklistRepo() {
2330
2367
  err
2331
2368
  );
2332
2369
  });
2333
- const dateStr = currentDate.toISOString().split("T")[0];
2334
2370
  logger14.info(
2335
2371
  `Created ${Object.keys(result.insertedIds).length} parent checklists for today: ${dateStr}`
2336
2372
  );
@@ -2438,6 +2474,23 @@ function useParentChecklistRepo() {
2438
2474
  throw error;
2439
2475
  }
2440
2476
  }
2477
+ async function getParentChecklistById(_id) {
2478
+ try {
2479
+ _id = new ObjectId7(_id);
2480
+ } catch (error) {
2481
+ throw new BadRequestError12("Invalid parent checklist ID format.");
2482
+ }
2483
+ try {
2484
+ const data = await collection.findOne({ _id });
2485
+ if (!data) {
2486
+ throw new BadRequestError12("Parent checklist not found.");
2487
+ }
2488
+ return data;
2489
+ } catch (error) {
2490
+ logger14.error("Failed to get parent checklist by ID", error);
2491
+ throw error;
2492
+ }
2493
+ }
2441
2494
  async function updateParentChecklistStatuses(_id, status, session) {
2442
2495
  try {
2443
2496
  _id = new ObjectId7(_id);
@@ -2552,13 +2605,38 @@ function useParentChecklistRepo() {
2552
2605
  throw error;
2553
2606
  }
2554
2607
  }
2608
+ async function getTodayParentChecklistsForAreaGen() {
2609
+ const now = /* @__PURE__ */ new Date();
2610
+ const start = new Date(now);
2611
+ start.setUTCHours(0, 0, 0, 0);
2612
+ const end = new Date(now);
2613
+ end.setUTCHours(23, 59, 59, 999);
2614
+ try {
2615
+ const items = await collection.find(
2616
+ {
2617
+ createdAt: { $gte: start, $lte: end },
2618
+ serviceType: { $exists: true, $ne: null }
2619
+ },
2620
+ { projection: { _id: 1, site: 1, serviceType: 1 } }
2621
+ ).toArray();
2622
+ return items;
2623
+ } catch (error) {
2624
+ logger14.error(
2625
+ "Failed to get today's parent checklists for area generation",
2626
+ error
2627
+ );
2628
+ throw error;
2629
+ }
2630
+ }
2555
2631
  return {
2556
2632
  createIndex,
2557
2633
  createParentChecklist,
2558
2634
  getAllParentChecklist,
2635
+ getParentChecklistById,
2559
2636
  updateParentChecklistStatuses,
2560
2637
  closeExpiredParentChecklists,
2561
- getTodayParentChecklists
2638
+ getTodayParentChecklists,
2639
+ getTodayParentChecklistsForAreaGen
2562
2640
  };
2563
2641
  }
2564
2642
 
@@ -2646,6 +2724,7 @@ import { BadRequestError as BadRequestError14, logger as logger16 } from "@7365a
2646
2724
  var allowedChecklistStatus = ["open", "completed", "closed"];
2647
2725
  var areaChecklistSchema = Joi8.object({
2648
2726
  schedule: Joi8.string().hex().required(),
2727
+ serviceType: Joi8.string().valid(...Object.values(ServiceType)).optional(),
2649
2728
  area: Joi8.string().hex().required(),
2650
2729
  name: Joi8.string().required(),
2651
2730
  type: Joi8.string().valid(...allowedTypes).required(),
@@ -2712,6 +2791,7 @@ function MAreaChecklist(value) {
2712
2791
  }
2713
2792
  return {
2714
2793
  schedule: value.schedule,
2794
+ serviceType: value.serviceType,
2715
2795
  area: value.area,
2716
2796
  name: value.name,
2717
2797
  type: value.type,
@@ -2833,7 +2913,8 @@ function useAreaChecklistRepo() {
2833
2913
  search = "",
2834
2914
  type = "all",
2835
2915
  status = "all",
2836
- schedule
2916
+ schedule,
2917
+ serviceType
2837
2918
  }, session) {
2838
2919
  page = page > 0 ? page - 1 : 0;
2839
2920
  const query = {};
@@ -2847,6 +2928,10 @@ function useAreaChecklistRepo() {
2847
2928
  } catch (error) {
2848
2929
  throw new BadRequestError15("Invalid parent checklist ID format.");
2849
2930
  }
2931
+ if (serviceType) {
2932
+ query.serviceType = serviceType;
2933
+ cacheOptions.serviceType = serviceType;
2934
+ }
2850
2935
  if (type && type !== "all") {
2851
2936
  query.type = type;
2852
2937
  cacheOptions.type = type;
@@ -3242,6 +3327,7 @@ function useAreaChecklistRepo() {
3242
3327
  page = 1,
3243
3328
  limit = 10,
3244
3329
  search = "",
3330
+ serviceType,
3245
3331
  _id
3246
3332
  }, session) {
3247
3333
  page = page > 0 ? page - 1 : 0;
@@ -3256,6 +3342,10 @@ function useAreaChecklistRepo() {
3256
3342
  } catch (error) {
3257
3343
  throw new BadRequestError15("Invalid area checklist ID format.");
3258
3344
  }
3345
+ if (serviceType) {
3346
+ query.serviceType = serviceType;
3347
+ cacheOptions.serviceType = serviceType;
3348
+ }
3259
3349
  if (search) {
3260
3350
  query.$text = { $search: search };
3261
3351
  cacheOptions.search = search;
@@ -3479,7 +3569,12 @@ function useAreaChecklistRepo() {
3479
3569
  throw error;
3480
3570
  }
3481
3571
  }
3482
- async function getAreaChecklistByAreaAndSchedule(schedule, area, session) {
3572
+ async function getAreaChecklistByAreaAndSchedule({
3573
+ schedule,
3574
+ serviceType,
3575
+ area,
3576
+ session
3577
+ }) {
3483
3578
  try {
3484
3579
  schedule = new ObjectId9(schedule);
3485
3580
  area = new ObjectId9(area);
@@ -3487,7 +3582,10 @@ function useAreaChecklistRepo() {
3487
3582
  throw new BadRequestError15("Invalid area checklist ID format.");
3488
3583
  }
3489
3584
  try {
3490
- const data = await collection.findOne({ schedule, area }, { session });
3585
+ const data = await collection.findOne(
3586
+ { schedule, serviceType, area },
3587
+ { session }
3588
+ );
3491
3589
  if (!data) {
3492
3590
  throw new BadRequestError15("Area checklist not found.");
3493
3591
  }
@@ -3696,7 +3794,7 @@ function useAreaChecklistRepo() {
3696
3794
  throw error;
3697
3795
  }
3698
3796
  }
3699
- async function pushScheduleTaskSets(scheduleId, areaId, scheduleTaskId, newSets) {
3797
+ async function pushScheduleTaskSets(scheduleId, serviceType, areaId, scheduleTaskId, newSets) {
3700
3798
  try {
3701
3799
  const schedule = new ObjectId9(scheduleId);
3702
3800
  const area = new ObjectId9(areaId);
@@ -3704,6 +3802,7 @@ function useAreaChecklistRepo() {
3704
3802
  const result = await collection.updateOne(
3705
3803
  {
3706
3804
  schedule,
3805
+ serviceType,
3707
3806
  area,
3708
3807
  checklist: { $not: { $elemMatch: { scheduleTaskId: taskId } } }
3709
3808
  },
@@ -3726,13 +3825,14 @@ function useAreaChecklistRepo() {
3726
3825
  throw error;
3727
3826
  }
3728
3827
  }
3729
- async function insertAutoGenSets(scheduleId, areaId, newSets) {
3828
+ async function insertAutoGenSets(scheduleId, serviceType, areaId, newSets) {
3730
3829
  try {
3731
3830
  const schedule = new ObjectId9(scheduleId);
3732
3831
  const area = new ObjectId9(areaId);
3733
3832
  const result = await collection.updateOne(
3734
3833
  {
3735
3834
  schedule,
3835
+ serviceType,
3736
3836
  area,
3737
3837
  checklist: {
3738
3838
  $not: { $elemMatch: { isScheduleTask: { $ne: true } } }
@@ -3878,14 +3978,25 @@ function useAreaChecklistService() {
3878
3978
  updateAreaChecklistStatus,
3879
3979
  insertAutoGenSets
3880
3980
  } = useAreaChecklistRepo();
3881
- const { updateParentChecklistStatuses } = useParentChecklistRepo();
3981
+ const { updateParentChecklistStatuses, getParentChecklistById } = useParentChecklistRepo();
3882
3982
  const { getUserById } = useUserRepo();
3883
3983
  async function createAreaChecklist(value) {
3884
3984
  const results = [];
3885
3985
  let totalChecklistsCreated = 0;
3886
3986
  try {
3887
3987
  const BATCH_SIZE = 10;
3888
- const areasResult = await getAreasForChecklist(value.site);
3988
+ const schedule = await getParentChecklistById(value.schedule.toString());
3989
+ const serviceType = schedule.serviceType;
3990
+ if (!serviceType) {
3991
+ logger18.warn(
3992
+ `createAreaChecklist: Parent checklist ${value.schedule} has no serviceType, skipping area checklist generation`
3993
+ );
3994
+ return results;
3995
+ }
3996
+ const areasResult = await getAreasForChecklist(
3997
+ value.site,
3998
+ serviceType
3999
+ );
3889
4000
  const areas = areasResult || [];
3890
4001
  if (areas.length > 0) {
3891
4002
  for (let i = 0; i < areas.length; i += BATCH_SIZE) {
@@ -3899,10 +4010,11 @@ function useAreaChecklistService() {
3899
4010
  }
3900
4011
  let existing = null;
3901
4012
  try {
3902
- existing = await getAreaChecklistByAreaAndSchedule(
3903
- value.schedule.toString(),
3904
- area._id.toString()
3905
- );
4013
+ existing = await getAreaChecklistByAreaAndSchedule({
4014
+ schedule: value.schedule.toString(),
4015
+ serviceType,
4016
+ area: area._id.toString()
4017
+ });
3906
4018
  } catch (_) {
3907
4019
  }
3908
4020
  if (existing) {
@@ -3950,6 +4062,7 @@ function useAreaChecklistService() {
3950
4062
  );
3951
4063
  await insertAutoGenSets(
3952
4064
  value.schedule.toString(),
4065
+ serviceType,
3953
4066
  area._id.toString(),
3954
4067
  autoGenSets
3955
4068
  );
@@ -3958,6 +4071,7 @@ function useAreaChecklistService() {
3958
4071
  const setCount = Number(area.set) || 1;
3959
4072
  const checklistData = {
3960
4073
  schedule: value.schedule,
4074
+ serviceType,
3961
4075
  area: area._id.toString(),
3962
4076
  name: area.name,
3963
4077
  type: area.type,
@@ -4088,7 +4202,11 @@ function useAreaChecklistService() {
4088
4202
 
4089
4203
  // src/controllers/hygiene-area-checklist.controller.ts
4090
4204
  import Joi9 from "joi";
4091
- import { BadRequestError as BadRequestError16, InternalServerError as InternalServerError7, logger as logger20 } from "@7365admin1/node-server-utils";
4205
+ import {
4206
+ BadRequestError as BadRequestError16,
4207
+ InternalServerError as InternalServerError7,
4208
+ logger as logger20
4209
+ } from "@7365admin1/node-server-utils";
4092
4210
 
4093
4211
  // src/services/hygiene-checklist-pdf.service.ts
4094
4212
  import { launch } from "puppeteer";
@@ -4357,7 +4475,8 @@ function useAreaChecklistController() {
4357
4475
  search: Joi9.string().optional().allow("", null),
4358
4476
  type: Joi9.string().valid(...allowedTypes, "all").optional().allow("", null),
4359
4477
  status: Joi9.string().valid(...allowedStatus, "all").optional().allow("", null),
4360
- schedule: Joi9.string().hex().required()
4478
+ schedule: Joi9.string().hex().required(),
4479
+ serviceType: Joi9.string().valid(...Object.values(ServiceType)).required()
4361
4480
  });
4362
4481
  const { error } = validation.validate(query);
4363
4482
  if (error) {
@@ -4371,6 +4490,7 @@ function useAreaChecklistController() {
4371
4490
  const type = req.query.type ?? "all";
4372
4491
  const status = req.query.status ?? "all";
4373
4492
  const schedule = req.params.schedule ?? "";
4493
+ const serviceType = req.query.serviceType ?? "";
4374
4494
  try {
4375
4495
  const data = await _getAllAreaChecklist({
4376
4496
  page,
@@ -4378,7 +4498,8 @@ function useAreaChecklistController() {
4378
4498
  search,
4379
4499
  type,
4380
4500
  status,
4381
- schedule
4501
+ schedule,
4502
+ serviceType
4382
4503
  });
4383
4504
  res.json(data);
4384
4505
  return;
@@ -4455,6 +4576,7 @@ function useAreaChecklistController() {
4455
4576
  page: Joi9.number().min(1).optional().allow("", null),
4456
4577
  limit: Joi9.number().min(1).optional().allow("", null),
4457
4578
  search: Joi9.string().optional().allow("", null),
4579
+ serviceType: Joi9.string().valid(...Object.values(ServiceType)).required(),
4458
4580
  id: Joi9.string().hex().required()
4459
4581
  });
4460
4582
  const { error } = validation.validate(query);
@@ -4466,12 +4588,14 @@ function useAreaChecklistController() {
4466
4588
  const page = parseInt(req.query.page) ?? 1;
4467
4589
  const limit = parseInt(req.query.limit) ?? 10;
4468
4590
  const search = req.query.search ?? "";
4591
+ const serviceType = req.query.serviceType ?? "";
4469
4592
  const _id = req.params.id ?? "";
4470
4593
  try {
4471
4594
  const data = await _getAreaChecklistUnits({
4472
4595
  page,
4473
4596
  limit,
4474
4597
  search,
4598
+ serviceType,
4475
4599
  _id
4476
4600
  });
4477
4601
  res.json(data);
@@ -4543,12 +4667,15 @@ function useAreaChecklistController() {
4543
4667
  try {
4544
4668
  const pdfBuffer = await _generateChecklistPdf(value);
4545
4669
  if (!pdfBuffer || pdfBuffer.length === 0) {
4546
- throw new InternalServerError7("Generated checklist PDF is empty or invalid.");
4670
+ throw new InternalServerError7(
4671
+ "Generated checklist PDF is empty or invalid."
4672
+ );
4547
4673
  }
4548
4674
  const date = /* @__PURE__ */ new Date();
4549
- const formattedDate = `${String(date.getDate()).padStart(2, "0")}_${String(
4550
- date.getMonth() + 1
4551
- ).padStart(2, "0")}_${date.getFullYear()}`;
4675
+ const formattedDate = `${String(date.getDate()).padStart(
4676
+ 2,
4677
+ "0"
4678
+ )}_${String(date.getMonth() + 1).padStart(2, "0")}_${date.getFullYear()}`;
4552
4679
  res.setHeader("Content-Type", "application/pdf");
4553
4680
  res.setHeader(
4554
4681
  "Content-Disposition",
@@ -5848,6 +5975,7 @@ import Joi16 from "joi";
5848
5975
  import { ObjectId as ObjectId18 } from "mongodb";
5849
5976
  var scheduleTaskSchema = Joi16.object({
5850
5977
  site: Joi16.string().hex().required(),
5978
+ serviceType: Joi16.string().valid(...Object.values(ServiceType)).required(),
5851
5979
  title: Joi16.string().required(),
5852
5980
  time: Joi16.string().pattern(/^([0-1]\d|2[0-3]):([0-5]\d)$/).required(),
5853
5981
  dates: Joi16.array().min(1).items(
@@ -5896,6 +6024,7 @@ function MScheduleTask(value) {
5896
6024
  }
5897
6025
  return {
5898
6026
  site: value.site,
6027
+ serviceType: value.serviceType,
5899
6028
  title: value.title,
5900
6029
  time: value.time,
5901
6030
  dates: value.dates,
@@ -5933,6 +6062,7 @@ function useScheduleTaskRepository() {
5933
6062
  try {
5934
6063
  await collection.createIndexes([
5935
6064
  { key: { site: 1 } },
6065
+ { key: { serviceType: 1 } },
5936
6066
  { key: { status: 1 } }
5937
6067
  ]);
5938
6068
  } catch (error) {
@@ -5971,15 +6101,18 @@ function useScheduleTaskRepository() {
5971
6101
  page = 1,
5972
6102
  limit = 10,
5973
6103
  search = "",
5974
- site
6104
+ site,
6105
+ serviceType
5975
6106
  }) {
5976
6107
  page = page > 0 ? page - 1 : 0;
5977
6108
  const query = {
5978
- status: { $ne: "deleted" }
6109
+ status: { $ne: "deleted" },
6110
+ serviceType
5979
6111
  };
5980
6112
  const cacheOptions = {
5981
6113
  page,
5982
- limit
6114
+ limit,
6115
+ serviceType
5983
6116
  };
5984
6117
  try {
5985
6118
  site = new ObjectId19(site);
@@ -6035,62 +6168,6 @@ function useScheduleTaskRepository() {
6035
6168
  throw error;
6036
6169
  }
6037
6170
  }
6038
- async function getTasksForScheduleTask({
6039
- page = 1,
6040
- limit = 10,
6041
- search = "",
6042
- site
6043
- }) {
6044
- page = page > 0 ? page - 1 : 0;
6045
- const query = {
6046
- status: { $ne: "deleted" }
6047
- };
6048
- const cacheOptions = {
6049
- page,
6050
- limit
6051
- };
6052
- try {
6053
- site = new ObjectId19(site);
6054
- query.site = site;
6055
- cacheOptions.site = site.toString();
6056
- } catch (error) {
6057
- throw new BadRequestError29("Invalid site ID format.");
6058
- }
6059
- if (search) {
6060
- query.$or = [{ name: { $regex: search, $options: "i" } }];
6061
- cacheOptions.search = search;
6062
- }
6063
- const cacheKey = makeCacheKey9(namespace_collection, cacheOptions);
6064
- const cachedData = await getCache(cacheKey);
6065
- if (cachedData) {
6066
- logger31.info(`Cache hit for key: ${cacheKey}`);
6067
- return cachedData;
6068
- }
6069
- try {
6070
- const items = await collection.aggregate([
6071
- { $match: query },
6072
- {
6073
- $project: {
6074
- createdAt: 1,
6075
- title: 1
6076
- }
6077
- },
6078
- { $sort: { _id: -1 } },
6079
- { $skip: page * limit },
6080
- { $limit: limit }
6081
- ]).toArray();
6082
- const length = await collection.countDocuments(query);
6083
- const data = paginate8(items, page, limit, length);
6084
- setCache(cacheKey, data, 15 * 60).then(() => {
6085
- logger31.info(`Cache set for key: ${cacheKey}`);
6086
- }).catch((err) => {
6087
- logger31.error(`Failed to set cache for key: ${cacheKey}`, err);
6088
- });
6089
- return data;
6090
- } catch (error) {
6091
- throw error;
6092
- }
6093
- }
6094
6171
  async function getScheduleTaskById(_id, session) {
6095
6172
  try {
6096
6173
  _id = new ObjectId19(_id);
@@ -6190,7 +6267,6 @@ function useScheduleTaskRepository() {
6190
6267
  createScheduleTask,
6191
6268
  getScheduleTasks,
6192
6269
  getAllScheduleTask,
6193
- getTasksForScheduleTask,
6194
6270
  getScheduleTaskById,
6195
6271
  updateScheduleTask
6196
6272
  };
@@ -6208,7 +6284,7 @@ function useScheduleTaskService() {
6208
6284
  pushScheduleTaskSets
6209
6285
  } = useAreaChecklistRepo();
6210
6286
  const { getAreaById } = useAreaRepo();
6211
- function checkScheduleConditions(schedule, currentDate = /* @__PURE__ */ new Date()) {
6287
+ function checkScheduleConditions(schedule, serviceType, currentDate = /* @__PURE__ */ new Date()) {
6212
6288
  try {
6213
6289
  const now = currentDate;
6214
6290
  const timeString = now.toLocaleTimeString("en-US", {
@@ -6299,12 +6375,17 @@ function useScheduleTaskService() {
6299
6375
  const validatedTasks = [];
6300
6376
  for (const scheduleTask of scheduleTasks) {
6301
6377
  try {
6378
+ const serviceType = scheduleTask.serviceType;
6302
6379
  logger32.info(
6303
6380
  `Checking schedule ${scheduleTask._id} - ${scheduleTask.title}: time=${scheduleTask.time}, dates=${JSON.stringify(
6304
6381
  scheduleTask.dates
6305
6382
  )}`
6306
6383
  );
6307
- const shouldRun = checkScheduleConditions(scheduleTask, currentDate);
6384
+ const shouldRun = checkScheduleConditions(
6385
+ scheduleTask,
6386
+ serviceType,
6387
+ currentDate
6388
+ );
6308
6389
  if (!shouldRun) {
6309
6390
  logger32.info(
6310
6391
  `Schedule ${scheduleTask._id} conditions not met, skipping`
@@ -6327,6 +6408,7 @@ function useScheduleTaskService() {
6327
6408
  );
6328
6409
  const parentChecklistIds = await createParentChecklist({
6329
6410
  site: scheduleTask.site.toString(),
6411
+ serviceType,
6330
6412
  createdAt: /* @__PURE__ */ new Date()
6331
6413
  });
6332
6414
  const parentChecklistId = Array.isArray(parentChecklistIds) ? parentChecklistIds[0] : parentChecklistIds;
@@ -6364,8 +6446,11 @@ function useScheduleTaskService() {
6364
6446
  let existingAreaChecklist;
6365
6447
  try {
6366
6448
  existingAreaChecklist = await getAreaChecklistByAreaAndSchedule(
6367
- parentChecklistId.toString(),
6368
- areaId
6449
+ {
6450
+ schedule: parentChecklistId.toString(),
6451
+ serviceType,
6452
+ area: areaId
6453
+ }
6369
6454
  );
6370
6455
  logger32.info(
6371
6456
  `Area ${area.name} (${areaId}): Existing area checklist found: ${existingAreaChecklist ? "Yes" : "No"}`
@@ -6402,6 +6487,7 @@ function useScheduleTaskService() {
6402
6487
  );
6403
6488
  const modified = await pushScheduleTaskSets(
6404
6489
  parentChecklistId.toString(),
6490
+ serviceType,
6405
6491
  areaId,
6406
6492
  scheduleTask._id.toString(),
6407
6493
  newSets
@@ -6429,6 +6515,7 @@ function useScheduleTaskService() {
6429
6515
  );
6430
6516
  const checklistData = {
6431
6517
  schedule: parentChecklistId.toString(),
6518
+ serviceType,
6432
6519
  area: areaId,
6433
6520
  name: area.name,
6434
6521
  type: areaDetails.type || "common",
@@ -6483,7 +6570,6 @@ function useScheduleTaskController() {
6483
6570
  const {
6484
6571
  createScheduleTask: _createScheduleTask,
6485
6572
  getScheduleTasks: _getScheduleTasks,
6486
- getTasksForScheduleTask: _getTasksForScheduleTask,
6487
6573
  getScheduleTaskById: _getScheduleTaskById,
6488
6574
  updateScheduleTask: _updateScheduleTask
6489
6575
  } = useScheduleTaskRepository();
@@ -6516,7 +6602,8 @@ function useScheduleTaskController() {
6516
6602
  page: Joi17.number().min(1).optional().allow("", null),
6517
6603
  limit: Joi17.number().min(1).optional().allow("", null),
6518
6604
  search: Joi17.string().optional().allow("", null),
6519
- site: Joi17.string().hex().required()
6605
+ site: Joi17.string().hex().required(),
6606
+ serviceType: Joi17.string().valid(...Object.values(ServiceType)).required()
6520
6607
  });
6521
6608
  const { error } = validation.validate(query);
6522
6609
  if (error) {
@@ -6528,45 +6615,14 @@ function useScheduleTaskController() {
6528
6615
  const limit = parseInt(req.query.limit) ?? 10;
6529
6616
  const search = req.query.search ?? "";
6530
6617
  const site = req.params.site ?? "";
6618
+ const serviceType = req.query.serviceType ?? "";
6531
6619
  try {
6532
6620
  const data = await _getScheduleTasks({
6533
6621
  page,
6534
6622
  limit,
6535
6623
  search,
6536
- site
6537
- });
6538
- res.json(data);
6539
- return;
6540
- } catch (error2) {
6541
- logger33.log({ level: "error", message: error2.message });
6542
- next(error2);
6543
- return;
6544
- }
6545
- }
6546
- async function getTasksForScheduleTask(req, res, next) {
6547
- const query = { ...req.query, ...req.params };
6548
- const validation = Joi17.object({
6549
- page: Joi17.number().min(1).optional().allow("", null),
6550
- limit: Joi17.number().min(1).optional().allow("", null),
6551
- search: Joi17.string().optional().allow("", null),
6552
- site: Joi17.string().hex().required()
6553
- });
6554
- const { error } = validation.validate(query);
6555
- if (error) {
6556
- logger33.log({ level: "error", message: error.message });
6557
- next(new BadRequestError30(error.message));
6558
- return;
6559
- }
6560
- const page = parseInt(req.query.page) ?? 1;
6561
- const limit = parseInt(req.query.limit) ?? 10;
6562
- const search = req.query.search ?? "";
6563
- const site = req.params.site ?? "";
6564
- try {
6565
- const data = await _getTasksForScheduleTask({
6566
- page,
6567
- limit,
6568
- search,
6569
- site
6624
+ site,
6625
+ serviceType
6570
6626
  });
6571
6627
  res.json(data);
6572
6628
  return;
@@ -6632,7 +6688,6 @@ function useScheduleTaskController() {
6632
6688
  return {
6633
6689
  createScheduleTask,
6634
6690
  getScheduleTasks,
6635
- getTasksForScheduleTask,
6636
6691
  getScheduleTaskById,
6637
6692
  updateScheduleTask
6638
6693
  };