@iservice365/module-hygiene 1.0.1 → 1.0.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @iservice365/module-hygiene
2
2
 
3
+ ## 1.0.3
4
+
5
+ ### Patch Changes
6
+
7
+ - fa485c3: Update dependencies
8
+ - 3ac64f5: Fix export modules
9
+
10
+ ## 1.0.2
11
+
12
+ ### Patch Changes
13
+
14
+ - 73be6af: Update dependencies
15
+
3
16
  ## 1.0.1
4
17
 
5
18
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -269,12 +269,13 @@ type TSupply = {
269
269
  name: string;
270
270
  unitOfMeasurement: string;
271
271
  qty: number;
272
+ status?: "active" | "deleted";
272
273
  createdAt?: string;
273
274
  updatedAt?: string;
274
275
  deletedAt?: string;
275
- status?: "active" | "deleted";
276
276
  };
277
277
  type TSupplyCreate = Pick<TSupply, "site" | "name" | "unitOfMeasurement" | "qty">;
278
+ type TSupplyGetById = Pick<TSupply, "_id" | "name" | "unitOfMeasurement" | "qty">;
278
279
  type TSupplyUpdate = Partial<Pick<TSupply, "name" | "unitOfMeasurement" | "qty">>;
279
280
  type TGetSupplysQuery = {
280
281
  page?: number;
@@ -287,8 +288,8 @@ declare function MSupply(value: TSupplyCreate): {
287
288
  name: string;
288
289
  unitOfMeasurement: string;
289
290
  qty: number;
290
- createdAt: Date;
291
291
  status: string;
292
+ createdAt: string;
292
293
  updatedAt: string;
293
294
  deletedAt: string;
294
295
  };
@@ -299,15 +300,65 @@ declare function useSupplyRepository(): {
299
300
  createUniqueIndex: () => Promise<void>;
300
301
  createSupply: (value: TSupplyCreate, session?: ClientSession) => Promise<ObjectId>;
301
302
  getSupplies: ({ page, limit, search, site, }: TGetSupplysQuery) => Promise<{}>;
303
+ getSupplyById: (_id: string | ObjectId) => Promise<TSupplyGetById>;
302
304
  updateSupply: (_id: string | ObjectId, value: TSupplyUpdate, session?: ClientSession) => Promise<number>;
303
305
  deleteSupply: (_id: string | ObjectId, session?: ClientSession) => Promise<number>;
304
306
  };
305
307
 
308
+ declare function useSupplyService(): {
309
+ createSupply: (value: TSupplyCreate) => Promise<bson.ObjectId>;
310
+ };
311
+
306
312
  declare function useSupplyController(): {
307
313
  createSupply: (req: Request, res: Response, next: NextFunction) => Promise<void>;
308
314
  getSupplies: (req: Request, res: Response, next: NextFunction) => Promise<void>;
315
+ getSupplyById: (req: Request, res: Response, next: NextFunction) => Promise<void>;
309
316
  updateSupply: (req: Request, res: Response, next: NextFunction) => Promise<void>;
310
317
  deleteSupply: (req: Request, res: Response, next: NextFunction) => Promise<void>;
311
318
  };
312
319
 
313
- export { MArea, MAreaChecklist, MParentChecklist, MSupply, MUnit, TArea, TAreaChecklist, TAreaChecklistCreate, TAreaChecklistUnits, TAreaChecklistUnitsUpdate, TAreaCreate, TAreaGetAreasForChecklist, TAreaGetQuery, TAreaUnits, TAreaUpdate, TAreaUpdateChecklist, TCleaningScheduleArea, TGetSupplysQuery, TGetUnitsQuery, TParentChecklist, TSupply, TSupplyCreate, TSupplyUpdate, TUnit, TUnitCreate, TUnitUpdate, allowedChecklistStatus, allowedStatus, allowedTypes, areaChecklistSchema, areaSchema, parentChecklistSchema, supplySchema, unitSchema, useAreaChecklistController, useAreaChecklistRepo, useAreaChecklistService, useAreaController, useAreaRepo, useAreaService, useParentChecklistController, useParentChecklistRepo, useSupplyController, useSupplyRepository, useUnitController, useUnitRepository, useUnitService };
320
+ type TStock = {
321
+ _id?: ObjectId;
322
+ site: string | ObjectId;
323
+ supply: string | ObjectId;
324
+ in?: number;
325
+ out?: number;
326
+ balance: number;
327
+ remarks?: string;
328
+ status?: "active" | "deleted";
329
+ createdAt?: string;
330
+ updatedAt?: string;
331
+ deletedAt?: string;
332
+ };
333
+ type TStockCreate = Pick<TStock, "site" | "supply" | "in" | "out" | "balance" | "remarks">;
334
+ type TStockCreateService = Pick<TStock, "site" | "supply" | "remarks"> & {
335
+ qty: number;
336
+ };
337
+ declare const stockSchema: Joi.ObjectSchema<any>;
338
+ declare function MStock(value: TStockCreate): {
339
+ site: string | ObjectId;
340
+ supply: string | ObjectId;
341
+ in: number;
342
+ out: number;
343
+ balance: number;
344
+ remarks: string;
345
+ createdAt: string;
346
+ status: string;
347
+ updatedAt: string;
348
+ deletedAt: string;
349
+ };
350
+
351
+ declare function useStockRepository(): {
352
+ createIndex: () => Promise<void>;
353
+ createStock: (value: TStockCreate, session?: ClientSession) => Promise<bson.ObjectId>;
354
+ };
355
+
356
+ declare function useStockService(): {
357
+ createStock: (value: TStockCreateService) => Promise<bson.ObjectId>;
358
+ };
359
+
360
+ declare function useStockController(): {
361
+ createStock: (req: Request, res: Response, next: NextFunction) => Promise<void>;
362
+ };
363
+
364
+ export { MArea, MAreaChecklist, MParentChecklist, MStock, MSupply, MUnit, TArea, TAreaChecklist, TAreaChecklistCreate, TAreaChecklistUnits, TAreaChecklistUnitsUpdate, TAreaCreate, TAreaGetAreasForChecklist, TAreaGetQuery, TAreaUnits, TAreaUpdate, TAreaUpdateChecklist, TCleaningScheduleArea, TGetSupplysQuery, TGetUnitsQuery, TParentChecklist, TStock, TStockCreate, TStockCreateService, TSupply, TSupplyCreate, TSupplyGetById, TSupplyUpdate, TUnit, TUnitCreate, TUnitUpdate, allowedChecklistStatus, allowedStatus, allowedTypes, areaChecklistSchema, areaSchema, parentChecklistSchema, stockSchema, supplySchema, unitSchema, useAreaChecklistController, useAreaChecklistRepo, useAreaChecklistService, useAreaController, useAreaRepo, useAreaService, useParentChecklistController, useParentChecklistRepo, useStockController, useStockRepository, useStockService, useSupplyController, useSupplyRepository, useSupplyService, useUnitController, useUnitRepository, useUnitService };
package/dist/index.js CHANGED
@@ -33,6 +33,7 @@ __export(src_exports, {
33
33
  MArea: () => MArea,
34
34
  MAreaChecklist: () => MAreaChecklist,
35
35
  MParentChecklist: () => MParentChecklist,
36
+ MStock: () => MStock,
36
37
  MSupply: () => MSupply,
37
38
  MUnit: () => MUnit,
38
39
  allowedChecklistStatus: () => allowedChecklistStatus,
@@ -41,6 +42,7 @@ __export(src_exports, {
41
42
  areaChecklistSchema: () => areaChecklistSchema,
42
43
  areaSchema: () => areaSchema,
43
44
  parentChecklistSchema: () => parentChecklistSchema,
45
+ stockSchema: () => stockSchema,
44
46
  supplySchema: () => supplySchema,
45
47
  unitSchema: () => unitSchema,
46
48
  useAreaChecklistController: () => useAreaChecklistController,
@@ -51,8 +53,12 @@ __export(src_exports, {
51
53
  useAreaService: () => useAreaService,
52
54
  useParentChecklistController: () => useParentChecklistController,
53
55
  useParentChecklistRepo: () => useParentChecklistRepo,
56
+ useStockController: () => useStockController,
57
+ useStockRepository: () => useStockRepository,
58
+ useStockService: () => useStockService,
54
59
  useSupplyController: () => useSupplyController,
55
60
  useSupplyRepository: () => useSupplyRepository,
61
+ useSupplyService: () => useSupplyService,
56
62
  useUnitController: () => useUnitController,
57
63
  useUnitRepository: () => useUnitRepository,
58
64
  useUnitService: () => useUnitService
@@ -2837,8 +2843,8 @@ function MSupply(value) {
2837
2843
  name: value.name,
2838
2844
  unitOfMeasurement: value.unitOfMeasurement,
2839
2845
  qty: value.qty,
2840
- createdAt: /* @__PURE__ */ new Date(),
2841
2846
  status: "active",
2847
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
2842
2848
  updatedAt: "",
2843
2849
  deletedAt: ""
2844
2850
  };
@@ -2962,6 +2968,48 @@ function useSupplyRepository() {
2962
2968
  throw error;
2963
2969
  }
2964
2970
  }
2971
+ async function getSupplyById(_id) {
2972
+ try {
2973
+ _id = new import_mongodb10.ObjectId(_id);
2974
+ } catch (error) {
2975
+ throw new import_node_server_utils17.BadRequestError("Invalid supply ID format.");
2976
+ }
2977
+ const query = {
2978
+ _id,
2979
+ status: { $ne: "deleted" }
2980
+ };
2981
+ const cacheKey = (0, import_node_server_utils17.makeCacheKey)(namespace_collection, {
2982
+ _id: _id.toString()
2983
+ });
2984
+ const cachedData = await getCache(cacheKey);
2985
+ if (cachedData) {
2986
+ import_node_server_utils17.logger.info(`Cache hit for key: ${cacheKey}`);
2987
+ return cachedData;
2988
+ }
2989
+ try {
2990
+ const data = await collection.aggregate([
2991
+ { $match: query },
2992
+ {
2993
+ $project: {
2994
+ name: 1,
2995
+ unitOfMeasurement: 1,
2996
+ qty: 1
2997
+ }
2998
+ }
2999
+ ]).toArray();
3000
+ if (!data || data.length === 0) {
3001
+ throw new import_node_server_utils17.NotFoundError("Supply not found.");
3002
+ }
3003
+ setCache(cacheKey, data[0], 15 * 60).then(() => {
3004
+ import_node_server_utils17.logger.info(`Cache set for key: ${cacheKey}`);
3005
+ }).catch((err) => {
3006
+ import_node_server_utils17.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
3007
+ });
3008
+ return data[0];
3009
+ } catch (error) {
3010
+ throw error;
3011
+ }
3012
+ }
2965
3013
  async function updateSupply(_id, value, session) {
2966
3014
  try {
2967
3015
  _id = new import_mongodb10.ObjectId(_id);
@@ -3034,27 +3082,158 @@ function useSupplyRepository() {
3034
3082
  createUniqueIndex,
3035
3083
  createSupply,
3036
3084
  getSupplies,
3085
+ getSupplyById,
3037
3086
  updateSupply,
3038
3087
  deleteSupply
3039
3088
  };
3040
3089
  }
3041
3090
 
3042
- // src/controllers/hygiene-supply.controller.ts
3091
+ // src/services/hygiene-supply.service.ts
3092
+ var import_node_server_utils20 = require("@iservice365/node-server-utils");
3093
+
3094
+ // src/models/hygiene-stock.model.ts
3043
3095
  var import_node_server_utils18 = require("@iservice365/node-server-utils");
3044
3096
  var import_joi10 = __toESM(require("joi"));
3097
+ var import_mongodb11 = require("mongodb");
3098
+ var stockSchema = import_joi10.default.object({
3099
+ site: import_joi10.default.string().hex().required(),
3100
+ supply: import_joi10.default.string().hex().required(),
3101
+ in: import_joi10.default.number().min(0).optional(),
3102
+ out: import_joi10.default.number().min(0).optional(),
3103
+ balance: import_joi10.default.number().min(0).required(),
3104
+ remarks: import_joi10.default.string().optional().allow("", null)
3105
+ });
3106
+ function MStock(value) {
3107
+ const { error } = stockSchema.validate(value);
3108
+ if (error) {
3109
+ import_node_server_utils18.logger.info(`Hygiene Stock Model: ${error.message}`);
3110
+ throw new import_node_server_utils18.BadRequestError(error.message);
3111
+ }
3112
+ if (value.site) {
3113
+ try {
3114
+ value.site = new import_mongodb11.ObjectId(value.site);
3115
+ } catch (error2) {
3116
+ throw new import_node_server_utils18.BadRequestError("Invalid site ID format.");
3117
+ }
3118
+ }
3119
+ if (value.supply) {
3120
+ try {
3121
+ value.supply = new import_mongodb11.ObjectId(value.supply);
3122
+ } catch (error2) {
3123
+ throw new import_node_server_utils18.BadRequestError("Invalid supply ID format.");
3124
+ }
3125
+ }
3126
+ return {
3127
+ site: value.site,
3128
+ supply: value.supply,
3129
+ in: value.in ?? 0,
3130
+ out: value.out ?? 0,
3131
+ balance: value.balance,
3132
+ remarks: value.remarks ?? "",
3133
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3134
+ status: "active",
3135
+ updatedAt: "",
3136
+ deletedAt: ""
3137
+ };
3138
+ }
3139
+
3140
+ // src/repositories/hygiene-stock.repository.ts
3141
+ var import_node_server_utils19 = require("@iservice365/node-server-utils");
3142
+ function useStockRepository() {
3143
+ const db = import_node_server_utils19.useAtlas.getDb();
3144
+ if (!db) {
3145
+ throw new import_node_server_utils19.InternalServerError("Unable to connect to server.");
3146
+ }
3147
+ const namespace_collection = "site.supply.stocks";
3148
+ const collection = db.collection(namespace_collection);
3149
+ const { delNamespace } = (0, import_node_server_utils19.useCache)(namespace_collection);
3150
+ async function createIndex() {
3151
+ try {
3152
+ await collection.createIndexes([
3153
+ { key: { site: 1 } },
3154
+ { key: { supply: 1 } },
3155
+ { key: { balance: 1 } },
3156
+ { key: { status: 1 } }
3157
+ ]);
3158
+ } catch (error) {
3159
+ throw new import_node_server_utils19.InternalServerError("Failed to create index on hygiene stock.");
3160
+ }
3161
+ }
3162
+ async function createStock(value, session) {
3163
+ try {
3164
+ value = MStock(value);
3165
+ const res = await collection.insertOne(value, { session });
3166
+ delNamespace().then(() => {
3167
+ import_node_server_utils19.logger.info(`Cache cleared for namespace: ${namespace_collection}`);
3168
+ }).catch((err) => {
3169
+ import_node_server_utils19.logger.error(
3170
+ `Failed to clear cache for namespace: ${namespace_collection}`,
3171
+ err
3172
+ );
3173
+ });
3174
+ return res.insertedId;
3175
+ } catch (error) {
3176
+ const isDuplicated = error.message.includes("duplicate");
3177
+ if (isDuplicated) {
3178
+ throw new import_node_server_utils19.BadRequestError("Stock already exists.");
3179
+ }
3180
+ throw error;
3181
+ }
3182
+ }
3183
+ return {
3184
+ createIndex,
3185
+ createStock
3186
+ };
3187
+ }
3188
+
3189
+ // src/services/hygiene-supply.service.ts
3190
+ function useSupplyService() {
3191
+ const { createSupply: _createSupply } = useSupplyRepository();
3192
+ const { createStock } = useStockRepository();
3193
+ async function createSupply(value) {
3194
+ const session = import_node_server_utils20.useAtlas.getClient()?.startSession();
3195
+ try {
3196
+ session?.startTransaction();
3197
+ const { qty, site } = value;
3198
+ const supply = await _createSupply(value, session);
3199
+ const createdSupply = await createStock(
3200
+ {
3201
+ site,
3202
+ supply: supply.toString(),
3203
+ in: qty,
3204
+ balance: qty
3205
+ },
3206
+ session
3207
+ );
3208
+ await session?.commitTransaction();
3209
+ return createdSupply;
3210
+ } catch (error) {
3211
+ await session?.abortTransaction();
3212
+ throw error;
3213
+ } finally {
3214
+ await session?.endSession();
3215
+ }
3216
+ }
3217
+ return { createSupply };
3218
+ }
3219
+
3220
+ // src/controllers/hygiene-supply.controller.ts
3221
+ var import_node_server_utils21 = require("@iservice365/node-server-utils");
3222
+ var import_joi11 = __toESM(require("joi"));
3045
3223
  function useSupplyController() {
3046
3224
  const {
3047
- createSupply: _createSupply,
3048
3225
  getSupplies: _getSupplies,
3226
+ getSupplyById: _getSupplyById,
3049
3227
  updateSupply: _updateSupply,
3050
3228
  deleteSupply: _deleteSupply
3051
3229
  } = useSupplyRepository();
3230
+ const { createSupply: _createSupply } = useSupplyService();
3052
3231
  async function createSupply(req, res, next) {
3053
3232
  const payload = { ...req.body, ...req.params };
3054
3233
  const { error } = supplySchema.validate(payload);
3055
3234
  if (error) {
3056
- import_node_server_utils18.logger.log({ level: "error", message: error.message });
3057
- next(new import_node_server_utils18.BadRequestError(error.message));
3235
+ import_node_server_utils21.logger.log({ level: "error", message: error.message });
3236
+ next(new import_node_server_utils21.BadRequestError(error.message));
3058
3237
  return;
3059
3238
  }
3060
3239
  try {
@@ -3062,23 +3241,23 @@ function useSupplyController() {
3062
3241
  res.status(201).json({ message: "Supply created successfully.", id });
3063
3242
  return;
3064
3243
  } catch (error2) {
3065
- import_node_server_utils18.logger.log({ level: "error", message: error2.message });
3244
+ import_node_server_utils21.logger.log({ level: "error", message: error2.message });
3066
3245
  next(error2);
3067
3246
  return;
3068
3247
  }
3069
3248
  }
3070
3249
  async function getSupplies(req, res, next) {
3071
3250
  const query = { ...req.query, ...req.params };
3072
- const validation = import_joi10.default.object({
3073
- page: import_joi10.default.number().min(1).optional().allow("", null),
3074
- limit: import_joi10.default.number().min(1).optional().allow("", null),
3075
- search: import_joi10.default.string().optional().allow("", null),
3076
- site: import_joi10.default.string().hex().required()
3251
+ const validation = import_joi11.default.object({
3252
+ page: import_joi11.default.number().min(1).optional().allow("", null),
3253
+ limit: import_joi11.default.number().min(1).optional().allow("", null),
3254
+ search: import_joi11.default.string().optional().allow("", null),
3255
+ site: import_joi11.default.string().hex().required()
3077
3256
  });
3078
3257
  const { error } = validation.validate(query);
3079
3258
  if (error) {
3080
- import_node_server_utils18.logger.log({ level: "error", message: error.message });
3081
- next(new import_node_server_utils18.BadRequestError(error.message));
3259
+ import_node_server_utils21.logger.log({ level: "error", message: error.message });
3260
+ next(new import_node_server_utils21.BadRequestError(error.message));
3082
3261
  return;
3083
3262
  }
3084
3263
  const page = parseInt(req.query.page) ?? 1;
@@ -3095,23 +3274,42 @@ function useSupplyController() {
3095
3274
  res.json(data);
3096
3275
  return;
3097
3276
  } catch (error2) {
3098
- import_node_server_utils18.logger.log({ level: "error", message: error2.message });
3277
+ import_node_server_utils21.logger.log({ level: "error", message: error2.message });
3278
+ next(error2);
3279
+ return;
3280
+ }
3281
+ }
3282
+ async function getSupplyById(req, res, next) {
3283
+ const validation = import_joi11.default.string().hex().required();
3284
+ const _id = req.params.id;
3285
+ const { error } = validation.validate(_id);
3286
+ if (error) {
3287
+ import_node_server_utils21.logger.log({ level: "error", message: error.message });
3288
+ next(new import_node_server_utils21.BadRequestError(error.message));
3289
+ return;
3290
+ }
3291
+ try {
3292
+ const data = await _getSupplyById(_id);
3293
+ res.json(data);
3294
+ return;
3295
+ } catch (error2) {
3296
+ import_node_server_utils21.logger.log({ level: "error", message: error2.message });
3099
3297
  next(error2);
3100
3298
  return;
3101
3299
  }
3102
3300
  }
3103
3301
  async function updateSupply(req, res, next) {
3104
3302
  const payload = { id: req.params.id, ...req.body };
3105
- const validation = import_joi10.default.object({
3106
- id: import_joi10.default.string().hex().required(),
3107
- name: import_joi10.default.string().optional().allow("", null),
3108
- unitOfMeasurement: import_joi10.default.string().optional().allow("", null),
3109
- qty: import_joi10.default.number().min(0).optional().allow("", null)
3303
+ const validation = import_joi11.default.object({
3304
+ id: import_joi11.default.string().hex().required(),
3305
+ name: import_joi11.default.string().optional().allow("", null),
3306
+ unitOfMeasurement: import_joi11.default.string().optional().allow("", null),
3307
+ qty: import_joi11.default.number().min(0).optional().allow("", null)
3110
3308
  });
3111
3309
  const { error } = validation.validate(payload);
3112
3310
  if (error) {
3113
- import_node_server_utils18.logger.log({ level: "error", message: error.message });
3114
- next(new import_node_server_utils18.BadRequestError(error.message));
3311
+ import_node_server_utils21.logger.log({ level: "error", message: error.message });
3312
+ next(new import_node_server_utils21.BadRequestError(error.message));
3115
3313
  return;
3116
3314
  }
3117
3315
  try {
@@ -3120,20 +3318,20 @@ function useSupplyController() {
3120
3318
  res.json({ message: "Supply updated successfully." });
3121
3319
  return;
3122
3320
  } catch (error2) {
3123
- import_node_server_utils18.logger.log({ level: "error", message: error2.message });
3321
+ import_node_server_utils21.logger.log({ level: "error", message: error2.message });
3124
3322
  next(error2);
3125
3323
  return;
3126
3324
  }
3127
3325
  }
3128
3326
  async function deleteSupply(req, res, next) {
3129
3327
  const id = req.params.id;
3130
- const validation = import_joi10.default.object({
3131
- id: import_joi10.default.string().hex().required()
3328
+ const validation = import_joi11.default.object({
3329
+ id: import_joi11.default.string().hex().required()
3132
3330
  });
3133
3331
  const { error } = validation.validate({ id });
3134
3332
  if (error) {
3135
- import_node_server_utils18.logger.log({ level: "error", message: error.message });
3136
- next(new import_node_server_utils18.BadRequestError(error.message));
3333
+ import_node_server_utils21.logger.log({ level: "error", message: error.message });
3334
+ next(new import_node_server_utils21.BadRequestError(error.message));
3137
3335
  return;
3138
3336
  }
3139
3337
  try {
@@ -3141,7 +3339,7 @@ function useSupplyController() {
3141
3339
  res.json({ message: "Supply deleted successfully." });
3142
3340
  return;
3143
3341
  } catch (error2) {
3144
- import_node_server_utils18.logger.log({ level: "error", message: error2.message });
3342
+ import_node_server_utils21.logger.log({ level: "error", message: error2.message });
3145
3343
  next(error2);
3146
3344
  return;
3147
3345
  }
@@ -3149,15 +3347,83 @@ function useSupplyController() {
3149
3347
  return {
3150
3348
  createSupply,
3151
3349
  getSupplies,
3350
+ getSupplyById,
3152
3351
  updateSupply,
3153
3352
  deleteSupply
3154
3353
  };
3155
3354
  }
3355
+
3356
+ // src/services/hygiene-stock.service.ts
3357
+ var import_node_server_utils22 = require("@iservice365/node-server-utils");
3358
+ function useStockService() {
3359
+ const { createStock: _createStock } = useStockRepository();
3360
+ const { getSupplyById, updateSupply } = useSupplyRepository();
3361
+ async function createStock(value) {
3362
+ const session = import_node_server_utils22.useAtlas.getClient()?.startSession();
3363
+ try {
3364
+ session?.startTransaction();
3365
+ const { qty, ...stockData } = value;
3366
+ const supply = await getSupplyById(value.supply);
3367
+ if (!supply || supply.qty === void 0) {
3368
+ throw new import_node_server_utils22.NotFoundError("Supply not found.");
3369
+ }
3370
+ const newSupplyQty = supply.qty + qty;
3371
+ await updateSupply(value.supply, { qty: newSupplyQty }, session);
3372
+ const createdStock = await _createStock(
3373
+ { ...stockData, in: qty, balance: newSupplyQty },
3374
+ session
3375
+ );
3376
+ await session?.commitTransaction();
3377
+ return createdStock;
3378
+ } catch (error) {
3379
+ await session?.abortTransaction();
3380
+ throw error;
3381
+ } finally {
3382
+ await session?.endSession();
3383
+ }
3384
+ }
3385
+ return { createStock };
3386
+ }
3387
+
3388
+ // src/controllers/hygiene-stock.controller.ts
3389
+ var import_node_server_utils23 = require("@iservice365/node-server-utils");
3390
+ var import_joi12 = __toESM(require("joi"));
3391
+ function useStockController() {
3392
+ const { createStock: _createStock } = useStockService();
3393
+ async function createStock(req, res, next) {
3394
+ const payload = { ...req.body, ...req.params };
3395
+ const validation = import_joi12.default.object({
3396
+ site: import_joi12.default.string().hex().required(),
3397
+ supply: import_joi12.default.string().hex().required(),
3398
+ qty: import_joi12.default.number().min(0).optional(),
3399
+ remarks: import_joi12.default.string().optional().allow("", null)
3400
+ });
3401
+ const { error } = validation.validate(payload);
3402
+ if (error) {
3403
+ import_node_server_utils23.logger.log({ level: "error", message: error.message });
3404
+ next(new import_node_server_utils23.BadRequestError(error.message));
3405
+ return;
3406
+ }
3407
+ try {
3408
+ const id = await _createStock(payload);
3409
+ res.status(201).json({ message: "Stock created successfully.", id });
3410
+ return;
3411
+ } catch (error2) {
3412
+ import_node_server_utils23.logger.log({ level: "error", message: error2.message });
3413
+ next(error2);
3414
+ return;
3415
+ }
3416
+ }
3417
+ return {
3418
+ createStock
3419
+ };
3420
+ }
3156
3421
  // Annotate the CommonJS export names for ESM import in node:
3157
3422
  0 && (module.exports = {
3158
3423
  MArea,
3159
3424
  MAreaChecklist,
3160
3425
  MParentChecklist,
3426
+ MStock,
3161
3427
  MSupply,
3162
3428
  MUnit,
3163
3429
  allowedChecklistStatus,
@@ -3166,6 +3432,7 @@ function useSupplyController() {
3166
3432
  areaChecklistSchema,
3167
3433
  areaSchema,
3168
3434
  parentChecklistSchema,
3435
+ stockSchema,
3169
3436
  supplySchema,
3170
3437
  unitSchema,
3171
3438
  useAreaChecklistController,
@@ -3176,8 +3443,12 @@ function useSupplyController() {
3176
3443
  useAreaService,
3177
3444
  useParentChecklistController,
3178
3445
  useParentChecklistRepo,
3446
+ useStockController,
3447
+ useStockRepository,
3448
+ useStockService,
3179
3449
  useSupplyController,
3180
3450
  useSupplyRepository,
3451
+ useSupplyService,
3181
3452
  useUnitController,
3182
3453
  useUnitRepository,
3183
3454
  useUnitService