@7365admin1/module-hygiene 2.0.0 → 3.0.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
@@ -4793,45 +4793,12 @@ function useRequestItemController() {
4793
4793
  import { BadRequestError as BadRequestError28, logger as logger27 } from "@7365admin1/node-server-utils";
4794
4794
  import Joi16 from "joi";
4795
4795
  import { ObjectId as ObjectId16 } from "mongodb";
4796
- var allowedFrequency = [
4797
- "daily",
4798
- "weekly",
4799
- "monthly",
4800
- "annually"
4801
- ];
4802
- var allowedDays = [
4803
- "Monday",
4804
- "Tuesday",
4805
- "Wednesday",
4806
- "Thursday",
4807
- "Friday",
4808
- "Saturday",
4809
- "Sunday"
4810
- ];
4811
- var allowedWeekOfMonth = ["start", "end"];
4812
- var allowedMonths = [
4813
- "January",
4814
- "February",
4815
- "March",
4816
- "April",
4817
- "May",
4818
- "June",
4819
- "July",
4820
- "August",
4821
- "September",
4822
- "October",
4823
- "November",
4824
- "December"
4825
- ];
4826
4796
  var scheduleTaskSchema = Joi16.object({
4827
4797
  site: Joi16.string().hex().required(),
4828
4798
  title: Joi16.string().required(),
4829
- frequency: Joi16.string().valid(...allowedFrequency).required(),
4830
4799
  time: Joi16.string().pattern(/^([0-1]\d|2[0-3]):([0-5]\d)$/).required(),
4831
- day: Joi16.array().items(Joi16.string().valid(...allowedDays)).optional().allow(null),
4832
- weekOfMonth: Joi16.string().valid(...allowedWeekOfMonth).optional().allow("", null),
4833
- month: Joi16.string().valid(...allowedMonths).optional().allow("", null),
4834
- date: Joi16.number().min(1).max(31).optional().allow(null),
4800
+ startDate: Joi16.string().pattern(/^\d{4}-\d{2}-\d{2}$/).required(),
4801
+ endDate: Joi16.string().pattern(/^\d{4}-\d{2}-\d{2}$/).optional().allow("", null),
4835
4802
  description: Joi16.string().optional().allow("", null),
4836
4803
  areas: Joi16.array().min(1).items(
4837
4804
  Joi16.object({
@@ -4868,12 +4835,9 @@ function MScheduleTask(value) {
4868
4835
  return {
4869
4836
  site: value.site,
4870
4837
  title: value.title,
4871
- frequency: value.frequency,
4872
4838
  time: value.time,
4873
- day: value.day,
4874
- weekOfMonth: value.weekOfMonth,
4875
- month: value.month,
4876
- date: value.date,
4839
+ startDate: value.startDate,
4840
+ endDate: value.endDate,
4877
4841
  description: value.description,
4878
4842
  areas: value.areas,
4879
4843
  status: "active",
@@ -5093,12 +5057,9 @@ function useScheduleTaskRepository() {
5093
5057
  {
5094
5058
  $project: {
5095
5059
  title: 1,
5096
- frequency: 1,
5097
5060
  time: 1,
5098
- day: 1,
5099
- weekOfMonth: 1,
5100
- month: 1,
5101
- date: 1,
5061
+ startDate: 1,
5062
+ endDate: 1,
5102
5063
  description: 1,
5103
5064
  areas: 1,
5104
5065
  status: 1,
@@ -5188,20 +5149,6 @@ function useScheduleTaskService() {
5188
5149
  function checkScheduleConditions(schedule, currentDate = /* @__PURE__ */ new Date()) {
5189
5150
  try {
5190
5151
  const now = currentDate;
5191
- const currentDay = now.toLocaleDateString("en-US", {
5192
- weekday: "long",
5193
- timeZone: "Asia/Singapore"
5194
- });
5195
- const currentMonth = now.toLocaleDateString("en-US", {
5196
- month: "long",
5197
- timeZone: "Asia/Singapore"
5198
- });
5199
- const currentDate_num = Number(
5200
- now.toLocaleDateString("en-US", {
5201
- day: "numeric",
5202
- timeZone: "Asia/Singapore"
5203
- })
5204
- );
5205
5152
  const timeString = now.toLocaleTimeString("en-US", {
5206
5153
  hour: "2-digit",
5207
5154
  minute: "2-digit",
@@ -5209,9 +5156,29 @@ function useScheduleTaskService() {
5209
5156
  timeZone: "Asia/Singapore"
5210
5157
  });
5211
5158
  const [currentHour, currentMinute] = timeString.split(":").map(Number);
5159
+ const currentDateString = now.toLocaleDateString("en-US", {
5160
+ timeZone: "Asia/Singapore"
5161
+ });
5212
5162
  logger29.info(
5213
- `Checking schedule ${schedule._id}: Current time ${currentHour}:${currentMinute}, Schedule time ${schedule.time}, Frequency ${schedule.frequency}`
5163
+ `Checking schedule ${schedule._id}: Current time ${currentHour}:${currentMinute}, Current date ${currentDateString}, Schedule time ${schedule.time}, Start date ${schedule.startDate}, End date ${schedule.endDate}`
5214
5164
  );
5165
+ const startDate = /* @__PURE__ */ new Date(schedule.startDate + "T00:00:00");
5166
+ const currentDateOnly = /* @__PURE__ */ new Date(currentDateString + "T00:00:00");
5167
+ if (currentDateOnly < startDate) {
5168
+ logger29.info(
5169
+ `Schedule ${schedule._id}: Current date ${currentDateString} is before start date ${schedule.startDate}`
5170
+ );
5171
+ return false;
5172
+ }
5173
+ if (schedule.endDate) {
5174
+ const endDate = /* @__PURE__ */ new Date(schedule.endDate + "T00:00:00");
5175
+ if (currentDateOnly > endDate) {
5176
+ logger29.info(
5177
+ `Schedule ${schedule._id}: Current date ${currentDateString} is after end date ${schedule.endDate}`
5178
+ );
5179
+ return false;
5180
+ }
5181
+ }
5215
5182
  const [scheduleHour, scheduleMinute] = schedule.time.split(":").map(Number);
5216
5183
  const timeMatches = currentHour === scheduleHour && currentMinute === scheduleMinute;
5217
5184
  if (!timeMatches) {
@@ -5221,68 +5188,9 @@ function useScheduleTaskService() {
5221
5188
  return false;
5222
5189
  }
5223
5190
  logger29.info(
5224
- `Schedule ${schedule._id}: Time matches, checking frequency...`
5191
+ `Schedule ${schedule._id}: All conditions matched - Date is within range and time matches`
5225
5192
  );
5226
- switch (schedule.frequency) {
5227
- case "daily":
5228
- logger29.info(`Schedule ${schedule._id}: Daily frequency matched`);
5229
- return true;
5230
- case "weekly":
5231
- if (!schedule.day || schedule.day.length === 0) {
5232
- logger29.warn(
5233
- `Schedule ${schedule._id} is weekly but has no days specified`
5234
- );
5235
- return false;
5236
- }
5237
- const dayMatches = schedule.day.includes(currentDay);
5238
- logger29.info(
5239
- `Schedule ${schedule._id}: Weekly - Current day: ${currentDay}, Schedule days: ${schedule.day.join(
5240
- ", "
5241
- )}, Match: ${dayMatches}`
5242
- );
5243
- return dayMatches;
5244
- case "monthly":
5245
- if (!schedule.weekOfMonth) {
5246
- logger29.warn(
5247
- `Schedule ${schedule._id} is monthly but has no weekOfMonth specified`
5248
- );
5249
- return false;
5250
- }
5251
- if (schedule.weekOfMonth === "start") {
5252
- const matches2 = currentDate_num >= 1 && currentDate_num <= 7;
5253
- logger29.info(
5254
- `Schedule ${schedule._id}: Monthly start - Current date: ${currentDate_num}, Match: ${matches2}`
5255
- );
5256
- return matches2;
5257
- } else if (schedule.weekOfMonth === "end") {
5258
- const lastDayOfMonth = new Date(
5259
- now.getFullYear(),
5260
- now.getMonth() + 1,
5261
- 0
5262
- ).getDate();
5263
- const matches2 = currentDate_num > lastDayOfMonth - 7;
5264
- logger29.info(
5265
- `Schedule ${schedule._id}: Monthly end - Current date: ${currentDate_num}, Last day: ${lastDayOfMonth}, Match: ${matches2}`
5266
- );
5267
- return matches2;
5268
- }
5269
- return false;
5270
- case "annually":
5271
- if (!schedule.month || !schedule.date) {
5272
- logger29.warn(
5273
- `Schedule ${schedule._id} is annually but has no month or date specified`
5274
- );
5275
- return false;
5276
- }
5277
- const matches = currentMonth === schedule.month && currentDate_num === schedule.date;
5278
- logger29.info(
5279
- `Schedule ${schedule._id}: Annually - Current: ${currentMonth} ${currentDate_num}, Expected: ${schedule.month} ${schedule.date}, Match: ${matches}`
5280
- );
5281
- return matches;
5282
- default:
5283
- logger29.warn(`Unknown frequency type: ${schedule.frequency}`);
5284
- return false;
5285
- }
5193
+ return true;
5286
5194
  } catch (error) {
5287
5195
  logger29.error(
5288
5196
  `Error checking schedule conditions for ${schedule._id}:`,
@@ -5306,7 +5214,7 @@ function useScheduleTaskService() {
5306
5214
  for (const scheduleTask of scheduleTasks) {
5307
5215
  try {
5308
5216
  logger29.info(
5309
- `Checking schedule ${scheduleTask._id} - ${scheduleTask.title}: time=${scheduleTask.time}, frequency=${scheduleTask.frequency}`
5217
+ `Checking schedule ${scheduleTask._id} - ${scheduleTask.title}: time=${scheduleTask.time}, startDate=${scheduleTask.startDate}, endDate=${scheduleTask.endDate}`
5310
5218
  );
5311
5219
  const shouldRun = checkScheduleConditions(scheduleTask, currentDate);
5312
5220
  if (!shouldRun) {
@@ -5337,48 +5245,18 @@ function useScheduleTaskService() {
5337
5245
  logger29.info(
5338
5246
  `Using parent checklist ${parentChecklistId}, now creating/updating area checklists`
5339
5247
  );
5340
- const fullScheduleTask = await getScheduleTaskById(
5341
- scheduleTask._id.toString()
5342
- );
5343
- const tasksMap = /* @__PURE__ */ new Map();
5344
- if (fullScheduleTask.tasks && Array.isArray(fullScheduleTask.tasks)) {
5345
- for (const task of fullScheduleTask.tasks) {
5346
- const areaId = task.area?.toString();
5347
- if (areaId) {
5348
- if (!tasksMap.has(areaId)) {
5349
- tasksMap.set(areaId, []);
5350
- }
5351
- tasksMap.get(areaId).push(task);
5352
- }
5353
- }
5354
- }
5355
5248
  for (const area of scheduleTask.areas) {
5356
5249
  try {
5357
5250
  const areaId = area.value.toString();
5358
5251
  const areaDetails = await getAreaById(areaId);
5359
5252
  let units = [];
5360
- const areaTasks = tasksMap.get(areaId);
5361
- if (areaTasks && areaTasks.length > 0) {
5362
- logger29.info(
5363
- `Area ${area.name} (${areaId}): Found ${areaTasks.length} tasks, using units from first task.`
5364
- );
5365
- units = areaTasks[0].units.map((unit) => ({
5366
- unit: unit.unit.toString(),
5367
- name: unit.name
5368
- }));
5369
- logger29.info(
5370
- `Area ${area.name} (${areaId}): Units from task: ${JSON.stringify(units)}`
5371
- );
5372
- } else if (areaDetails.units && areaDetails.units.length > 0) {
5373
- logger29.info(
5374
- `Area ${area.name} (${areaId}): No tasks found, using units from area details.`
5375
- );
5253
+ if (areaDetails.units && areaDetails.units.length > 0) {
5376
5254
  units = areaDetails.units.map((unit) => ({
5377
5255
  unit: unit.unit.toString(),
5378
5256
  name: unit.name
5379
5257
  }));
5380
5258
  logger29.info(
5381
- `Area ${area.name} (${areaId}): Units from area details: ${JSON.stringify(
5259
+ `Area ${area.name} (${areaId}): Using units from area details: ${JSON.stringify(
5382
5260
  units
5383
5261
  )}`
5384
5262
  );
@@ -5639,12 +5517,9 @@ function useScheduleTaskController() {
5639
5517
  const validation = Joi17.object({
5640
5518
  id: Joi17.string().hex().required(),
5641
5519
  title: Joi17.string().optional().allow("", null),
5642
- frequency: Joi17.string().valid(...allowedFrequency).optional().allow("", null),
5643
5520
  time: Joi17.string().pattern(/^([0-1]\d|2[0-3]):([0-5]\d)$/).optional().allow("", null),
5644
- day: Joi17.array().items(Joi17.string().valid(...allowedDays)).optional().allow(null),
5645
- weekOfMonth: Joi17.string().valid(...allowedWeekOfMonth).optional().allow("", null),
5646
- month: Joi17.string().valid(...allowedMonths).optional().allow("", null),
5647
- date: Joi17.number().min(1).max(31).optional().allow(null),
5521
+ startDate: Joi17.string().pattern(/^\d{4}-\d{2}-\d{2}$/).optional().allow("", null),
5522
+ endDate: Joi17.string().pattern(/^\d{4}-\d{2}-\d{2}$/).optional().allow("", null),
5648
5523
  description: Joi17.string().optional().allow("", null),
5649
5524
  areas: Joi17.array().min(1).items(
5650
5525
  Joi17.object({
@@ -5688,14 +5563,10 @@ export {
5688
5563
  MSupply,
5689
5564
  MUnit,
5690
5565
  allowedChecklistStatus,
5691
- allowedDays,
5692
- allowedFrequency,
5693
- allowedMonths,
5694
5566
  allowedPeriods,
5695
5567
  allowedRequestItemStatus,
5696
5568
  allowedStatus,
5697
5569
  allowedTypes,
5698
- allowedWeekOfMonth,
5699
5570
  areaChecklistSchema,
5700
5571
  areaSchema,
5701
5572
  parentChecklistSchema,