@eeplatform/core 1.3.0 → 1.4.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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +64 -43
- package/dist/index.js +742 -59
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +746 -56
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -27782,7 +27782,7 @@ import Joi34 from "joi";
|
|
|
27782
27782
|
// src/services/building.service.ts
|
|
27783
27783
|
import {
|
|
27784
27784
|
BadRequestError as BadRequestError62,
|
|
27785
|
-
NotFoundError as
|
|
27785
|
+
NotFoundError as NotFoundError10,
|
|
27786
27786
|
useAtlas as useAtlas33
|
|
27787
27787
|
} from "@eeplatform/nodejs-utils";
|
|
27788
27788
|
function useBuildingService() {
|
|
@@ -27798,7 +27798,7 @@ function useBuildingService() {
|
|
|
27798
27798
|
try {
|
|
27799
27799
|
const building = await _getById(id);
|
|
27800
27800
|
if (!building) {
|
|
27801
|
-
throw new
|
|
27801
|
+
throw new NotFoundError10("Building not found.");
|
|
27802
27802
|
}
|
|
27803
27803
|
if (data.levels < building.levels) {
|
|
27804
27804
|
const unit = await getByBuildingLevel(id, building.levels);
|
|
@@ -28177,19 +28177,19 @@ function useBuildingUnitController() {
|
|
|
28177
28177
|
};
|
|
28178
28178
|
}
|
|
28179
28179
|
|
|
28180
|
-
// src/models/
|
|
28180
|
+
// src/models/asset.model.ts
|
|
28181
28181
|
import { BadRequestError as BadRequestError65 } from "@eeplatform/nodejs-utils";
|
|
28182
28182
|
import Joi36 from "joi";
|
|
28183
28183
|
import { ObjectId as ObjectId42 } from "mongodb";
|
|
28184
|
-
var
|
|
28184
|
+
var schemaAsset = Joi36.object({
|
|
28185
28185
|
_id: Joi36.string().hex().optional(),
|
|
28186
28186
|
school: Joi36.string().hex().required(),
|
|
28187
|
+
asset_type: Joi36.string().required().allow("supply", "furniture-equipment", "fixed-asset"),
|
|
28187
28188
|
name: Joi36.string().required(),
|
|
28188
28189
|
category: Joi36.string().optional().allow("", null),
|
|
28189
28190
|
type: Joi36.string().optional().allow("", null),
|
|
28190
28191
|
brand: Joi36.string().optional().allow("", null),
|
|
28191
|
-
|
|
28192
|
-
unit_of_measurement_type: Joi36.string().required(),
|
|
28192
|
+
unit: Joi36.string().required(),
|
|
28193
28193
|
status: Joi36.string().optional().allow("", null),
|
|
28194
28194
|
createdAt: Joi36.date().optional().allow("", null),
|
|
28195
28195
|
updatedAt: Joi36.date().optional().allow("", null),
|
|
@@ -28205,14 +28205,13 @@ var schemaInventory = Joi36.object({
|
|
|
28205
28205
|
language: Joi36.string().optional().allow("", null)
|
|
28206
28206
|
}).optional().allow(null)
|
|
28207
28207
|
});
|
|
28208
|
-
var
|
|
28208
|
+
var schemaAssetUpdateOption = Joi36.object({
|
|
28209
28209
|
name: Joi36.string().optional().allow("", null),
|
|
28210
28210
|
category: Joi36.string().optional().allow("", null),
|
|
28211
28211
|
type: Joi36.string().optional().allow("", null),
|
|
28212
28212
|
brand: Joi36.string().optional().allow("", null),
|
|
28213
28213
|
qty: Joi36.number().integer().min(0).optional().allow("", null),
|
|
28214
|
-
|
|
28215
|
-
unit_of_measurement_type: Joi36.string().optional().allow("", null),
|
|
28214
|
+
unit: Joi36.string().optional().allow("", null),
|
|
28216
28215
|
metadata: Joi36.object({
|
|
28217
28216
|
title: Joi36.string().optional().allow("", null),
|
|
28218
28217
|
isbn: Joi36.string().optional().allow("", null),
|
|
@@ -28224,8 +28223,8 @@ var schemaInventoryUpdateOption = Joi36.object({
|
|
|
28224
28223
|
language: Joi36.string().optional().allow("", null)
|
|
28225
28224
|
}).optional().allow(null)
|
|
28226
28225
|
});
|
|
28227
|
-
function
|
|
28228
|
-
const { error } =
|
|
28226
|
+
function MAsset(value) {
|
|
28227
|
+
const { error } = schemaAsset.validate(value);
|
|
28229
28228
|
if (error) {
|
|
28230
28229
|
throw new BadRequestError65(error.message);
|
|
28231
28230
|
}
|
|
@@ -28247,6 +28246,7 @@ function MInventory(value) {
|
|
|
28247
28246
|
return {
|
|
28248
28247
|
_id: value._id ?? new ObjectId42(),
|
|
28249
28248
|
school: value.school,
|
|
28249
|
+
asset_type: value.asset_type ?? "supply",
|
|
28250
28250
|
name: value.name,
|
|
28251
28251
|
category: value.category ?? "",
|
|
28252
28252
|
type: value.type ?? "",
|
|
@@ -28258,8 +28258,7 @@ function MInventory(value) {
|
|
|
28258
28258
|
lost: 0,
|
|
28259
28259
|
damaged: 0
|
|
28260
28260
|
},
|
|
28261
|
-
|
|
28262
|
-
unit_of_measurement_type: value.unit_of_measurement_type ?? "",
|
|
28261
|
+
unit: value.unit ?? "",
|
|
28263
28262
|
status: value.status ?? "active",
|
|
28264
28263
|
createdAt: value.createdAt,
|
|
28265
28264
|
updatedAt: value.updatedAt,
|
|
@@ -28268,7 +28267,7 @@ function MInventory(value) {
|
|
|
28268
28267
|
};
|
|
28269
28268
|
}
|
|
28270
28269
|
|
|
28271
|
-
// src/repositories/
|
|
28270
|
+
// src/repositories/asset.repository.ts
|
|
28272
28271
|
import {
|
|
28273
28272
|
BadRequestError as BadRequestError66,
|
|
28274
28273
|
logger as logger33,
|
|
@@ -28278,12 +28277,12 @@ import {
|
|
|
28278
28277
|
useCache as useCache22
|
|
28279
28278
|
} from "@eeplatform/nodejs-utils";
|
|
28280
28279
|
import { ObjectId as ObjectId43 } from "mongodb";
|
|
28281
|
-
function
|
|
28280
|
+
function useAssetRepo() {
|
|
28282
28281
|
const db = useAtlas35.getDb();
|
|
28283
28282
|
if (!db) {
|
|
28284
28283
|
throw new BadRequestError66("Unable to connect to server.");
|
|
28285
28284
|
}
|
|
28286
|
-
const namespace_collection = "school.
|
|
28285
|
+
const namespace_collection = "school.assets";
|
|
28287
28286
|
const collection = db.collection(namespace_collection);
|
|
28288
28287
|
const { getCache, setCache, delNamespace } = useCache22(namespace_collection);
|
|
28289
28288
|
function delCachedData() {
|
|
@@ -28307,21 +28306,21 @@ function useInventoryRepo() {
|
|
|
28307
28306
|
{ key: { name: "text" } }
|
|
28308
28307
|
]);
|
|
28309
28308
|
} catch (error) {
|
|
28310
|
-
throw new BadRequestError66("Failed to create index on
|
|
28309
|
+
throw new BadRequestError66("Failed to create index on asset.");
|
|
28311
28310
|
}
|
|
28312
28311
|
}
|
|
28313
28312
|
async function add(value) {
|
|
28314
28313
|
try {
|
|
28315
|
-
value =
|
|
28314
|
+
value = MAsset(value);
|
|
28316
28315
|
const res = await collection.insertOne(value);
|
|
28317
28316
|
delCachedData();
|
|
28318
28317
|
return res.insertedId;
|
|
28319
28318
|
} catch (error) {
|
|
28320
|
-
throw new BadRequestError66("Failed to create
|
|
28319
|
+
throw new BadRequestError66("Failed to create asset item.");
|
|
28321
28320
|
}
|
|
28322
28321
|
}
|
|
28323
|
-
async function updateById(_id, value) {
|
|
28324
|
-
const { error } =
|
|
28322
|
+
async function updateById(_id, value, session) {
|
|
28323
|
+
const { error } = schemaAssetUpdateOption.validate(value);
|
|
28325
28324
|
if (error) {
|
|
28326
28325
|
throw new BadRequestError66(error.message);
|
|
28327
28326
|
}
|
|
@@ -28331,13 +28330,17 @@ function useInventoryRepo() {
|
|
|
28331
28330
|
throw new BadRequestError66("Invalid ID.");
|
|
28332
28331
|
}
|
|
28333
28332
|
try {
|
|
28334
|
-
const res = await collection.updateOne(
|
|
28333
|
+
const res = await collection.updateOne(
|
|
28334
|
+
{ _id },
|
|
28335
|
+
{ $set: value },
|
|
28336
|
+
{ session }
|
|
28337
|
+
);
|
|
28335
28338
|
if (res.modifiedCount) {
|
|
28336
28339
|
delCachedData();
|
|
28337
28340
|
}
|
|
28338
|
-
return "Successfully updated
|
|
28341
|
+
return "Successfully updated asset item.";
|
|
28339
28342
|
} catch (error2) {
|
|
28340
|
-
throw new BadRequestError66("Failed to update
|
|
28343
|
+
throw new BadRequestError66("Failed to update asset item.");
|
|
28341
28344
|
}
|
|
28342
28345
|
}
|
|
28343
28346
|
async function deleteById(_id) {
|
|
@@ -28350,11 +28353,11 @@ function useInventoryRepo() {
|
|
|
28350
28353
|
const res = await collection.deleteOne({ _id });
|
|
28351
28354
|
if (res.deletedCount) {
|
|
28352
28355
|
delCachedData();
|
|
28353
|
-
return "Successfully deleted
|
|
28356
|
+
return "Successfully deleted asset item.";
|
|
28354
28357
|
}
|
|
28355
|
-
return "Successfully deleted
|
|
28358
|
+
return "Successfully deleted asset item.";
|
|
28356
28359
|
} catch (error) {
|
|
28357
|
-
throw new BadRequestError66("Failed to delete
|
|
28360
|
+
throw new BadRequestError66("Failed to delete asset item.");
|
|
28358
28361
|
}
|
|
28359
28362
|
}
|
|
28360
28363
|
async function getById(_id) {
|
|
@@ -28371,17 +28374,17 @@ function useInventoryRepo() {
|
|
|
28371
28374
|
try {
|
|
28372
28375
|
const res = await collection.findOne({ _id });
|
|
28373
28376
|
if (!res) {
|
|
28374
|
-
throw new BadRequestError66("
|
|
28377
|
+
throw new BadRequestError66("Asset item not found.");
|
|
28375
28378
|
}
|
|
28376
28379
|
setCache(cacheKey, res).then(() => {
|
|
28377
28380
|
logger33.log({
|
|
28378
28381
|
level: "info",
|
|
28379
|
-
message: `Cache set for
|
|
28382
|
+
message: `Cache set for asset item by ID: ${cacheKey}`
|
|
28380
28383
|
});
|
|
28381
28384
|
}).catch((err) => {
|
|
28382
28385
|
logger33.log({
|
|
28383
28386
|
level: "error",
|
|
28384
|
-
message: `Failed to set cache for
|
|
28387
|
+
message: `Failed to set cache for asset item by ID: ${cacheKey} - ${err.message}`
|
|
28385
28388
|
});
|
|
28386
28389
|
});
|
|
28387
28390
|
return res;
|
|
@@ -28389,7 +28392,7 @@ function useInventoryRepo() {
|
|
|
28389
28392
|
if (error instanceof BadRequestError66) {
|
|
28390
28393
|
throw error;
|
|
28391
28394
|
}
|
|
28392
|
-
throw new BadRequestError66("Failed to retrieve
|
|
28395
|
+
throw new BadRequestError66("Failed to retrieve asset item.");
|
|
28393
28396
|
}
|
|
28394
28397
|
}
|
|
28395
28398
|
async function getAll({
|
|
@@ -28398,7 +28401,8 @@ function useInventoryRepo() {
|
|
|
28398
28401
|
limit = 20,
|
|
28399
28402
|
status = "active",
|
|
28400
28403
|
school = "",
|
|
28401
|
-
sort = { _id: -1 }
|
|
28404
|
+
sort = { _id: -1 },
|
|
28405
|
+
asset_type = "supply"
|
|
28402
28406
|
} = {}) {
|
|
28403
28407
|
page = page ? page - 1 : 0;
|
|
28404
28408
|
try {
|
|
@@ -28408,14 +28412,16 @@ function useInventoryRepo() {
|
|
|
28408
28412
|
}
|
|
28409
28413
|
const query = {
|
|
28410
28414
|
school,
|
|
28411
|
-
status
|
|
28415
|
+
status,
|
|
28416
|
+
asset_type
|
|
28412
28417
|
};
|
|
28413
28418
|
const cacheKeyOptions = {
|
|
28414
28419
|
page,
|
|
28415
28420
|
limit,
|
|
28416
28421
|
status,
|
|
28417
28422
|
school: String(school),
|
|
28418
|
-
sort: JSON.stringify(sort)
|
|
28423
|
+
sort: JSON.stringify(sort),
|
|
28424
|
+
asset_type
|
|
28419
28425
|
};
|
|
28420
28426
|
if (search) {
|
|
28421
28427
|
query.$text = { $search: search };
|
|
@@ -28427,7 +28433,7 @@ function useInventoryRepo() {
|
|
|
28427
28433
|
if (cached) {
|
|
28428
28434
|
logger33.log({
|
|
28429
28435
|
level: "info",
|
|
28430
|
-
message: `Cache hit for getAll
|
|
28436
|
+
message: `Cache hit for getAll asset items: ${cacheKey}`
|
|
28431
28437
|
});
|
|
28432
28438
|
return cached;
|
|
28433
28439
|
}
|
|
@@ -28448,18 +28454,194 @@ function useInventoryRepo() {
|
|
|
28448
28454
|
setCache(cacheKey, data, 500).then(() => {
|
|
28449
28455
|
logger33.log({
|
|
28450
28456
|
level: "info",
|
|
28451
|
-
message: `Cache set for getAll
|
|
28457
|
+
message: `Cache set for getAll asset items: ${cacheKey}`
|
|
28452
28458
|
});
|
|
28453
28459
|
}).catch((err) => {
|
|
28454
28460
|
logger33.log({
|
|
28455
28461
|
level: "error",
|
|
28456
|
-
message: `Failed to set cache for getAll
|
|
28462
|
+
message: `Failed to set cache for getAll asset items: ${err.message}`
|
|
28457
28463
|
});
|
|
28458
28464
|
});
|
|
28459
28465
|
return data;
|
|
28460
28466
|
} catch (error) {
|
|
28461
28467
|
console.log("Error in getAll:", error);
|
|
28462
|
-
throw new BadRequestError66("Failed to retrieve
|
|
28468
|
+
throw new BadRequestError66("Failed to retrieve asset items.");
|
|
28469
|
+
}
|
|
28470
|
+
}
|
|
28471
|
+
async function getCategories(school, asset_type) {
|
|
28472
|
+
try {
|
|
28473
|
+
school = new ObjectId43(school);
|
|
28474
|
+
} catch (error) {
|
|
28475
|
+
throw new BadRequestError66("Invalid school ID.");
|
|
28476
|
+
}
|
|
28477
|
+
const cacheKey = makeCacheKey21(namespace_collection, {
|
|
28478
|
+
school,
|
|
28479
|
+
asset_type,
|
|
28480
|
+
type: "category-as-options"
|
|
28481
|
+
});
|
|
28482
|
+
const cachedData = await getCache(cacheKey);
|
|
28483
|
+
if (cachedData) {
|
|
28484
|
+
return cachedData;
|
|
28485
|
+
}
|
|
28486
|
+
try {
|
|
28487
|
+
const categories = await collection.aggregate([
|
|
28488
|
+
{
|
|
28489
|
+
$match: { school, asset_type }
|
|
28490
|
+
},
|
|
28491
|
+
{
|
|
28492
|
+
$group: {
|
|
28493
|
+
_id: "$category",
|
|
28494
|
+
value: { $first: "$category" },
|
|
28495
|
+
title: {
|
|
28496
|
+
$first: "$category"
|
|
28497
|
+
}
|
|
28498
|
+
}
|
|
28499
|
+
},
|
|
28500
|
+
{
|
|
28501
|
+
$project: {
|
|
28502
|
+
_id: 0,
|
|
28503
|
+
title: 1,
|
|
28504
|
+
value: 1
|
|
28505
|
+
}
|
|
28506
|
+
},
|
|
28507
|
+
{ $sort: { title: 1 } }
|
|
28508
|
+
]).toArray();
|
|
28509
|
+
setCache(cacheKey, categories).then(() => {
|
|
28510
|
+
logger33.log({
|
|
28511
|
+
level: "info",
|
|
28512
|
+
message: `Cache set for getCategoriesBySchool: ${cacheKey}`
|
|
28513
|
+
});
|
|
28514
|
+
}).catch((err) => {
|
|
28515
|
+
logger33.log({
|
|
28516
|
+
level: "error",
|
|
28517
|
+
message: `Failed to set cache for getCategoriesBySchool: ${err.message}`
|
|
28518
|
+
});
|
|
28519
|
+
});
|
|
28520
|
+
return categories;
|
|
28521
|
+
} catch (error) {
|
|
28522
|
+
if (error instanceof BadRequestError66) {
|
|
28523
|
+
throw error;
|
|
28524
|
+
}
|
|
28525
|
+
throw new BadRequestError66("Failed to retrieve asset categories.");
|
|
28526
|
+
}
|
|
28527
|
+
}
|
|
28528
|
+
async function getTypes(school, asset_type) {
|
|
28529
|
+
try {
|
|
28530
|
+
school = new ObjectId43(school);
|
|
28531
|
+
} catch (error) {
|
|
28532
|
+
throw new BadRequestError66("Invalid school ID.");
|
|
28533
|
+
}
|
|
28534
|
+
const cacheKey = makeCacheKey21(namespace_collection, {
|
|
28535
|
+
school,
|
|
28536
|
+
asset_type,
|
|
28537
|
+
type: "types-as-options"
|
|
28538
|
+
});
|
|
28539
|
+
const cachedData = await getCache(cacheKey);
|
|
28540
|
+
if (cachedData) {
|
|
28541
|
+
return cachedData;
|
|
28542
|
+
}
|
|
28543
|
+
try {
|
|
28544
|
+
const categories = await collection.aggregate([
|
|
28545
|
+
{
|
|
28546
|
+
$match: { school, asset_type }
|
|
28547
|
+
},
|
|
28548
|
+
{
|
|
28549
|
+
$group: {
|
|
28550
|
+
_id: "$type",
|
|
28551
|
+
value: { $first: "$type" },
|
|
28552
|
+
category: { $first: "$category" },
|
|
28553
|
+
title: { $first: "$name" },
|
|
28554
|
+
subtitle: { $first: "" }
|
|
28555
|
+
// keep empty for now, can be filled later
|
|
28556
|
+
}
|
|
28557
|
+
},
|
|
28558
|
+
{
|
|
28559
|
+
$project: {
|
|
28560
|
+
_id: 0,
|
|
28561
|
+
title: 1,
|
|
28562
|
+
value: 1,
|
|
28563
|
+
category: 1,
|
|
28564
|
+
subtitle: 1
|
|
28565
|
+
}
|
|
28566
|
+
},
|
|
28567
|
+
{ $sort: { title: 1 } }
|
|
28568
|
+
]).toArray();
|
|
28569
|
+
setCache(cacheKey, categories).then(() => {
|
|
28570
|
+
logger33.log({
|
|
28571
|
+
level: "info",
|
|
28572
|
+
message: `Cache set for getCategoriesBySchool: ${cacheKey}`
|
|
28573
|
+
});
|
|
28574
|
+
}).catch((err) => {
|
|
28575
|
+
logger33.log({
|
|
28576
|
+
level: "error",
|
|
28577
|
+
message: `Failed to set cache for getCategoriesBySchool: ${err.message}`
|
|
28578
|
+
});
|
|
28579
|
+
});
|
|
28580
|
+
return categories;
|
|
28581
|
+
} catch (error) {
|
|
28582
|
+
if (error instanceof BadRequestError66) {
|
|
28583
|
+
throw error;
|
|
28584
|
+
}
|
|
28585
|
+
throw new BadRequestError66("Failed to retrieve asset categories.");
|
|
28586
|
+
}
|
|
28587
|
+
}
|
|
28588
|
+
async function getUnitsBySchool(school) {
|
|
28589
|
+
try {
|
|
28590
|
+
school = new ObjectId43(school);
|
|
28591
|
+
} catch (error) {
|
|
28592
|
+
throw new BadRequestError66("Invalid school ID.");
|
|
28593
|
+
}
|
|
28594
|
+
const cacheKey = makeCacheKey21(namespace_collection, {
|
|
28595
|
+
school,
|
|
28596
|
+
type: "unit-as-options"
|
|
28597
|
+
});
|
|
28598
|
+
const cachedData = await getCache(cacheKey);
|
|
28599
|
+
if (cachedData) {
|
|
28600
|
+
return cachedData;
|
|
28601
|
+
}
|
|
28602
|
+
try {
|
|
28603
|
+
const categories = await collection.aggregate([
|
|
28604
|
+
{
|
|
28605
|
+
$match: { school }
|
|
28606
|
+
},
|
|
28607
|
+
{
|
|
28608
|
+
$group: {
|
|
28609
|
+
_id: "$unit",
|
|
28610
|
+
value: { $first: "$unit" },
|
|
28611
|
+
title: {
|
|
28612
|
+
$first: "$unit"
|
|
28613
|
+
}
|
|
28614
|
+
}
|
|
28615
|
+
},
|
|
28616
|
+
{
|
|
28617
|
+
$project: {
|
|
28618
|
+
_id: 0,
|
|
28619
|
+
title: 1,
|
|
28620
|
+
value: 1
|
|
28621
|
+
}
|
|
28622
|
+
},
|
|
28623
|
+
{ $sort: { title: 1 } }
|
|
28624
|
+
]).toArray();
|
|
28625
|
+
setCache(cacheKey, categories).then(() => {
|
|
28626
|
+
logger33.log({
|
|
28627
|
+
level: "info",
|
|
28628
|
+
message: `Cache set for getUnitBy: ${cacheKey}`
|
|
28629
|
+
});
|
|
28630
|
+
}).catch((err) => {
|
|
28631
|
+
logger33.log({
|
|
28632
|
+
level: "error",
|
|
28633
|
+
message: `Failed to set cache for getUnitBy: ${err.message}`
|
|
28634
|
+
});
|
|
28635
|
+
});
|
|
28636
|
+
return categories;
|
|
28637
|
+
} catch (error) {
|
|
28638
|
+
console.log(error);
|
|
28639
|
+
if (error instanceof BadRequestError66) {
|
|
28640
|
+
throw error;
|
|
28641
|
+
}
|
|
28642
|
+
throw new BadRequestError66(
|
|
28643
|
+
"Failed to retrieve asset unit of measurements."
|
|
28644
|
+
);
|
|
28463
28645
|
}
|
|
28464
28646
|
}
|
|
28465
28647
|
return {
|
|
@@ -28468,30 +28650,37 @@ function useInventoryRepo() {
|
|
|
28468
28650
|
updateById,
|
|
28469
28651
|
deleteById,
|
|
28470
28652
|
getById,
|
|
28471
|
-
getAll
|
|
28653
|
+
getAll,
|
|
28654
|
+
getCategories,
|
|
28655
|
+
getTypes,
|
|
28656
|
+
getUnitsBySchool
|
|
28472
28657
|
};
|
|
28473
28658
|
}
|
|
28474
28659
|
|
|
28475
|
-
// src/controllers/
|
|
28660
|
+
// src/controllers/asset.controller.ts
|
|
28476
28661
|
import { BadRequestError as BadRequestError67 } from "@eeplatform/nodejs-utils";
|
|
28477
28662
|
import Joi37 from "joi";
|
|
28478
|
-
function
|
|
28663
|
+
function useAssetController() {
|
|
28479
28664
|
const {
|
|
28480
28665
|
add: _add,
|
|
28481
28666
|
getAll: _getAll,
|
|
28482
28667
|
deleteById: _deleteById,
|
|
28483
|
-
updateById: _updateById
|
|
28484
|
-
|
|
28668
|
+
updateById: _updateById,
|
|
28669
|
+
getById: _getById,
|
|
28670
|
+
getCategories: _getCategories,
|
|
28671
|
+
getTypes: _getTypes,
|
|
28672
|
+
getUnitsBySchool: _getUnitsBySchool
|
|
28673
|
+
} = useAssetRepo();
|
|
28485
28674
|
async function add(req, res, next) {
|
|
28486
28675
|
const value = req.body;
|
|
28487
|
-
const { error } =
|
|
28676
|
+
const { error } = schemaAsset.validate(value);
|
|
28488
28677
|
if (error) {
|
|
28489
28678
|
next(new BadRequestError67(error.message));
|
|
28490
28679
|
return;
|
|
28491
28680
|
}
|
|
28492
28681
|
try {
|
|
28493
28682
|
const id = await _add(value);
|
|
28494
|
-
res.json({ message: "Successfully added
|
|
28683
|
+
res.json({ message: "Successfully added asset item.", id });
|
|
28495
28684
|
} catch (error2) {
|
|
28496
28685
|
next(error2);
|
|
28497
28686
|
}
|
|
@@ -28503,7 +28692,8 @@ function useInventoryController() {
|
|
|
28503
28692
|
limit: Joi37.number().min(1).optional().allow("", null),
|
|
28504
28693
|
search: Joi37.string().optional().allow("", null),
|
|
28505
28694
|
status: Joi37.string().optional().allow("", null),
|
|
28506
|
-
school: Joi37.string().hex().optional().allow("", null)
|
|
28695
|
+
school: Joi37.string().hex().optional().allow("", null),
|
|
28696
|
+
asset_type: Joi37.string().required().allow("supply", "furniture-equipment", "fixed-asset")
|
|
28507
28697
|
});
|
|
28508
28698
|
const { error } = validation.validate(query);
|
|
28509
28699
|
const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
|
|
@@ -28511,6 +28701,7 @@ function useInventoryController() {
|
|
|
28511
28701
|
const search = req.query.search ?? "";
|
|
28512
28702
|
const status = req.query.status ?? "active";
|
|
28513
28703
|
const school = req.query.school ?? "";
|
|
28704
|
+
const asset_type = req.query.asset_type ?? "supply";
|
|
28514
28705
|
const isPageNumber = isFinite(page);
|
|
28515
28706
|
if (!isPageNumber) {
|
|
28516
28707
|
next(new BadRequestError67("Invalid page number."));
|
|
@@ -28526,7 +28717,14 @@ function useInventoryController() {
|
|
|
28526
28717
|
return;
|
|
28527
28718
|
}
|
|
28528
28719
|
try {
|
|
28529
|
-
const regions = await _getAll({
|
|
28720
|
+
const regions = await _getAll({
|
|
28721
|
+
page,
|
|
28722
|
+
limit,
|
|
28723
|
+
search,
|
|
28724
|
+
status,
|
|
28725
|
+
school,
|
|
28726
|
+
asset_type
|
|
28727
|
+
});
|
|
28530
28728
|
res.json(regions);
|
|
28531
28729
|
return;
|
|
28532
28730
|
} catch (error2) {
|
|
@@ -28543,7 +28741,22 @@ function useInventoryController() {
|
|
|
28543
28741
|
}
|
|
28544
28742
|
try {
|
|
28545
28743
|
await _deleteById(id);
|
|
28546
|
-
res.json({ message: "Successfully deleted
|
|
28744
|
+
res.json({ message: "Successfully deleted asset item.", id });
|
|
28745
|
+
} catch (error2) {
|
|
28746
|
+
next(error2);
|
|
28747
|
+
}
|
|
28748
|
+
}
|
|
28749
|
+
async function getById(req, res, next) {
|
|
28750
|
+
const id = req.params.id;
|
|
28751
|
+
const validation = Joi37.string().hex().required();
|
|
28752
|
+
const { error } = validation.validate(id);
|
|
28753
|
+
if (error) {
|
|
28754
|
+
next(new BadRequestError67(error.message));
|
|
28755
|
+
return;
|
|
28756
|
+
}
|
|
28757
|
+
try {
|
|
28758
|
+
const data = await _getById(id);
|
|
28759
|
+
res.json(data);
|
|
28547
28760
|
} catch (error2) {
|
|
28548
28761
|
next(error2);
|
|
28549
28762
|
}
|
|
@@ -28551,14 +28764,61 @@ function useInventoryController() {
|
|
|
28551
28764
|
async function updateById(req, res, next) {
|
|
28552
28765
|
const id = req.params.id;
|
|
28553
28766
|
const value = req.body;
|
|
28554
|
-
const { error } =
|
|
28767
|
+
const { error } = schemaAssetUpdateOption.validate(value);
|
|
28555
28768
|
if (error) {
|
|
28556
28769
|
next(new BadRequestError67(error.message));
|
|
28557
28770
|
return;
|
|
28558
28771
|
}
|
|
28559
28772
|
try {
|
|
28560
28773
|
await _updateById(id, value);
|
|
28561
|
-
res.json({ message: "Successfully updated
|
|
28774
|
+
res.json({ message: "Successfully updated asset item.", id });
|
|
28775
|
+
} catch (error2) {
|
|
28776
|
+
next(error2);
|
|
28777
|
+
}
|
|
28778
|
+
}
|
|
28779
|
+
async function getCategories(req, res, next) {
|
|
28780
|
+
const school = req.params.school;
|
|
28781
|
+
const asset_type = req.params.asset_type;
|
|
28782
|
+
const validation = Joi37.string().hex().required();
|
|
28783
|
+
const { error } = validation.validate(school);
|
|
28784
|
+
if (error) {
|
|
28785
|
+
next(new BadRequestError67(error.message));
|
|
28786
|
+
return;
|
|
28787
|
+
}
|
|
28788
|
+
try {
|
|
28789
|
+
const data = await _getCategories(school, asset_type);
|
|
28790
|
+
res.json(data);
|
|
28791
|
+
} catch (error2) {
|
|
28792
|
+
next(error2);
|
|
28793
|
+
}
|
|
28794
|
+
}
|
|
28795
|
+
async function getTypes(req, res, next) {
|
|
28796
|
+
const school = req.params.school;
|
|
28797
|
+
const asset_type = req.params.asset_type;
|
|
28798
|
+
const validation = Joi37.string().hex().required();
|
|
28799
|
+
const { error } = validation.validate(school);
|
|
28800
|
+
if (error) {
|
|
28801
|
+
next(new BadRequestError67(error.message));
|
|
28802
|
+
return;
|
|
28803
|
+
}
|
|
28804
|
+
try {
|
|
28805
|
+
const data = await _getTypes(school, asset_type);
|
|
28806
|
+
res.json(data);
|
|
28807
|
+
} catch (error2) {
|
|
28808
|
+
next(error2);
|
|
28809
|
+
}
|
|
28810
|
+
}
|
|
28811
|
+
async function getUnitsBySchool(req, res, next) {
|
|
28812
|
+
const school = req.params.school;
|
|
28813
|
+
const validation = Joi37.string().hex().required();
|
|
28814
|
+
const { error } = validation.validate(school);
|
|
28815
|
+
if (error) {
|
|
28816
|
+
next(new BadRequestError67(error.message));
|
|
28817
|
+
return;
|
|
28818
|
+
}
|
|
28819
|
+
try {
|
|
28820
|
+
const data = await _getUnitsBySchool(school);
|
|
28821
|
+
res.json(data);
|
|
28562
28822
|
} catch (error2) {
|
|
28563
28823
|
next(error2);
|
|
28564
28824
|
}
|
|
@@ -28567,7 +28827,433 @@ function useInventoryController() {
|
|
|
28567
28827
|
add,
|
|
28568
28828
|
getAll,
|
|
28569
28829
|
deleteById,
|
|
28570
|
-
updateById
|
|
28830
|
+
updateById,
|
|
28831
|
+
getById,
|
|
28832
|
+
getCategories,
|
|
28833
|
+
getTypes,
|
|
28834
|
+
getUnitsBySchool
|
|
28835
|
+
};
|
|
28836
|
+
}
|
|
28837
|
+
|
|
28838
|
+
// src/models/stock-card.model.ts
|
|
28839
|
+
import { BadRequestError as BadRequestError68 } from "@eeplatform/nodejs-utils";
|
|
28840
|
+
import Joi38 from "joi";
|
|
28841
|
+
import { ObjectId as ObjectId44 } from "mongodb";
|
|
28842
|
+
var schemaStockCard = Joi38.object({
|
|
28843
|
+
_id: Joi38.string().hex().optional().allow("", null),
|
|
28844
|
+
school: Joi38.string().hex().required(),
|
|
28845
|
+
item: Joi38.string().hex().required(),
|
|
28846
|
+
balance: Joi38.number().optional().allow(null, 0),
|
|
28847
|
+
qty: Joi38.number().required(),
|
|
28848
|
+
unitCost: Joi38.number().optional().allow(null, 0),
|
|
28849
|
+
totalCost: Joi38.number().optional().allow(null, 0),
|
|
28850
|
+
status: Joi38.string().required(),
|
|
28851
|
+
condition: Joi38.string().required(),
|
|
28852
|
+
supplier: Joi38.string().optional().allow("", null),
|
|
28853
|
+
location: Joi38.string().optional().allow("", null),
|
|
28854
|
+
locationName: Joi38.string().optional().allow("", null),
|
|
28855
|
+
reason: Joi38.string().optional().allow("", null),
|
|
28856
|
+
remarks: Joi38.string().optional().allow("", null),
|
|
28857
|
+
createdAt: Joi38.date().optional().allow("", null),
|
|
28858
|
+
updatedAt: Joi38.date().optional().allow("", null),
|
|
28859
|
+
deletedAt: Joi38.date().optional().allow("", null)
|
|
28860
|
+
});
|
|
28861
|
+
function MStockCard(value) {
|
|
28862
|
+
const { error } = schemaStockCard.validate(value);
|
|
28863
|
+
if (error) {
|
|
28864
|
+
throw new BadRequestError68(`Invalid stock card data: ${error.message}`);
|
|
28865
|
+
}
|
|
28866
|
+
if (value._id && typeof value._id === "string") {
|
|
28867
|
+
try {
|
|
28868
|
+
value._id = new ObjectId44(value._id);
|
|
28869
|
+
} catch (err) {
|
|
28870
|
+
throw new BadRequestError68("Invalid stock card ID.");
|
|
28871
|
+
}
|
|
28872
|
+
}
|
|
28873
|
+
try {
|
|
28874
|
+
value.item = new ObjectId44(value.item);
|
|
28875
|
+
} catch (err) {
|
|
28876
|
+
throw new BadRequestError68("Invalid item ID.");
|
|
28877
|
+
}
|
|
28878
|
+
try {
|
|
28879
|
+
value.school = new ObjectId44(value.school);
|
|
28880
|
+
} catch (err) {
|
|
28881
|
+
throw new BadRequestError68("Invalid school ID.");
|
|
28882
|
+
}
|
|
28883
|
+
return {
|
|
28884
|
+
_id: value._id ?? void 0,
|
|
28885
|
+
school: value.school,
|
|
28886
|
+
item: value.item,
|
|
28887
|
+
balance: value.balance,
|
|
28888
|
+
qty: value.qty,
|
|
28889
|
+
unitCost: value.unitCost,
|
|
28890
|
+
totalCost: value.totalCost,
|
|
28891
|
+
status: value.status,
|
|
28892
|
+
condition: value.condition,
|
|
28893
|
+
supplier: value.supplier ?? "",
|
|
28894
|
+
location: value.location ?? void 0,
|
|
28895
|
+
locationName: value.locationName ?? "",
|
|
28896
|
+
reason: value.reason ?? "",
|
|
28897
|
+
remarks: value.remarks ?? "",
|
|
28898
|
+
createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
|
|
28899
|
+
updatedAt: value.updatedAt ?? "",
|
|
28900
|
+
deletedAt: value.deletedAt ?? ""
|
|
28901
|
+
};
|
|
28902
|
+
}
|
|
28903
|
+
|
|
28904
|
+
// src/repositories/stock-card.repository.ts
|
|
28905
|
+
import {
|
|
28906
|
+
BadRequestError as BadRequestError69,
|
|
28907
|
+
logger as logger34,
|
|
28908
|
+
makeCacheKey as makeCacheKey22,
|
|
28909
|
+
paginate as paginate18,
|
|
28910
|
+
useAtlas as useAtlas36,
|
|
28911
|
+
useCache as useCache23
|
|
28912
|
+
} from "@eeplatform/nodejs-utils";
|
|
28913
|
+
import { ObjectId as ObjectId45 } from "mongodb";
|
|
28914
|
+
function useStockCardRepository() {
|
|
28915
|
+
const db = useAtlas36.getDb();
|
|
28916
|
+
if (!db) {
|
|
28917
|
+
throw new BadRequestError69("Unable to connect to server.");
|
|
28918
|
+
}
|
|
28919
|
+
const namespace_collection = "school.assets.stock-cards";
|
|
28920
|
+
const collection = db.collection(namespace_collection);
|
|
28921
|
+
const { getCache, setCache, delNamespace } = useCache23(namespace_collection);
|
|
28922
|
+
function delCachedData() {
|
|
28923
|
+
delNamespace().then(() => {
|
|
28924
|
+
logger34.log({
|
|
28925
|
+
level: "info",
|
|
28926
|
+
message: `Cache namespace cleared for ${namespace_collection}`
|
|
28927
|
+
});
|
|
28928
|
+
}).catch((err) => {
|
|
28929
|
+
logger34.log({
|
|
28930
|
+
level: "error",
|
|
28931
|
+
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
28932
|
+
});
|
|
28933
|
+
});
|
|
28934
|
+
}
|
|
28935
|
+
async function createIndex() {
|
|
28936
|
+
try {
|
|
28937
|
+
await collection.createIndexes([
|
|
28938
|
+
{ key: { school: 1 } },
|
|
28939
|
+
{ key: { item: 1 } }
|
|
28940
|
+
]);
|
|
28941
|
+
} catch (error) {
|
|
28942
|
+
throw new BadRequestError69("Failed to create index on stock card.");
|
|
28943
|
+
}
|
|
28944
|
+
}
|
|
28945
|
+
async function add(value, session) {
|
|
28946
|
+
try {
|
|
28947
|
+
value = MStockCard(value);
|
|
28948
|
+
const res = await collection.insertOne(value, { session });
|
|
28949
|
+
delCachedData();
|
|
28950
|
+
return res.insertedId;
|
|
28951
|
+
} catch (error) {
|
|
28952
|
+
throw new BadRequestError69("Failed to create stock card.");
|
|
28953
|
+
}
|
|
28954
|
+
}
|
|
28955
|
+
async function getById(_id) {
|
|
28956
|
+
try {
|
|
28957
|
+
_id = new ObjectId45(_id);
|
|
28958
|
+
} catch (error) {
|
|
28959
|
+
throw new BadRequestError69("Invalid ID.");
|
|
28960
|
+
}
|
|
28961
|
+
const cacheKey = makeCacheKey22(namespace_collection, { _id: String(_id) });
|
|
28962
|
+
const cachedData = await getCache(cacheKey);
|
|
28963
|
+
if (cachedData) {
|
|
28964
|
+
return cachedData;
|
|
28965
|
+
}
|
|
28966
|
+
try {
|
|
28967
|
+
const res = await collection.findOne({ _id });
|
|
28968
|
+
if (!res) {
|
|
28969
|
+
throw new BadRequestError69("Asset item not found.");
|
|
28970
|
+
}
|
|
28971
|
+
setCache(cacheKey, res).then(() => {
|
|
28972
|
+
logger34.log({
|
|
28973
|
+
level: "info",
|
|
28974
|
+
message: `Cache set for stock card by ID: ${cacheKey}`
|
|
28975
|
+
});
|
|
28976
|
+
}).catch((err) => {
|
|
28977
|
+
logger34.log({
|
|
28978
|
+
level: "error",
|
|
28979
|
+
message: `Failed to set cache for stock card by ID: ${cacheKey} - ${err.message}`
|
|
28980
|
+
});
|
|
28981
|
+
});
|
|
28982
|
+
return res;
|
|
28983
|
+
} catch (error) {
|
|
28984
|
+
if (error instanceof BadRequestError69) {
|
|
28985
|
+
throw error;
|
|
28986
|
+
}
|
|
28987
|
+
throw new BadRequestError69("Failed to retrieve stock card.");
|
|
28988
|
+
}
|
|
28989
|
+
}
|
|
28990
|
+
async function getAll({ page = 1, limit = 20, school = "", sort = { _id: 1 }, id = "" } = {}) {
|
|
28991
|
+
page = page ? page - 1 : 0;
|
|
28992
|
+
try {
|
|
28993
|
+
school = new ObjectId45(school);
|
|
28994
|
+
} catch (error) {
|
|
28995
|
+
throw new BadRequestError69("Invalid school ID.");
|
|
28996
|
+
}
|
|
28997
|
+
try {
|
|
28998
|
+
id = new ObjectId45(id);
|
|
28999
|
+
} catch (error) {
|
|
29000
|
+
throw new BadRequestError69("Invalid ID.");
|
|
29001
|
+
}
|
|
29002
|
+
const query = {
|
|
29003
|
+
school,
|
|
29004
|
+
item: id
|
|
29005
|
+
};
|
|
29006
|
+
const cacheKeyOptions = {
|
|
29007
|
+
page,
|
|
29008
|
+
limit,
|
|
29009
|
+
school: String(school),
|
|
29010
|
+
sort: JSON.stringify(sort),
|
|
29011
|
+
item: id
|
|
29012
|
+
};
|
|
29013
|
+
try {
|
|
29014
|
+
const cacheKey = makeCacheKey22(namespace_collection, cacheKeyOptions);
|
|
29015
|
+
const cached = await getCache(cacheKey);
|
|
29016
|
+
if (cached) {
|
|
29017
|
+
logger34.log({
|
|
29018
|
+
level: "info",
|
|
29019
|
+
message: `Cache hit for getAll stock cards: ${cacheKey}`
|
|
29020
|
+
});
|
|
29021
|
+
return cached;
|
|
29022
|
+
}
|
|
29023
|
+
const items = await collection.aggregate([
|
|
29024
|
+
{ $match: query },
|
|
29025
|
+
{
|
|
29026
|
+
$sort: sort
|
|
29027
|
+
},
|
|
29028
|
+
{
|
|
29029
|
+
$skip: page * limit
|
|
29030
|
+
},
|
|
29031
|
+
{
|
|
29032
|
+
$limit: limit
|
|
29033
|
+
}
|
|
29034
|
+
]).toArray();
|
|
29035
|
+
const length = await collection.countDocuments(query);
|
|
29036
|
+
const data = paginate18(items, page, limit, length);
|
|
29037
|
+
setCache(cacheKey, data, 500).then(() => {
|
|
29038
|
+
logger34.log({
|
|
29039
|
+
level: "info",
|
|
29040
|
+
message: `Cache set for getAll stock cards: ${cacheKey}`
|
|
29041
|
+
});
|
|
29042
|
+
}).catch((err) => {
|
|
29043
|
+
logger34.log({
|
|
29044
|
+
level: "error",
|
|
29045
|
+
message: `Failed to set cache for getAll stock cards: ${err.message}`
|
|
29046
|
+
});
|
|
29047
|
+
});
|
|
29048
|
+
return data;
|
|
29049
|
+
} catch (error) {
|
|
29050
|
+
console.log("Error in getAll:", error);
|
|
29051
|
+
throw new BadRequestError69("Failed to retrieve stock cards.");
|
|
29052
|
+
}
|
|
29053
|
+
}
|
|
29054
|
+
async function getSuppliers(school) {
|
|
29055
|
+
try {
|
|
29056
|
+
school = new ObjectId45(school);
|
|
29057
|
+
} catch (error) {
|
|
29058
|
+
throw new BadRequestError69("Invalid school ID.");
|
|
29059
|
+
}
|
|
29060
|
+
const cacheKey = makeCacheKey22(namespace_collection, {
|
|
29061
|
+
school,
|
|
29062
|
+
type: "supplier-as-options"
|
|
29063
|
+
});
|
|
29064
|
+
const cachedData = await getCache(cacheKey);
|
|
29065
|
+
if (cachedData) {
|
|
29066
|
+
return cachedData;
|
|
29067
|
+
}
|
|
29068
|
+
try {
|
|
29069
|
+
const suppliers = await collection.aggregate([
|
|
29070
|
+
{
|
|
29071
|
+
$match: { school }
|
|
29072
|
+
},
|
|
29073
|
+
{
|
|
29074
|
+
$group: {
|
|
29075
|
+
_id: "$supplier",
|
|
29076
|
+
value: { $first: "$supplier" },
|
|
29077
|
+
title: {
|
|
29078
|
+
$first: "$supplier"
|
|
29079
|
+
}
|
|
29080
|
+
}
|
|
29081
|
+
},
|
|
29082
|
+
{
|
|
29083
|
+
$project: {
|
|
29084
|
+
_id: 0,
|
|
29085
|
+
title: 1,
|
|
29086
|
+
value: 1
|
|
29087
|
+
}
|
|
29088
|
+
},
|
|
29089
|
+
{ $sort: { title: 1 } }
|
|
29090
|
+
]).toArray();
|
|
29091
|
+
setCache(cacheKey, suppliers).then(() => {
|
|
29092
|
+
logger34.log({
|
|
29093
|
+
level: "info",
|
|
29094
|
+
message: `Cache set for getSuppliersBySchool: ${cacheKey}`
|
|
29095
|
+
});
|
|
29096
|
+
}).catch((err) => {
|
|
29097
|
+
logger34.log({
|
|
29098
|
+
level: "error",
|
|
29099
|
+
message: `Failed to set cache for getSuppliersBySchool: ${err.message}`
|
|
29100
|
+
});
|
|
29101
|
+
});
|
|
29102
|
+
return suppliers;
|
|
29103
|
+
} catch (error) {
|
|
29104
|
+
if (error instanceof BadRequestError69) {
|
|
29105
|
+
throw error;
|
|
29106
|
+
}
|
|
29107
|
+
throw new BadRequestError69("Failed to retrieve asset suppliers.");
|
|
29108
|
+
}
|
|
29109
|
+
}
|
|
29110
|
+
return {
|
|
29111
|
+
createIndex,
|
|
29112
|
+
add,
|
|
29113
|
+
getById,
|
|
29114
|
+
getAll,
|
|
29115
|
+
getSuppliers
|
|
29116
|
+
};
|
|
29117
|
+
}
|
|
29118
|
+
|
|
29119
|
+
// src/controllers/stock-card.controller.ts
|
|
29120
|
+
import { BadRequestError as BadRequestError71 } from "@eeplatform/nodejs-utils";
|
|
29121
|
+
import Joi39 from "joi";
|
|
29122
|
+
|
|
29123
|
+
// src/services/stock-card.service.ts
|
|
29124
|
+
import {
|
|
29125
|
+
BadRequestError as BadRequestError70,
|
|
29126
|
+
NotFoundError as NotFoundError11,
|
|
29127
|
+
useAtlas as useAtlas37
|
|
29128
|
+
} from "@eeplatform/nodejs-utils";
|
|
29129
|
+
function useStockCardService() {
|
|
29130
|
+
const { add: _add, getById: _getById } = useStockCardRepository();
|
|
29131
|
+
const { getById: _getAssetById, updateById: updateAssetById } = useAssetRepo();
|
|
29132
|
+
async function add(data) {
|
|
29133
|
+
const session = useAtlas37.getClient()?.startSession();
|
|
29134
|
+
if (!session) {
|
|
29135
|
+
throw new BadRequestError70("Unable to start database session.");
|
|
29136
|
+
}
|
|
29137
|
+
session.startTransaction();
|
|
29138
|
+
try {
|
|
29139
|
+
const asset = await _getAssetById(data.item);
|
|
29140
|
+
if (!asset) {
|
|
29141
|
+
throw new NotFoundError11("Asset not found.");
|
|
29142
|
+
}
|
|
29143
|
+
data.balance = (asset.qty ?? 0) + data.qty;
|
|
29144
|
+
await _add(data, session);
|
|
29145
|
+
await updateAssetById(data.item, { qty: data.balance }, session);
|
|
29146
|
+
await session.commitTransaction();
|
|
29147
|
+
return "Successfully added stock card.";
|
|
29148
|
+
} catch (error) {
|
|
29149
|
+
await session.abortTransaction();
|
|
29150
|
+
throw new BadRequestError70("Failed to add stock card.");
|
|
29151
|
+
} finally {
|
|
29152
|
+
session.endSession();
|
|
29153
|
+
}
|
|
29154
|
+
}
|
|
29155
|
+
return {
|
|
29156
|
+
add
|
|
29157
|
+
};
|
|
29158
|
+
}
|
|
29159
|
+
|
|
29160
|
+
// src/controllers/stock-card.controller.ts
|
|
29161
|
+
function useStockCardController() {
|
|
29162
|
+
const {
|
|
29163
|
+
getAll: _getAll,
|
|
29164
|
+
getById: _getById,
|
|
29165
|
+
getSuppliers: _getSuppliers
|
|
29166
|
+
} = useStockCardRepository();
|
|
29167
|
+
const { add: _add } = useStockCardService();
|
|
29168
|
+
async function add(req, res, next) {
|
|
29169
|
+
const value = req.body;
|
|
29170
|
+
const { error } = schemaStockCard.validate(value);
|
|
29171
|
+
if (error) {
|
|
29172
|
+
next(new BadRequestError71(error.message));
|
|
29173
|
+
return;
|
|
29174
|
+
}
|
|
29175
|
+
try {
|
|
29176
|
+
const id = await _add(value);
|
|
29177
|
+
res.json({ message: "Successfully added asset item.", id });
|
|
29178
|
+
} catch (error2) {
|
|
29179
|
+
next(error2);
|
|
29180
|
+
}
|
|
29181
|
+
}
|
|
29182
|
+
async function getAll(req, res, next) {
|
|
29183
|
+
const query = req.query;
|
|
29184
|
+
const validation = Joi39.object({
|
|
29185
|
+
page: Joi39.number().min(1).optional().allow("", null),
|
|
29186
|
+
limit: Joi39.number().min(1).optional().allow("", null),
|
|
29187
|
+
school: Joi39.string().hex().optional().allow("", null),
|
|
29188
|
+
id: Joi39.string().hex().required()
|
|
29189
|
+
});
|
|
29190
|
+
const { error } = validation.validate(query);
|
|
29191
|
+
const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
|
|
29192
|
+
const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
|
|
29193
|
+
const school = req.query.school ?? "";
|
|
29194
|
+
const id = req.query.id ?? "supply";
|
|
29195
|
+
const isPageNumber = isFinite(page);
|
|
29196
|
+
if (!isPageNumber) {
|
|
29197
|
+
next(new BadRequestError71("Invalid page number."));
|
|
29198
|
+
return;
|
|
29199
|
+
}
|
|
29200
|
+
const isLimitNumber = isFinite(limit);
|
|
29201
|
+
if (!isLimitNumber) {
|
|
29202
|
+
next(new BadRequestError71("Invalid limit number."));
|
|
29203
|
+
return;
|
|
29204
|
+
}
|
|
29205
|
+
if (error) {
|
|
29206
|
+
next(new BadRequestError71(error.message));
|
|
29207
|
+
return;
|
|
29208
|
+
}
|
|
29209
|
+
try {
|
|
29210
|
+
const regions = await _getAll({
|
|
29211
|
+
page,
|
|
29212
|
+
limit,
|
|
29213
|
+
id,
|
|
29214
|
+
school
|
|
29215
|
+
});
|
|
29216
|
+
res.json(regions);
|
|
29217
|
+
return;
|
|
29218
|
+
} catch (error2) {
|
|
29219
|
+
next(error2);
|
|
29220
|
+
}
|
|
29221
|
+
}
|
|
29222
|
+
async function getById(req, res, next) {
|
|
29223
|
+
const id = req.params.id;
|
|
29224
|
+
const validation = Joi39.string().hex().required();
|
|
29225
|
+
const { error } = validation.validate(id);
|
|
29226
|
+
if (error) {
|
|
29227
|
+
next(new BadRequestError71(error.message));
|
|
29228
|
+
return;
|
|
29229
|
+
}
|
|
29230
|
+
try {
|
|
29231
|
+
const data = await _getById(id);
|
|
29232
|
+
res.json(data);
|
|
29233
|
+
} catch (error2) {
|
|
29234
|
+
next(error2);
|
|
29235
|
+
}
|
|
29236
|
+
}
|
|
29237
|
+
async function getSuppliers(req, res, next) {
|
|
29238
|
+
const school = req.params.school;
|
|
29239
|
+
const validation = Joi39.string().hex().required();
|
|
29240
|
+
const { error } = validation.validate(school);
|
|
29241
|
+
if (error) {
|
|
29242
|
+
next(new BadRequestError71(error.message));
|
|
29243
|
+
return;
|
|
29244
|
+
}
|
|
29245
|
+
try {
|
|
29246
|
+
const data = await _getSuppliers(school);
|
|
29247
|
+
res.json(data);
|
|
29248
|
+
} catch (error2) {
|
|
29249
|
+
next(error2);
|
|
29250
|
+
}
|
|
29251
|
+
}
|
|
29252
|
+
return {
|
|
29253
|
+
add,
|
|
29254
|
+
getAll,
|
|
29255
|
+
getById,
|
|
29256
|
+
getSuppliers
|
|
28571
29257
|
};
|
|
28572
29258
|
}
|
|
28573
29259
|
export {
|
|
@@ -28588,12 +29274,12 @@ export {
|
|
|
28588
29274
|
MAILER_TRANSPORT_PORT,
|
|
28589
29275
|
MAILER_TRANSPORT_SECURE,
|
|
28590
29276
|
MAddress,
|
|
29277
|
+
MAsset,
|
|
28591
29278
|
MBuilding,
|
|
28592
29279
|
MBuildingUnit,
|
|
28593
29280
|
MDivision,
|
|
28594
29281
|
MEntity,
|
|
28595
29282
|
MFile,
|
|
28596
|
-
MInventory,
|
|
28597
29283
|
MMember,
|
|
28598
29284
|
MONGO_DB,
|
|
28599
29285
|
MONGO_URI,
|
|
@@ -28604,6 +29290,7 @@ export {
|
|
|
28604
29290
|
MRegion,
|
|
28605
29291
|
MRole,
|
|
28606
29292
|
MSchool,
|
|
29293
|
+
MStockCard,
|
|
28607
29294
|
MSubscription,
|
|
28608
29295
|
MToken,
|
|
28609
29296
|
MUser,
|
|
@@ -28631,16 +29318,19 @@ export {
|
|
|
28631
29318
|
addressSchema,
|
|
28632
29319
|
isDev,
|
|
28633
29320
|
schema,
|
|
29321
|
+
schemaAsset,
|
|
29322
|
+
schemaAssetUpdateOption,
|
|
28634
29323
|
schemaBuilding,
|
|
28635
29324
|
schemaBuildingUnit,
|
|
28636
29325
|
schemaDivision,
|
|
28637
|
-
schemaInventory,
|
|
28638
|
-
schemaInventoryUpdateOption,
|
|
28639
29326
|
schemaRegion,
|
|
28640
29327
|
schemaSchool,
|
|
29328
|
+
schemaStockCard,
|
|
28641
29329
|
schemaUpdateOptions,
|
|
28642
29330
|
useAddressController,
|
|
28643
29331
|
useAddressRepo,
|
|
29332
|
+
useAssetController,
|
|
29333
|
+
useAssetRepo,
|
|
28644
29334
|
useAuthController,
|
|
28645
29335
|
useAuthService,
|
|
28646
29336
|
useBuildingController,
|
|
@@ -28657,8 +29347,6 @@ export {
|
|
|
28657
29347
|
useFileController,
|
|
28658
29348
|
useFileRepo,
|
|
28659
29349
|
useFileService,
|
|
28660
|
-
useInventoryController,
|
|
28661
|
-
useInventoryRepo,
|
|
28662
29350
|
useInvoiceController,
|
|
28663
29351
|
useInvoiceModel,
|
|
28664
29352
|
useInvoiceRepo,
|
|
@@ -28690,6 +29378,8 @@ export {
|
|
|
28690
29378
|
useSchoolController,
|
|
28691
29379
|
useSchoolRepo,
|
|
28692
29380
|
useSchoolService,
|
|
29381
|
+
useStockCardController,
|
|
29382
|
+
useStockCardRepository,
|
|
28693
29383
|
useSubscriptionController,
|
|
28694
29384
|
useSubscriptionRepo,
|
|
28695
29385
|
useSubscriptionService,
|