@eeplatform/core 1.1.0 → 1.2.1

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
@@ -27063,6 +27063,16 @@ var schemaBuildingUnit = Joi33.object({
27063
27063
  area: Joi33.number().positive().required(),
27064
27064
  status: Joi33.string().optional().allow("", null)
27065
27065
  });
27066
+ var schemaUpdateOptions = Joi33.object({
27067
+ name: Joi33.string().optional().allow("", null),
27068
+ building: Joi33.string().hex().optional().allow("", null),
27069
+ level: Joi33.number().integer().min(1).optional().allow("", null),
27070
+ category: Joi33.string().optional().allow("", null),
27071
+ type: Joi33.string().optional().allow("", null),
27072
+ seating_capacity: Joi33.number().integer().min(0).optional().allow("", null),
27073
+ standing_capacity: Joi33.number().integer().min(0).optional().allow("", null),
27074
+ area: Joi33.number().positive().optional().allow("", null)
27075
+ });
27066
27076
  function MBuilding(value) {
27067
27077
  const { error } = schemaBuilding.validate(value);
27068
27078
  if (error) {
@@ -27157,26 +27167,17 @@ function useBuildingRepo() {
27157
27167
  const namespace_collection = "school.buildings";
27158
27168
  const collection = db.collection(namespace_collection);
27159
27169
  const { getCache, setCache, delNamespace } = useCache20(namespace_collection);
27160
- async function createIndex() {
27170
+ async function createIndexes() {
27161
27171
  try {
27162
- await collection.createIndex([
27163
- { name: 1 },
27164
- { school: 1 },
27165
- { createdAt: 1 }
27172
+ await collection.createIndexes([
27173
+ { key: { name: 1 }, unique: true, name: "unique_name_index" },
27174
+ { key: { school: 1 } },
27175
+ { key: { status: 1 } }
27166
27176
  ]);
27167
27177
  } catch (error) {
27168
27178
  throw new Error("Failed to create index on buildings.");
27169
27179
  }
27170
27180
  }
27171
- async function createTextIndex() {
27172
- try {
27173
- await collection.createIndex({
27174
- name: "text"
27175
- });
27176
- } catch (error) {
27177
- throw new Error("Failed to create text index on building name.");
27178
- }
27179
- }
27180
27181
  async function add(value, session) {
27181
27182
  try {
27182
27183
  value = MBuilding(value);
@@ -27199,6 +27200,32 @@ function useBuildingRepo() {
27199
27200
  }
27200
27201
  }
27201
27202
  }
27203
+ async function updateById(_id, value, session) {
27204
+ try {
27205
+ _id = new ObjectId40(_id);
27206
+ } catch (error) {
27207
+ throw new BadRequestError60("Invalid ID.");
27208
+ }
27209
+ try {
27210
+ const res = await collection.updateOne(
27211
+ { _id },
27212
+ { $set: value },
27213
+ { session }
27214
+ );
27215
+ delCachedData();
27216
+ return res;
27217
+ } catch (error) {
27218
+ logger30.log({
27219
+ level: "error",
27220
+ message: error.message
27221
+ });
27222
+ if (error instanceof AppError14) {
27223
+ throw error;
27224
+ } else {
27225
+ throw new Error("Failed to update building.");
27226
+ }
27227
+ }
27228
+ }
27202
27229
  async function getAll({
27203
27230
  search = "",
27204
27231
  page = 1,
@@ -27289,12 +27316,8 @@ function useBuildingRepo() {
27289
27316
  return cached;
27290
27317
  }
27291
27318
  const result = await collection.findOne({
27292
- _id,
27293
- deletedAt: { $in: ["", null] }
27319
+ _id
27294
27320
  });
27295
- if (!result) {
27296
- throw new BadRequestError60("Building not found.");
27297
- }
27298
27321
  setCache(cacheKey, result, 300).then(() => {
27299
27322
  logger30.log({
27300
27323
  level: "info",
@@ -27315,6 +27338,31 @@ function useBuildingRepo() {
27315
27338
  }
27316
27339
  }
27317
27340
  }
27341
+ async function deleteById(_id, session) {
27342
+ try {
27343
+ _id = new ObjectId40(_id);
27344
+ } catch (error) {
27345
+ throw new BadRequestError60("Invalid ID.");
27346
+ }
27347
+ try {
27348
+ const res = await collection.updateOne(
27349
+ { _id },
27350
+ { $set: { status: "deleted", deletedAt: /* @__PURE__ */ new Date() } }
27351
+ );
27352
+ delCachedData();
27353
+ return res;
27354
+ } catch (error) {
27355
+ logger30.log({
27356
+ level: "error",
27357
+ message: error.message
27358
+ });
27359
+ if (error instanceof AppError14) {
27360
+ throw error;
27361
+ } else {
27362
+ throw new InternalServerError25("Failed to delete building.");
27363
+ }
27364
+ }
27365
+ }
27318
27366
  function delCachedData() {
27319
27367
  delNamespace().then(() => {
27320
27368
  logger30.log({
@@ -27329,11 +27377,12 @@ function useBuildingRepo() {
27329
27377
  });
27330
27378
  }
27331
27379
  return {
27332
- createIndex,
27333
- createTextIndex,
27380
+ createIndexes,
27334
27381
  add,
27335
27382
  getAll,
27336
- getById
27383
+ getById,
27384
+ updateById,
27385
+ deleteById
27337
27386
  };
27338
27387
  }
27339
27388
 
@@ -27357,9 +27406,14 @@ function useBuildingUnitRepo() {
27357
27406
  const namespace_collection = "school.building-units";
27358
27407
  const collection = db.collection(namespace_collection);
27359
27408
  const { getCache, setCache, delNamespace } = useCache21(namespace_collection);
27360
- async function createIndex() {
27409
+ async function createIndexes() {
27361
27410
  try {
27362
27411
  await collection.createIndexes([
27412
+ {
27413
+ key: { name: 1, building: 1, level: 1 },
27414
+ unique: true,
27415
+ name: "unique_name_index"
27416
+ },
27363
27417
  { key: { school: 1 } },
27364
27418
  { key: { building: 1 } },
27365
27419
  { key: { status: 1 } },
@@ -27408,6 +27462,36 @@ function useBuildingUnitRepo() {
27408
27462
  }
27409
27463
  }
27410
27464
  }
27465
+ async function updateById(_id, value, session) {
27466
+ const { error } = schemaUpdateOptions.validate(value);
27467
+ if (error) {
27468
+ throw new BadRequestError61(error.message);
27469
+ }
27470
+ try {
27471
+ _id = new ObjectId41(_id);
27472
+ } catch (error2) {
27473
+ throw new BadRequestError61("Invalid ID.");
27474
+ }
27475
+ try {
27476
+ const res = await collection.updateOne(
27477
+ { _id },
27478
+ { $set: value },
27479
+ { session }
27480
+ );
27481
+ delCachedData();
27482
+ return res;
27483
+ } catch (error2) {
27484
+ logger31.log({
27485
+ level: "error",
27486
+ message: error2.message
27487
+ });
27488
+ if (error2 instanceof AppError15) {
27489
+ throw error2;
27490
+ } else {
27491
+ throw new Error("Failed to create building unit.");
27492
+ }
27493
+ }
27494
+ }
27411
27495
  async function getAll({
27412
27496
  search = "",
27413
27497
  page = 1,
@@ -27535,19 +27619,188 @@ function useBuildingUnitRepo() {
27535
27619
  }
27536
27620
  }
27537
27621
  }
27622
+ async function getByBuildingLevel(building, level) {
27623
+ try {
27624
+ building = new ObjectId41(building);
27625
+ } catch (error) {
27626
+ throw new BadRequestError61("Invalid building ID.");
27627
+ }
27628
+ const cacheKey = makeCacheKey20(namespace_collection, {
27629
+ building: String(building),
27630
+ level
27631
+ });
27632
+ try {
27633
+ const cached = await getCache(cacheKey);
27634
+ if (cached) {
27635
+ logger31.log({
27636
+ level: "info",
27637
+ message: `Cache hit for getById building unit: ${cacheKey}`
27638
+ });
27639
+ return cached;
27640
+ }
27641
+ const result = await collection.findOne({
27642
+ building,
27643
+ level,
27644
+ status: "active"
27645
+ });
27646
+ setCache(cacheKey, result, 300).then(() => {
27647
+ logger31.log({
27648
+ level: "info",
27649
+ message: `Cache set for building unit by id: ${cacheKey}`
27650
+ });
27651
+ }).catch((err) => {
27652
+ logger31.log({
27653
+ level: "error",
27654
+ message: `Failed to set cache for building unit by id: ${err.message}`
27655
+ });
27656
+ });
27657
+ return result;
27658
+ } catch (error) {
27659
+ if (error instanceof AppError15) {
27660
+ throw error;
27661
+ } else {
27662
+ throw new InternalServerError26("Failed to get building unit.");
27663
+ }
27664
+ }
27665
+ }
27666
+ async function getByBuilding(building) {
27667
+ try {
27668
+ building = new ObjectId41(building);
27669
+ } catch (error) {
27670
+ throw new BadRequestError61("Invalid building ID.");
27671
+ }
27672
+ const cacheKey = makeCacheKey20(namespace_collection, {
27673
+ building: String(building)
27674
+ });
27675
+ try {
27676
+ const cached = await getCache(cacheKey);
27677
+ if (cached) {
27678
+ logger31.log({
27679
+ level: "info",
27680
+ message: `Cache hit for getById building unit: ${cacheKey}`
27681
+ });
27682
+ return cached;
27683
+ }
27684
+ const result = await collection.findOne({
27685
+ building,
27686
+ status: "active"
27687
+ });
27688
+ setCache(cacheKey, result, 300).then(() => {
27689
+ logger31.log({
27690
+ level: "info",
27691
+ message: `Cache set for building unit by id: ${cacheKey}`
27692
+ });
27693
+ }).catch((err) => {
27694
+ logger31.log({
27695
+ level: "error",
27696
+ message: `Failed to set cache for building unit by id: ${err.message}`
27697
+ });
27698
+ });
27699
+ return result;
27700
+ } catch (error) {
27701
+ if (error instanceof AppError15) {
27702
+ throw error;
27703
+ } else {
27704
+ throw new InternalServerError26("Failed to get building unit.");
27705
+ }
27706
+ }
27707
+ }
27708
+ async function deleteById(_id, session) {
27709
+ try {
27710
+ _id = new ObjectId41(_id);
27711
+ } catch (error) {
27712
+ throw new BadRequestError61("Invalid ID.");
27713
+ }
27714
+ try {
27715
+ const res = await collection.updateOne(
27716
+ { _id },
27717
+ { $set: { status: "deleted", deletedAt: /* @__PURE__ */ new Date() } },
27718
+ { session }
27719
+ );
27720
+ delCachedData();
27721
+ return "Room/Facility deleted successfully.";
27722
+ } catch (error) {
27723
+ logger31.log({
27724
+ level: "error",
27725
+ message: error.message
27726
+ });
27727
+ if (error instanceof AppError15) {
27728
+ throw error;
27729
+ } else {
27730
+ throw new Error("Failed to deleted room/facility.");
27731
+ }
27732
+ }
27733
+ }
27538
27734
  return {
27539
- createIndex,
27735
+ createIndexes,
27540
27736
  add,
27541
27737
  getAll,
27542
- getById
27738
+ getById,
27739
+ getByBuildingLevel,
27740
+ updateById,
27741
+ getByBuilding,
27742
+ deleteById
27543
27743
  };
27544
27744
  }
27545
27745
 
27546
27746
  // src/controllers/building.controller.ts
27547
- import { BadRequestError as BadRequestError62, logger as logger32 } from "@eeplatform/nodejs-utils";
27747
+ import { BadRequestError as BadRequestError63, logger as logger32 } from "@eeplatform/nodejs-utils";
27548
27748
  import Joi34 from "joi";
27749
+
27750
+ // src/services/building.service.ts
27751
+ import { BadRequestError as BadRequestError62, NotFoundError as NotFoundError11 } from "@eeplatform/nodejs-utils";
27752
+ function useBuildingService() {
27753
+ const {
27754
+ updateById: _updateById,
27755
+ getById: _getById,
27756
+ deleteById: _deleteById
27757
+ } = useBuildingRepo();
27758
+ const { getByBuildingLevel, getByBuilding } = useBuildingUnitRepo();
27759
+ async function updateById(id, data) {
27760
+ data.levels = Number(data.levels);
27761
+ try {
27762
+ const building = await _getById(id);
27763
+ if (!building) {
27764
+ throw new NotFoundError11("Building not found.");
27765
+ }
27766
+ if (data.levels < building.levels) {
27767
+ const unit = await getByBuildingLevel(id, building.levels);
27768
+ if (unit) {
27769
+ throw new BadRequestError62(
27770
+ "Cannot reduce floors, there are existing building units at higher floors."
27771
+ );
27772
+ }
27773
+ }
27774
+ const result = await _updateById(id, data);
27775
+ return result;
27776
+ } catch (error) {
27777
+ throw error;
27778
+ }
27779
+ }
27780
+ async function deleteById(id) {
27781
+ const building = await getByBuilding(id);
27782
+ if (building) {
27783
+ throw new BadRequestError62(
27784
+ "Cannot delete building with existing room/facility. Please delete room/facility first."
27785
+ );
27786
+ }
27787
+ try {
27788
+ await _deleteById(id);
27789
+ return "Building deleted successfully.";
27790
+ } catch (error) {
27791
+ throw error;
27792
+ }
27793
+ }
27794
+ return {
27795
+ updateById,
27796
+ deleteById
27797
+ };
27798
+ }
27799
+
27800
+ // src/controllers/building.controller.ts
27549
27801
  function useBuildingController() {
27550
27802
  const { getAll: _getAll, getById: _getById, add: _add } = useBuildingRepo();
27803
+ const { updateById: _updateById, deleteById: _deleteById } = useBuildingService();
27551
27804
  async function createBuilding(req, res, next) {
27552
27805
  const value = req.body;
27553
27806
  const validation = Joi34.object({
@@ -27559,7 +27812,7 @@ function useBuildingController() {
27559
27812
  });
27560
27813
  const { error } = validation.validate(value);
27561
27814
  if (error) {
27562
- next(new BadRequestError62(error.message));
27815
+ next(new BadRequestError63(error.message));
27563
27816
  logger32.info(`Controller: ${error.message}`);
27564
27817
  return;
27565
27818
  }
@@ -27571,6 +27824,31 @@ function useBuildingController() {
27571
27824
  next(error2);
27572
27825
  }
27573
27826
  }
27827
+ async function updateById(req, res, next) {
27828
+ const value = req.body;
27829
+ const id = req.params.id ?? "";
27830
+ const validation = Joi34.object({
27831
+ id: Joi34.string().hex().required(),
27832
+ value: Joi34.object({
27833
+ name: Joi34.string().required(),
27834
+ serial: Joi34.string().optional().allow("", null),
27835
+ levels: Joi34.number().integer().min(1).required()
27836
+ })
27837
+ });
27838
+ const { error } = validation.validate({ id, value });
27839
+ if (error) {
27840
+ next(new BadRequestError63(error.message));
27841
+ logger32.info(`Controller: ${error.message}`);
27842
+ return;
27843
+ }
27844
+ try {
27845
+ const result = await _updateById(id, value);
27846
+ res.json(result);
27847
+ return;
27848
+ } catch (error2) {
27849
+ next(error2);
27850
+ }
27851
+ }
27574
27852
  async function getAll(req, res, next) {
27575
27853
  const query = req.query;
27576
27854
  const validation = Joi34.object({
@@ -27582,7 +27860,7 @@ function useBuildingController() {
27582
27860
  });
27583
27861
  const { error } = validation.validate(query);
27584
27862
  if (error) {
27585
- next(new BadRequestError62(error.message));
27863
+ next(new BadRequestError63(error.message));
27586
27864
  return;
27587
27865
  }
27588
27866
  const page = parseInt(req.query.page) ?? 1;
@@ -27621,7 +27899,7 @@ function useBuildingController() {
27621
27899
  });
27622
27900
  const { error } = validation.validate({ id });
27623
27901
  if (error) {
27624
- next(new BadRequestError62(error.message));
27902
+ next(new BadRequestError63(error.message));
27625
27903
  return;
27626
27904
  }
27627
27905
  try {
@@ -27635,15 +27913,35 @@ function useBuildingController() {
27635
27913
  next(error2);
27636
27914
  }
27637
27915
  }
27916
+ async function deleteById(req, res, next) {
27917
+ const id = req.params.id;
27918
+ const validation = Joi34.object({
27919
+ id: Joi34.string().hex().required()
27920
+ });
27921
+ const { error } = validation.validate({ id });
27922
+ if (error) {
27923
+ next(new BadRequestError63(error.message));
27924
+ return;
27925
+ }
27926
+ try {
27927
+ const message = await _deleteById(id);
27928
+ res.json(message);
27929
+ return;
27930
+ } catch (error2) {
27931
+ next(error2);
27932
+ }
27933
+ }
27638
27934
  return {
27639
27935
  createBuilding,
27640
27936
  getAll,
27641
- getById
27937
+ getById,
27938
+ updateById,
27939
+ deleteById
27642
27940
  };
27643
27941
  }
27644
27942
 
27645
27943
  // src/controllers/building-unit.controller.ts
27646
- import { BadRequestError as BadRequestError63 } from "@eeplatform/nodejs-utils";
27944
+ import { BadRequestError as BadRequestError64 } from "@eeplatform/nodejs-utils";
27647
27945
  import Joi35 from "joi";
27648
27946
 
27649
27947
  // src/services/building-unit.service.ts
@@ -27676,7 +27974,12 @@ function useBuildingUnitService() {
27676
27974
 
27677
27975
  // src/controllers/building-unit.controller.ts
27678
27976
  function useBuildingUnitController() {
27679
- const { getAll: _getAll, getById: _getById } = useBuildingUnitRepo();
27977
+ const {
27978
+ getAll: _getAll,
27979
+ getById: _getById,
27980
+ updateById: _updateById,
27981
+ deleteById: _deleteById
27982
+ } = useBuildingUnitRepo();
27680
27983
  const { add: _add } = useBuildingUnitService();
27681
27984
  async function add(req, res, next) {
27682
27985
  const data = req.body;
@@ -27700,7 +28003,7 @@ function useBuildingUnitController() {
27700
28003
  });
27701
28004
  const { error } = validation.validate(data);
27702
28005
  if (error) {
27703
- next(new BadRequestError63(error.message));
28006
+ next(new BadRequestError64(error.message));
27704
28007
  return;
27705
28008
  }
27706
28009
  try {
@@ -27713,6 +28016,28 @@ function useBuildingUnitController() {
27713
28016
  next(error2);
27714
28017
  }
27715
28018
  }
28019
+ async function updateById(req, res, next) {
28020
+ const data = req.body;
28021
+ const id = req.params.id ?? "";
28022
+ const validation = Joi35.object({
28023
+ id: Joi35.string().hex().required(),
28024
+ value: schemaUpdateOptions
28025
+ });
28026
+ const { error } = validation.validate({ id, value: data });
28027
+ if (error) {
28028
+ next(new BadRequestError64(error.message));
28029
+ return;
28030
+ }
28031
+ try {
28032
+ const buildingUnit = await _updateById(id, data);
28033
+ res.json({
28034
+ message: "Building unit updated successfully.",
28035
+ data: { buildingUnit }
28036
+ });
28037
+ } catch (error2) {
28038
+ next(error2);
28039
+ }
28040
+ }
27716
28041
  async function getAll(req, res, next) {
27717
28042
  const query = req.query;
27718
28043
  const validation = Joi35.object({
@@ -27725,7 +28050,7 @@ function useBuildingUnitController() {
27725
28050
  });
27726
28051
  const { error } = validation.validate(query);
27727
28052
  if (error) {
27728
- next(new BadRequestError63(error.message));
28053
+ next(new BadRequestError64(error.message));
27729
28054
  return;
27730
28055
  }
27731
28056
  const page = parseInt(req.query.page) ?? 1;
@@ -27766,7 +28091,7 @@ function useBuildingUnitController() {
27766
28091
  });
27767
28092
  const { error } = validation.validate({ id });
27768
28093
  if (error) {
27769
- next(new BadRequestError63(error.message));
28094
+ next(new BadRequestError64(error.message));
27770
28095
  return;
27771
28096
  }
27772
28097
  try {
@@ -27780,10 +28105,30 @@ function useBuildingUnitController() {
27780
28105
  next(error2);
27781
28106
  }
27782
28107
  }
28108
+ async function deleteById(req, res, next) {
28109
+ const id = req.params.id;
28110
+ const validation = Joi35.object({
28111
+ id: Joi35.string().hex().required()
28112
+ });
28113
+ const { error } = validation.validate({ id });
28114
+ if (error) {
28115
+ next(new BadRequestError64(error.message));
28116
+ return;
28117
+ }
28118
+ try {
28119
+ const message = await _deleteById(id);
28120
+ res.json({ message });
28121
+ return;
28122
+ } catch (error2) {
28123
+ next(error2);
28124
+ }
28125
+ }
27783
28126
  return {
27784
28127
  add,
27785
28128
  getAll,
27786
- getById
28129
+ getById,
28130
+ updateById,
28131
+ deleteById
27787
28132
  };
27788
28133
  }
27789
28134
  export {
@@ -27851,6 +28196,7 @@ export {
27851
28196
  schemaDivision,
27852
28197
  schemaRegion,
27853
28198
  schemaSchool,
28199
+ schemaUpdateOptions,
27854
28200
  useAddressController,
27855
28201
  useAddressRepo,
27856
28202
  useAuthController,