@eeplatform/core 1.2.0 → 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),
@@ -27167,26 +27168,17 @@ function useBuildingRepo() {
27167
27168
  const namespace_collection = "school.buildings";
27168
27169
  const collection = db.collection(namespace_collection);
27169
27170
  const { getCache, setCache, delNamespace } = useCache20(namespace_collection);
27170
- async function createIndex() {
27171
+ async function createIndexes() {
27171
27172
  try {
27172
- await collection.createIndex([
27173
- { name: 1 },
27174
- { school: 1 },
27175
- { createdAt: 1 }
27173
+ await collection.createIndexes([
27174
+ { key: { name: 1 }, unique: true, name: "unique_name_index" },
27175
+ { key: { school: 1 } },
27176
+ { key: { status: 1 } }
27176
27177
  ]);
27177
27178
  } catch (error) {
27178
27179
  throw new Error("Failed to create index on buildings.");
27179
27180
  }
27180
27181
  }
27181
- async function createTextIndex() {
27182
- try {
27183
- await collection.createIndex({
27184
- name: "text"
27185
- });
27186
- } catch (error) {
27187
- throw new Error("Failed to create text index on building name.");
27188
- }
27189
- }
27190
27182
  async function add(value, session) {
27191
27183
  try {
27192
27184
  value = MBuilding(value);
@@ -27386,8 +27378,7 @@ function useBuildingRepo() {
27386
27378
  });
27387
27379
  }
27388
27380
  return {
27389
- createIndex,
27390
- createTextIndex,
27381
+ createIndexes,
27391
27382
  add,
27392
27383
  getAll,
27393
27384
  getById,
@@ -27416,9 +27407,14 @@ function useBuildingUnitRepo() {
27416
27407
  const namespace_collection = "school.building-units";
27417
27408
  const collection = db.collection(namespace_collection);
27418
27409
  const { getCache, setCache, delNamespace } = useCache21(namespace_collection);
27419
- async function createIndex() {
27410
+ async function createIndexes() {
27420
27411
  try {
27421
27412
  await collection.createIndexes([
27413
+ {
27414
+ key: { name: 1, building: 1, level: 1 },
27415
+ unique: true,
27416
+ name: "unique_name_index"
27417
+ },
27422
27418
  { key: { school: 1 } },
27423
27419
  { key: { building: 1 } },
27424
27420
  { key: { status: 1 } },
@@ -27497,6 +27493,36 @@ function useBuildingUnitRepo() {
27497
27493
  }
27498
27494
  }
27499
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
+ }
27500
27526
  async function getAll({
27501
27527
  search = "",
27502
27528
  page = 1,
@@ -27737,14 +27763,15 @@ function useBuildingUnitRepo() {
27737
27763
  }
27738
27764
  }
27739
27765
  return {
27740
- createIndex,
27766
+ createIndexes,
27741
27767
  add,
27742
27768
  getAll,
27743
27769
  getById,
27744
27770
  getByBuildingLevel,
27745
27771
  updateById,
27746
27772
  getByBuilding,
27747
- deleteById
27773
+ deleteById,
27774
+ updateByBuildingId
27748
27775
  };
27749
27776
  }
27750
27777
 
@@ -27753,16 +27780,21 @@ import { BadRequestError as BadRequestError63, logger as logger32 } from "@eepla
27753
27780
  import Joi34 from "joi";
27754
27781
 
27755
27782
  // src/services/building.service.ts
27756
- 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";
27757
27788
  function useBuildingService() {
27758
27789
  const {
27759
27790
  updateById: _updateById,
27760
27791
  getById: _getById,
27761
27792
  deleteById: _deleteById
27762
27793
  } = useBuildingRepo();
27763
- const { getByBuildingLevel, getByBuilding } = useBuildingUnitRepo();
27794
+ const { getByBuildingLevel, getByBuilding, updateByBuildingId } = useBuildingUnitRepo();
27764
27795
  async function updateById(id, data) {
27765
27796
  data.levels = Number(data.levels);
27797
+ const session = useAtlas33.getClient()?.startSession();
27766
27798
  try {
27767
27799
  const building = await _getById(id);
27768
27800
  if (!building) {
@@ -27776,10 +27808,18 @@ function useBuildingService() {
27776
27808
  );
27777
27809
  }
27778
27810
  }
27779
- 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();
27780
27817
  return result;
27781
27818
  } catch (error) {
27819
+ await session?.abortTransaction();
27782
27820
  throw error;
27821
+ } finally {
27822
+ session?.endSession();
27783
27823
  }
27784
27824
  }
27785
27825
  async function deleteById(id) {
@@ -27950,11 +27990,11 @@ import { BadRequestError as BadRequestError64 } from "@eeplatform/nodejs-utils";
27950
27990
  import Joi35 from "joi";
27951
27991
 
27952
27992
  // src/services/building-unit.service.ts
27953
- import { useAtlas as useAtlas33 } from "@eeplatform/nodejs-utils";
27993
+ import { useAtlas as useAtlas34 } from "@eeplatform/nodejs-utils";
27954
27994
  function useBuildingUnitService() {
27955
27995
  const { add: _add } = useBuildingUnitRepo();
27956
27996
  async function add(value) {
27957
- const session = useAtlas33.getClient()?.startSession();
27997
+ const session = useAtlas34.getClient()?.startSession();
27958
27998
  if (!session) {
27959
27999
  throw new Error("Unable to start session for building unit service.");
27960
28000
  }
@@ -28136,6 +28176,400 @@ function useBuildingUnitController() {
28136
28176
  deleteById
28137
28177
  };
28138
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
+ }
28139
28573
  export {
28140
28574
  ACCESS_TOKEN_EXPIRY,
28141
28575
  ACCESS_TOKEN_SECRET,
@@ -28159,6 +28593,7 @@ export {
28159
28593
  MDivision,
28160
28594
  MEntity,
28161
28595
  MFile,
28596
+ MInventory,
28162
28597
  MMember,
28163
28598
  MONGO_DB,
28164
28599
  MONGO_URI,
@@ -28199,6 +28634,8 @@ export {
28199
28634
  schemaBuilding,
28200
28635
  schemaBuildingUnit,
28201
28636
  schemaDivision,
28637
+ schemaInventory,
28638
+ schemaInventoryUpdateOption,
28202
28639
  schemaRegion,
28203
28640
  schemaSchool,
28204
28641
  schemaUpdateOptions,
@@ -28220,6 +28657,8 @@ export {
28220
28657
  useFileController,
28221
28658
  useFileRepo,
28222
28659
  useFileService,
28660
+ useInventoryController,
28661
+ useInventoryRepo,
28223
28662
  useInvoiceController,
28224
28663
  useInvoiceModel,
28225
28664
  useInvoiceRepo,