@iservice365/module-hygiene 1.0.2 → 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 +7 -0
- package/dist/index.d.ts +53 -4
- package/dist/index.js +238 -31
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +238 -31
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
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,11 +300,15 @@ 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<{}>;
|
|
302
|
-
getSupplyById: (_id: string | ObjectId) => Promise<
|
|
303
|
+
getSupplyById: (_id: string | ObjectId) => Promise<TSupplyGetById>;
|
|
303
304
|
updateSupply: (_id: string | ObjectId, value: TSupplyUpdate, session?: ClientSession) => Promise<number>;
|
|
304
305
|
deleteSupply: (_id: string | ObjectId, session?: ClientSession) => Promise<number>;
|
|
305
306
|
};
|
|
306
307
|
|
|
308
|
+
declare function useSupplyService(): {
|
|
309
|
+
createSupply: (value: TSupplyCreate) => Promise<bson.ObjectId>;
|
|
310
|
+
};
|
|
311
|
+
|
|
307
312
|
declare function useSupplyController(): {
|
|
308
313
|
createSupply: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
309
314
|
getSupplies: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
@@ -312,4 +317,48 @@ declare function useSupplyController(): {
|
|
|
312
317
|
deleteSupply: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
313
318
|
};
|
|
314
319
|
|
|
315
|
-
|
|
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
|
};
|
|
@@ -3082,23 +3088,152 @@ function useSupplyRepository() {
|
|
|
3082
3088
|
};
|
|
3083
3089
|
}
|
|
3084
3090
|
|
|
3085
|
-
// src/
|
|
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
|
|
3086
3095
|
var import_node_server_utils18 = require("@iservice365/node-server-utils");
|
|
3087
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"));
|
|
3088
3223
|
function useSupplyController() {
|
|
3089
3224
|
const {
|
|
3090
|
-
createSupply: _createSupply,
|
|
3091
3225
|
getSupplies: _getSupplies,
|
|
3092
3226
|
getSupplyById: _getSupplyById,
|
|
3093
3227
|
updateSupply: _updateSupply,
|
|
3094
3228
|
deleteSupply: _deleteSupply
|
|
3095
3229
|
} = useSupplyRepository();
|
|
3230
|
+
const { createSupply: _createSupply } = useSupplyService();
|
|
3096
3231
|
async function createSupply(req, res, next) {
|
|
3097
3232
|
const payload = { ...req.body, ...req.params };
|
|
3098
3233
|
const { error } = supplySchema.validate(payload);
|
|
3099
3234
|
if (error) {
|
|
3100
|
-
|
|
3101
|
-
next(new
|
|
3235
|
+
import_node_server_utils21.logger.log({ level: "error", message: error.message });
|
|
3236
|
+
next(new import_node_server_utils21.BadRequestError(error.message));
|
|
3102
3237
|
return;
|
|
3103
3238
|
}
|
|
3104
3239
|
try {
|
|
@@ -3106,23 +3241,23 @@ function useSupplyController() {
|
|
|
3106
3241
|
res.status(201).json({ message: "Supply created successfully.", id });
|
|
3107
3242
|
return;
|
|
3108
3243
|
} catch (error2) {
|
|
3109
|
-
|
|
3244
|
+
import_node_server_utils21.logger.log({ level: "error", message: error2.message });
|
|
3110
3245
|
next(error2);
|
|
3111
3246
|
return;
|
|
3112
3247
|
}
|
|
3113
3248
|
}
|
|
3114
3249
|
async function getSupplies(req, res, next) {
|
|
3115
3250
|
const query = { ...req.query, ...req.params };
|
|
3116
|
-
const validation =
|
|
3117
|
-
page:
|
|
3118
|
-
limit:
|
|
3119
|
-
search:
|
|
3120
|
-
site:
|
|
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()
|
|
3121
3256
|
});
|
|
3122
3257
|
const { error } = validation.validate(query);
|
|
3123
3258
|
if (error) {
|
|
3124
|
-
|
|
3125
|
-
next(new
|
|
3259
|
+
import_node_server_utils21.logger.log({ level: "error", message: error.message });
|
|
3260
|
+
next(new import_node_server_utils21.BadRequestError(error.message));
|
|
3126
3261
|
return;
|
|
3127
3262
|
}
|
|
3128
3263
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -3139,18 +3274,18 @@ function useSupplyController() {
|
|
|
3139
3274
|
res.json(data);
|
|
3140
3275
|
return;
|
|
3141
3276
|
} catch (error2) {
|
|
3142
|
-
|
|
3277
|
+
import_node_server_utils21.logger.log({ level: "error", message: error2.message });
|
|
3143
3278
|
next(error2);
|
|
3144
3279
|
return;
|
|
3145
3280
|
}
|
|
3146
3281
|
}
|
|
3147
3282
|
async function getSupplyById(req, res, next) {
|
|
3148
|
-
const validation =
|
|
3283
|
+
const validation = import_joi11.default.string().hex().required();
|
|
3149
3284
|
const _id = req.params.id;
|
|
3150
3285
|
const { error } = validation.validate(_id);
|
|
3151
3286
|
if (error) {
|
|
3152
|
-
|
|
3153
|
-
next(new
|
|
3287
|
+
import_node_server_utils21.logger.log({ level: "error", message: error.message });
|
|
3288
|
+
next(new import_node_server_utils21.BadRequestError(error.message));
|
|
3154
3289
|
return;
|
|
3155
3290
|
}
|
|
3156
3291
|
try {
|
|
@@ -3158,23 +3293,23 @@ function useSupplyController() {
|
|
|
3158
3293
|
res.json(data);
|
|
3159
3294
|
return;
|
|
3160
3295
|
} catch (error2) {
|
|
3161
|
-
|
|
3296
|
+
import_node_server_utils21.logger.log({ level: "error", message: error2.message });
|
|
3162
3297
|
next(error2);
|
|
3163
3298
|
return;
|
|
3164
3299
|
}
|
|
3165
3300
|
}
|
|
3166
3301
|
async function updateSupply(req, res, next) {
|
|
3167
3302
|
const payload = { id: req.params.id, ...req.body };
|
|
3168
|
-
const validation =
|
|
3169
|
-
id:
|
|
3170
|
-
name:
|
|
3171
|
-
unitOfMeasurement:
|
|
3172
|
-
qty:
|
|
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)
|
|
3173
3308
|
});
|
|
3174
3309
|
const { error } = validation.validate(payload);
|
|
3175
3310
|
if (error) {
|
|
3176
|
-
|
|
3177
|
-
next(new
|
|
3311
|
+
import_node_server_utils21.logger.log({ level: "error", message: error.message });
|
|
3312
|
+
next(new import_node_server_utils21.BadRequestError(error.message));
|
|
3178
3313
|
return;
|
|
3179
3314
|
}
|
|
3180
3315
|
try {
|
|
@@ -3183,20 +3318,20 @@ function useSupplyController() {
|
|
|
3183
3318
|
res.json({ message: "Supply updated successfully." });
|
|
3184
3319
|
return;
|
|
3185
3320
|
} catch (error2) {
|
|
3186
|
-
|
|
3321
|
+
import_node_server_utils21.logger.log({ level: "error", message: error2.message });
|
|
3187
3322
|
next(error2);
|
|
3188
3323
|
return;
|
|
3189
3324
|
}
|
|
3190
3325
|
}
|
|
3191
3326
|
async function deleteSupply(req, res, next) {
|
|
3192
3327
|
const id = req.params.id;
|
|
3193
|
-
const validation =
|
|
3194
|
-
id:
|
|
3328
|
+
const validation = import_joi11.default.object({
|
|
3329
|
+
id: import_joi11.default.string().hex().required()
|
|
3195
3330
|
});
|
|
3196
3331
|
const { error } = validation.validate({ id });
|
|
3197
3332
|
if (error) {
|
|
3198
|
-
|
|
3199
|
-
next(new
|
|
3333
|
+
import_node_server_utils21.logger.log({ level: "error", message: error.message });
|
|
3334
|
+
next(new import_node_server_utils21.BadRequestError(error.message));
|
|
3200
3335
|
return;
|
|
3201
3336
|
}
|
|
3202
3337
|
try {
|
|
@@ -3204,7 +3339,7 @@ function useSupplyController() {
|
|
|
3204
3339
|
res.json({ message: "Supply deleted successfully." });
|
|
3205
3340
|
return;
|
|
3206
3341
|
} catch (error2) {
|
|
3207
|
-
|
|
3342
|
+
import_node_server_utils21.logger.log({ level: "error", message: error2.message });
|
|
3208
3343
|
next(error2);
|
|
3209
3344
|
return;
|
|
3210
3345
|
}
|
|
@@ -3217,11 +3352,78 @@ function useSupplyController() {
|
|
|
3217
3352
|
deleteSupply
|
|
3218
3353
|
};
|
|
3219
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
|
+
}
|
|
3220
3421
|
// Annotate the CommonJS export names for ESM import in node:
|
|
3221
3422
|
0 && (module.exports = {
|
|
3222
3423
|
MArea,
|
|
3223
3424
|
MAreaChecklist,
|
|
3224
3425
|
MParentChecklist,
|
|
3426
|
+
MStock,
|
|
3225
3427
|
MSupply,
|
|
3226
3428
|
MUnit,
|
|
3227
3429
|
allowedChecklistStatus,
|
|
@@ -3230,6 +3432,7 @@ function useSupplyController() {
|
|
|
3230
3432
|
areaChecklistSchema,
|
|
3231
3433
|
areaSchema,
|
|
3232
3434
|
parentChecklistSchema,
|
|
3435
|
+
stockSchema,
|
|
3233
3436
|
supplySchema,
|
|
3234
3437
|
unitSchema,
|
|
3235
3438
|
useAreaChecklistController,
|
|
@@ -3240,8 +3443,12 @@ function useSupplyController() {
|
|
|
3240
3443
|
useAreaService,
|
|
3241
3444
|
useParentChecklistController,
|
|
3242
3445
|
useParentChecklistRepo,
|
|
3446
|
+
useStockController,
|
|
3447
|
+
useStockRepository,
|
|
3448
|
+
useStockService,
|
|
3243
3449
|
useSupplyController,
|
|
3244
3450
|
useSupplyRepository,
|
|
3451
|
+
useSupplyService,
|
|
3245
3452
|
useUnitController,
|
|
3246
3453
|
useUnitRepository,
|
|
3247
3454
|
useUnitService
|