@7365admin1/module-hygiene 4.28.2-staging.10 → 4.28.2-staging.11

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.
@@ -0,0 +1,5 @@
1
+ ---
2
+ "@7365admin1/module-hygiene": minor
3
+ ---
4
+
5
+ Disapproving a hygiene check out item returns the drawn-down quantity to stock, in the same transaction as the decision.
package/dist/index.d.ts CHANGED
@@ -541,6 +541,21 @@ declare function buildCheckOutDecision(decision: TCheckOutDecision, remarks?: st
541
541
  remarks: string;
542
542
  updatedAt: string;
543
543
  };
544
+ /**
545
+ * Sequence a decision: the guarded update first, the stock return only if that
546
+ * update actually decided the row.
547
+ *
548
+ * Idempotency comes from the update's `{ _id, status: "pending" }` match, not
549
+ * from anything here. `decide` reports how many rows it changed (and the
550
+ * repository throws on a match of none), so a row someone already decided
551
+ * moves no stock. That is what stops a second disapprove crediting stock
552
+ * twice, and stops an approve after a disapprove deducting it again — the
553
+ * approve is refused outright, so it never reaches a stock call at all.
554
+ *
555
+ * Kept here rather than in the service so the ordering can be tested with
556
+ * fakes, the same way the rest of this file is tested without a database.
557
+ */
558
+ declare function applyCheckOutDecision(decision: TCheckOutDecision, decide: () => Promise<number>, returnStock: () => Promise<unknown>): Promise<number>;
544
559
 
545
560
  declare const allowedCheckOutItemStatus: string[];
546
561
  type TCheckOutItem = {
@@ -611,6 +626,7 @@ declare function useCheckOutItemRepository(): {
611
626
  declare function useCheckOutItemService(): {
612
627
  createCheckOutItem: (value: TCheckOutItemCreateService) => Promise<bson.ObjectId>;
613
628
  createCheckOutItemByBatch: (value: TCheckOutItemCreateByBatchService) => Promise<bson.ObjectId[]>;
629
+ decideCheckOutItem: (_id: string, value: TCheckOutItemDecision) => Promise<number>;
614
630
  };
615
631
 
616
632
  declare function useCheckOutItemController(): {
@@ -705,4 +721,4 @@ declare function useQRController(): {
705
721
  generateQR: (req: Request, res: Response, next: NextFunction) => Promise<void>;
706
722
  };
707
723
 
708
- export { CHECKOUT_DECIDABLE_STATUS, CHECKOUT_DECISIONS, MArea, MAreaChecklist, MCheckOutItem, MParentChecklist, MScheduleTask, MStock, MSupply, MUnit, TArea, TAreaChecklist, TAreaChecklistBatchCreate, TAreaChecklistCreate, TAreaChecklistUnits, TAreaChecklistUnitsUpdate, TAreaChecklistUpdate, TAreaCreate, TAreaGetQuery, TAreaUnits, TAreaUpdate, TAreaUpdateChecklist, TCheckOutDecision, TCheckOutItem, TCheckOutItemCreate, TCheckOutItemCreateByBatchService, TCheckOutItemCreateService, TCheckOutItemDecision, TCheckOutItemGetById, TCheckOutItemGetQuery, TCleaningScheduleArea, TCleaningScheduleAreaGetQuery, TGetStocksQuery, TParentChecklist, TParentChecklistCreate, TParentChecklistGetQuery, TScheduleTask, TScheduleTaskCreate, TScheduleTaskGetById, TScheduleTaskGetQuery, TScheduleTaskUpdate, TStock, TStockCreate, TStockCreateService, TSupply, TSupplyCreate, TSupplyGetById, TSupplyGetQuery, TSupplyUpdate, TUnit, TUnitCreate, TUnitGetQuery, TUnitUpdate, allowedCheckOutItemStatus, allowedChecklistStatus, allowedPeriods, allowedStatus, allowedTypes, areaChecklistSchema, areaSchema, buildCheckOutDecision, checkOutItemSchema, isCheckOutDecision, parentChecklistSchema, scheduleTaskSchema, stockSchema, supplySchema, unitSchema, useAreaChecklistController, useAreaChecklistRepo, useAreaChecklistService, useAreaController, useAreaExportService, useAreaRepo, useAreaService, useCheckOutItemController, useCheckOutItemRepository, useCheckOutItemService, useHygieneDashboardController, useHygieneDashboardRepository, useParentChecklistController, useParentChecklistRepo, useQRController, useQRService, useScheduleTaskController, useScheduleTaskRepository, useScheduleTaskService, useStockController, useStockRepository, useStockService, useSupplyController, useSupplyRepository, useUnitController, useUnitExportService, useUnitRepository, useUnitService };
724
+ export { CHECKOUT_DECIDABLE_STATUS, CHECKOUT_DECISIONS, MArea, MAreaChecklist, MCheckOutItem, MParentChecklist, MScheduleTask, MStock, MSupply, MUnit, TArea, TAreaChecklist, TAreaChecklistBatchCreate, TAreaChecklistCreate, TAreaChecklistUnits, TAreaChecklistUnitsUpdate, TAreaChecklistUpdate, TAreaCreate, TAreaGetQuery, TAreaUnits, TAreaUpdate, TAreaUpdateChecklist, TCheckOutDecision, TCheckOutItem, TCheckOutItemCreate, TCheckOutItemCreateByBatchService, TCheckOutItemCreateService, TCheckOutItemDecision, TCheckOutItemGetById, TCheckOutItemGetQuery, TCleaningScheduleArea, TCleaningScheduleAreaGetQuery, TGetStocksQuery, TParentChecklist, TParentChecklistCreate, TParentChecklistGetQuery, TScheduleTask, TScheduleTaskCreate, TScheduleTaskGetById, TScheduleTaskGetQuery, TScheduleTaskUpdate, TStock, TStockCreate, TStockCreateService, TSupply, TSupplyCreate, TSupplyGetById, TSupplyGetQuery, TSupplyUpdate, TUnit, TUnitCreate, TUnitGetQuery, TUnitUpdate, allowedCheckOutItemStatus, allowedChecklistStatus, allowedPeriods, allowedStatus, allowedTypes, applyCheckOutDecision, areaChecklistSchema, areaSchema, buildCheckOutDecision, checkOutItemSchema, isCheckOutDecision, parentChecklistSchema, scheduleTaskSchema, stockSchema, supplySchema, unitSchema, useAreaChecklistController, useAreaChecklistRepo, useAreaChecklistService, useAreaController, useAreaExportService, useAreaRepo, useAreaService, useCheckOutItemController, useCheckOutItemRepository, useCheckOutItemService, useHygieneDashboardController, useHygieneDashboardRepository, useParentChecklistController, useParentChecklistRepo, useQRController, useQRService, useScheduleTaskController, useScheduleTaskRepository, useScheduleTaskService, useStockController, useStockRepository, useStockService, useSupplyController, useSupplyRepository, useUnitController, useUnitExportService, useUnitRepository, useUnitService };
package/dist/index.js CHANGED
@@ -45,6 +45,7 @@ __export(src_exports, {
45
45
  allowedPeriods: () => allowedPeriods,
46
46
  allowedStatus: () => allowedStatus,
47
47
  allowedTypes: () => allowedTypes,
48
+ applyCheckOutDecision: () => applyCheckOutDecision,
48
49
  areaChecklistSchema: () => areaChecklistSchema,
49
50
  areaSchema: () => areaSchema,
50
51
  buildCheckOutDecision: () => buildCheckOutDecision,
@@ -5896,6 +5897,13 @@ function buildCheckOutDecision(decision, remarks, now = (/* @__PURE__ */ new Dat
5896
5897
  updatedAt: now
5897
5898
  };
5898
5899
  }
5900
+ async function applyCheckOutDecision(decision, decide, returnStock) {
5901
+ const decided = await decide();
5902
+ if (decided > 0 && decision === "disapprove") {
5903
+ await returnStock();
5904
+ }
5905
+ return decided;
5906
+ }
5899
5907
 
5900
5908
  // src/models/hygiene-checkout-item.model.ts
5901
5909
  var import_joi14 = __toESM(require("joi"));
@@ -6261,7 +6269,8 @@ function useCheckOutItemService() {
6261
6269
  const {
6262
6270
  createCheckOutItem: _createCheckOutItem,
6263
6271
  getCheckOutItemById: _getCheckOutItemById,
6264
- completeCheckOutItem
6272
+ completeCheckOutItem,
6273
+ decideCheckOutItem: _decideCheckOutItem
6265
6274
  } = useCheckOutItemRepository();
6266
6275
  const { getSupplyById } = useSupplyRepository();
6267
6276
  const { getUserById } = (0, import_core17.useUserRepo)();
@@ -6352,9 +6361,41 @@ function useCheckOutItemService() {
6352
6361
  await session?.endSession();
6353
6362
  }
6354
6363
  }
6364
+ async function decideCheckOutItem(_id, value) {
6365
+ const session = import_node_server_utils30.useAtlas.getClient()?.startSession();
6366
+ try {
6367
+ session?.startTransaction();
6368
+ const checkOutItem = await _getCheckOutItemById(_id, session);
6369
+ if (!checkOutItem) {
6370
+ throw new import_node_server_utils30.BadRequestError("Check out item not found.");
6371
+ }
6372
+ const decided = await applyCheckOutDecision(
6373
+ value.approve === true ? "approve" : "disapprove",
6374
+ () => _decideCheckOutItem(_id, value, session),
6375
+ () => createStock(
6376
+ {
6377
+ site: checkOutItem.site.toString(),
6378
+ serviceType: checkOutItem.serviceType,
6379
+ supply: checkOutItem.supply.toString(),
6380
+ qty: checkOutItem.qty
6381
+ },
6382
+ false,
6383
+ session
6384
+ )
6385
+ );
6386
+ await session?.commitTransaction();
6387
+ return decided;
6388
+ } catch (error) {
6389
+ await session?.abortTransaction();
6390
+ throw error;
6391
+ } finally {
6392
+ await session?.endSession();
6393
+ }
6394
+ }
6355
6395
  return {
6356
6396
  createCheckOutItem,
6357
- createCheckOutItemByBatch
6397
+ createCheckOutItemByBatch,
6398
+ decideCheckOutItem
6358
6399
  };
6359
6400
  }
6360
6401
 
@@ -6365,12 +6406,12 @@ var import_core18 = require("@7365admin1/core");
6365
6406
  function useCheckOutItemController() {
6366
6407
  const {
6367
6408
  getCheckOutItems: _getCheckOutItems,
6368
- getCheckOutItemById: _getCheckOutItemById,
6369
- decideCheckOutItem: _decideCheckOutItem
6409
+ getCheckOutItemById: _getCheckOutItemById
6370
6410
  } = useCheckOutItemRepository();
6371
6411
  const {
6372
6412
  createCheckOutItem: _createCheckOutItem,
6373
- createCheckOutItemByBatch: _createCheckOutItemByBatch
6413
+ createCheckOutItemByBatch: _createCheckOutItemByBatch,
6414
+ decideCheckOutItem: _decideCheckOutItem
6374
6415
  } = useCheckOutItemService();
6375
6416
  async function createCheckOutItem(req, res, next) {
6376
6417
  const cookies = req.headers.cookie ? req.headers.cookie.split(";").map((cookie) => cookie.trim().split("=")).reduce(
@@ -7610,6 +7651,7 @@ function useQRController() {
7610
7651
  allowedPeriods,
7611
7652
  allowedStatus,
7612
7653
  allowedTypes,
7654
+ applyCheckOutDecision,
7613
7655
  areaChecklistSchema,
7614
7656
  areaSchema,
7615
7657
  buildCheckOutDecision,