@7365admin1/module-hygiene 4.27.2-staging.3 → 4.27.2-staging.5

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
@@ -3284,7 +3284,19 @@ function useAreaChecklistRepo() {
3284
3284
  }
3285
3285
  async function createUniqueIndex() {
3286
3286
  try {
3287
- await collection.createIndex({ schedule: 1, area: 1 }, { unique: true });
3287
+ await collection.createIndex(
3288
+ { schedule: 1, area: 1, serviceType: 1 },
3289
+ { unique: true }
3290
+ );
3291
+ const legacyIndex = (await collection.indexes()).find((index) => {
3292
+ return index.unique === true && JSON.stringify(index.key) === JSON.stringify({ schedule: 1, area: 1 });
3293
+ });
3294
+ if (legacyIndex?.name) {
3295
+ await collection.dropIndex(legacyIndex.name);
3296
+ logger17.info(
3297
+ `Dropped legacy hygiene area checklist unique index: ${legacyIndex.name}`
3298
+ );
3299
+ }
3288
3300
  } catch (error) {
3289
3301
  throw new InternalServerError5(
3290
3302
  "Failed to create unique index on hygiene checklist area."
@@ -3296,7 +3308,7 @@ function useAreaChecklistRepo() {
3296
3308
  const parentChecklistId = new ObjectId9(value.schedule);
3297
3309
  const areaId = new ObjectId9(value.area);
3298
3310
  const existingChecklist = await collection.findOne(
3299
- { schedule: parentChecklistId, area: areaId },
3311
+ { schedule: parentChecklistId, area: areaId, serviceType: value.serviceType },
3300
3312
  { session }
3301
3313
  );
3302
3314
  if (existingChecklist) {
@@ -3314,7 +3326,8 @@ function useAreaChecklistRepo() {
3314
3326
  logger17.info(`Duplicate area checklist skipped for area ${value.name}`);
3315
3327
  const existing = await collection.findOne({
3316
3328
  schedule: new ObjectId9(value.schedule),
3317
- area: new ObjectId9(value.area)
3329
+ area: new ObjectId9(value.area),
3330
+ serviceType: value.serviceType
3318
3331
  });
3319
3332
  return existing._id;
3320
3333
  }
@@ -5163,9 +5176,18 @@ function useSupplyRepository() {
5163
5176
  async function createUniqueIndex() {
5164
5177
  try {
5165
5178
  await collection.createIndex(
5166
- { site: 1, name: 1, deletedAt: 1 },
5179
+ { site: 1, serviceType: 1, name: 1, deletedAt: 1 },
5167
5180
  { unique: true }
5168
5181
  );
5182
+ const legacyIndex = (await collection.indexes()).find((index) => {
5183
+ return index.unique === true && JSON.stringify(index.key) === JSON.stringify({ site: 1, name: 1, deletedAt: 1 });
5184
+ });
5185
+ if (legacyIndex?.name) {
5186
+ await collection.dropIndex(legacyIndex.name);
5187
+ logger22.info(
5188
+ `Dropped legacy hygiene supply unique index: ${legacyIndex.name}`
5189
+ );
5190
+ }
5169
5191
  } catch (error) {
5170
5192
  throw new InternalServerError8(
5171
5193
  "Failed to create unique index on hygiene supply."
@@ -5844,6 +5866,7 @@ var checkOutItemSchema = Joi14.object({
5844
5866
  serviceType: Joi14.string().valid(...Object.values(AppServiceType15)).required(),
5845
5867
  supply: Joi14.string().hex().required(),
5846
5868
  supplyName: Joi14.string().required(),
5869
+ supplyQty: Joi14.number().min(0).required(),
5847
5870
  qty: Joi14.number().min(0).required(),
5848
5871
  attachment: Joi14.array().items(Joi14.string()).optional().allow(null),
5849
5872
  createdBy: Joi14.string().hex().required(),
@@ -5874,6 +5897,7 @@ function MCheckOutItem(value) {
5874
5897
  serviceType: value.serviceType,
5875
5898
  supply: value.supply,
5876
5899
  supplyName: value.supplyName,
5900
+ supplyQty: value.supplyQty,
5877
5901
  qty: value.qty,
5878
5902
  attachment: value.attachment || [],
5879
5903
  createdBy: value.createdBy,
@@ -5987,32 +6011,6 @@ function useCheckOutItemRepository() {
5987
6011
  try {
5988
6012
  const items = await collection.aggregate([
5989
6013
  { $match: query },
5990
- {
5991
- $lookup: {
5992
- from: "site.supplies",
5993
- let: { supplyId: "$supply" },
5994
- pipeline: [
5995
- {
5996
- $match: {
5997
- $expr: {
5998
- $and: [
5999
- { $ne: ["$$supplyId", ""] },
6000
- { $eq: ["$_id", "$$supplyId"] }
6001
- ]
6002
- }
6003
- }
6004
- },
6005
- { $project: { qty: 1 } }
6006
- ],
6007
- as: "supplyDoc"
6008
- }
6009
- },
6010
- {
6011
- $unwind: {
6012
- path: "$supplyDoc",
6013
- preserveNullAndEmptyArrays: true
6014
- }
6015
- },
6016
6014
  {
6017
6015
  $lookup: {
6018
6016
  from: "users",
@@ -6039,7 +6037,7 @@ function useCheckOutItemRepository() {
6039
6037
  {
6040
6038
  $project: {
6041
6039
  supplyName: 1,
6042
- supplyQty: "$supplyDoc.qty",
6040
+ supplyQty: 1,
6043
6041
  checkOutByName: "$createdByName",
6044
6042
  profile: "$createdByDoc.profile",
6045
6043
  checkOutQty: "$qty",
@@ -6109,7 +6107,7 @@ function useCheckOutItemRepository() {
6109
6107
  qty: 1,
6110
6108
  status: 1,
6111
6109
  unitOfMeasurement: "$supplyDetails.unitOfMeasurement",
6112
- stockQty: "$supplyDetails.qty",
6110
+ stockQty: "$supplyQty",
6113
6111
  attachment: 1
6114
6112
  }
6115
6113
  }
@@ -6193,6 +6191,7 @@ function useCheckOutItemService() {
6193
6191
  {
6194
6192
  ...value,
6195
6193
  supplyName: supplyData?.name || "",
6194
+ supplyQty: Math.max((supplyData?.qty || 0) - value.qty, 0),
6196
6195
  createdByName: createdByData?.name || ""
6197
6196
  },
6198
6197
  session
@@ -6239,6 +6238,7 @@ function useCheckOutItemService() {
6239
6238
  serviceType,
6240
6239
  supply: item.supply,
6241
6240
  supplyName: supplyData?.name || "",
6241
+ supplyQty: Math.max((supplyData?.qty || 0) - item.qty, 0),
6242
6242
  qty: item.qty,
6243
6243
  attachment: item.attachment,
6244
6244
  createdBy,