@7365admin1/module-hygiene 4.10.0 → 4.11.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
@@ -2651,7 +2651,12 @@ function MAreaChecklist(value) {
2651
2651
  }
2652
2652
 
2653
2653
  // src/services/hygiene-area-checklist.service.ts
2654
- import { logger as logger18, useAtlas as useAtlas7 } from "@7365admin1/node-server-utils";
2654
+ import {
2655
+ logger as logger18,
2656
+ UnauthorizedError,
2657
+ useAtlas as useAtlas7
2658
+ } from "@7365admin1/node-server-utils";
2659
+ import { useUserRepo } from "@7365admin1/core";
2655
2660
 
2656
2661
  // src/repositories/hygiene-area-checklist.repository.ts
2657
2662
  import { ObjectId as ObjectId9 } from "mongodb";
@@ -3192,7 +3197,19 @@ function useAreaChecklistRepo() {
3192
3197
  },
3193
3198
  {
3194
3199
  $addFields: {
3195
- isCompleted: { $ne: ["$checklist.units.completedBy", ""] }
3200
+ isCompleted: { $ne: ["$checklist.units.completedBy", ""] },
3201
+ isActioned: {
3202
+ $cond: {
3203
+ if: {
3204
+ $or: [
3205
+ { $eq: ["$checklist.units.approve", true] },
3206
+ { $eq: ["$checklist.units.reject", true] }
3207
+ ]
3208
+ },
3209
+ then: 1,
3210
+ else: 0
3211
+ }
3212
+ }
3196
3213
  }
3197
3214
  },
3198
3215
  {
@@ -3221,7 +3238,14 @@ function useAreaChecklistRepo() {
3221
3238
  preserveNullAndEmptyArrays: true
3222
3239
  }
3223
3240
  },
3224
- { $sort: { set: 1, isCompleted: -1, "checklist.units.timestamp": 1 } },
3241
+ {
3242
+ $sort: {
3243
+ set: 1,
3244
+ isActioned: 1,
3245
+ "checklist.units.timestamp": 1,
3246
+ isCompleted: -1
3247
+ }
3248
+ },
3225
3249
  {
3226
3250
  $project: {
3227
3251
  _id: 0,
@@ -3285,7 +3309,25 @@ function useAreaChecklistRepo() {
3285
3309
  units: 1
3286
3310
  }
3287
3311
  },
3288
- { $sort: { set: 1 } },
3312
+ {
3313
+ $addFields: {
3314
+ isFullyActioned: {
3315
+ $allElementsTrue: {
3316
+ $map: {
3317
+ input: "$units",
3318
+ as: "u",
3319
+ in: {
3320
+ $or: [
3321
+ { $eq: ["$$u.approve", true] },
3322
+ { $eq: ["$$u.reject", true] }
3323
+ ]
3324
+ }
3325
+ }
3326
+ }
3327
+ }
3328
+ }
3329
+ },
3330
+ { $sort: { isFullyActioned: 1 } },
3289
3331
  { $skip: page * limit },
3290
3332
  { $limit: limit }
3291
3333
  ];
@@ -3489,6 +3531,20 @@ function useAreaChecklistRepo() {
3489
3531
  throw error;
3490
3532
  }
3491
3533
  }
3534
+ async function getAreaChecklistSetOwner(_id, set, session) {
3535
+ try {
3536
+ _id = new ObjectId9(_id);
3537
+ } catch (error) {
3538
+ throw new BadRequestError15("Invalid area checklist ID format.");
3539
+ }
3540
+ const existing = await collection.findOne(
3541
+ { _id, "checklist.set": set },
3542
+ { projection: { "checklist.$": 1 }, session }
3543
+ );
3544
+ if (!existing?.checklist?.length)
3545
+ return null;
3546
+ return existing.checklist[0].units.map((u) => u.completedBy).find((cb) => cb && cb.toString() !== "") ?? null;
3547
+ }
3492
3548
  async function getMaxSetNumberForArea(areaId, session) {
3493
3549
  try {
3494
3550
  const _id = new ObjectId9(areaId);
@@ -3552,6 +3608,7 @@ function useAreaChecklistRepo() {
3552
3608
  getAreaChecklistUnits,
3553
3609
  getAreaChecklistById,
3554
3610
  getAreaChecklistByAreaAndSchedule,
3611
+ getAreaChecklistSetOwner,
3555
3612
  updateAreaChecklist,
3556
3613
  updateAreaChecklistStatus,
3557
3614
  updateAreaChecklistUnits,
@@ -3568,11 +3625,13 @@ function useAreaChecklistService() {
3568
3625
  getAllAreaChecklist,
3569
3626
  getAreaChecklistUnits,
3570
3627
  getAreaChecklistById,
3628
+ getAreaChecklistSetOwner,
3571
3629
  updateAreaChecklistUnits: _updateAreaChecklistUnits,
3572
3630
  updateAreaChecklistStatus,
3573
3631
  getMaxSetNumberForArea
3574
3632
  } = useAreaChecklistRepo();
3575
3633
  const { updateParentChecklistStatuses } = useParentChecklistRepo();
3634
+ const { getUserById } = useUserRepo();
3576
3635
  async function createAreaChecklist(value) {
3577
3636
  const session = useAtlas7.getClient()?.startSession();
3578
3637
  try {
@@ -3643,6 +3702,13 @@ function useAreaChecklistService() {
3643
3702
  const session = useAtlas7.getClient()?.startSession();
3644
3703
  try {
3645
3704
  session?.startTransaction();
3705
+ const setOwner = await getAreaChecklistSetOwner(_id, set, session);
3706
+ if (setOwner && value.completedBy && setOwner.toString() !== value.completedBy.toString()) {
3707
+ const acceptedBy = await getUserById(setOwner.toString());
3708
+ throw new UnauthorizedError(
3709
+ `${acceptedBy.name} has already taken this set.`
3710
+ );
3711
+ }
3646
3712
  await _updateAreaChecklistUnits(_id, set, unitId, value, session);
3647
3713
  const allUnitsResult = await getAreaChecklistUnits(
3648
3714
  {
@@ -5240,7 +5306,7 @@ function useCheckOutItemRepository() {
5240
5306
  }
5241
5307
 
5242
5308
  // src/services/hygiene-checkout-item.service.ts
5243
- import { useUserRepo } from "@7365admin1/core";
5309
+ import { useUserRepo as useUserRepo2 } from "@7365admin1/core";
5244
5310
  import { BadRequestError as BadRequestError26, useAtlas as useAtlas13 } from "@7365admin1/node-server-utils";
5245
5311
  function useCheckOutItemService() {
5246
5312
  const {
@@ -5249,7 +5315,7 @@ function useCheckOutItemService() {
5249
5315
  completeCheckOutItem
5250
5316
  } = useCheckOutItemRepository();
5251
5317
  const { getSupplyById } = useSupplyRepository();
5252
- const { getUserById } = useUserRepo();
5318
+ const { getUserById } = useUserRepo2();
5253
5319
  const { createStock } = useStockService();
5254
5320
  async function createCheckOutItem(value) {
5255
5321
  const session = useAtlas13.getClient()?.startSession();