@eeplatform/core 1.1.0 → 1.2.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
@@ -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) {
@@ -27199,6 +27209,32 @@ function useBuildingRepo() {
27199
27209
  }
27200
27210
  }
27201
27211
  }
27212
+ async function updateById(_id, value, session) {
27213
+ try {
27214
+ _id = new ObjectId40(_id);
27215
+ } catch (error) {
27216
+ throw new BadRequestError60("Invalid ID.");
27217
+ }
27218
+ try {
27219
+ const res = await collection.updateOne(
27220
+ { _id },
27221
+ { $set: value },
27222
+ { session }
27223
+ );
27224
+ delCachedData();
27225
+ return res;
27226
+ } catch (error) {
27227
+ logger30.log({
27228
+ level: "error",
27229
+ message: error.message
27230
+ });
27231
+ if (error instanceof AppError14) {
27232
+ throw error;
27233
+ } else {
27234
+ throw new Error("Failed to update building.");
27235
+ }
27236
+ }
27237
+ }
27202
27238
  async function getAll({
27203
27239
  search = "",
27204
27240
  page = 1,
@@ -27289,12 +27325,8 @@ function useBuildingRepo() {
27289
27325
  return cached;
27290
27326
  }
27291
27327
  const result = await collection.findOne({
27292
- _id,
27293
- deletedAt: { $in: ["", null] }
27328
+ _id
27294
27329
  });
27295
- if (!result) {
27296
- throw new BadRequestError60("Building not found.");
27297
- }
27298
27330
  setCache(cacheKey, result, 300).then(() => {
27299
27331
  logger30.log({
27300
27332
  level: "info",
@@ -27315,6 +27347,31 @@ function useBuildingRepo() {
27315
27347
  }
27316
27348
  }
27317
27349
  }
27350
+ async function deleteById(_id, session) {
27351
+ try {
27352
+ _id = new ObjectId40(_id);
27353
+ } catch (error) {
27354
+ throw new BadRequestError60("Invalid ID.");
27355
+ }
27356
+ try {
27357
+ const res = await collection.updateOne(
27358
+ { _id },
27359
+ { $set: { status: "deleted", deletedAt: /* @__PURE__ */ new Date() } }
27360
+ );
27361
+ delCachedData();
27362
+ return res;
27363
+ } catch (error) {
27364
+ logger30.log({
27365
+ level: "error",
27366
+ message: error.message
27367
+ });
27368
+ if (error instanceof AppError14) {
27369
+ throw error;
27370
+ } else {
27371
+ throw new InternalServerError25("Failed to delete building.");
27372
+ }
27373
+ }
27374
+ }
27318
27375
  function delCachedData() {
27319
27376
  delNamespace().then(() => {
27320
27377
  logger30.log({
@@ -27333,7 +27390,9 @@ function useBuildingRepo() {
27333
27390
  createTextIndex,
27334
27391
  add,
27335
27392
  getAll,
27336
- getById
27393
+ getById,
27394
+ updateById,
27395
+ deleteById
27337
27396
  };
27338
27397
  }
27339
27398
 
@@ -27408,6 +27467,36 @@ function useBuildingUnitRepo() {
27408
27467
  }
27409
27468
  }
27410
27469
  }
27470
+ async function updateById(_id, value, session) {
27471
+ const { error } = schemaUpdateOptions.validate(value);
27472
+ if (error) {
27473
+ throw new BadRequestError61(error.message);
27474
+ }
27475
+ try {
27476
+ _id = new ObjectId41(_id);
27477
+ } catch (error2) {
27478
+ throw new BadRequestError61("Invalid ID.");
27479
+ }
27480
+ try {
27481
+ const res = await collection.updateOne(
27482
+ { _id },
27483
+ { $set: value },
27484
+ { session }
27485
+ );
27486
+ delCachedData();
27487
+ return res;
27488
+ } catch (error2) {
27489
+ logger31.log({
27490
+ level: "error",
27491
+ message: error2.message
27492
+ });
27493
+ if (error2 instanceof AppError15) {
27494
+ throw error2;
27495
+ } else {
27496
+ throw new Error("Failed to create building unit.");
27497
+ }
27498
+ }
27499
+ }
27411
27500
  async function getAll({
27412
27501
  search = "",
27413
27502
  page = 1,
@@ -27535,19 +27624,188 @@ function useBuildingUnitRepo() {
27535
27624
  }
27536
27625
  }
27537
27626
  }
27627
+ async function getByBuildingLevel(building, level) {
27628
+ try {
27629
+ building = new ObjectId41(building);
27630
+ } catch (error) {
27631
+ throw new BadRequestError61("Invalid building ID.");
27632
+ }
27633
+ const cacheKey = makeCacheKey20(namespace_collection, {
27634
+ building: String(building),
27635
+ level
27636
+ });
27637
+ try {
27638
+ const cached = await getCache(cacheKey);
27639
+ if (cached) {
27640
+ logger31.log({
27641
+ level: "info",
27642
+ message: `Cache hit for getById building unit: ${cacheKey}`
27643
+ });
27644
+ return cached;
27645
+ }
27646
+ const result = await collection.findOne({
27647
+ building,
27648
+ level,
27649
+ status: "active"
27650
+ });
27651
+ setCache(cacheKey, result, 300).then(() => {
27652
+ logger31.log({
27653
+ level: "info",
27654
+ message: `Cache set for building unit by id: ${cacheKey}`
27655
+ });
27656
+ }).catch((err) => {
27657
+ logger31.log({
27658
+ level: "error",
27659
+ message: `Failed to set cache for building unit by id: ${err.message}`
27660
+ });
27661
+ });
27662
+ return result;
27663
+ } catch (error) {
27664
+ if (error instanceof AppError15) {
27665
+ throw error;
27666
+ } else {
27667
+ throw new InternalServerError26("Failed to get building unit.");
27668
+ }
27669
+ }
27670
+ }
27671
+ async function getByBuilding(building) {
27672
+ try {
27673
+ building = new ObjectId41(building);
27674
+ } catch (error) {
27675
+ throw new BadRequestError61("Invalid building ID.");
27676
+ }
27677
+ const cacheKey = makeCacheKey20(namespace_collection, {
27678
+ building: String(building)
27679
+ });
27680
+ try {
27681
+ const cached = await getCache(cacheKey);
27682
+ if (cached) {
27683
+ logger31.log({
27684
+ level: "info",
27685
+ message: `Cache hit for getById building unit: ${cacheKey}`
27686
+ });
27687
+ return cached;
27688
+ }
27689
+ const result = await collection.findOne({
27690
+ building,
27691
+ status: "active"
27692
+ });
27693
+ setCache(cacheKey, result, 300).then(() => {
27694
+ logger31.log({
27695
+ level: "info",
27696
+ message: `Cache set for building unit by id: ${cacheKey}`
27697
+ });
27698
+ }).catch((err) => {
27699
+ logger31.log({
27700
+ level: "error",
27701
+ message: `Failed to set cache for building unit by id: ${err.message}`
27702
+ });
27703
+ });
27704
+ return result;
27705
+ } catch (error) {
27706
+ if (error instanceof AppError15) {
27707
+ throw error;
27708
+ } else {
27709
+ throw new InternalServerError26("Failed to get building unit.");
27710
+ }
27711
+ }
27712
+ }
27713
+ async function deleteById(_id, session) {
27714
+ try {
27715
+ _id = new ObjectId41(_id);
27716
+ } catch (error) {
27717
+ throw new BadRequestError61("Invalid ID.");
27718
+ }
27719
+ try {
27720
+ const res = await collection.updateOne(
27721
+ { _id },
27722
+ { $set: { status: "deleted", deletedAt: /* @__PURE__ */ new Date() } },
27723
+ { session }
27724
+ );
27725
+ delCachedData();
27726
+ return "Room/Facility deleted successfully.";
27727
+ } catch (error) {
27728
+ logger31.log({
27729
+ level: "error",
27730
+ message: error.message
27731
+ });
27732
+ if (error instanceof AppError15) {
27733
+ throw error;
27734
+ } else {
27735
+ throw new Error("Failed to deleted room/facility.");
27736
+ }
27737
+ }
27738
+ }
27538
27739
  return {
27539
27740
  createIndex,
27540
27741
  add,
27541
27742
  getAll,
27542
- getById
27743
+ getById,
27744
+ getByBuildingLevel,
27745
+ updateById,
27746
+ getByBuilding,
27747
+ deleteById
27543
27748
  };
27544
27749
  }
27545
27750
 
27546
27751
  // src/controllers/building.controller.ts
27547
- import { BadRequestError as BadRequestError62, logger as logger32 } from "@eeplatform/nodejs-utils";
27752
+ import { BadRequestError as BadRequestError63, logger as logger32 } from "@eeplatform/nodejs-utils";
27548
27753
  import Joi34 from "joi";
27754
+
27755
+ // src/services/building.service.ts
27756
+ import { BadRequestError as BadRequestError62, NotFoundError as NotFoundError11 } from "@eeplatform/nodejs-utils";
27757
+ function useBuildingService() {
27758
+ const {
27759
+ updateById: _updateById,
27760
+ getById: _getById,
27761
+ deleteById: _deleteById
27762
+ } = useBuildingRepo();
27763
+ const { getByBuildingLevel, getByBuilding } = useBuildingUnitRepo();
27764
+ async function updateById(id, data) {
27765
+ data.levels = Number(data.levels);
27766
+ try {
27767
+ const building = await _getById(id);
27768
+ if (!building) {
27769
+ throw new NotFoundError11("Building not found.");
27770
+ }
27771
+ if (data.levels < building.levels) {
27772
+ const unit = await getByBuildingLevel(id, building.levels);
27773
+ if (unit) {
27774
+ throw new BadRequestError62(
27775
+ "Cannot reduce floors, there are existing building units at higher floors."
27776
+ );
27777
+ }
27778
+ }
27779
+ const result = await _updateById(id, data);
27780
+ return result;
27781
+ } catch (error) {
27782
+ throw error;
27783
+ }
27784
+ }
27785
+ async function deleteById(id) {
27786
+ const building = await getByBuilding(id);
27787
+ if (building) {
27788
+ throw new BadRequestError62(
27789
+ "Cannot delete building with existing room/facility. Please delete room/facility first."
27790
+ );
27791
+ }
27792
+ try {
27793
+ await _deleteById(id);
27794
+ return "Building deleted successfully.";
27795
+ } catch (error) {
27796
+ throw error;
27797
+ }
27798
+ }
27799
+ return {
27800
+ updateById,
27801
+ deleteById
27802
+ };
27803
+ }
27804
+
27805
+ // src/controllers/building.controller.ts
27549
27806
  function useBuildingController() {
27550
27807
  const { getAll: _getAll, getById: _getById, add: _add } = useBuildingRepo();
27808
+ const { updateById: _updateById, deleteById: _deleteById } = useBuildingService();
27551
27809
  async function createBuilding(req, res, next) {
27552
27810
  const value = req.body;
27553
27811
  const validation = Joi34.object({
@@ -27559,7 +27817,7 @@ function useBuildingController() {
27559
27817
  });
27560
27818
  const { error } = validation.validate(value);
27561
27819
  if (error) {
27562
- next(new BadRequestError62(error.message));
27820
+ next(new BadRequestError63(error.message));
27563
27821
  logger32.info(`Controller: ${error.message}`);
27564
27822
  return;
27565
27823
  }
@@ -27571,6 +27829,31 @@ function useBuildingController() {
27571
27829
  next(error2);
27572
27830
  }
27573
27831
  }
27832
+ async function updateById(req, res, next) {
27833
+ const value = req.body;
27834
+ const id = req.params.id ?? "";
27835
+ const validation = Joi34.object({
27836
+ id: Joi34.string().hex().required(),
27837
+ value: Joi34.object({
27838
+ name: Joi34.string().required(),
27839
+ serial: Joi34.string().optional().allow("", null),
27840
+ levels: Joi34.number().integer().min(1).required()
27841
+ })
27842
+ });
27843
+ const { error } = validation.validate({ id, value });
27844
+ if (error) {
27845
+ next(new BadRequestError63(error.message));
27846
+ logger32.info(`Controller: ${error.message}`);
27847
+ return;
27848
+ }
27849
+ try {
27850
+ const result = await _updateById(id, value);
27851
+ res.json(result);
27852
+ return;
27853
+ } catch (error2) {
27854
+ next(error2);
27855
+ }
27856
+ }
27574
27857
  async function getAll(req, res, next) {
27575
27858
  const query = req.query;
27576
27859
  const validation = Joi34.object({
@@ -27582,7 +27865,7 @@ function useBuildingController() {
27582
27865
  });
27583
27866
  const { error } = validation.validate(query);
27584
27867
  if (error) {
27585
- next(new BadRequestError62(error.message));
27868
+ next(new BadRequestError63(error.message));
27586
27869
  return;
27587
27870
  }
27588
27871
  const page = parseInt(req.query.page) ?? 1;
@@ -27621,7 +27904,7 @@ function useBuildingController() {
27621
27904
  });
27622
27905
  const { error } = validation.validate({ id });
27623
27906
  if (error) {
27624
- next(new BadRequestError62(error.message));
27907
+ next(new BadRequestError63(error.message));
27625
27908
  return;
27626
27909
  }
27627
27910
  try {
@@ -27635,15 +27918,35 @@ function useBuildingController() {
27635
27918
  next(error2);
27636
27919
  }
27637
27920
  }
27921
+ async function deleteById(req, res, next) {
27922
+ const id = req.params.id;
27923
+ const validation = Joi34.object({
27924
+ id: Joi34.string().hex().required()
27925
+ });
27926
+ const { error } = validation.validate({ id });
27927
+ if (error) {
27928
+ next(new BadRequestError63(error.message));
27929
+ return;
27930
+ }
27931
+ try {
27932
+ const message = await _deleteById(id);
27933
+ res.json(message);
27934
+ return;
27935
+ } catch (error2) {
27936
+ next(error2);
27937
+ }
27938
+ }
27638
27939
  return {
27639
27940
  createBuilding,
27640
27941
  getAll,
27641
- getById
27942
+ getById,
27943
+ updateById,
27944
+ deleteById
27642
27945
  };
27643
27946
  }
27644
27947
 
27645
27948
  // src/controllers/building-unit.controller.ts
27646
- import { BadRequestError as BadRequestError63 } from "@eeplatform/nodejs-utils";
27949
+ import { BadRequestError as BadRequestError64 } from "@eeplatform/nodejs-utils";
27647
27950
  import Joi35 from "joi";
27648
27951
 
27649
27952
  // src/services/building-unit.service.ts
@@ -27676,7 +27979,12 @@ function useBuildingUnitService() {
27676
27979
 
27677
27980
  // src/controllers/building-unit.controller.ts
27678
27981
  function useBuildingUnitController() {
27679
- const { getAll: _getAll, getById: _getById } = useBuildingUnitRepo();
27982
+ const {
27983
+ getAll: _getAll,
27984
+ getById: _getById,
27985
+ updateById: _updateById,
27986
+ deleteById: _deleteById
27987
+ } = useBuildingUnitRepo();
27680
27988
  const { add: _add } = useBuildingUnitService();
27681
27989
  async function add(req, res, next) {
27682
27990
  const data = req.body;
@@ -27700,7 +28008,7 @@ function useBuildingUnitController() {
27700
28008
  });
27701
28009
  const { error } = validation.validate(data);
27702
28010
  if (error) {
27703
- next(new BadRequestError63(error.message));
28011
+ next(new BadRequestError64(error.message));
27704
28012
  return;
27705
28013
  }
27706
28014
  try {
@@ -27713,6 +28021,28 @@ function useBuildingUnitController() {
27713
28021
  next(error2);
27714
28022
  }
27715
28023
  }
28024
+ async function updateById(req, res, next) {
28025
+ const data = req.body;
28026
+ const id = req.params.id ?? "";
28027
+ const validation = Joi35.object({
28028
+ id: Joi35.string().hex().required(),
28029
+ value: schemaUpdateOptions
28030
+ });
28031
+ const { error } = validation.validate({ id, value: data });
28032
+ if (error) {
28033
+ next(new BadRequestError64(error.message));
28034
+ return;
28035
+ }
28036
+ try {
28037
+ const buildingUnit = await _updateById(id, data);
28038
+ res.json({
28039
+ message: "Building unit updated successfully.",
28040
+ data: { buildingUnit }
28041
+ });
28042
+ } catch (error2) {
28043
+ next(error2);
28044
+ }
28045
+ }
27716
28046
  async function getAll(req, res, next) {
27717
28047
  const query = req.query;
27718
28048
  const validation = Joi35.object({
@@ -27725,7 +28055,7 @@ function useBuildingUnitController() {
27725
28055
  });
27726
28056
  const { error } = validation.validate(query);
27727
28057
  if (error) {
27728
- next(new BadRequestError63(error.message));
28058
+ next(new BadRequestError64(error.message));
27729
28059
  return;
27730
28060
  }
27731
28061
  const page = parseInt(req.query.page) ?? 1;
@@ -27766,7 +28096,7 @@ function useBuildingUnitController() {
27766
28096
  });
27767
28097
  const { error } = validation.validate({ id });
27768
28098
  if (error) {
27769
- next(new BadRequestError63(error.message));
28099
+ next(new BadRequestError64(error.message));
27770
28100
  return;
27771
28101
  }
27772
28102
  try {
@@ -27780,10 +28110,30 @@ function useBuildingUnitController() {
27780
28110
  next(error2);
27781
28111
  }
27782
28112
  }
28113
+ async function deleteById(req, res, next) {
28114
+ const id = req.params.id;
28115
+ const validation = Joi35.object({
28116
+ id: Joi35.string().hex().required()
28117
+ });
28118
+ const { error } = validation.validate({ id });
28119
+ if (error) {
28120
+ next(new BadRequestError64(error.message));
28121
+ return;
28122
+ }
28123
+ try {
28124
+ const message = await _deleteById(id);
28125
+ res.json({ message });
28126
+ return;
28127
+ } catch (error2) {
28128
+ next(error2);
28129
+ }
28130
+ }
27783
28131
  return {
27784
28132
  add,
27785
28133
  getAll,
27786
- getById
28134
+ getById,
28135
+ updateById,
28136
+ deleteById
27787
28137
  };
27788
28138
  }
27789
28139
  export {
@@ -27851,6 +28201,7 @@ export {
27851
28201
  schemaDivision,
27852
28202
  schemaRegion,
27853
28203
  schemaSchool,
28204
+ schemaUpdateOptions,
27854
28205
  useAddressController,
27855
28206
  useAddressRepo,
27856
28207
  useAuthController,