@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.js CHANGED
@@ -11342,6 +11342,8 @@ __export(src_exports, {
11342
11342
  MAILER_TRANSPORT_PORT: () => MAILER_TRANSPORT_PORT,
11343
11343
  MAILER_TRANSPORT_SECURE: () => MAILER_TRANSPORT_SECURE,
11344
11344
  MAddress: () => MAddress,
11345
+ MBuilding: () => MBuilding,
11346
+ MBuildingUnit: () => MBuildingUnit,
11345
11347
  MDivision: () => MDivision,
11346
11348
  MEntity: () => MEntity,
11347
11349
  MFile: () => MFile,
@@ -11382,6 +11384,8 @@ __export(src_exports, {
11382
11384
  addressSchema: () => addressSchema,
11383
11385
  isDev: () => isDev,
11384
11386
  schema: () => schema,
11387
+ schemaBuilding: () => schemaBuilding,
11388
+ schemaBuildingUnit: () => schemaBuildingUnit,
11385
11389
  schemaDivision: () => schemaDivision,
11386
11390
  schemaRegion: () => schemaRegion,
11387
11391
  schemaSchool: () => schemaSchool,
@@ -11389,10 +11393,15 @@ __export(src_exports, {
11389
11393
  useAddressRepo: () => useAddressRepo,
11390
11394
  useAuthController: () => useAuthController,
11391
11395
  useAuthService: () => useAuthService,
11396
+ useBuildingController: () => useBuildingController,
11397
+ useBuildingRepo: () => useBuildingRepo,
11398
+ useBuildingUnitController: () => useBuildingUnitController,
11399
+ useBuildingUnitRepo: () => useBuildingUnitRepo,
11392
11400
  useCounterModel: () => useCounterModel,
11393
11401
  useCounterRepo: () => useCounterRepo,
11394
11402
  useDivisionController: () => useDivisionController,
11395
11403
  useDivisionRepo: () => useDivisionRepo,
11404
+ useDivisionService: () => useDivisionService,
11396
11405
  useEntityController: () => useEntityController,
11397
11406
  useEntityRepo: () => useEntityRepo,
11398
11407
  useFileController: () => useFileController,
@@ -11423,6 +11432,7 @@ __export(src_exports, {
11423
11432
  usePromoCodeRepo: () => usePromoCodeRepo,
11424
11433
  useRegionController: () => useRegionController,
11425
11434
  useRegionRepo: () => useRegionRepo,
11435
+ useRegionService: () => useRegionService,
11426
11436
  useRoleController: () => useRoleController,
11427
11437
  useRoleRepo: () => useRoleRepo,
11428
11438
  useSchoolController: () => useSchoolController,
@@ -25597,10 +25607,6 @@ function useRegionRepo() {
25597
25607
  };
25598
25608
  }
25599
25609
 
25600
- // src/controllers/region.controller.ts
25601
- var import_nodejs_utils58 = require("@eeplatform/nodejs-utils");
25602
- var import_joi28 = __toESM(require("joi"));
25603
-
25604
25610
  // src/services/region.service.ts
25605
25611
  var import_nodejs_utils57 = require("@eeplatform/nodejs-utils");
25606
25612
  function useRegionService() {
@@ -25640,6 +25646,8 @@ function useRegionService() {
25640
25646
  }
25641
25647
 
25642
25648
  // src/controllers/region.controller.ts
25649
+ var import_nodejs_utils58 = require("@eeplatform/nodejs-utils");
25650
+ var import_joi28 = __toESM(require("joi"));
25643
25651
  function useRegionController() {
25644
25652
  const {
25645
25653
  getAll: _getAll,
@@ -26151,10 +26159,6 @@ function useDivisionRepo() {
26151
26159
  };
26152
26160
  }
26153
26161
 
26154
- // src/controllers/division.controller.ts
26155
- var import_nodejs_utils62 = require("@eeplatform/nodejs-utils");
26156
- var import_joi30 = __toESM(require("joi"));
26157
-
26158
26162
  // src/services/division.service.ts
26159
26163
  var import_nodejs_utils61 = require("@eeplatform/nodejs-utils");
26160
26164
  function useDivisionService() {
@@ -26194,6 +26198,8 @@ function useDivisionService() {
26194
26198
  }
26195
26199
 
26196
26200
  // src/controllers/division.controller.ts
26201
+ var import_nodejs_utils62 = require("@eeplatform/nodejs-utils");
26202
+ var import_joi30 = __toESM(require("joi"));
26197
26203
  function useDivisionController() {
26198
26204
  const {
26199
26205
  getAll: _getAll,
@@ -26949,6 +26955,743 @@ function useSchoolController() {
26949
26955
  approveSchool
26950
26956
  };
26951
26957
  }
26958
+
26959
+ // src/models/building.model.ts
26960
+ var import_nodejs_utils66 = require("@eeplatform/nodejs-utils");
26961
+ var import_joi33 = __toESM(require("joi"));
26962
+ var import_mongodb39 = require("mongodb");
26963
+ var schemaBuilding = import_joi33.default.object({
26964
+ _id: import_joi33.default.string().hex().optional(),
26965
+ school: import_joi33.default.string().hex().required(),
26966
+ serial: import_joi33.default.string().optional().allow("", null),
26967
+ name: import_joi33.default.string().required(),
26968
+ levels: import_joi33.default.number().integer().min(1).required(),
26969
+ createdAt: import_joi33.default.date().optional().allow("", null),
26970
+ updatedAt: import_joi33.default.date().optional().allow("", null),
26971
+ deletedAt: import_joi33.default.date().optional().allow("", null),
26972
+ status: import_joi33.default.string().optional().allow("", null)
26973
+ });
26974
+ var schemaBuildingUnit = import_joi33.default.object({
26975
+ _id: import_joi33.default.string().hex().optional(),
26976
+ school: import_joi33.default.string().hex().required(),
26977
+ name: import_joi33.default.string().optional().allow("", null),
26978
+ building: import_joi33.default.string().hex().required(),
26979
+ buildingName: import_joi33.default.string().optional().allow("", null),
26980
+ level: import_joi33.default.number().integer().min(1).required(),
26981
+ category: import_joi33.default.string().required(),
26982
+ type: import_joi33.default.string().required(),
26983
+ seating_capacity: import_joi33.default.number().integer().min(0).required(),
26984
+ standing_capacity: import_joi33.default.number().integer().min(0).required(),
26985
+ description: import_joi33.default.string().optional().allow("", null),
26986
+ unit_of_measurement: import_joi33.default.string().valid("sqm").required(),
26987
+ area: import_joi33.default.number().positive().required(),
26988
+ status: import_joi33.default.string().optional().allow("", null)
26989
+ });
26990
+ function MBuilding(value) {
26991
+ const { error } = schemaBuilding.validate(value);
26992
+ if (error) {
26993
+ import_nodejs_utils66.logger.info(`Building Model: ${error.message}`);
26994
+ throw new import_nodejs_utils66.BadRequestError(error.message);
26995
+ }
26996
+ if (value._id && typeof value._id === "string") {
26997
+ try {
26998
+ value._id = new import_mongodb39.ObjectId(value._id);
26999
+ } catch (error2) {
27000
+ throw new import_nodejs_utils66.BadRequestError("Invalid _id format");
27001
+ }
27002
+ }
27003
+ try {
27004
+ value.school = new import_mongodb39.ObjectId(value.school);
27005
+ } catch (error2) {
27006
+ throw new import_nodejs_utils66.BadRequestError("Invalid school format");
27007
+ }
27008
+ return {
27009
+ _id: value._id ?? void 0,
27010
+ school: value.school,
27011
+ serial: value.serial ?? "",
27012
+ name: value.name ?? "",
27013
+ levels: value.levels ?? 0,
27014
+ status: value.status ?? "active",
27015
+ createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
27016
+ updatedAt: value.updatedAt ?? "",
27017
+ deletedAt: value.deletedAt ?? ""
27018
+ };
27019
+ }
27020
+ function MBuildingUnit(value) {
27021
+ const { error } = schemaBuildingUnit.validate(value);
27022
+ if (error) {
27023
+ import_nodejs_utils66.logger.info(`Building Unit Model: ${error.message}`);
27024
+ throw new import_nodejs_utils66.BadRequestError(error.message);
27025
+ }
27026
+ if (value._id && typeof value._id === "string") {
27027
+ try {
27028
+ value._id = new import_mongodb39.ObjectId(value._id);
27029
+ } catch (error2) {
27030
+ throw new import_nodejs_utils66.BadRequestError("Invalid ID");
27031
+ }
27032
+ }
27033
+ try {
27034
+ value.school = new import_mongodb39.ObjectId(value.school);
27035
+ } catch (error2) {
27036
+ throw new import_nodejs_utils66.BadRequestError("Invalid school ID");
27037
+ }
27038
+ try {
27039
+ value.building = new import_mongodb39.ObjectId(value.building);
27040
+ } catch (error2) {
27041
+ throw new import_nodejs_utils66.BadRequestError("Invalid building ID");
27042
+ }
27043
+ return {
27044
+ _id: value._id ?? void 0,
27045
+ school: value.school,
27046
+ name: value.name ?? "",
27047
+ building: value.building,
27048
+ buildingName: value.buildingName ?? "",
27049
+ level: value.level ?? 0,
27050
+ category: value.category ?? "",
27051
+ type: value.type ?? "",
27052
+ seating_capacity: value.seating_capacity ?? 0,
27053
+ standing_capacity: value.standing_capacity ?? 0,
27054
+ description: value.description ?? "",
27055
+ unit_of_measurement: value.unit_of_measurement ?? "sqm",
27056
+ area: value.area ?? 0,
27057
+ status: value.status ?? "active",
27058
+ createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
27059
+ updatedAt: value.updatedAt ?? "",
27060
+ deletedAt: value.deletedAt ?? ""
27061
+ };
27062
+ }
27063
+
27064
+ // src/repositories/building.repository.ts
27065
+ var import_nodejs_utils67 = require("@eeplatform/nodejs-utils");
27066
+ var import_mongodb40 = require("mongodb");
27067
+ function useBuildingRepo() {
27068
+ const db = import_nodejs_utils67.useAtlas.getDb();
27069
+ if (!db) {
27070
+ throw new Error("Unable to connect to server.");
27071
+ }
27072
+ const namespace_collection = "school.buildings";
27073
+ const collection = db.collection(namespace_collection);
27074
+ const { getCache, setCache, delNamespace } = (0, import_nodejs_utils67.useCache)(namespace_collection);
27075
+ async function createIndex() {
27076
+ try {
27077
+ await collection.createIndex([
27078
+ { name: 1 },
27079
+ { school: 1 },
27080
+ { createdAt: 1 }
27081
+ ]);
27082
+ } catch (error) {
27083
+ throw new Error("Failed to create index on buildings.");
27084
+ }
27085
+ }
27086
+ async function createTextIndex() {
27087
+ try {
27088
+ await collection.createIndex({
27089
+ name: "text"
27090
+ });
27091
+ } catch (error) {
27092
+ throw new Error("Failed to create text index on building name.");
27093
+ }
27094
+ }
27095
+ async function add(value, session) {
27096
+ try {
27097
+ value = MBuilding(value);
27098
+ const res = await collection.insertOne(value, { session });
27099
+ delCachedData();
27100
+ return res.insertedId;
27101
+ } catch (error) {
27102
+ import_nodejs_utils67.logger.log({
27103
+ level: "error",
27104
+ message: error.message
27105
+ });
27106
+ if (error instanceof import_nodejs_utils67.AppError) {
27107
+ throw error;
27108
+ } else {
27109
+ const isDuplicated = error.message.includes("duplicate");
27110
+ if (isDuplicated) {
27111
+ throw new import_nodejs_utils67.BadRequestError("Building already exists.");
27112
+ }
27113
+ throw new Error("Failed to create building.");
27114
+ }
27115
+ }
27116
+ }
27117
+ async function getAll({
27118
+ search = "",
27119
+ page = 1,
27120
+ limit = 10,
27121
+ sort = {},
27122
+ school = "",
27123
+ status = "active"
27124
+ } = {}) {
27125
+ page = page > 0 ? page - 1 : 0;
27126
+ const query = {
27127
+ status
27128
+ };
27129
+ sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
27130
+ if (search) {
27131
+ query.$text = { $search: search };
27132
+ }
27133
+ if (school) {
27134
+ try {
27135
+ query.school = new import_mongodb40.ObjectId(school);
27136
+ } catch (error) {
27137
+ throw new import_nodejs_utils67.BadRequestError("Invalid school ID.");
27138
+ }
27139
+ }
27140
+ const cacheParams = {
27141
+ page,
27142
+ limit,
27143
+ sort: JSON.stringify(sort)
27144
+ };
27145
+ if (search)
27146
+ cacheParams.search = search;
27147
+ if (school)
27148
+ cacheParams.school = school;
27149
+ if (status !== "active")
27150
+ cacheParams.status = status;
27151
+ const cacheKey = (0, import_nodejs_utils67.makeCacheKey)(namespace_collection, cacheParams);
27152
+ import_nodejs_utils67.logger.log({
27153
+ level: "info",
27154
+ message: `Cache key for getAll buildings: ${cacheKey}`
27155
+ });
27156
+ try {
27157
+ const cached = await getCache(cacheKey);
27158
+ if (cached) {
27159
+ import_nodejs_utils67.logger.log({
27160
+ level: "info",
27161
+ message: `Cache hit for getAll buildings: ${cacheKey}`
27162
+ });
27163
+ return cached;
27164
+ }
27165
+ const items = await collection.aggregate([
27166
+ { $match: query },
27167
+ { $sort: sort },
27168
+ { $skip: page * limit },
27169
+ { $limit: limit }
27170
+ ]).toArray();
27171
+ const length = await collection.countDocuments(query);
27172
+ const data = (0, import_nodejs_utils67.paginate)(items, page, limit, length);
27173
+ setCache(cacheKey, data, 600).then(() => {
27174
+ import_nodejs_utils67.logger.log({
27175
+ level: "info",
27176
+ message: `Cache set for getAll buildings: ${cacheKey}`
27177
+ });
27178
+ }).catch((err) => {
27179
+ import_nodejs_utils67.logger.log({
27180
+ level: "error",
27181
+ message: `Failed to set cache for getAll buildings: ${err.message}`
27182
+ });
27183
+ });
27184
+ return data;
27185
+ } catch (error) {
27186
+ import_nodejs_utils67.logger.log({ level: "error", message: `${error}` });
27187
+ throw error;
27188
+ }
27189
+ }
27190
+ async function getById(_id) {
27191
+ try {
27192
+ _id = new import_mongodb40.ObjectId(_id);
27193
+ } catch (error) {
27194
+ throw new import_nodejs_utils67.BadRequestError("Invalid ID.");
27195
+ }
27196
+ const cacheKey = (0, import_nodejs_utils67.makeCacheKey)(namespace_collection, { _id: String(_id) });
27197
+ try {
27198
+ const cached = await getCache(cacheKey);
27199
+ if (cached) {
27200
+ import_nodejs_utils67.logger.log({
27201
+ level: "info",
27202
+ message: `Cache hit for getById building: ${cacheKey}`
27203
+ });
27204
+ return cached;
27205
+ }
27206
+ const result = await collection.findOne({
27207
+ _id,
27208
+ deletedAt: { $in: ["", null] }
27209
+ });
27210
+ if (!result) {
27211
+ throw new import_nodejs_utils67.BadRequestError("Building not found.");
27212
+ }
27213
+ setCache(cacheKey, result, 300).then(() => {
27214
+ import_nodejs_utils67.logger.log({
27215
+ level: "info",
27216
+ message: `Cache set for building by id: ${cacheKey}`
27217
+ });
27218
+ }).catch((err) => {
27219
+ import_nodejs_utils67.logger.log({
27220
+ level: "error",
27221
+ message: `Failed to set cache for building by id: ${err.message}`
27222
+ });
27223
+ });
27224
+ return result;
27225
+ } catch (error) {
27226
+ if (error instanceof import_nodejs_utils67.AppError) {
27227
+ throw error;
27228
+ } else {
27229
+ throw new import_nodejs_utils67.InternalServerError("Failed to get building.");
27230
+ }
27231
+ }
27232
+ }
27233
+ function delCachedData() {
27234
+ delNamespace().then(() => {
27235
+ import_nodejs_utils67.logger.log({
27236
+ level: "info",
27237
+ message: `Cache namespace cleared for ${namespace_collection}`
27238
+ });
27239
+ }).catch((err) => {
27240
+ import_nodejs_utils67.logger.log({
27241
+ level: "error",
27242
+ message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
27243
+ });
27244
+ });
27245
+ }
27246
+ return {
27247
+ createIndex,
27248
+ createTextIndex,
27249
+ add,
27250
+ getAll,
27251
+ getById
27252
+ };
27253
+ }
27254
+
27255
+ // src/repositories/building-unit.repository.ts
27256
+ var import_nodejs_utils68 = require("@eeplatform/nodejs-utils");
27257
+ var import_mongodb41 = require("mongodb");
27258
+ function useBuildingUnitRepo() {
27259
+ const db = import_nodejs_utils68.useAtlas.getDb();
27260
+ if (!db) {
27261
+ throw new Error("Unable to connect to server.");
27262
+ }
27263
+ const namespace_collection = "school.building-units";
27264
+ const collection = db.collection(namespace_collection);
27265
+ const { getCache, setCache, delNamespace } = (0, import_nodejs_utils68.useCache)(namespace_collection);
27266
+ async function createIndex() {
27267
+ try {
27268
+ await collection.createIndexes([
27269
+ { key: { school: 1 } },
27270
+ { key: { building: 1 } },
27271
+ { key: { status: 1 } },
27272
+ { key: { createdAt: 1 } },
27273
+ {
27274
+ key: {
27275
+ name: "text",
27276
+ buildingName: "text",
27277
+ category: "text",
27278
+ type: "text"
27279
+ }
27280
+ }
27281
+ ]);
27282
+ } catch (error) {
27283
+ throw new Error("Failed to create index on building units.");
27284
+ }
27285
+ }
27286
+ function delCachedData() {
27287
+ delNamespace().then(() => {
27288
+ import_nodejs_utils68.logger.log({
27289
+ level: "info",
27290
+ message: `Cache namespace cleared for ${namespace_collection}`
27291
+ });
27292
+ }).catch((err) => {
27293
+ import_nodejs_utils68.logger.log({
27294
+ level: "error",
27295
+ message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
27296
+ });
27297
+ });
27298
+ }
27299
+ async function add(value, session) {
27300
+ try {
27301
+ value = MBuildingUnit(value);
27302
+ const res = await collection.insertOne(value, { session });
27303
+ delCachedData();
27304
+ return res.insertedId;
27305
+ } catch (error) {
27306
+ import_nodejs_utils68.logger.log({
27307
+ level: "error",
27308
+ message: error.message
27309
+ });
27310
+ if (error instanceof import_nodejs_utils68.AppError) {
27311
+ throw error;
27312
+ } else {
27313
+ throw new Error("Failed to create building unit.");
27314
+ }
27315
+ }
27316
+ }
27317
+ async function getAll({
27318
+ search = "",
27319
+ page = 1,
27320
+ limit = 10,
27321
+ sort = {},
27322
+ school = "",
27323
+ building = "",
27324
+ status = "active"
27325
+ } = {}) {
27326
+ page = page > 0 ? page - 1 : 0;
27327
+ const query = {
27328
+ deletedAt: { $in: ["", null] },
27329
+ status: { $in: [status, "", null] }
27330
+ };
27331
+ sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
27332
+ if (search) {
27333
+ query.$text = { $search: search };
27334
+ }
27335
+ if (school) {
27336
+ try {
27337
+ query.school = new import_mongodb41.ObjectId(school);
27338
+ } catch (error) {
27339
+ throw new import_nodejs_utils68.BadRequestError("Invalid school ID.");
27340
+ }
27341
+ }
27342
+ if (building) {
27343
+ try {
27344
+ query.building = new import_mongodb41.ObjectId(building);
27345
+ } catch (error) {
27346
+ throw new import_nodejs_utils68.BadRequestError("Invalid building ID.");
27347
+ }
27348
+ }
27349
+ const cacheParams = {
27350
+ page,
27351
+ limit,
27352
+ sort: JSON.stringify(sort)
27353
+ };
27354
+ if (search)
27355
+ cacheParams.search = search;
27356
+ if (school)
27357
+ cacheParams.school = school;
27358
+ if (building)
27359
+ cacheParams.building = building;
27360
+ if (status !== "active")
27361
+ cacheParams.status = status;
27362
+ const cacheKey = (0, import_nodejs_utils68.makeCacheKey)(namespace_collection, cacheParams);
27363
+ import_nodejs_utils68.logger.log({
27364
+ level: "info",
27365
+ message: `Cache key for getAll building units: ${cacheKey}`
27366
+ });
27367
+ try {
27368
+ const cached = await getCache(cacheKey);
27369
+ if (cached) {
27370
+ import_nodejs_utils68.logger.log({
27371
+ level: "info",
27372
+ message: `Cache hit for getAll building units: ${cacheKey}`
27373
+ });
27374
+ return cached;
27375
+ }
27376
+ const items = await collection.aggregate([
27377
+ { $match: query },
27378
+ { $sort: sort },
27379
+ { $skip: page * limit },
27380
+ { $limit: limit }
27381
+ ]).toArray();
27382
+ const length = await collection.countDocuments(query);
27383
+ const data = (0, import_nodejs_utils68.paginate)(items, page, limit, length);
27384
+ setCache(cacheKey, data, 600).then(() => {
27385
+ import_nodejs_utils68.logger.log({
27386
+ level: "info",
27387
+ message: `Cache set for getAll building units: ${cacheKey}`
27388
+ });
27389
+ }).catch((err) => {
27390
+ import_nodejs_utils68.logger.log({
27391
+ level: "error",
27392
+ message: `Failed to set cache for getAll building units: ${err.message}`
27393
+ });
27394
+ });
27395
+ return data;
27396
+ } catch (error) {
27397
+ import_nodejs_utils68.logger.log({ level: "error", message: `${error}` });
27398
+ throw error;
27399
+ }
27400
+ }
27401
+ async function getById(_id) {
27402
+ try {
27403
+ _id = new import_mongodb41.ObjectId(_id);
27404
+ } catch (error) {
27405
+ throw new import_nodejs_utils68.BadRequestError("Invalid ID.");
27406
+ }
27407
+ const cacheKey = (0, import_nodejs_utils68.makeCacheKey)(namespace_collection, { _id: String(_id) });
27408
+ try {
27409
+ const cached = await getCache(cacheKey);
27410
+ if (cached) {
27411
+ import_nodejs_utils68.logger.log({
27412
+ level: "info",
27413
+ message: `Cache hit for getById building unit: ${cacheKey}`
27414
+ });
27415
+ return cached;
27416
+ }
27417
+ const result = await collection.findOne({
27418
+ _id,
27419
+ deletedAt: { $in: ["", null] }
27420
+ });
27421
+ if (!result) {
27422
+ throw new import_nodejs_utils68.BadRequestError("Building unit not found.");
27423
+ }
27424
+ setCache(cacheKey, result, 300).then(() => {
27425
+ import_nodejs_utils68.logger.log({
27426
+ level: "info",
27427
+ message: `Cache set for building unit by id: ${cacheKey}`
27428
+ });
27429
+ }).catch((err) => {
27430
+ import_nodejs_utils68.logger.log({
27431
+ level: "error",
27432
+ message: `Failed to set cache for building unit by id: ${err.message}`
27433
+ });
27434
+ });
27435
+ return result;
27436
+ } catch (error) {
27437
+ if (error instanceof import_nodejs_utils68.AppError) {
27438
+ throw error;
27439
+ } else {
27440
+ throw new import_nodejs_utils68.InternalServerError("Failed to get building unit.");
27441
+ }
27442
+ }
27443
+ }
27444
+ return {
27445
+ createIndex,
27446
+ add,
27447
+ getAll,
27448
+ getById
27449
+ };
27450
+ }
27451
+
27452
+ // src/controllers/building.controller.ts
27453
+ var import_nodejs_utils69 = require("@eeplatform/nodejs-utils");
27454
+ var import_joi34 = __toESM(require("joi"));
27455
+ function useBuildingController() {
27456
+ const { getAll: _getAll, getById: _getById, add: _add } = useBuildingRepo();
27457
+ async function createBuilding(req, res, next) {
27458
+ const value = req.body;
27459
+ const validation = import_joi34.default.object({
27460
+ name: import_joi34.default.string().required(),
27461
+ school: import_joi34.default.string().hex().required(),
27462
+ levels: import_joi34.default.number().integer().min(1).required(),
27463
+ serial: import_joi34.default.string().optional().allow("", null),
27464
+ status: import_joi34.default.string().optional().allow("", null)
27465
+ });
27466
+ const { error } = validation.validate(value);
27467
+ if (error) {
27468
+ next(new import_nodejs_utils69.BadRequestError(error.message));
27469
+ import_nodejs_utils69.logger.info(`Controller: ${error.message}`);
27470
+ return;
27471
+ }
27472
+ try {
27473
+ const result = await _add(value);
27474
+ res.json(result);
27475
+ return;
27476
+ } catch (error2) {
27477
+ next(error2);
27478
+ }
27479
+ }
27480
+ async function getAll(req, res, next) {
27481
+ const query = req.query;
27482
+ const validation = import_joi34.default.object({
27483
+ page: import_joi34.default.number().min(1).optional().allow("", null),
27484
+ limit: import_joi34.default.number().min(1).optional().allow("", null),
27485
+ search: import_joi34.default.string().optional().allow("", null),
27486
+ school: import_joi34.default.string().hex().optional().allow("", null),
27487
+ status: import_joi34.default.string().optional().allow("", null)
27488
+ });
27489
+ const { error } = validation.validate(query);
27490
+ if (error) {
27491
+ next(new import_nodejs_utils69.BadRequestError(error.message));
27492
+ return;
27493
+ }
27494
+ const page = parseInt(req.query.page) ?? 1;
27495
+ let limit = parseInt(req.query.limit) ?? 20;
27496
+ limit = isNaN(limit) ? 20 : limit;
27497
+ const sort = req.query.sort ? String(req.query.sort).split(",") : "";
27498
+ const sortOrder = req.query.sortOrder ? String(req.query.sortOrder).split(",") : "";
27499
+ const sortObj = {};
27500
+ if (sort && Array.isArray(sort) && sort.length && sortOrder && Array.isArray(sortOrder) && sortOrder.length) {
27501
+ sort.forEach((field, index) => {
27502
+ sortObj[field] = sortOrder[index] === "desc" ? -1 : 1;
27503
+ });
27504
+ }
27505
+ const status = req.query.status ?? "active";
27506
+ const school = req.query.school ?? "";
27507
+ const search = req.query.search ?? "";
27508
+ try {
27509
+ const buildings = await _getAll({
27510
+ page,
27511
+ limit,
27512
+ sort: sortObj,
27513
+ status,
27514
+ school,
27515
+ search
27516
+ });
27517
+ res.json(buildings);
27518
+ return;
27519
+ } catch (error2) {
27520
+ next(error2);
27521
+ }
27522
+ }
27523
+ async function getById(req, res, next) {
27524
+ const id = req.params.id;
27525
+ const validation = import_joi34.default.object({
27526
+ id: import_joi34.default.string().hex().required()
27527
+ });
27528
+ const { error } = validation.validate({ id });
27529
+ if (error) {
27530
+ next(new import_nodejs_utils69.BadRequestError(error.message));
27531
+ return;
27532
+ }
27533
+ try {
27534
+ const building = await _getById(id);
27535
+ res.json({
27536
+ message: "Successfully retrieved building.",
27537
+ data: { building }
27538
+ });
27539
+ return;
27540
+ } catch (error2) {
27541
+ next(error2);
27542
+ }
27543
+ }
27544
+ return {
27545
+ createBuilding,
27546
+ getAll,
27547
+ getById
27548
+ };
27549
+ }
27550
+
27551
+ // src/controllers/building-unit.controller.ts
27552
+ var import_nodejs_utils71 = require("@eeplatform/nodejs-utils");
27553
+ var import_joi35 = __toESM(require("joi"));
27554
+
27555
+ // src/services/building-unit.service.ts
27556
+ var import_nodejs_utils70 = require("@eeplatform/nodejs-utils");
27557
+ function useBuildingUnitService() {
27558
+ const { add: _add } = useBuildingUnitRepo();
27559
+ async function add(value) {
27560
+ const session = import_nodejs_utils70.useAtlas.getClient()?.startSession();
27561
+ if (!session) {
27562
+ throw new Error("Unable to start session for building unit service.");
27563
+ }
27564
+ try {
27565
+ await session.startTransaction();
27566
+ for (let index = 0; index < value.qty; index++) {
27567
+ await _add({ ...value.building }, session);
27568
+ }
27569
+ await session.commitTransaction();
27570
+ return "Building unit added successfully.";
27571
+ } catch (error) {
27572
+ await session.abortTransaction();
27573
+ throw error;
27574
+ } finally {
27575
+ session.endSession();
27576
+ }
27577
+ }
27578
+ return {
27579
+ add
27580
+ };
27581
+ }
27582
+
27583
+ // src/controllers/building-unit.controller.ts
27584
+ function useBuildingUnitController() {
27585
+ const { getAll: _getAll, getById: _getById } = useBuildingUnitRepo();
27586
+ const { add: _add } = useBuildingUnitService();
27587
+ async function add(req, res, next) {
27588
+ const data = req.body;
27589
+ const validation = import_joi35.default.object({
27590
+ building: import_joi35.default.object({
27591
+ school: import_joi35.default.string().hex().required(),
27592
+ name: import_joi35.default.string().optional().allow("", null),
27593
+ building: import_joi35.default.string().hex().required(),
27594
+ buildingName: import_joi35.default.string().optional().allow("", null),
27595
+ level: import_joi35.default.number().integer().min(1).required(),
27596
+ category: import_joi35.default.string().required(),
27597
+ type: import_joi35.default.string().required(),
27598
+ seating_capacity: import_joi35.default.number().integer().min(0).required(),
27599
+ standing_capacity: import_joi35.default.number().integer().min(0).required(),
27600
+ description: import_joi35.default.string().optional().allow("", null),
27601
+ unit_of_measurement: import_joi35.default.string().valid("sqm").required(),
27602
+ area: import_joi35.default.number().positive().required(),
27603
+ status: import_joi35.default.string().optional().allow("", null)
27604
+ }),
27605
+ qty: import_joi35.default.number().integer().min(1).max(20).optional().default(1)
27606
+ });
27607
+ const { error } = validation.validate(data);
27608
+ if (error) {
27609
+ next(new import_nodejs_utils71.BadRequestError(error.message));
27610
+ return;
27611
+ }
27612
+ try {
27613
+ const buildingUnit = await _add(data);
27614
+ res.json({
27615
+ message: "Building unit added successfully.",
27616
+ data: { buildingUnit }
27617
+ });
27618
+ } catch (error2) {
27619
+ next(error2);
27620
+ }
27621
+ }
27622
+ async function getAll(req, res, next) {
27623
+ const query = req.query;
27624
+ const validation = import_joi35.default.object({
27625
+ page: import_joi35.default.number().min(1).optional().allow("", null),
27626
+ limit: import_joi35.default.number().min(1).optional().allow("", null),
27627
+ search: import_joi35.default.string().optional().allow("", null),
27628
+ school: import_joi35.default.string().hex().optional().allow("", null),
27629
+ building: import_joi35.default.string().hex().optional().allow("", null),
27630
+ status: import_joi35.default.string().optional().allow("", null)
27631
+ });
27632
+ const { error } = validation.validate(query);
27633
+ if (error) {
27634
+ next(new import_nodejs_utils71.BadRequestError(error.message));
27635
+ return;
27636
+ }
27637
+ const page = parseInt(req.query.page) ?? 1;
27638
+ let limit = parseInt(req.query.limit) ?? 20;
27639
+ limit = isNaN(limit) ? 20 : limit;
27640
+ const sort = req.query.sort ? String(req.query.sort).split(",") : "";
27641
+ const sortOrder = req.query.sortOrder ? String(req.query.sortOrder).split(",") : "";
27642
+ const sortObj = {};
27643
+ if (sort && Array.isArray(sort) && sort.length && sortOrder && Array.isArray(sortOrder) && sortOrder.length) {
27644
+ sort.forEach((field, index) => {
27645
+ sortObj[field] = sortOrder[index] === "desc" ? -1 : 1;
27646
+ });
27647
+ }
27648
+ const status = req.query.status ?? "active";
27649
+ const school = req.query.school ?? "";
27650
+ const building = req.query.building ?? "";
27651
+ const search = req.query.search ?? "";
27652
+ try {
27653
+ const buildings = await _getAll({
27654
+ page,
27655
+ limit,
27656
+ sort: sortObj,
27657
+ status,
27658
+ school,
27659
+ search,
27660
+ building
27661
+ });
27662
+ res.json(buildings);
27663
+ return;
27664
+ } catch (error2) {
27665
+ next(error2);
27666
+ }
27667
+ }
27668
+ async function getById(req, res, next) {
27669
+ const id = req.params.id;
27670
+ const validation = import_joi35.default.object({
27671
+ id: import_joi35.default.string().hex().required()
27672
+ });
27673
+ const { error } = validation.validate({ id });
27674
+ if (error) {
27675
+ next(new import_nodejs_utils71.BadRequestError(error.message));
27676
+ return;
27677
+ }
27678
+ try {
27679
+ const buildingUnit = await _getById(id);
27680
+ res.json({
27681
+ message: "Successfully retrieved building unit.",
27682
+ data: { buildingUnit }
27683
+ });
27684
+ return;
27685
+ } catch (error2) {
27686
+ next(error2);
27687
+ }
27688
+ }
27689
+ return {
27690
+ add,
27691
+ getAll,
27692
+ getById
27693
+ };
27694
+ }
26952
27695
  // Annotate the CommonJS export names for ESM import in node:
26953
27696
  0 && (module.exports = {
26954
27697
  ACCESS_TOKEN_EXPIRY,
@@ -26968,6 +27711,8 @@ function useSchoolController() {
26968
27711
  MAILER_TRANSPORT_PORT,
26969
27712
  MAILER_TRANSPORT_SECURE,
26970
27713
  MAddress,
27714
+ MBuilding,
27715
+ MBuildingUnit,
26971
27716
  MDivision,
26972
27717
  MEntity,
26973
27718
  MFile,
@@ -27008,6 +27753,8 @@ function useSchoolController() {
27008
27753
  addressSchema,
27009
27754
  isDev,
27010
27755
  schema,
27756
+ schemaBuilding,
27757
+ schemaBuildingUnit,
27011
27758
  schemaDivision,
27012
27759
  schemaRegion,
27013
27760
  schemaSchool,
@@ -27015,10 +27762,15 @@ function useSchoolController() {
27015
27762
  useAddressRepo,
27016
27763
  useAuthController,
27017
27764
  useAuthService,
27765
+ useBuildingController,
27766
+ useBuildingRepo,
27767
+ useBuildingUnitController,
27768
+ useBuildingUnitRepo,
27018
27769
  useCounterModel,
27019
27770
  useCounterRepo,
27020
27771
  useDivisionController,
27021
27772
  useDivisionRepo,
27773
+ useDivisionService,
27022
27774
  useEntityController,
27023
27775
  useEntityRepo,
27024
27776
  useFileController,
@@ -27049,6 +27801,7 @@ function useSchoolController() {
27049
27801
  usePromoCodeRepo,
27050
27802
  useRegionController,
27051
27803
  useRegionRepo,
27804
+ useRegionService,
27052
27805
  useRoleController,
27053
27806
  useRoleRepo,
27054
27807
  useSchoolController,