@eeplatform/core 1.2.1 → 1.3.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/dist/index.mjs CHANGED
@@ -27066,6 +27066,7 @@ var schemaBuildingUnit = Joi33.object({
27066
27066
  var schemaUpdateOptions = Joi33.object({
27067
27067
  name: Joi33.string().optional().allow("", null),
27068
27068
  building: Joi33.string().hex().optional().allow("", null),
27069
+ buildingName: Joi33.string().optional().allow("", null),
27069
27070
  level: Joi33.number().integer().min(1).optional().allow("", null),
27070
27071
  category: Joi33.string().optional().allow("", null),
27071
27072
  type: Joi33.string().optional().allow("", null),
@@ -27492,6 +27493,36 @@ function useBuildingUnitRepo() {
27492
27493
  }
27493
27494
  }
27494
27495
  }
27496
+ async function updateByBuildingId(building, value, session) {
27497
+ const { error } = schemaUpdateOptions.validate(value);
27498
+ if (error) {
27499
+ throw new BadRequestError61(error.message);
27500
+ }
27501
+ try {
27502
+ building = new ObjectId41(building);
27503
+ } catch (error2) {
27504
+ throw new BadRequestError61("Invalid building ID.");
27505
+ }
27506
+ try {
27507
+ const res = await collection.updateMany(
27508
+ { building },
27509
+ { $set: value },
27510
+ { session }
27511
+ );
27512
+ delCachedData();
27513
+ return res;
27514
+ } catch (error2) {
27515
+ logger31.log({
27516
+ level: "error",
27517
+ message: error2.message
27518
+ });
27519
+ if (error2 instanceof AppError15) {
27520
+ throw error2;
27521
+ } else {
27522
+ throw new Error("Failed to update building unit.");
27523
+ }
27524
+ }
27525
+ }
27495
27526
  async function getAll({
27496
27527
  search = "",
27497
27528
  page = 1,
@@ -27739,7 +27770,8 @@ function useBuildingUnitRepo() {
27739
27770
  getByBuildingLevel,
27740
27771
  updateById,
27741
27772
  getByBuilding,
27742
- deleteById
27773
+ deleteById,
27774
+ updateByBuildingId
27743
27775
  };
27744
27776
  }
27745
27777
 
@@ -27748,16 +27780,21 @@ import { BadRequestError as BadRequestError63, logger as logger32 } from "@eepla
27748
27780
  import Joi34 from "joi";
27749
27781
 
27750
27782
  // src/services/building.service.ts
27751
- import { BadRequestError as BadRequestError62, NotFoundError as NotFoundError11 } from "@eeplatform/nodejs-utils";
27783
+ import {
27784
+ BadRequestError as BadRequestError62,
27785
+ NotFoundError as NotFoundError11,
27786
+ useAtlas as useAtlas33
27787
+ } from "@eeplatform/nodejs-utils";
27752
27788
  function useBuildingService() {
27753
27789
  const {
27754
27790
  updateById: _updateById,
27755
27791
  getById: _getById,
27756
27792
  deleteById: _deleteById
27757
27793
  } = useBuildingRepo();
27758
- const { getByBuildingLevel, getByBuilding } = useBuildingUnitRepo();
27794
+ const { getByBuildingLevel, getByBuilding, updateByBuildingId } = useBuildingUnitRepo();
27759
27795
  async function updateById(id, data) {
27760
27796
  data.levels = Number(data.levels);
27797
+ const session = useAtlas33.getClient()?.startSession();
27761
27798
  try {
27762
27799
  const building = await _getById(id);
27763
27800
  if (!building) {
@@ -27771,10 +27808,18 @@ function useBuildingService() {
27771
27808
  );
27772
27809
  }
27773
27810
  }
27774
- const result = await _updateById(id, data);
27811
+ session?.startTransaction();
27812
+ if (building.name !== data.name) {
27813
+ await updateByBuildingId(id, { buildingName: data.name }, session);
27814
+ }
27815
+ const result = await _updateById(id, data, session);
27816
+ await session?.commitTransaction();
27775
27817
  return result;
27776
27818
  } catch (error) {
27819
+ await session?.abortTransaction();
27777
27820
  throw error;
27821
+ } finally {
27822
+ session?.endSession();
27778
27823
  }
27779
27824
  }
27780
27825
  async function deleteById(id) {
@@ -27945,11 +27990,11 @@ import { BadRequestError as BadRequestError64 } from "@eeplatform/nodejs-utils";
27945
27990
  import Joi35 from "joi";
27946
27991
 
27947
27992
  // src/services/building-unit.service.ts
27948
- import { useAtlas as useAtlas33 } from "@eeplatform/nodejs-utils";
27993
+ import { useAtlas as useAtlas34 } from "@eeplatform/nodejs-utils";
27949
27994
  function useBuildingUnitService() {
27950
27995
  const { add: _add } = useBuildingUnitRepo();
27951
27996
  async function add(value) {
27952
- const session = useAtlas33.getClient()?.startSession();
27997
+ const session = useAtlas34.getClient()?.startSession();
27953
27998
  if (!session) {
27954
27999
  throw new Error("Unable to start session for building unit service.");
27955
28000
  }
@@ -28131,6 +28176,400 @@ function useBuildingUnitController() {
28131
28176
  deleteById
28132
28177
  };
28133
28178
  }
28179
+
28180
+ // src/models/inventory.model.ts
28181
+ import { BadRequestError as BadRequestError65 } from "@eeplatform/nodejs-utils";
28182
+ import Joi36 from "joi";
28183
+ import { ObjectId as ObjectId42 } from "mongodb";
28184
+ var schemaInventory = Joi36.object({
28185
+ _id: Joi36.string().hex().optional(),
28186
+ school: Joi36.string().hex().required(),
28187
+ name: Joi36.string().required(),
28188
+ category: Joi36.string().optional().allow("", null),
28189
+ type: Joi36.string().optional().allow("", null),
28190
+ brand: Joi36.string().optional().allow("", null),
28191
+ unit_of_measurement_category: Joi36.string().required(),
28192
+ unit_of_measurement_type: Joi36.string().required(),
28193
+ status: Joi36.string().optional().allow("", null),
28194
+ createdAt: Joi36.date().optional().allow("", null),
28195
+ updatedAt: Joi36.date().optional().allow("", null),
28196
+ deletedAt: Joi36.date().optional().allow("", null),
28197
+ metadata: Joi36.object({
28198
+ title: Joi36.string().optional().allow("", null),
28199
+ isbn: Joi36.string().optional().allow("", null),
28200
+ author: Joi36.string().optional().allow("", null),
28201
+ edition: Joi36.string().optional().allow("", null),
28202
+ subject: Joi36.string().optional().allow("", null),
28203
+ grade_level: Joi36.number().integer().min(0).optional().allow("", null),
28204
+ publisher: Joi36.string().optional().allow("", null),
28205
+ language: Joi36.string().optional().allow("", null)
28206
+ }).optional().allow(null)
28207
+ });
28208
+ var schemaInventoryUpdateOption = Joi36.object({
28209
+ name: Joi36.string().optional().allow("", null),
28210
+ category: Joi36.string().optional().allow("", null),
28211
+ type: Joi36.string().optional().allow("", null),
28212
+ brand: Joi36.string().optional().allow("", null),
28213
+ qty: Joi36.number().integer().min(0).optional().allow("", null),
28214
+ unit_of_measurement_category: Joi36.string().optional().allow("", null),
28215
+ unit_of_measurement_type: Joi36.string().optional().allow("", null),
28216
+ metadata: Joi36.object({
28217
+ title: Joi36.string().optional().allow("", null),
28218
+ isbn: Joi36.string().optional().allow("", null),
28219
+ author: Joi36.string().optional().allow("", null),
28220
+ edition: Joi36.string().optional().allow("", null),
28221
+ subject: Joi36.string().optional().allow("", null),
28222
+ grade_level: Joi36.number().integer().min(0).optional().allow("", null),
28223
+ publisher: Joi36.string().optional().allow("", null),
28224
+ language: Joi36.string().optional().allow("", null)
28225
+ }).optional().allow(null)
28226
+ });
28227
+ function MInventory(value) {
28228
+ const { error } = schemaInventory.validate(value);
28229
+ if (error) {
28230
+ throw new BadRequestError65(error.message);
28231
+ }
28232
+ if (value._id && typeof value._id === "string") {
28233
+ try {
28234
+ value._id = new ObjectId42();
28235
+ } catch (error2) {
28236
+ throw new BadRequestError65("Invalid ID.");
28237
+ }
28238
+ }
28239
+ try {
28240
+ value.school = new ObjectId42(value.school);
28241
+ } catch (error2) {
28242
+ throw new BadRequestError65("Invalid school ID.");
28243
+ }
28244
+ value.createdAt = value.createdAt ? new Date(value.createdAt) : /* @__PURE__ */ new Date();
28245
+ value.updatedAt = value.updatedAt ? new Date(value.updatedAt) : "";
28246
+ value.deletedAt = value.deletedAt ? new Date(value.deletedAt) : "";
28247
+ return {
28248
+ _id: value._id ?? new ObjectId42(),
28249
+ school: value.school,
28250
+ name: value.name,
28251
+ category: value.category ?? "",
28252
+ type: value.type ?? "",
28253
+ brand: value.brand ?? "",
28254
+ qty: value.qty ?? 0,
28255
+ condition: value.condition ?? {
28256
+ good: 0,
28257
+ disposal: 0,
28258
+ lost: 0,
28259
+ damaged: 0
28260
+ },
28261
+ unit_of_measurement_category: value.unit_of_measurement_category ?? "",
28262
+ unit_of_measurement_type: value.unit_of_measurement_type ?? "",
28263
+ status: value.status ?? "active",
28264
+ createdAt: value.createdAt,
28265
+ updatedAt: value.updatedAt,
28266
+ deletedAt: value.deletedAt,
28267
+ metadata: value.metadata ?? {}
28268
+ };
28269
+ }
28270
+
28271
+ // src/repositories/inventory.repository.ts
28272
+ import {
28273
+ BadRequestError as BadRequestError66,
28274
+ logger as logger33,
28275
+ makeCacheKey as makeCacheKey21,
28276
+ paginate as paginate17,
28277
+ useAtlas as useAtlas35,
28278
+ useCache as useCache22
28279
+ } from "@eeplatform/nodejs-utils";
28280
+ import { ObjectId as ObjectId43 } from "mongodb";
28281
+ function useInventoryRepo() {
28282
+ const db = useAtlas35.getDb();
28283
+ if (!db) {
28284
+ throw new BadRequestError66("Unable to connect to server.");
28285
+ }
28286
+ const namespace_collection = "school.inventories";
28287
+ const collection = db.collection(namespace_collection);
28288
+ const { getCache, setCache, delNamespace } = useCache22(namespace_collection);
28289
+ function delCachedData() {
28290
+ delNamespace().then(() => {
28291
+ logger33.log({
28292
+ level: "info",
28293
+ message: `Cache namespace cleared for ${namespace_collection}`
28294
+ });
28295
+ }).catch((err) => {
28296
+ logger33.log({
28297
+ level: "error",
28298
+ message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
28299
+ });
28300
+ });
28301
+ }
28302
+ async function createIndex() {
28303
+ try {
28304
+ await collection.createIndexes([
28305
+ { key: { school: 1 } },
28306
+ { key: { name: 1 } },
28307
+ { key: { name: "text" } }
28308
+ ]);
28309
+ } catch (error) {
28310
+ throw new BadRequestError66("Failed to create index on inventory.");
28311
+ }
28312
+ }
28313
+ async function add(value) {
28314
+ try {
28315
+ value = MInventory(value);
28316
+ const res = await collection.insertOne(value);
28317
+ delCachedData();
28318
+ return res.insertedId;
28319
+ } catch (error) {
28320
+ throw new BadRequestError66("Failed to create inventory item.");
28321
+ }
28322
+ }
28323
+ async function updateById(_id, value) {
28324
+ const { error } = schemaInventoryUpdateOption.validate(value);
28325
+ if (error) {
28326
+ throw new BadRequestError66(error.message);
28327
+ }
28328
+ try {
28329
+ _id = new ObjectId43(_id);
28330
+ } catch (error2) {
28331
+ throw new BadRequestError66("Invalid ID.");
28332
+ }
28333
+ try {
28334
+ const res = await collection.updateOne({ _id }, { $set: value });
28335
+ if (res.modifiedCount) {
28336
+ delCachedData();
28337
+ }
28338
+ return "Successfully updated inventory item.";
28339
+ } catch (error2) {
28340
+ throw new BadRequestError66("Failed to update inventory item.");
28341
+ }
28342
+ }
28343
+ async function deleteById(_id) {
28344
+ try {
28345
+ _id = new ObjectId43(_id);
28346
+ } catch (error) {
28347
+ throw new BadRequestError66("Invalid ID.");
28348
+ }
28349
+ try {
28350
+ const res = await collection.deleteOne({ _id });
28351
+ if (res.deletedCount) {
28352
+ delCachedData();
28353
+ return "Successfully deleted inventory item.";
28354
+ }
28355
+ return "Successfully deleted inventory item.";
28356
+ } catch (error) {
28357
+ throw new BadRequestError66("Failed to delete inventory item.");
28358
+ }
28359
+ }
28360
+ async function getById(_id) {
28361
+ try {
28362
+ _id = new ObjectId43(_id);
28363
+ } catch (error) {
28364
+ throw new BadRequestError66("Invalid ID.");
28365
+ }
28366
+ const cacheKey = makeCacheKey21(namespace_collection, { _id: String(_id) });
28367
+ const cachedData = await getCache(cacheKey);
28368
+ if (cachedData) {
28369
+ return cachedData;
28370
+ }
28371
+ try {
28372
+ const res = await collection.findOne({ _id });
28373
+ if (!res) {
28374
+ throw new BadRequestError66("Inventory item not found.");
28375
+ }
28376
+ setCache(cacheKey, res).then(() => {
28377
+ logger33.log({
28378
+ level: "info",
28379
+ message: `Cache set for inventory item by ID: ${cacheKey}`
28380
+ });
28381
+ }).catch((err) => {
28382
+ logger33.log({
28383
+ level: "error",
28384
+ message: `Failed to set cache for inventory item by ID: ${cacheKey} - ${err.message}`
28385
+ });
28386
+ });
28387
+ return res;
28388
+ } catch (error) {
28389
+ if (error instanceof BadRequestError66) {
28390
+ throw error;
28391
+ }
28392
+ throw new BadRequestError66("Failed to retrieve inventory item.");
28393
+ }
28394
+ }
28395
+ async function getAll({
28396
+ page = 1,
28397
+ search = "",
28398
+ limit = 20,
28399
+ status = "active",
28400
+ school = "",
28401
+ sort = { _id: -1 }
28402
+ } = {}) {
28403
+ page = page ? page - 1 : 0;
28404
+ try {
28405
+ school = new ObjectId43(school);
28406
+ } catch (error) {
28407
+ throw new BadRequestError66("Invalid school ID.");
28408
+ }
28409
+ const query = {
28410
+ school,
28411
+ status
28412
+ };
28413
+ const cacheKeyOptions = {
28414
+ page,
28415
+ limit,
28416
+ status,
28417
+ school: String(school),
28418
+ sort: JSON.stringify(sort)
28419
+ };
28420
+ if (search) {
28421
+ query.$text = { $search: search };
28422
+ cacheKeyOptions.search = search;
28423
+ }
28424
+ try {
28425
+ const cacheKey = makeCacheKey21(namespace_collection, cacheKeyOptions);
28426
+ const cached = await getCache(cacheKey);
28427
+ if (cached) {
28428
+ logger33.log({
28429
+ level: "info",
28430
+ message: `Cache hit for getAll inventory items: ${cacheKey}`
28431
+ });
28432
+ return cached;
28433
+ }
28434
+ const items = await collection.aggregate([
28435
+ { $match: query },
28436
+ {
28437
+ $sort: sort
28438
+ },
28439
+ {
28440
+ $skip: page * limit
28441
+ },
28442
+ {
28443
+ $limit: limit
28444
+ }
28445
+ ]).toArray();
28446
+ const length = await collection.countDocuments(query);
28447
+ const data = paginate17(items, page, limit, length);
28448
+ setCache(cacheKey, data, 500).then(() => {
28449
+ logger33.log({
28450
+ level: "info",
28451
+ message: `Cache set for getAll inventory items: ${cacheKey}`
28452
+ });
28453
+ }).catch((err) => {
28454
+ logger33.log({
28455
+ level: "error",
28456
+ message: `Failed to set cache for getAll inventory items: ${err.message}`
28457
+ });
28458
+ });
28459
+ return data;
28460
+ } catch (error) {
28461
+ console.log("Error in getAll:", error);
28462
+ throw new BadRequestError66("Failed to retrieve inventory items.");
28463
+ }
28464
+ }
28465
+ return {
28466
+ createIndex,
28467
+ add,
28468
+ updateById,
28469
+ deleteById,
28470
+ getById,
28471
+ getAll
28472
+ };
28473
+ }
28474
+
28475
+ // src/controllers/inventory.controller.ts
28476
+ import { BadRequestError as BadRequestError67 } from "@eeplatform/nodejs-utils";
28477
+ import Joi37 from "joi";
28478
+ function useInventoryController() {
28479
+ const {
28480
+ add: _add,
28481
+ getAll: _getAll,
28482
+ deleteById: _deleteById,
28483
+ updateById: _updateById
28484
+ } = useInventoryRepo();
28485
+ async function add(req, res, next) {
28486
+ const value = req.body;
28487
+ const { error } = schemaInventory.validate(value);
28488
+ if (error) {
28489
+ next(new BadRequestError67(error.message));
28490
+ return;
28491
+ }
28492
+ try {
28493
+ const id = await _add(value);
28494
+ res.json({ message: "Successfully added inventory item.", id });
28495
+ } catch (error2) {
28496
+ next(error2);
28497
+ }
28498
+ }
28499
+ async function getAll(req, res, next) {
28500
+ const query = req.query;
28501
+ const validation = Joi37.object({
28502
+ page: Joi37.number().min(1).optional().allow("", null),
28503
+ limit: Joi37.number().min(1).optional().allow("", null),
28504
+ search: Joi37.string().optional().allow("", null),
28505
+ status: Joi37.string().optional().allow("", null),
28506
+ school: Joi37.string().hex().optional().allow("", null)
28507
+ });
28508
+ const { error } = validation.validate(query);
28509
+ const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
28510
+ const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
28511
+ const search = req.query.search ?? "";
28512
+ const status = req.query.status ?? "active";
28513
+ const school = req.query.school ?? "";
28514
+ const isPageNumber = isFinite(page);
28515
+ if (!isPageNumber) {
28516
+ next(new BadRequestError67("Invalid page number."));
28517
+ return;
28518
+ }
28519
+ const isLimitNumber = isFinite(limit);
28520
+ if (!isLimitNumber) {
28521
+ next(new BadRequestError67("Invalid limit number."));
28522
+ return;
28523
+ }
28524
+ if (error) {
28525
+ next(new BadRequestError67(error.message));
28526
+ return;
28527
+ }
28528
+ try {
28529
+ const regions = await _getAll({ page, limit, search, status, school });
28530
+ res.json(regions);
28531
+ return;
28532
+ } catch (error2) {
28533
+ next(error2);
28534
+ }
28535
+ }
28536
+ async function deleteById(req, res, next) {
28537
+ const id = req.params.id;
28538
+ const validation = Joi37.string().hex().required();
28539
+ const { error } = validation.validate(id);
28540
+ if (error) {
28541
+ next(new BadRequestError67(error.message));
28542
+ return;
28543
+ }
28544
+ try {
28545
+ await _deleteById(id);
28546
+ res.json({ message: "Successfully deleted inventory item.", id });
28547
+ } catch (error2) {
28548
+ next(error2);
28549
+ }
28550
+ }
28551
+ async function updateById(req, res, next) {
28552
+ const id = req.params.id;
28553
+ const value = req.body;
28554
+ const { error } = schemaInventoryUpdateOption.validate(value);
28555
+ if (error) {
28556
+ next(new BadRequestError67(error.message));
28557
+ return;
28558
+ }
28559
+ try {
28560
+ await _updateById(id, value);
28561
+ res.json({ message: "Successfully updated inventory item.", id });
28562
+ } catch (error2) {
28563
+ next(error2);
28564
+ }
28565
+ }
28566
+ return {
28567
+ add,
28568
+ getAll,
28569
+ deleteById,
28570
+ updateById
28571
+ };
28572
+ }
28134
28573
  export {
28135
28574
  ACCESS_TOKEN_EXPIRY,
28136
28575
  ACCESS_TOKEN_SECRET,
@@ -28154,6 +28593,7 @@ export {
28154
28593
  MDivision,
28155
28594
  MEntity,
28156
28595
  MFile,
28596
+ MInventory,
28157
28597
  MMember,
28158
28598
  MONGO_DB,
28159
28599
  MONGO_URI,
@@ -28194,6 +28634,8 @@ export {
28194
28634
  schemaBuilding,
28195
28635
  schemaBuildingUnit,
28196
28636
  schemaDivision,
28637
+ schemaInventory,
28638
+ schemaInventoryUpdateOption,
28197
28639
  schemaRegion,
28198
28640
  schemaSchool,
28199
28641
  schemaUpdateOptions,
@@ -28215,6 +28657,8 @@ export {
28215
28657
  useFileController,
28216
28658
  useFileRepo,
28217
28659
  useFileService,
28660
+ useInventoryController,
28661
+ useInventoryRepo,
28218
28662
  useInvoiceController,
28219
28663
  useInvoiceModel,
28220
28664
  useInvoiceRepo,