@eeplatform/core 1.0.3 → 1.1.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
@@ -25667,10 +25667,6 @@ function useRegionRepo() {
25667
25667
  };
25668
25668
  }
25669
25669
 
25670
- // src/controllers/region.controller.ts
25671
- import { BadRequestError as BadRequestError52 } from "@eeplatform/nodejs-utils";
25672
- import Joi28 from "joi";
25673
-
25674
25670
  // src/services/region.service.ts
25675
25671
  import { useAtlas as useAtlas26 } from "@eeplatform/nodejs-utils";
25676
25672
  function useRegionService() {
@@ -25710,6 +25706,8 @@ function useRegionService() {
25710
25706
  }
25711
25707
 
25712
25708
  // src/controllers/region.controller.ts
25709
+ import { BadRequestError as BadRequestError52 } from "@eeplatform/nodejs-utils";
25710
+ import Joi28 from "joi";
25713
25711
  function useRegionController() {
25714
25712
  const {
25715
25713
  getAll: _getAll,
@@ -26230,10 +26228,6 @@ function useDivisionRepo() {
26230
26228
  };
26231
26229
  }
26232
26230
 
26233
- // src/controllers/division.controller.ts
26234
- import { BadRequestError as BadRequestError55 } from "@eeplatform/nodejs-utils";
26235
- import Joi30 from "joi";
26236
-
26237
26231
  // src/services/division.service.ts
26238
26232
  import { useAtlas as useAtlas28 } from "@eeplatform/nodejs-utils";
26239
26233
  function useDivisionService() {
@@ -26273,6 +26267,8 @@ function useDivisionService() {
26273
26267
  }
26274
26268
 
26275
26269
  // src/controllers/division.controller.ts
26270
+ import { BadRequestError as BadRequestError55 } from "@eeplatform/nodejs-utils";
26271
+ import Joi30 from "joi";
26276
26272
  function useDivisionController() {
26277
26273
  const {
26278
26274
  getAll: _getAll,
@@ -27035,6 +27031,761 @@ function useSchoolController() {
27035
27031
  approveSchool
27036
27032
  };
27037
27033
  }
27034
+
27035
+ // src/models/building.model.ts
27036
+ import { BadRequestError as BadRequestError59, logger as logger29 } from "@eeplatform/nodejs-utils";
27037
+ import Joi33 from "joi";
27038
+ import { ObjectId as ObjectId39 } from "mongodb";
27039
+ var schemaBuilding = Joi33.object({
27040
+ _id: Joi33.string().hex().optional(),
27041
+ school: Joi33.string().hex().required(),
27042
+ serial: Joi33.string().optional().allow("", null),
27043
+ name: Joi33.string().required(),
27044
+ levels: Joi33.number().integer().min(1).required(),
27045
+ createdAt: Joi33.date().optional().allow("", null),
27046
+ updatedAt: Joi33.date().optional().allow("", null),
27047
+ deletedAt: Joi33.date().optional().allow("", null),
27048
+ status: Joi33.string().optional().allow("", null)
27049
+ });
27050
+ var schemaBuildingUnit = Joi33.object({
27051
+ _id: Joi33.string().hex().optional(),
27052
+ school: Joi33.string().hex().required(),
27053
+ name: Joi33.string().optional().allow("", null),
27054
+ building: Joi33.string().hex().required(),
27055
+ buildingName: Joi33.string().optional().allow("", null),
27056
+ level: Joi33.number().integer().min(1).required(),
27057
+ category: Joi33.string().required(),
27058
+ type: Joi33.string().required(),
27059
+ seating_capacity: Joi33.number().integer().min(0).required(),
27060
+ standing_capacity: Joi33.number().integer().min(0).required(),
27061
+ description: Joi33.string().optional().allow("", null),
27062
+ unit_of_measurement: Joi33.string().valid("sqm").required(),
27063
+ area: Joi33.number().positive().required(),
27064
+ status: Joi33.string().optional().allow("", null)
27065
+ });
27066
+ function MBuilding(value) {
27067
+ const { error } = schemaBuilding.validate(value);
27068
+ if (error) {
27069
+ logger29.info(`Building Model: ${error.message}`);
27070
+ throw new BadRequestError59(error.message);
27071
+ }
27072
+ if (value._id && typeof value._id === "string") {
27073
+ try {
27074
+ value._id = new ObjectId39(value._id);
27075
+ } catch (error2) {
27076
+ throw new BadRequestError59("Invalid _id format");
27077
+ }
27078
+ }
27079
+ try {
27080
+ value.school = new ObjectId39(value.school);
27081
+ } catch (error2) {
27082
+ throw new BadRequestError59("Invalid school format");
27083
+ }
27084
+ return {
27085
+ _id: value._id ?? void 0,
27086
+ school: value.school,
27087
+ serial: value.serial ?? "",
27088
+ name: value.name ?? "",
27089
+ levels: value.levels ?? 0,
27090
+ status: value.status ?? "active",
27091
+ createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
27092
+ updatedAt: value.updatedAt ?? "",
27093
+ deletedAt: value.deletedAt ?? ""
27094
+ };
27095
+ }
27096
+ function MBuildingUnit(value) {
27097
+ const { error } = schemaBuildingUnit.validate(value);
27098
+ if (error) {
27099
+ logger29.info(`Building Unit Model: ${error.message}`);
27100
+ throw new BadRequestError59(error.message);
27101
+ }
27102
+ if (value._id && typeof value._id === "string") {
27103
+ try {
27104
+ value._id = new ObjectId39(value._id);
27105
+ } catch (error2) {
27106
+ throw new BadRequestError59("Invalid ID");
27107
+ }
27108
+ }
27109
+ try {
27110
+ value.school = new ObjectId39(value.school);
27111
+ } catch (error2) {
27112
+ throw new BadRequestError59("Invalid school ID");
27113
+ }
27114
+ try {
27115
+ value.building = new ObjectId39(value.building);
27116
+ } catch (error2) {
27117
+ throw new BadRequestError59("Invalid building ID");
27118
+ }
27119
+ return {
27120
+ _id: value._id ?? void 0,
27121
+ school: value.school,
27122
+ name: value.name ?? "",
27123
+ building: value.building,
27124
+ buildingName: value.buildingName ?? "",
27125
+ level: value.level ?? 0,
27126
+ category: value.category ?? "",
27127
+ type: value.type ?? "",
27128
+ seating_capacity: value.seating_capacity ?? 0,
27129
+ standing_capacity: value.standing_capacity ?? 0,
27130
+ description: value.description ?? "",
27131
+ unit_of_measurement: value.unit_of_measurement ?? "sqm",
27132
+ area: value.area ?? 0,
27133
+ status: value.status ?? "active",
27134
+ createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
27135
+ updatedAt: value.updatedAt ?? "",
27136
+ deletedAt: value.deletedAt ?? ""
27137
+ };
27138
+ }
27139
+
27140
+ // src/repositories/building.repository.ts
27141
+ import {
27142
+ AppError as AppError14,
27143
+ BadRequestError as BadRequestError60,
27144
+ InternalServerError as InternalServerError25,
27145
+ logger as logger30,
27146
+ makeCacheKey as makeCacheKey19,
27147
+ paginate as paginate15,
27148
+ useAtlas as useAtlas31,
27149
+ useCache as useCache20
27150
+ } from "@eeplatform/nodejs-utils";
27151
+ import { ObjectId as ObjectId40 } from "mongodb";
27152
+ function useBuildingRepo() {
27153
+ const db = useAtlas31.getDb();
27154
+ if (!db) {
27155
+ throw new Error("Unable to connect to server.");
27156
+ }
27157
+ const namespace_collection = "school.buildings";
27158
+ const collection = db.collection(namespace_collection);
27159
+ const { getCache, setCache, delNamespace } = useCache20(namespace_collection);
27160
+ async function createIndex() {
27161
+ try {
27162
+ await collection.createIndex([
27163
+ { name: 1 },
27164
+ { school: 1 },
27165
+ { createdAt: 1 }
27166
+ ]);
27167
+ } catch (error) {
27168
+ throw new Error("Failed to create index on buildings.");
27169
+ }
27170
+ }
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
+ async function add(value, session) {
27181
+ try {
27182
+ value = MBuilding(value);
27183
+ const res = await collection.insertOne(value, { session });
27184
+ delCachedData();
27185
+ return res.insertedId;
27186
+ } catch (error) {
27187
+ logger30.log({
27188
+ level: "error",
27189
+ message: error.message
27190
+ });
27191
+ if (error instanceof AppError14) {
27192
+ throw error;
27193
+ } else {
27194
+ const isDuplicated = error.message.includes("duplicate");
27195
+ if (isDuplicated) {
27196
+ throw new BadRequestError60("Building already exists.");
27197
+ }
27198
+ throw new Error("Failed to create building.");
27199
+ }
27200
+ }
27201
+ }
27202
+ async function getAll({
27203
+ search = "",
27204
+ page = 1,
27205
+ limit = 10,
27206
+ sort = {},
27207
+ school = "",
27208
+ status = "active"
27209
+ } = {}) {
27210
+ page = page > 0 ? page - 1 : 0;
27211
+ const query = {
27212
+ status
27213
+ };
27214
+ sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
27215
+ if (search) {
27216
+ query.$text = { $search: search };
27217
+ }
27218
+ if (school) {
27219
+ try {
27220
+ query.school = new ObjectId40(school);
27221
+ } catch (error) {
27222
+ throw new BadRequestError60("Invalid school ID.");
27223
+ }
27224
+ }
27225
+ const cacheParams = {
27226
+ page,
27227
+ limit,
27228
+ sort: JSON.stringify(sort)
27229
+ };
27230
+ if (search)
27231
+ cacheParams.search = search;
27232
+ if (school)
27233
+ cacheParams.school = school;
27234
+ if (status !== "active")
27235
+ cacheParams.status = status;
27236
+ const cacheKey = makeCacheKey19(namespace_collection, cacheParams);
27237
+ logger30.log({
27238
+ level: "info",
27239
+ message: `Cache key for getAll buildings: ${cacheKey}`
27240
+ });
27241
+ try {
27242
+ const cached = await getCache(cacheKey);
27243
+ if (cached) {
27244
+ logger30.log({
27245
+ level: "info",
27246
+ message: `Cache hit for getAll buildings: ${cacheKey}`
27247
+ });
27248
+ return cached;
27249
+ }
27250
+ const items = await collection.aggregate([
27251
+ { $match: query },
27252
+ { $sort: sort },
27253
+ { $skip: page * limit },
27254
+ { $limit: limit }
27255
+ ]).toArray();
27256
+ const length = await collection.countDocuments(query);
27257
+ const data = paginate15(items, page, limit, length);
27258
+ setCache(cacheKey, data, 600).then(() => {
27259
+ logger30.log({
27260
+ level: "info",
27261
+ message: `Cache set for getAll buildings: ${cacheKey}`
27262
+ });
27263
+ }).catch((err) => {
27264
+ logger30.log({
27265
+ level: "error",
27266
+ message: `Failed to set cache for getAll buildings: ${err.message}`
27267
+ });
27268
+ });
27269
+ return data;
27270
+ } catch (error) {
27271
+ logger30.log({ level: "error", message: `${error}` });
27272
+ throw error;
27273
+ }
27274
+ }
27275
+ async function getById(_id) {
27276
+ try {
27277
+ _id = new ObjectId40(_id);
27278
+ } catch (error) {
27279
+ throw new BadRequestError60("Invalid ID.");
27280
+ }
27281
+ const cacheKey = makeCacheKey19(namespace_collection, { _id: String(_id) });
27282
+ try {
27283
+ const cached = await getCache(cacheKey);
27284
+ if (cached) {
27285
+ logger30.log({
27286
+ level: "info",
27287
+ message: `Cache hit for getById building: ${cacheKey}`
27288
+ });
27289
+ return cached;
27290
+ }
27291
+ const result = await collection.findOne({
27292
+ _id,
27293
+ deletedAt: { $in: ["", null] }
27294
+ });
27295
+ if (!result) {
27296
+ throw new BadRequestError60("Building not found.");
27297
+ }
27298
+ setCache(cacheKey, result, 300).then(() => {
27299
+ logger30.log({
27300
+ level: "info",
27301
+ message: `Cache set for building by id: ${cacheKey}`
27302
+ });
27303
+ }).catch((err) => {
27304
+ logger30.log({
27305
+ level: "error",
27306
+ message: `Failed to set cache for building by id: ${err.message}`
27307
+ });
27308
+ });
27309
+ return result;
27310
+ } catch (error) {
27311
+ if (error instanceof AppError14) {
27312
+ throw error;
27313
+ } else {
27314
+ throw new InternalServerError25("Failed to get building.");
27315
+ }
27316
+ }
27317
+ }
27318
+ function delCachedData() {
27319
+ delNamespace().then(() => {
27320
+ logger30.log({
27321
+ level: "info",
27322
+ message: `Cache namespace cleared for ${namespace_collection}`
27323
+ });
27324
+ }).catch((err) => {
27325
+ logger30.log({
27326
+ level: "error",
27327
+ message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
27328
+ });
27329
+ });
27330
+ }
27331
+ return {
27332
+ createIndex,
27333
+ createTextIndex,
27334
+ add,
27335
+ getAll,
27336
+ getById
27337
+ };
27338
+ }
27339
+
27340
+ // src/repositories/building-unit.repository.ts
27341
+ import {
27342
+ AppError as AppError15,
27343
+ BadRequestError as BadRequestError61,
27344
+ InternalServerError as InternalServerError26,
27345
+ logger as logger31,
27346
+ makeCacheKey as makeCacheKey20,
27347
+ paginate as paginate16,
27348
+ useAtlas as useAtlas32,
27349
+ useCache as useCache21
27350
+ } from "@eeplatform/nodejs-utils";
27351
+ import { ObjectId as ObjectId41 } from "mongodb";
27352
+ function useBuildingUnitRepo() {
27353
+ const db = useAtlas32.getDb();
27354
+ if (!db) {
27355
+ throw new Error("Unable to connect to server.");
27356
+ }
27357
+ const namespace_collection = "school.building-units";
27358
+ const collection = db.collection(namespace_collection);
27359
+ const { getCache, setCache, delNamespace } = useCache21(namespace_collection);
27360
+ async function createIndex() {
27361
+ try {
27362
+ await collection.createIndexes([
27363
+ { key: { school: 1 } },
27364
+ { key: { building: 1 } },
27365
+ { key: { status: 1 } },
27366
+ { key: { createdAt: 1 } },
27367
+ {
27368
+ key: {
27369
+ name: "text",
27370
+ buildingName: "text",
27371
+ category: "text",
27372
+ type: "text"
27373
+ }
27374
+ }
27375
+ ]);
27376
+ } catch (error) {
27377
+ throw new Error("Failed to create index on building units.");
27378
+ }
27379
+ }
27380
+ function delCachedData() {
27381
+ delNamespace().then(() => {
27382
+ logger31.log({
27383
+ level: "info",
27384
+ message: `Cache namespace cleared for ${namespace_collection}`
27385
+ });
27386
+ }).catch((err) => {
27387
+ logger31.log({
27388
+ level: "error",
27389
+ message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
27390
+ });
27391
+ });
27392
+ }
27393
+ async function add(value, session) {
27394
+ try {
27395
+ value = MBuildingUnit(value);
27396
+ const res = await collection.insertOne(value, { session });
27397
+ delCachedData();
27398
+ return res.insertedId;
27399
+ } catch (error) {
27400
+ logger31.log({
27401
+ level: "error",
27402
+ message: error.message
27403
+ });
27404
+ if (error instanceof AppError15) {
27405
+ throw error;
27406
+ } else {
27407
+ throw new Error("Failed to create building unit.");
27408
+ }
27409
+ }
27410
+ }
27411
+ async function getAll({
27412
+ search = "",
27413
+ page = 1,
27414
+ limit = 10,
27415
+ sort = {},
27416
+ school = "",
27417
+ building = "",
27418
+ status = "active"
27419
+ } = {}) {
27420
+ page = page > 0 ? page - 1 : 0;
27421
+ const query = {
27422
+ deletedAt: { $in: ["", null] },
27423
+ status: { $in: [status, "", null] }
27424
+ };
27425
+ sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
27426
+ if (search) {
27427
+ query.$text = { $search: search };
27428
+ }
27429
+ if (school) {
27430
+ try {
27431
+ query.school = new ObjectId41(school);
27432
+ } catch (error) {
27433
+ throw new BadRequestError61("Invalid school ID.");
27434
+ }
27435
+ }
27436
+ if (building) {
27437
+ try {
27438
+ query.building = new ObjectId41(building);
27439
+ } catch (error) {
27440
+ throw new BadRequestError61("Invalid building ID.");
27441
+ }
27442
+ }
27443
+ const cacheParams = {
27444
+ page,
27445
+ limit,
27446
+ sort: JSON.stringify(sort)
27447
+ };
27448
+ if (search)
27449
+ cacheParams.search = search;
27450
+ if (school)
27451
+ cacheParams.school = school;
27452
+ if (building)
27453
+ cacheParams.building = building;
27454
+ if (status !== "active")
27455
+ cacheParams.status = status;
27456
+ const cacheKey = makeCacheKey20(namespace_collection, cacheParams);
27457
+ logger31.log({
27458
+ level: "info",
27459
+ message: `Cache key for getAll building units: ${cacheKey}`
27460
+ });
27461
+ try {
27462
+ const cached = await getCache(cacheKey);
27463
+ if (cached) {
27464
+ logger31.log({
27465
+ level: "info",
27466
+ message: `Cache hit for getAll building units: ${cacheKey}`
27467
+ });
27468
+ return cached;
27469
+ }
27470
+ const items = await collection.aggregate([
27471
+ { $match: query },
27472
+ { $sort: sort },
27473
+ { $skip: page * limit },
27474
+ { $limit: limit }
27475
+ ]).toArray();
27476
+ const length = await collection.countDocuments(query);
27477
+ const data = paginate16(items, page, limit, length);
27478
+ setCache(cacheKey, data, 600).then(() => {
27479
+ logger31.log({
27480
+ level: "info",
27481
+ message: `Cache set for getAll building units: ${cacheKey}`
27482
+ });
27483
+ }).catch((err) => {
27484
+ logger31.log({
27485
+ level: "error",
27486
+ message: `Failed to set cache for getAll building units: ${err.message}`
27487
+ });
27488
+ });
27489
+ return data;
27490
+ } catch (error) {
27491
+ logger31.log({ level: "error", message: `${error}` });
27492
+ throw error;
27493
+ }
27494
+ }
27495
+ async function getById(_id) {
27496
+ try {
27497
+ _id = new ObjectId41(_id);
27498
+ } catch (error) {
27499
+ throw new BadRequestError61("Invalid ID.");
27500
+ }
27501
+ const cacheKey = makeCacheKey20(namespace_collection, { _id: String(_id) });
27502
+ try {
27503
+ const cached = await getCache(cacheKey);
27504
+ if (cached) {
27505
+ logger31.log({
27506
+ level: "info",
27507
+ message: `Cache hit for getById building unit: ${cacheKey}`
27508
+ });
27509
+ return cached;
27510
+ }
27511
+ const result = await collection.findOne({
27512
+ _id,
27513
+ deletedAt: { $in: ["", null] }
27514
+ });
27515
+ if (!result) {
27516
+ throw new BadRequestError61("Building unit not found.");
27517
+ }
27518
+ setCache(cacheKey, result, 300).then(() => {
27519
+ logger31.log({
27520
+ level: "info",
27521
+ message: `Cache set for building unit by id: ${cacheKey}`
27522
+ });
27523
+ }).catch((err) => {
27524
+ logger31.log({
27525
+ level: "error",
27526
+ message: `Failed to set cache for building unit by id: ${err.message}`
27527
+ });
27528
+ });
27529
+ return result;
27530
+ } catch (error) {
27531
+ if (error instanceof AppError15) {
27532
+ throw error;
27533
+ } else {
27534
+ throw new InternalServerError26("Failed to get building unit.");
27535
+ }
27536
+ }
27537
+ }
27538
+ return {
27539
+ createIndex,
27540
+ add,
27541
+ getAll,
27542
+ getById
27543
+ };
27544
+ }
27545
+
27546
+ // src/controllers/building.controller.ts
27547
+ import { BadRequestError as BadRequestError62, logger as logger32 } from "@eeplatform/nodejs-utils";
27548
+ import Joi34 from "joi";
27549
+ function useBuildingController() {
27550
+ const { getAll: _getAll, getById: _getById, add: _add } = useBuildingRepo();
27551
+ async function createBuilding(req, res, next) {
27552
+ const value = req.body;
27553
+ const validation = Joi34.object({
27554
+ name: Joi34.string().required(),
27555
+ school: Joi34.string().hex().required(),
27556
+ levels: Joi34.number().integer().min(1).required(),
27557
+ serial: Joi34.string().optional().allow("", null),
27558
+ status: Joi34.string().optional().allow("", null)
27559
+ });
27560
+ const { error } = validation.validate(value);
27561
+ if (error) {
27562
+ next(new BadRequestError62(error.message));
27563
+ logger32.info(`Controller: ${error.message}`);
27564
+ return;
27565
+ }
27566
+ try {
27567
+ const result = await _add(value);
27568
+ res.json(result);
27569
+ return;
27570
+ } catch (error2) {
27571
+ next(error2);
27572
+ }
27573
+ }
27574
+ async function getAll(req, res, next) {
27575
+ const query = req.query;
27576
+ const validation = Joi34.object({
27577
+ page: Joi34.number().min(1).optional().allow("", null),
27578
+ limit: Joi34.number().min(1).optional().allow("", null),
27579
+ search: Joi34.string().optional().allow("", null),
27580
+ school: Joi34.string().hex().optional().allow("", null),
27581
+ status: Joi34.string().optional().allow("", null)
27582
+ });
27583
+ const { error } = validation.validate(query);
27584
+ if (error) {
27585
+ next(new BadRequestError62(error.message));
27586
+ return;
27587
+ }
27588
+ const page = parseInt(req.query.page) ?? 1;
27589
+ let limit = parseInt(req.query.limit) ?? 20;
27590
+ limit = isNaN(limit) ? 20 : limit;
27591
+ const sort = req.query.sort ? String(req.query.sort).split(",") : "";
27592
+ const sortOrder = req.query.sortOrder ? String(req.query.sortOrder).split(",") : "";
27593
+ const sortObj = {};
27594
+ if (sort && Array.isArray(sort) && sort.length && sortOrder && Array.isArray(sortOrder) && sortOrder.length) {
27595
+ sort.forEach((field, index) => {
27596
+ sortObj[field] = sortOrder[index] === "desc" ? -1 : 1;
27597
+ });
27598
+ }
27599
+ const status = req.query.status ?? "active";
27600
+ const school = req.query.school ?? "";
27601
+ const search = req.query.search ?? "";
27602
+ try {
27603
+ const buildings = await _getAll({
27604
+ page,
27605
+ limit,
27606
+ sort: sortObj,
27607
+ status,
27608
+ school,
27609
+ search
27610
+ });
27611
+ res.json(buildings);
27612
+ return;
27613
+ } catch (error2) {
27614
+ next(error2);
27615
+ }
27616
+ }
27617
+ async function getById(req, res, next) {
27618
+ const id = req.params.id;
27619
+ const validation = Joi34.object({
27620
+ id: Joi34.string().hex().required()
27621
+ });
27622
+ const { error } = validation.validate({ id });
27623
+ if (error) {
27624
+ next(new BadRequestError62(error.message));
27625
+ return;
27626
+ }
27627
+ try {
27628
+ const building = await _getById(id);
27629
+ res.json({
27630
+ message: "Successfully retrieved building.",
27631
+ data: { building }
27632
+ });
27633
+ return;
27634
+ } catch (error2) {
27635
+ next(error2);
27636
+ }
27637
+ }
27638
+ return {
27639
+ createBuilding,
27640
+ getAll,
27641
+ getById
27642
+ };
27643
+ }
27644
+
27645
+ // src/controllers/building-unit.controller.ts
27646
+ import { BadRequestError as BadRequestError63 } from "@eeplatform/nodejs-utils";
27647
+ import Joi35 from "joi";
27648
+
27649
+ // src/services/building-unit.service.ts
27650
+ import { useAtlas as useAtlas33 } from "@eeplatform/nodejs-utils";
27651
+ function useBuildingUnitService() {
27652
+ const { add: _add } = useBuildingUnitRepo();
27653
+ async function add(value) {
27654
+ const session = useAtlas33.getClient()?.startSession();
27655
+ if (!session) {
27656
+ throw new Error("Unable to start session for building unit service.");
27657
+ }
27658
+ try {
27659
+ await session.startTransaction();
27660
+ for (let index = 0; index < value.qty; index++) {
27661
+ await _add({ ...value.building }, session);
27662
+ }
27663
+ await session.commitTransaction();
27664
+ return "Building unit added successfully.";
27665
+ } catch (error) {
27666
+ await session.abortTransaction();
27667
+ throw error;
27668
+ } finally {
27669
+ session.endSession();
27670
+ }
27671
+ }
27672
+ return {
27673
+ add
27674
+ };
27675
+ }
27676
+
27677
+ // src/controllers/building-unit.controller.ts
27678
+ function useBuildingUnitController() {
27679
+ const { getAll: _getAll, getById: _getById } = useBuildingUnitRepo();
27680
+ const { add: _add } = useBuildingUnitService();
27681
+ async function add(req, res, next) {
27682
+ const data = req.body;
27683
+ const validation = Joi35.object({
27684
+ building: Joi35.object({
27685
+ school: Joi35.string().hex().required(),
27686
+ name: Joi35.string().optional().allow("", null),
27687
+ building: Joi35.string().hex().required(),
27688
+ buildingName: Joi35.string().optional().allow("", null),
27689
+ level: Joi35.number().integer().min(1).required(),
27690
+ category: Joi35.string().required(),
27691
+ type: Joi35.string().required(),
27692
+ seating_capacity: Joi35.number().integer().min(0).required(),
27693
+ standing_capacity: Joi35.number().integer().min(0).required(),
27694
+ description: Joi35.string().optional().allow("", null),
27695
+ unit_of_measurement: Joi35.string().valid("sqm").required(),
27696
+ area: Joi35.number().positive().required(),
27697
+ status: Joi35.string().optional().allow("", null)
27698
+ }),
27699
+ qty: Joi35.number().integer().min(1).max(20).optional().default(1)
27700
+ });
27701
+ const { error } = validation.validate(data);
27702
+ if (error) {
27703
+ next(new BadRequestError63(error.message));
27704
+ return;
27705
+ }
27706
+ try {
27707
+ const buildingUnit = await _add(data);
27708
+ res.json({
27709
+ message: "Building unit added successfully.",
27710
+ data: { buildingUnit }
27711
+ });
27712
+ } catch (error2) {
27713
+ next(error2);
27714
+ }
27715
+ }
27716
+ async function getAll(req, res, next) {
27717
+ const query = req.query;
27718
+ const validation = Joi35.object({
27719
+ page: Joi35.number().min(1).optional().allow("", null),
27720
+ limit: Joi35.number().min(1).optional().allow("", null),
27721
+ search: Joi35.string().optional().allow("", null),
27722
+ school: Joi35.string().hex().optional().allow("", null),
27723
+ building: Joi35.string().hex().optional().allow("", null),
27724
+ status: Joi35.string().optional().allow("", null)
27725
+ });
27726
+ const { error } = validation.validate(query);
27727
+ if (error) {
27728
+ next(new BadRequestError63(error.message));
27729
+ return;
27730
+ }
27731
+ const page = parseInt(req.query.page) ?? 1;
27732
+ let limit = parseInt(req.query.limit) ?? 20;
27733
+ limit = isNaN(limit) ? 20 : limit;
27734
+ const sort = req.query.sort ? String(req.query.sort).split(",") : "";
27735
+ const sortOrder = req.query.sortOrder ? String(req.query.sortOrder).split(",") : "";
27736
+ const sortObj = {};
27737
+ if (sort && Array.isArray(sort) && sort.length && sortOrder && Array.isArray(sortOrder) && sortOrder.length) {
27738
+ sort.forEach((field, index) => {
27739
+ sortObj[field] = sortOrder[index] === "desc" ? -1 : 1;
27740
+ });
27741
+ }
27742
+ const status = req.query.status ?? "active";
27743
+ const school = req.query.school ?? "";
27744
+ const building = req.query.building ?? "";
27745
+ const search = req.query.search ?? "";
27746
+ try {
27747
+ const buildings = await _getAll({
27748
+ page,
27749
+ limit,
27750
+ sort: sortObj,
27751
+ status,
27752
+ school,
27753
+ search,
27754
+ building
27755
+ });
27756
+ res.json(buildings);
27757
+ return;
27758
+ } catch (error2) {
27759
+ next(error2);
27760
+ }
27761
+ }
27762
+ async function getById(req, res, next) {
27763
+ const id = req.params.id;
27764
+ const validation = Joi35.object({
27765
+ id: Joi35.string().hex().required()
27766
+ });
27767
+ const { error } = validation.validate({ id });
27768
+ if (error) {
27769
+ next(new BadRequestError63(error.message));
27770
+ return;
27771
+ }
27772
+ try {
27773
+ const buildingUnit = await _getById(id);
27774
+ res.json({
27775
+ message: "Successfully retrieved building unit.",
27776
+ data: { buildingUnit }
27777
+ });
27778
+ return;
27779
+ } catch (error2) {
27780
+ next(error2);
27781
+ }
27782
+ }
27783
+ return {
27784
+ add,
27785
+ getAll,
27786
+ getById
27787
+ };
27788
+ }
27038
27789
  export {
27039
27790
  ACCESS_TOKEN_EXPIRY,
27040
27791
  ACCESS_TOKEN_SECRET,
@@ -27053,6 +27804,8 @@ export {
27053
27804
  MAILER_TRANSPORT_PORT,
27054
27805
  MAILER_TRANSPORT_SECURE,
27055
27806
  MAddress,
27807
+ MBuilding,
27808
+ MBuildingUnit,
27056
27809
  MDivision,
27057
27810
  MEntity,
27058
27811
  MFile,
@@ -27093,6 +27846,8 @@ export {
27093
27846
  addressSchema,
27094
27847
  isDev,
27095
27848
  schema,
27849
+ schemaBuilding,
27850
+ schemaBuildingUnit,
27096
27851
  schemaDivision,
27097
27852
  schemaRegion,
27098
27853
  schemaSchool,
@@ -27100,10 +27855,15 @@ export {
27100
27855
  useAddressRepo,
27101
27856
  useAuthController,
27102
27857
  useAuthService,
27858
+ useBuildingController,
27859
+ useBuildingRepo,
27860
+ useBuildingUnitController,
27861
+ useBuildingUnitRepo,
27103
27862
  useCounterModel,
27104
27863
  useCounterRepo,
27105
27864
  useDivisionController,
27106
27865
  useDivisionRepo,
27866
+ useDivisionService,
27107
27867
  useEntityController,
27108
27868
  useEntityRepo,
27109
27869
  useFileController,
@@ -27134,6 +27894,7 @@ export {
27134
27894
  usePromoCodeRepo,
27135
27895
  useRegionController,
27136
27896
  useRegionRepo,
27897
+ useRegionService,
27137
27898
  useRoleController,
27138
27899
  useRoleRepo,
27139
27900
  useSchoolController,