@eeplatform/basic-edu 1.3.0 → 1.3.2

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
@@ -4373,268 +4373,61 @@ function useDivisionRepo() {
4373
4373
  }
4374
4374
 
4375
4375
  // src/resources/division/division.service.ts
4376
- var import_nodejs_utils16 = require("@eeplatform/nodejs-utils");
4377
- var import_core = require("@eeplatform/core");
4378
- function useDivisionService() {
4379
- const { add: _add } = useDivisionRepo();
4380
- const { addRole } = (0, import_core.useRoleRepo)();
4381
- async function add(value) {
4382
- const session = import_nodejs_utils16.useAtlas.getClient()?.startSession();
4383
- if (!session) {
4384
- throw new Error("Unable to start session for division service.");
4385
- }
4386
- try {
4387
- session.startTransaction();
4388
- const division = await _add(value, session);
4389
- await addRole(
4390
- {
4391
- id: division.toString(),
4392
- name: "Admin",
4393
- type: "basic-edu-sdo",
4394
- permissions: ["*"],
4395
- status: "active",
4396
- default: true
4397
- },
4398
- session
4399
- );
4400
- await session.commitTransaction();
4401
- return "Division and admin role created successfully.";
4402
- } catch (error) {
4403
- session.abortTransaction();
4404
- throw error;
4405
- } finally {
4406
- session.endSession();
4407
- }
4408
- }
4409
- return {
4410
- add
4411
- };
4412
- }
4413
-
4414
- // src/resources/division/division.controller.ts
4415
- var import_nodejs_utils17 = require("@eeplatform/nodejs-utils");
4416
- var import_joi10 = __toESM(require("joi"));
4417
- function useDivisionController() {
4418
- const { add: _add } = useDivisionService();
4419
- const {
4420
- getAll: _getAll,
4421
- getById: _getById,
4422
- getByName: _getByName,
4423
- updateFieldById: _updateFieldById,
4424
- updateById: _updateById,
4425
- deleteById: _deleteById
4426
- } = useDivisionRepo();
4427
- async function add(req, res, next) {
4428
- const value = req.body;
4429
- const { error } = schemaDivision.validate(value);
4430
- if (error) {
4431
- next(new import_nodejs_utils17.BadRequestError(error.message));
4432
- return;
4433
- }
4434
- try {
4435
- const data = await _add(value);
4436
- res.json({
4437
- message: "Successfully created division.",
4438
- data
4439
- });
4440
- return;
4441
- } catch (error2) {
4442
- next(error2);
4443
- }
4444
- }
4445
- async function getAll(req, res, next) {
4446
- const query = req.query;
4447
- const validation = import_joi10.default.object({
4448
- page: import_joi10.default.number().min(1).optional().allow("", null),
4449
- limit: import_joi10.default.number().min(1).optional().allow("", null),
4450
- search: import_joi10.default.string().optional().allow("", null),
4451
- status: import_joi10.default.string().optional().allow("", null),
4452
- region: import_joi10.default.string().hex().optional().allow("", null)
4453
- });
4454
- const { error } = validation.validate(query);
4455
- const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
4456
- const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
4457
- const search = req.query.search ?? "";
4458
- const status = req.query.status ?? "active";
4459
- const region = req.query.region ?? "";
4460
- const isPageNumber = isFinite(page);
4461
- if (!isPageNumber) {
4462
- next(new import_nodejs_utils17.BadRequestError("Invalid page number."));
4463
- return;
4464
- }
4465
- const isLimitNumber = isFinite(limit);
4466
- if (!isLimitNumber) {
4467
- next(new import_nodejs_utils17.BadRequestError("Invalid limit number."));
4468
- return;
4469
- }
4470
- if (error) {
4471
- next(new import_nodejs_utils17.BadRequestError(error.message));
4472
- return;
4473
- }
4474
- try {
4475
- const data = await _getAll({ page, limit, search, status, region });
4476
- res.json(data);
4477
- return;
4478
- } catch (error2) {
4479
- next(error2);
4480
- }
4481
- }
4482
- async function getById(req, res, next) {
4483
- const id = req.params.id;
4484
- const validation = import_joi10.default.object({
4485
- id: import_joi10.default.string().hex().required()
4486
- });
4487
- const { error } = validation.validate({ id });
4488
- if (error) {
4489
- next(new import_nodejs_utils17.BadRequestError(error.message));
4490
- return;
4491
- }
4492
- try {
4493
- const data = await _getById(id);
4494
- res.json({
4495
- message: "Successfully retrieved division.",
4496
- data
4497
- });
4498
- return;
4499
- } catch (error2) {
4500
- next(error2);
4501
- }
4502
- }
4503
- async function getByName(req, res, next) {
4504
- const name = req.params.name;
4505
- const validation = import_joi10.default.object({
4506
- name: import_joi10.default.string().required()
4507
- });
4508
- const { error } = validation.validate({ name });
4509
- if (error) {
4510
- next(new import_nodejs_utils17.BadRequestError(error.message));
4511
- return;
4512
- }
4513
- try {
4514
- const data = await _getByName(name);
4515
- res.json({
4516
- message: "Successfully retrieved division.",
4517
- data
4518
- });
4519
- return;
4520
- } catch (error2) {
4521
- next(error2);
4522
- }
4523
- }
4524
- async function updateField(req, res, next) {
4525
- const _id = req.params.id;
4526
- const { field, value } = req.body;
4527
- const validation = import_joi10.default.object({
4528
- _id: import_joi10.default.string().hex().required(),
4529
- field: import_joi10.default.string().valid("name", "director", "directorName").required(),
4530
- value: import_joi10.default.string().required()
4531
- });
4532
- const { error } = validation.validate({ _id, field, value });
4533
- if (error) {
4534
- next(new import_nodejs_utils17.BadRequestError(error.message));
4535
- return;
4536
- }
4537
- try {
4538
- const message = await _updateFieldById({ _id, field, value });
4539
- res.json({ message });
4540
- return;
4541
- } catch (error2) {
4542
- next(error2);
4543
- }
4544
- }
4545
- async function updateById(req, res, next) {
4546
- const _id = req.params.id;
4547
- const payload = req.body;
4548
- const { error } = schemaDivisionUpdate.validate({ _id, ...payload });
4549
- if (error) {
4550
- next(new import_nodejs_utils17.BadRequestError(error.message));
4551
- return;
4552
- }
4553
- try {
4554
- const message = await _updateById(_id, payload);
4555
- res.json({ message });
4556
- return;
4557
- } catch (error2) {
4558
- next(error2);
4559
- }
4560
- }
4561
- async function deleteById(req, res, next) {
4562
- const _id = req.params.id;
4563
- const validation = import_joi10.default.object({
4564
- _id: import_joi10.default.string().hex().required()
4565
- });
4566
- const { error } = validation.validate({ _id });
4567
- if (error) {
4568
- next(new import_nodejs_utils17.BadRequestError(error.message));
4569
- return;
4570
- }
4571
- try {
4572
- const message = await _deleteById(_id);
4573
- res.json({ message });
4574
- return;
4575
- } catch (error2) {
4576
- next(error2);
4577
- }
4578
- }
4579
- return {
4580
- add,
4581
- getAll,
4582
- getById,
4583
- getByName,
4584
- updateField,
4585
- updateById,
4586
- deleteById
4587
- };
4588
- }
4376
+ var import_nodejs_utils20 = require("@eeplatform/nodejs-utils");
4377
+ var import_core2 = require("@eeplatform/core");
4589
4378
 
4590
4379
  // src/resources/school/school.model.ts
4591
- var import_nodejs_utils18 = require("@eeplatform/nodejs-utils");
4592
- var import_joi11 = __toESM(require("joi"));
4380
+ var import_nodejs_utils16 = require("@eeplatform/nodejs-utils");
4381
+ var import_joi10 = __toESM(require("joi"));
4593
4382
  var import_mongodb11 = require("mongodb");
4594
- var schemaSchool = import_joi11.default.object({
4595
- _id: import_joi11.default.string().hex().optional().allow(null, ""),
4596
- id: import_joi11.default.string().min(1).max(50).required(),
4597
- name: import_joi11.default.string().min(1).max(100).required(),
4598
- region: import_joi11.default.string().hex().required(),
4599
- regionName: import_joi11.default.string().min(1).max(100).optional().allow(null, ""),
4600
- division: import_joi11.default.string().hex().required(),
4601
- divisionName: import_joi11.default.string().min(1).max(100).optional().allow(null, ""),
4602
- principal: import_joi11.default.string().hex().optional().allow(null, ""),
4603
- principalName: import_joi11.default.string().min(1).max(100).optional().allow(null, ""),
4604
- street: import_joi11.default.string().max(200).optional().allow(null, ""),
4605
- barangay: import_joi11.default.string().max(200).optional().allow(null, ""),
4606
- city: import_joi11.default.string().max(100).optional().allow(null, ""),
4607
- province: import_joi11.default.string().max(100).optional().allow(null, ""),
4608
- postalCode: import_joi11.default.string().max(20).optional().allow(null, ""),
4609
- contactNumber: import_joi11.default.string().max(20).optional().allow(null, ""),
4610
- email: import_joi11.default.string().email().max(100).optional().allow(null, ""),
4611
- status: import_joi11.default.string().optional().allow(null, ""),
4612
- createdBy: import_joi11.default.string().optional().allow(null, ""),
4613
- createdAt: import_joi11.default.string().isoDate().optional().allow(null, ""),
4614
- updatedAt: import_joi11.default.string().isoDate().optional().allow(null, ""),
4615
- deletedAt: import_joi11.default.string().isoDate().optional().allow(null, "")
4383
+ var schemaSchool = import_joi10.default.object({
4384
+ _id: import_joi10.default.string().hex().optional().allow(null, ""),
4385
+ id: import_joi10.default.string().min(1).max(50).required(),
4386
+ name: import_joi10.default.string().min(1).max(100).required(),
4387
+ region: import_joi10.default.string().hex().required(),
4388
+ regionName: import_joi10.default.string().min(1).max(100).optional().allow(null, ""),
4389
+ division: import_joi10.default.string().hex().required(),
4390
+ divisionName: import_joi10.default.string().min(1).max(100).optional().allow(null, ""),
4391
+ principal: import_joi10.default.string().hex().optional().allow(null, ""),
4392
+ principalName: import_joi10.default.string().min(1).max(100).optional().allow(null, ""),
4393
+ street: import_joi10.default.string().max(200).optional().allow(null, ""),
4394
+ barangay: import_joi10.default.string().max(200).optional().allow(null, ""),
4395
+ cityMunicipality: import_joi10.default.string().max(100).optional().allow(null, ""),
4396
+ province: import_joi10.default.string().max(100).optional().allow(null, ""),
4397
+ provincePSGC: import_joi10.default.number().optional().allow(null, ""),
4398
+ cityMunicipalityPSGC: import_joi10.default.number().optional().allow(null, ""),
4399
+ postalCode: import_joi10.default.string().max(20).optional().allow(null, ""),
4400
+ contactNumber: import_joi10.default.string().max(20).optional().allow(null, ""),
4401
+ email: import_joi10.default.string().email().max(100).optional().allow(null, ""),
4402
+ status: import_joi10.default.string().optional().allow(null, ""),
4403
+ createdBy: import_joi10.default.string().optional().allow(null, ""),
4404
+ createdAt: import_joi10.default.string().isoDate().optional().allow(null, ""),
4405
+ updatedAt: import_joi10.default.string().isoDate().optional().allow(null, ""),
4406
+ deletedAt: import_joi10.default.string().isoDate().optional().allow(null, "")
4616
4407
  });
4617
- var schemaSchoolUpdate = import_joi11.default.object({
4618
- id: import_joi11.default.string().min(1).max(50).required(),
4619
- name: import_joi11.default.string().min(1).max(100).required(),
4620
- region: import_joi11.default.string().hex().required(),
4621
- regionName: import_joi11.default.string().min(1).max(100).optional().allow(null, ""),
4622
- division: import_joi11.default.string().hex().required(),
4623
- divisionName: import_joi11.default.string().min(1).max(100).optional().allow(null, ""),
4624
- principal: import_joi11.default.string().hex().optional().allow(null, ""),
4625
- principalName: import_joi11.default.string().min(1).max(100).optional().allow(null, ""),
4626
- street: import_joi11.default.string().max(200).optional().allow(null, ""),
4627
- barangay: import_joi11.default.string().max(200).optional().allow(null, ""),
4628
- city: import_joi11.default.string().max(100).optional().allow(null, ""),
4629
- province: import_joi11.default.string().max(100).optional().allow(null, ""),
4630
- postalCode: import_joi11.default.string().max(20).optional().allow(null, ""),
4631
- contactNumber: import_joi11.default.string().max(20).optional().allow(null, ""),
4632
- email: import_joi11.default.string().email().max(100).optional().allow(null, "")
4408
+ var schemaSchoolUpdate = import_joi10.default.object({
4409
+ id: import_joi10.default.string().min(1).max(50).required(),
4410
+ name: import_joi10.default.string().min(1).max(100).required(),
4411
+ region: import_joi10.default.string().hex().required(),
4412
+ regionName: import_joi10.default.string().min(1).max(100).optional().allow(null, ""),
4413
+ division: import_joi10.default.string().hex().required(),
4414
+ divisionName: import_joi10.default.string().min(1).max(100).optional().allow(null, ""),
4415
+ principal: import_joi10.default.string().hex().optional().allow(null, ""),
4416
+ principalName: import_joi10.default.string().min(1).max(100).optional().allow(null, ""),
4417
+ street: import_joi10.default.string().max(200).optional().allow(null, ""),
4418
+ barangay: import_joi10.default.string().max(200).optional().allow(null, ""),
4419
+ cityMunicipality: import_joi10.default.string().max(100).optional().allow(null, ""),
4420
+ province: import_joi10.default.string().max(100).optional().allow(null, ""),
4421
+ provincePSGC: import_joi10.default.number().optional().allow(null, ""),
4422
+ cityMunicipalityPSGC: import_joi10.default.number().optional().allow(null, ""),
4423
+ postalCode: import_joi10.default.string().max(20).optional().allow(null, ""),
4424
+ contactNumber: import_joi10.default.string().max(20).optional().allow(null, ""),
4425
+ email: import_joi10.default.string().email().max(100).optional().allow(null, "")
4633
4426
  });
4634
4427
  function modelSchool(value) {
4635
4428
  const { error } = schemaSchool.validate(value);
4636
4429
  if (error) {
4637
- throw new import_nodejs_utils18.BadRequestError(`Invalid sdo data: ${error.message}`);
4430
+ throw new import_nodejs_utils16.BadRequestError(`Invalid sdo data: ${error.message}`);
4638
4431
  }
4639
4432
  if (value._id && typeof value._id === "string") {
4640
4433
  try {
@@ -4676,12 +4469,14 @@ function modelSchool(value) {
4676
4469
  principalName: value.principalName ?? "",
4677
4470
  street: value.street ?? "",
4678
4471
  barangay: value.barangay ?? "",
4679
- city: value.city ?? "",
4472
+ cityMunicipality: value.cityMunicipality ?? "",
4680
4473
  province: value.province ?? "",
4681
4474
  postalCode: value.postalCode ?? "",
4682
4475
  contactNumber: value.contactNumber ?? "",
4683
4476
  email: value.email ?? "",
4684
4477
  status: value.status ?? "active",
4478
+ provincePSGC: value.provincePSGC ?? 0,
4479
+ cityMunicipalityPSGC: value.cityMunicipalityPSGC ?? 0,
4685
4480
  createdBy: value.createdBy ?? "",
4686
4481
  createdAt: value.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
4687
4482
  updatedAt: value.updatedAt ?? "",
@@ -4690,16 +4485,16 @@ function modelSchool(value) {
4690
4485
  }
4691
4486
 
4692
4487
  // src/resources/school/school.repository.ts
4693
- var import_nodejs_utils19 = require("@eeplatform/nodejs-utils");
4488
+ var import_nodejs_utils17 = require("@eeplatform/nodejs-utils");
4694
4489
  var import_mongodb12 = require("mongodb");
4695
4490
  function useSchoolRepo() {
4696
- const db = import_nodejs_utils19.useAtlas.getDb();
4491
+ const db = import_nodejs_utils17.useAtlas.getDb();
4697
4492
  if (!db) {
4698
4493
  throw new Error("Unable to connect to server.");
4699
4494
  }
4700
4495
  const namespace_collection = "deped.schools";
4701
4496
  const collection = db.collection(namespace_collection);
4702
- const { getCache, setCache, delNamespace } = (0, import_nodejs_utils19.useCache)(namespace_collection);
4497
+ const { getCache, setCache, delNamespace } = (0, import_nodejs_utils17.useCache)(namespace_collection);
4703
4498
  async function createIndexes() {
4704
4499
  try {
4705
4500
  await collection.createIndexes([
@@ -4714,12 +4509,12 @@ function useSchoolRepo() {
4714
4509
  }
4715
4510
  function delCachedData() {
4716
4511
  delNamespace().then(() => {
4717
- import_nodejs_utils19.logger.log({
4512
+ import_nodejs_utils17.logger.log({
4718
4513
  level: "info",
4719
4514
  message: `Cache namespace cleared for ${namespace_collection}`
4720
4515
  });
4721
4516
  }).catch((err) => {
4722
- import_nodejs_utils19.logger.log({
4517
+ import_nodejs_utils17.logger.log({
4723
4518
  level: "error",
4724
4519
  message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
4725
4520
  });
@@ -4734,7 +4529,7 @@ function useSchoolRepo() {
4734
4529
  }
4735
4530
  return res.insertedId;
4736
4531
  } catch (error) {
4737
- import_nodejs_utils19.logger.log({
4532
+ import_nodejs_utils17.logger.log({
4738
4533
  level: "error",
4739
4534
  message: `Failed to add school: ${error.message}`,
4740
4535
  metadata: {
@@ -4746,14 +4541,14 @@ function useSchoolRepo() {
4746
4541
  transactionNumber: error.errorResponse?.txnNumber || "unknown"
4747
4542
  }
4748
4543
  });
4749
- if (error instanceof import_nodejs_utils19.AppError) {
4544
+ if (error instanceof import_nodejs_utils17.AppError) {
4750
4545
  throw error;
4751
4546
  } else {
4752
4547
  const isDuplicated = error.message.includes("duplicate");
4753
4548
  if (isDuplicated) {
4754
- throw new import_nodejs_utils19.BadRequestError("Duplicate, school already exists.");
4549
+ throw new import_nodejs_utils17.BadRequestError("Duplicate, school already exists.");
4755
4550
  }
4756
- throw new import_nodejs_utils19.InternalServerError("Failed to add school.");
4551
+ throw new import_nodejs_utils17.InternalServerError("Failed to add school.");
4757
4552
  }
4758
4553
  }
4759
4554
  }
@@ -4780,15 +4575,15 @@ function useSchoolRepo() {
4780
4575
  query.$text = { $search: search };
4781
4576
  cacheKeyOptions.search = search;
4782
4577
  }
4783
- const cacheKey = (0, import_nodejs_utils19.makeCacheKey)(namespace_collection, cacheKeyOptions);
4784
- import_nodejs_utils19.logger.log({
4578
+ const cacheKey = (0, import_nodejs_utils17.makeCacheKey)(namespace_collection, cacheKeyOptions);
4579
+ import_nodejs_utils17.logger.log({
4785
4580
  level: "info",
4786
4581
  message: `Cache key for getAll schools: ${cacheKey}`
4787
4582
  });
4788
4583
  try {
4789
4584
  const cached = await getCache(cacheKey);
4790
4585
  if (cached) {
4791
- import_nodejs_utils19.logger.log({
4586
+ import_nodejs_utils17.logger.log({
4792
4587
  level: "info",
4793
4588
  message: `Cache hit for getAll schools: ${cacheKey}`
4794
4589
  });
@@ -4801,21 +4596,21 @@ function useSchoolRepo() {
4801
4596
  { $limit: limit }
4802
4597
  ]).toArray();
4803
4598
  const length = await collection.countDocuments(query);
4804
- const data = (0, import_nodejs_utils19.paginate)(items, page, limit, length);
4599
+ const data = (0, import_nodejs_utils17.paginate)(items, page, limit, length);
4805
4600
  setCache(cacheKey, data, 600).then(() => {
4806
- import_nodejs_utils19.logger.log({
4601
+ import_nodejs_utils17.logger.log({
4807
4602
  level: "info",
4808
4603
  message: `Cache set for getAll schools: ${cacheKey}`
4809
4604
  });
4810
4605
  }).catch((err) => {
4811
- import_nodejs_utils19.logger.log({
4606
+ import_nodejs_utils17.logger.log({
4812
4607
  level: "error",
4813
4608
  message: `Failed to set cache for getAll schools: ${err.message}`
4814
4609
  });
4815
4610
  });
4816
4611
  return data;
4817
4612
  } catch (error) {
4818
- import_nodejs_utils19.logger.log({ level: "error", message: `${error}` });
4613
+ import_nodejs_utils17.logger.log({ level: "error", message: `${error}` });
4819
4614
  throw error;
4820
4615
  }
4821
4616
  }
@@ -4823,7 +4618,7 @@ function useSchoolRepo() {
4823
4618
  try {
4824
4619
  _id = new import_mongodb12.ObjectId(_id);
4825
4620
  } catch (error) {
4826
- throw new import_nodejs_utils19.BadRequestError("Invalid ID.");
4621
+ throw new import_nodejs_utils17.BadRequestError("Invalid ID.");
4827
4622
  }
4828
4623
  const query = { _id };
4829
4624
  const cacheKeyOptions = {
@@ -4834,11 +4629,11 @@ function useSchoolRepo() {
4834
4629
  query.status = status;
4835
4630
  cacheKeyOptions.status = status;
4836
4631
  }
4837
- const cacheKey = (0, import_nodejs_utils19.makeCacheKey)(namespace_collection, cacheKeyOptions);
4632
+ const cacheKey = (0, import_nodejs_utils17.makeCacheKey)(namespace_collection, cacheKeyOptions);
4838
4633
  try {
4839
4634
  const cached = await getCache(cacheKey);
4840
4635
  if (cached) {
4841
- import_nodejs_utils19.logger.log({
4636
+ import_nodejs_utils17.logger.log({
4842
4637
  level: "info",
4843
4638
  message: `Cache hit for getById school: ${cacheKey}`
4844
4639
  });
@@ -4846,25 +4641,25 @@ function useSchoolRepo() {
4846
4641
  }
4847
4642
  const result = await collection.findOne(query);
4848
4643
  if (!result) {
4849
- throw new import_nodejs_utils19.BadRequestError("School not found.");
4644
+ throw new import_nodejs_utils17.BadRequestError("School not found.");
4850
4645
  }
4851
4646
  setCache(cacheKey, result, 300).then(() => {
4852
- import_nodejs_utils19.logger.log({
4647
+ import_nodejs_utils17.logger.log({
4853
4648
  level: "info",
4854
4649
  message: `Cache set for school by id: ${cacheKey}`
4855
4650
  });
4856
4651
  }).catch((err) => {
4857
- import_nodejs_utils19.logger.log({
4652
+ import_nodejs_utils17.logger.log({
4858
4653
  level: "error",
4859
4654
  message: `Failed to set cache for school by id: ${err.message}`
4860
4655
  });
4861
4656
  });
4862
4657
  return result;
4863
4658
  } catch (error) {
4864
- if (error instanceof import_nodejs_utils19.AppError) {
4659
+ if (error instanceof import_nodejs_utils17.AppError) {
4865
4660
  throw error;
4866
4661
  } else {
4867
- throw new import_nodejs_utils19.InternalServerError("Failed to get school.");
4662
+ throw new import_nodejs_utils17.InternalServerError("Failed to get school.");
4868
4663
  }
4869
4664
  }
4870
4665
  }
@@ -4872,13 +4667,13 @@ function useSchoolRepo() {
4872
4667
  try {
4873
4668
  createdBy = new import_mongodb12.ObjectId(createdBy);
4874
4669
  } catch (error) {
4875
- throw new import_nodejs_utils19.BadRequestError("Invalid ID.");
4670
+ throw new import_nodejs_utils17.BadRequestError("Invalid ID.");
4876
4671
  }
4877
- const cacheKey = (0, import_nodejs_utils19.makeCacheKey)(namespace_collection, { createdBy });
4672
+ const cacheKey = (0, import_nodejs_utils17.makeCacheKey)(namespace_collection, { createdBy });
4878
4673
  try {
4879
4674
  const cached = await getCache(cacheKey);
4880
4675
  if (cached) {
4881
- import_nodejs_utils19.logger.log({
4676
+ import_nodejs_utils17.logger.log({
4882
4677
  level: "info",
4883
4678
  message: `Cache hit for getById school: ${cacheKey}`
4884
4679
  });
@@ -4889,34 +4684,34 @@ function useSchoolRepo() {
4889
4684
  status: "pending"
4890
4685
  });
4891
4686
  if (!result) {
4892
- throw new import_nodejs_utils19.BadRequestError("School not found.");
4687
+ throw new import_nodejs_utils17.BadRequestError("School not found.");
4893
4688
  }
4894
4689
  setCache(cacheKey, result, 300).then(() => {
4895
- import_nodejs_utils19.logger.log({
4690
+ import_nodejs_utils17.logger.log({
4896
4691
  level: "info",
4897
4692
  message: `Cache set for school by id: ${cacheKey}`
4898
4693
  });
4899
4694
  }).catch((err) => {
4900
- import_nodejs_utils19.logger.log({
4695
+ import_nodejs_utils17.logger.log({
4901
4696
  level: "error",
4902
4697
  message: `Failed to set cache for school by id: ${err.message}`
4903
4698
  });
4904
4699
  });
4905
4700
  return result;
4906
4701
  } catch (error) {
4907
- if (error instanceof import_nodejs_utils19.AppError) {
4702
+ if (error instanceof import_nodejs_utils17.AppError) {
4908
4703
  throw error;
4909
4704
  } else {
4910
- throw new import_nodejs_utils19.InternalServerError("Failed to get school.");
4705
+ throw new import_nodejs_utils17.InternalServerError("Failed to get school.");
4911
4706
  }
4912
4707
  }
4913
4708
  }
4914
4709
  async function getByName(name) {
4915
- const cacheKey = (0, import_nodejs_utils19.makeCacheKey)(namespace_collection, { name });
4710
+ const cacheKey = (0, import_nodejs_utils17.makeCacheKey)(namespace_collection, { name });
4916
4711
  try {
4917
4712
  const cached = await getCache(cacheKey);
4918
4713
  if (cached) {
4919
- import_nodejs_utils19.logger.log({
4714
+ import_nodejs_utils17.logger.log({
4920
4715
  level: "info",
4921
4716
  message: `Cache hit for getByName school: ${cacheKey}`
4922
4717
  });
@@ -4927,25 +4722,25 @@ function useSchoolRepo() {
4927
4722
  deletedAt: { $in: ["", null] }
4928
4723
  });
4929
4724
  if (!result) {
4930
- throw new import_nodejs_utils19.BadRequestError("School not found.");
4725
+ throw new import_nodejs_utils17.BadRequestError("School not found.");
4931
4726
  }
4932
4727
  setCache(cacheKey, result, 300).then(() => {
4933
- import_nodejs_utils19.logger.log({
4728
+ import_nodejs_utils17.logger.log({
4934
4729
  level: "info",
4935
4730
  message: `Cache set for school by name: ${cacheKey}`
4936
4731
  });
4937
4732
  }).catch((err) => {
4938
- import_nodejs_utils19.logger.log({
4733
+ import_nodejs_utils17.logger.log({
4939
4734
  level: "error",
4940
4735
  message: `Failed to set cache for school by name: ${err.message}`
4941
4736
  });
4942
4737
  });
4943
4738
  return result;
4944
4739
  } catch (error) {
4945
- if (error instanceof import_nodejs_utils19.AppError) {
4740
+ if (error instanceof import_nodejs_utils17.AppError) {
4946
4741
  throw error;
4947
4742
  } else {
4948
- throw new import_nodejs_utils19.InternalServerError("Failed to get school.");
4743
+ throw new import_nodejs_utils17.InternalServerError("Failed to get school.");
4949
4744
  }
4950
4745
  }
4951
4746
  }
@@ -4953,7 +4748,7 @@ function useSchoolRepo() {
4953
4748
  try {
4954
4749
  _id = new import_mongodb12.ObjectId(_id);
4955
4750
  } catch (error) {
4956
- throw new import_nodejs_utils19.BadRequestError("Invalid ID.");
4751
+ throw new import_nodejs_utils17.BadRequestError("Invalid ID.");
4957
4752
  }
4958
4753
  try {
4959
4754
  await collection.updateOne(
@@ -4964,24 +4759,30 @@ function useSchoolRepo() {
4964
4759
  delCachedData();
4965
4760
  return `Successfully updated school status to ${status}.`;
4966
4761
  } catch (error) {
4967
- if (error instanceof import_nodejs_utils19.AppError) {
4762
+ if (error instanceof import_nodejs_utils17.AppError) {
4968
4763
  throw error;
4969
4764
  } else {
4970
- throw new import_nodejs_utils19.InternalServerError("Failed to update school status.");
4765
+ throw new import_nodejs_utils17.InternalServerError("Failed to update school status.");
4971
4766
  }
4972
4767
  }
4973
4768
  }
4974
4769
  async function updateFieldById({ _id, field, value } = {}, session) {
4975
- const allowedFields = ["name"];
4770
+ const allowedFields = [
4771
+ "name",
4772
+ "division",
4773
+ "divisionName",
4774
+ "region",
4775
+ "regionName"
4776
+ ];
4976
4777
  if (!allowedFields.includes(field)) {
4977
- throw new import_nodejs_utils19.BadRequestError(
4778
+ throw new import_nodejs_utils17.BadRequestError(
4978
4779
  `Field "${field}" is not allowed to be updated.`
4979
4780
  );
4980
4781
  }
4981
4782
  try {
4982
4783
  _id = new import_mongodb12.ObjectId(_id);
4983
4784
  } catch (error) {
4984
- throw new import_nodejs_utils19.BadRequestError("Invalid ID.");
4785
+ throw new import_nodejs_utils17.BadRequestError("Invalid ID.");
4985
4786
  }
4986
4787
  try {
4987
4788
  await collection.updateOne(
@@ -4992,32 +4793,56 @@ function useSchoolRepo() {
4992
4793
  delCachedData();
4993
4794
  return `Successfully updated school ${field}.`;
4994
4795
  } catch (error) {
4995
- throw new import_nodejs_utils19.InternalServerError(`Failed to update school ${field}.`);
4796
+ throw new import_nodejs_utils17.InternalServerError(`Failed to update school ${field}.`);
4797
+ }
4798
+ }
4799
+ async function updateDivisionNameByDivision(division, name, session) {
4800
+ try {
4801
+ division = new import_mongodb12.ObjectId(division);
4802
+ } catch (error) {
4803
+ throw new import_nodejs_utils17.BadRequestError("Invalid division.");
4804
+ }
4805
+ try {
4806
+ const result = await collection.updateMany(
4807
+ { division },
4808
+ { $set: { divisionName: name, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
4809
+ { session }
4810
+ );
4811
+ if (result.modifiedCount === 0) {
4812
+ import_nodejs_utils17.logger.log({
4813
+ level: "info",
4814
+ message: `No schools found for division ${division} to update divisionName.`
4815
+ });
4816
+ }
4817
+ delCachedData();
4818
+ return `Successfully updated school divisionName.`;
4819
+ } catch (error) {
4820
+ throw new import_nodejs_utils17.InternalServerError(`Failed to update school divisionName.`);
4996
4821
  }
4997
4822
  }
4998
4823
  async function updateById(_id, options, session) {
4999
4824
  const { error } = schemaSchoolUpdate.validate(options);
5000
4825
  if (error) {
5001
- throw new import_nodejs_utils19.BadRequestError(`Invalid school data: ${error.message}`);
4826
+ throw new import_nodejs_utils17.BadRequestError(`Invalid school data: ${error.message}`);
5002
4827
  }
5003
4828
  try {
5004
4829
  _id = new import_mongodb12.ObjectId(_id);
5005
4830
  } catch (error2) {
5006
- throw new import_nodejs_utils19.BadRequestError("Invalid ID.");
4831
+ throw new import_nodejs_utils17.BadRequestError("Invalid ID.");
5007
4832
  }
5008
4833
  try {
5009
4834
  await collection.updateOne({ _id }, { $set: options }, { session });
5010
4835
  delCachedData();
5011
4836
  return `Successfully updated school.`;
5012
4837
  } catch (error2) {
5013
- throw new import_nodejs_utils19.InternalServerError(`Failed to update school.`);
4838
+ throw new import_nodejs_utils17.InternalServerError(`Failed to update school.`);
5014
4839
  }
5015
4840
  }
5016
4841
  async function deleteById(_id) {
5017
4842
  try {
5018
4843
  _id = new import_mongodb12.ObjectId(_id);
5019
4844
  } catch (error) {
5020
- throw new import_nodejs_utils19.BadRequestError("Invalid ID.");
4845
+ throw new import_nodejs_utils17.BadRequestError("Invalid ID.");
5021
4846
  }
5022
4847
  try {
5023
4848
  await collection.updateOne(
@@ -5027,7 +4852,7 @@ function useSchoolRepo() {
5027
4852
  delCachedData();
5028
4853
  return "Successfully deleted school.";
5029
4854
  } catch (error) {
5030
- throw new import_nodejs_utils19.InternalServerError("Failed to delete school.");
4855
+ throw new import_nodejs_utils17.InternalServerError("Failed to delete school.");
5031
4856
  }
5032
4857
  }
5033
4858
  return {
@@ -5041,13 +4866,14 @@ function useSchoolRepo() {
5041
4866
  updateById,
5042
4867
  deleteById,
5043
4868
  getByName,
5044
- delCachedData
4869
+ delCachedData,
4870
+ updateDivisionNameByDivision
5045
4871
  };
5046
4872
  }
5047
4873
 
5048
4874
  // src/resources/school/school.service.ts
5049
- var import_nodejs_utils20 = require("@eeplatform/nodejs-utils");
5050
- var import_core2 = require("@eeplatform/core");
4875
+ var import_nodejs_utils18 = require("@eeplatform/nodejs-utils");
4876
+ var import_core = require("@eeplatform/core");
5051
4877
 
5052
4878
  // node_modules/xlsx/xlsx.mjs
5053
4879
  var XLSX = {};
@@ -33215,17 +33041,17 @@ function useSchoolService() {
33215
33041
  getById,
33216
33042
  delCachedData: delCachedSchoolData
33217
33043
  } = useSchoolRepo();
33218
- const { addRole, delCachedData: delCachedRoleData } = (0, import_core2.useRoleRepo)();
33219
- const { getUserById } = (0, import_core2.useUserRepo)();
33220
- const { add: addMember } = (0, import_core2.useMemberRepo)();
33044
+ const { addRole, delCachedData: delCachedRoleData } = (0, import_core.useRoleRepo)();
33045
+ const { getUserById } = (0, import_core.useUserRepo)();
33046
+ const { add: addMember } = (0, import_core.useMemberRepo)();
33221
33047
  async function register(value) {
33222
33048
  const { error } = schemaSchool.validate(value);
33223
33049
  if (error) {
33224
- throw new import_nodejs_utils20.BadRequestError(error.message);
33050
+ throw new import_nodejs_utils18.BadRequestError(error.message);
33225
33051
  }
33226
33052
  const existingSchool = await getPendingByCreatedBy(value.createdBy ?? "");
33227
33053
  if (existingSchool) {
33228
- throw new import_nodejs_utils20.BadRequestError(
33054
+ throw new import_nodejs_utils18.BadRequestError(
33229
33055
  "You already have a pending school registration."
33230
33056
  );
33231
33057
  }
@@ -33240,9 +33066,9 @@ function useSchoolService() {
33240
33066
  async function approve(id) {
33241
33067
  const school = await getById(id, "pending");
33242
33068
  if (!school) {
33243
- throw new import_nodejs_utils20.BadRequestError("School registration not found.");
33069
+ throw new import_nodejs_utils18.BadRequestError("School registration not found.");
33244
33070
  }
33245
- const session = import_nodejs_utils20.useAtlas.getClient()?.startSession();
33071
+ const session = import_nodejs_utils18.useAtlas.getClient()?.startSession();
33246
33072
  if (!session) {
33247
33073
  throw new Error("Unable to start session for school service.");
33248
33074
  }
@@ -33264,11 +33090,11 @@ function useSchoolService() {
33264
33090
  session
33265
33091
  );
33266
33092
  if (!school.createdBy) {
33267
- throw new import_nodejs_utils20.BadRequestError("School must have a creator.");
33093
+ throw new import_nodejs_utils18.BadRequestError("School must have a creator.");
33268
33094
  }
33269
33095
  const user = await getUserById(school.createdBy ?? "");
33270
33096
  if (!user) {
33271
- throw new import_nodejs_utils20.BadRequestError("User not found for the school creator.");
33097
+ throw new import_nodejs_utils18.BadRequestError("User not found for the school creator.");
33272
33098
  }
33273
33099
  await addMember(
33274
33100
  {
@@ -33285,7 +33111,7 @@ function useSchoolService() {
33285
33111
  await session.commitTransaction();
33286
33112
  return "School registration has been approved.";
33287
33113
  } catch (error) {
33288
- import_nodejs_utils20.logger.log({
33114
+ import_nodejs_utils18.logger.log({
33289
33115
  level: "error",
33290
33116
  message: `Error approving school registration: ${error.message}`
33291
33117
  });
@@ -33298,9 +33124,9 @@ function useSchoolService() {
33298
33124
  async function add(value) {
33299
33125
  const { error } = schemaSchool.validate(value);
33300
33126
  if (error) {
33301
- throw new import_nodejs_utils20.BadRequestError(error.message);
33127
+ throw new import_nodejs_utils18.BadRequestError(error.message);
33302
33128
  }
33303
- const session = import_nodejs_utils20.useAtlas.getClient()?.startSession();
33129
+ const session = import_nodejs_utils18.useAtlas.getClient()?.startSession();
33304
33130
  if (!session) {
33305
33131
  throw new Error("Unable to start session for school service.");
33306
33132
  }
@@ -33322,11 +33148,11 @@ function useSchoolService() {
33322
33148
  session
33323
33149
  );
33324
33150
  if (!value.createdBy) {
33325
- throw new import_nodejs_utils20.BadRequestError("School must have a creator.");
33151
+ throw new import_nodejs_utils18.BadRequestError("School must have a creator.");
33326
33152
  }
33327
33153
  const user = await getUserById(value.createdBy ?? "");
33328
33154
  if (!user) {
33329
- throw new import_nodejs_utils20.BadRequestError("User not found for the school creator.");
33155
+ throw new import_nodejs_utils18.BadRequestError("User not found for the school creator.");
33330
33156
  }
33331
33157
  await addMember(
33332
33158
  {
@@ -33343,7 +33169,7 @@ function useSchoolService() {
33343
33169
  await session.commitTransaction();
33344
33170
  return "School has been added and activated successfully.";
33345
33171
  } catch (error2) {
33346
- import_nodejs_utils20.logger.log({
33172
+ import_nodejs_utils18.logger.log({
33347
33173
  level: "error",
33348
33174
  message: `Error adding school: ${error2.message}`
33349
33175
  });
@@ -33357,7 +33183,7 @@ function useSchoolService() {
33357
33183
  const isCSV = file.mimetype.includes("csv") || file.originalname.endsWith(".csv");
33358
33184
  const isExcel = file.mimetype.includes("sheet") || file.originalname.endsWith(".xlsx") || file.originalname.endsWith(".xls");
33359
33185
  if (!isCSV && !isExcel) {
33360
- throw new import_nodejs_utils20.BadRequestError("Only CSV and Excel files are supported");
33186
+ throw new import_nodejs_utils18.BadRequestError("Only CSV and Excel files are supported");
33361
33187
  }
33362
33188
  let rawData = [];
33363
33189
  try {
@@ -33374,17 +33200,17 @@ function useSchoolService() {
33374
33200
  transformHeader: (header) => header.trim()
33375
33201
  });
33376
33202
  if (parseResult.errors.length > 0) {
33377
- throw new import_nodejs_utils20.BadRequestError(
33203
+ throw new import_nodejs_utils18.BadRequestError(
33378
33204
  `CSV parsing error: ${parseResult.errors[0].message}`
33379
33205
  );
33380
33206
  }
33381
33207
  rawData = parseResult.data;
33382
33208
  }
33383
33209
  } catch (error) {
33384
- throw new import_nodejs_utils20.BadRequestError(`File parsing error: ${error.message}`);
33210
+ throw new import_nodejs_utils18.BadRequestError(`File parsing error: ${error.message}`);
33385
33211
  }
33386
33212
  if (!rawData.length) {
33387
- throw new import_nodejs_utils20.BadRequestError("No data found in file");
33213
+ throw new import_nodejs_utils18.BadRequestError("No data found in file");
33388
33214
  }
33389
33215
  const schools = [];
33390
33216
  const errors = [];
@@ -33393,6 +33219,8 @@ function useSchoolService() {
33393
33219
  const rowNum = i + 1;
33394
33220
  const schoolId = (row.schoolId || row.id || "").toString().trim();
33395
33221
  const name = (row.name || "").toString().trim();
33222
+ const province = (row.province || "").toString().trim();
33223
+ const cityMunicipality = (row.cityMunicipality || "").toString().trim();
33396
33224
  if (!schoolId) {
33397
33225
  errors.push(`Row ${rowNum}: schoolId is required`);
33398
33226
  continue;
@@ -33401,6 +33229,14 @@ function useSchoolService() {
33401
33229
  errors.push(`Row ${rowNum}: name is required`);
33402
33230
  continue;
33403
33231
  }
33232
+ if (!province) {
33233
+ errors.push(`Row ${rowNum}: province is required`);
33234
+ continue;
33235
+ }
33236
+ if (!cityMunicipality) {
33237
+ errors.push(`Row ${rowNum}: city/municipality is required`);
33238
+ continue;
33239
+ }
33404
33240
  const school = {
33405
33241
  id: schoolId,
33406
33242
  name,
@@ -33408,6 +33244,8 @@ function useSchoolService() {
33408
33244
  regionName: payload.regionName,
33409
33245
  division: payload.division,
33410
33246
  divisionName: payload.divisionName,
33247
+ province,
33248
+ cityMunicipality,
33411
33249
  status: "active"
33412
33250
  };
33413
33251
  const { error } = schemaSchool.validate(school);
@@ -33418,7 +33256,7 @@ function useSchoolService() {
33418
33256
  schools.push(school);
33419
33257
  }
33420
33258
  if (errors.length > 0) {
33421
- throw new import_nodejs_utils20.BadRequestError(
33259
+ throw new import_nodejs_utils18.BadRequestError(
33422
33260
  `Validation errors:
33423
33261
  ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33424
33262
  ...and ${errors.length - 5} more` : ""}`
@@ -33430,14 +33268,36 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33430
33268
  skipped: 0,
33431
33269
  errors: []
33432
33270
  };
33271
+ const { getByName: getPSGCByName } = (0, import_core.usePSGCRepo)();
33433
33272
  for (let i = 0; i < schools.length; i++) {
33434
- const session = import_nodejs_utils20.useAtlas.getClient()?.startSession();
33273
+ const session = import_nodejs_utils18.useAtlas.getClient()?.startSession();
33435
33274
  if (!session) {
33436
33275
  throw new Error("Unable to start MongoDB session");
33437
33276
  }
33438
33277
  session.startTransaction();
33439
33278
  const school = schools[i];
33440
33279
  try {
33280
+ const provincePSGC = await getPSGCByName({
33281
+ name: school.province ?? "",
33282
+ type: "Prov"
33283
+ });
33284
+ if (!provincePSGC) {
33285
+ throw new import_nodejs_utils18.BadRequestError(
33286
+ `Province '${school.province}' not found in PSGC data.`
33287
+ );
33288
+ }
33289
+ const cityMunPSGC = await getPSGCByName({
33290
+ name: school.cityMunicipality ?? "",
33291
+ cityMunicipality: true,
33292
+ code: provincePSGC.code
33293
+ });
33294
+ if (!cityMunPSGC) {
33295
+ throw new import_nodejs_utils18.BadRequestError(
33296
+ `City/Municipality '${school.cityMunicipality}' not found in PSGC data.`
33297
+ );
33298
+ }
33299
+ school.provincePSGC = provincePSGC.code ?? 0;
33300
+ school.cityMunicipalityPSGC = cityMunPSGC.code ?? 0;
33441
33301
  const schoolId = await addSchool(school, session, false);
33442
33302
  await addRole(
33443
33303
  {
@@ -33484,8 +33344,8 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33484
33344
  }
33485
33345
 
33486
33346
  // src/resources/school/school.controller.ts
33487
- var import_nodejs_utils21 = require("@eeplatform/nodejs-utils");
33488
- var import_joi12 = __toESM(require("joi"));
33347
+ var import_nodejs_utils19 = require("@eeplatform/nodejs-utils");
33348
+ var import_joi11 = __toESM(require("joi"));
33489
33349
  function useSchoolController() {
33490
33350
  const {
33491
33351
  getAll: _getAll,
@@ -33505,7 +33365,7 @@ function useSchoolController() {
33505
33365
  const payload = req.body;
33506
33366
  const { error } = schemaSchool.validate(payload);
33507
33367
  if (error) {
33508
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33368
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33509
33369
  return;
33510
33370
  }
33511
33371
  try {
@@ -33518,19 +33378,19 @@ function useSchoolController() {
33518
33378
  }
33519
33379
  }
33520
33380
  async function getAll(req, res, next) {
33521
- const validation = import_joi12.default.object({
33522
- page: import_joi12.default.number().optional().allow(null, ""),
33523
- limit: import_joi12.default.number().optional().allow(null, ""),
33524
- sort: import_joi12.default.string().optional().allow(null, ""),
33525
- sortOrder: import_joi12.default.string().optional().allow(null, ""),
33526
- status: import_joi12.default.string().optional().allow(null, ""),
33527
- org: import_joi12.default.string().hex().optional().allow(null, ""),
33528
- app: import_joi12.default.string().optional().allow(null, ""),
33529
- search: import_joi12.default.string().optional().allow(null, "")
33381
+ const validation = import_joi11.default.object({
33382
+ page: import_joi11.default.number().optional().allow(null, ""),
33383
+ limit: import_joi11.default.number().optional().allow(null, ""),
33384
+ sort: import_joi11.default.string().optional().allow(null, ""),
33385
+ sortOrder: import_joi11.default.string().optional().allow(null, ""),
33386
+ status: import_joi11.default.string().optional().allow(null, ""),
33387
+ org: import_joi11.default.string().hex().optional().allow(null, ""),
33388
+ app: import_joi11.default.string().optional().allow(null, ""),
33389
+ search: import_joi11.default.string().optional().allow(null, "")
33530
33390
  });
33531
33391
  const { error } = validation.validate(req.query);
33532
33392
  if (error) {
33533
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33393
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33534
33394
  return;
33535
33395
  }
33536
33396
  const page = parseInt(req.query.page) ?? 1;
@@ -33562,10 +33422,10 @@ function useSchoolController() {
33562
33422
  }
33563
33423
  async function getByCreatedBy(req, res, next) {
33564
33424
  const createdBy = req.params.createdBy;
33565
- const validation = import_joi12.default.string().hex().required();
33425
+ const validation = import_joi11.default.string().hex().required();
33566
33426
  const { error } = validation.validate(createdBy);
33567
33427
  if (error) {
33568
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33428
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33569
33429
  return;
33570
33430
  }
33571
33431
  try {
@@ -33579,13 +33439,13 @@ function useSchoolController() {
33579
33439
  async function updateStatusById(req, res, next) {
33580
33440
  const schoolId = req.params.id;
33581
33441
  const status = req.params.status;
33582
- const validation = import_joi12.default.object({
33583
- id: import_joi12.default.string().hex().required(),
33584
- status: import_joi12.default.string().valid("active", "deleted", "suspended").required()
33442
+ const validation = import_joi11.default.object({
33443
+ id: import_joi11.default.string().hex().required(),
33444
+ status: import_joi11.default.string().valid("active", "deleted", "suspended").required()
33585
33445
  });
33586
33446
  const { error } = validation.validate({ id: schoolId, status });
33587
33447
  if (error) {
33588
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33448
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33589
33449
  return;
33590
33450
  }
33591
33451
  try {
@@ -33600,7 +33460,7 @@ function useSchoolController() {
33600
33460
  const payload = req.body;
33601
33461
  const { error } = schemaSchool.validate(payload);
33602
33462
  if (error) {
33603
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33463
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33604
33464
  return;
33605
33465
  }
33606
33466
  try {
@@ -33614,12 +33474,12 @@ function useSchoolController() {
33614
33474
  }
33615
33475
  async function approveSchool(req, res, next) {
33616
33476
  const schoolId = req.params.id;
33617
- const validation = import_joi12.default.object({
33618
- id: import_joi12.default.string().hex().required()
33477
+ const validation = import_joi11.default.object({
33478
+ id: import_joi11.default.string().hex().required()
33619
33479
  });
33620
33480
  const { error } = validation.validate({ id: schoolId });
33621
33481
  if (error) {
33622
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33482
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33623
33483
  return;
33624
33484
  }
33625
33485
  try {
@@ -33638,11 +33498,11 @@ function useSchoolController() {
33638
33498
  return;
33639
33499
  }
33640
33500
  const { region, regionName, division, divisionName } = req.body;
33641
- const validation = import_joi12.default.object({
33642
- region: import_joi12.default.string().hex().required(),
33643
- regionName: import_joi12.default.string().min(1).required(),
33644
- division: import_joi12.default.string().hex().required(),
33645
- divisionName: import_joi12.default.string().min(1).required()
33501
+ const validation = import_joi11.default.object({
33502
+ region: import_joi11.default.string().hex().required(),
33503
+ regionName: import_joi11.default.string().min(1).required(),
33504
+ division: import_joi11.default.string().hex().required(),
33505
+ divisionName: import_joi11.default.string().min(1).required()
33646
33506
  });
33647
33507
  const { error } = validation.validate({
33648
33508
  region,
@@ -33651,7 +33511,7 @@ function useSchoolController() {
33651
33511
  divisionName
33652
33512
  });
33653
33513
  if (error) {
33654
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33514
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33655
33515
  return;
33656
33516
  }
33657
33517
  try {
@@ -33671,14 +33531,14 @@ function useSchoolController() {
33671
33531
  async function updateFieldById(req, res, next) {
33672
33532
  const _id = req.params.id;
33673
33533
  const { field, value } = req.body;
33674
- const validation = import_joi12.default.object({
33675
- _id: import_joi12.default.string().hex().required(),
33676
- field: import_joi12.default.string().valid("name", "director", "directorName").required(),
33677
- value: import_joi12.default.string().required()
33534
+ const validation = import_joi11.default.object({
33535
+ _id: import_joi11.default.string().hex().required(),
33536
+ field: import_joi11.default.string().valid("name", "director", "directorName").required(),
33537
+ value: import_joi11.default.string().required()
33678
33538
  });
33679
33539
  const { error } = validation.validate({ _id, field, value });
33680
33540
  if (error) {
33681
- next(new import_nodejs_utils21.BadRequestError(error.message));
33541
+ next(new import_nodejs_utils19.BadRequestError(error.message));
33682
33542
  return;
33683
33543
  }
33684
33544
  try {
@@ -33692,17 +33552,17 @@ function useSchoolController() {
33692
33552
  async function updateById(req, res, next) {
33693
33553
  const id = req.params.id;
33694
33554
  const payload = req.body;
33695
- const validation = import_joi12.default.object({
33696
- id: import_joi12.default.string().hex().required()
33555
+ const validation = import_joi11.default.object({
33556
+ id: import_joi11.default.string().hex().required()
33697
33557
  });
33698
33558
  const { error: idError } = validation.validate({ id });
33699
33559
  if (idError) {
33700
- next(new import_nodejs_utils21.BadRequestError(idError.message));
33560
+ next(new import_nodejs_utils19.BadRequestError(idError.message));
33701
33561
  return;
33702
33562
  }
33703
33563
  const { error } = schemaSchoolUpdate.validate(payload);
33704
33564
  if (error) {
33705
- next(new import_nodejs_utils21.BadRequestError(error.message));
33565
+ next(new import_nodejs_utils19.BadRequestError(error.message));
33706
33566
  return;
33707
33567
  }
33708
33568
  try {
@@ -33715,12 +33575,12 @@ function useSchoolController() {
33715
33575
  }
33716
33576
  async function deleteById(req, res, next) {
33717
33577
  const _id = req.params.id;
33718
- const validation = import_joi12.default.object({
33719
- _id: import_joi12.default.string().hex().required()
33578
+ const validation = import_joi11.default.object({
33579
+ _id: import_joi11.default.string().hex().required()
33720
33580
  });
33721
33581
  const { error } = validation.validate({ _id });
33722
33582
  if (error) {
33723
- next(new import_nodejs_utils21.BadRequestError(error.message));
33583
+ next(new import_nodejs_utils19.BadRequestError(error.message));
33724
33584
  return;
33725
33585
  }
33726
33586
  try {
@@ -33745,6 +33605,245 @@ function useSchoolController() {
33745
33605
  };
33746
33606
  }
33747
33607
 
33608
+ // src/resources/division/division.service.ts
33609
+ function useDivisionService() {
33610
+ const {
33611
+ add: _add,
33612
+ updateById: _updateById,
33613
+ getById: _getById
33614
+ } = useDivisionRepo();
33615
+ const { addRole } = (0, import_core2.useRoleRepo)();
33616
+ const { updateDivisionNameByDivision } = useSchoolRepo();
33617
+ async function add(value) {
33618
+ const session = import_nodejs_utils20.useAtlas.getClient()?.startSession();
33619
+ if (!session) {
33620
+ throw new Error("Unable to start session for division service.");
33621
+ }
33622
+ try {
33623
+ session.startTransaction();
33624
+ const division = await _add(value, session);
33625
+ await addRole(
33626
+ {
33627
+ id: division.toString(),
33628
+ name: "Admin",
33629
+ type: "basic-edu-sdo",
33630
+ permissions: ["*"],
33631
+ status: "active",
33632
+ default: true
33633
+ },
33634
+ session
33635
+ );
33636
+ await session.commitTransaction();
33637
+ return "Division and admin role created successfully.";
33638
+ } catch (error) {
33639
+ session.abortTransaction();
33640
+ throw error;
33641
+ } finally {
33642
+ session.endSession();
33643
+ }
33644
+ }
33645
+ async function updateById(id, value) {
33646
+ const session = import_nodejs_utils20.useAtlas.getClient()?.startSession();
33647
+ if (!session) {
33648
+ throw new Error("Unable to start session for division service.");
33649
+ }
33650
+ try {
33651
+ session.startTransaction();
33652
+ const division = await _getById(id);
33653
+ await _updateById(id, value, session);
33654
+ if (division && division._id && division.name !== value.name) {
33655
+ await updateDivisionNameByDivision(division._id, value.name, session);
33656
+ }
33657
+ await session.commitTransaction();
33658
+ return "Division and admin role created successfully.";
33659
+ } catch (error) {
33660
+ session.abortTransaction();
33661
+ throw error;
33662
+ } finally {
33663
+ session.endSession();
33664
+ }
33665
+ }
33666
+ return {
33667
+ add,
33668
+ updateById
33669
+ };
33670
+ }
33671
+
33672
+ // src/resources/division/division.controller.ts
33673
+ var import_nodejs_utils21 = require("@eeplatform/nodejs-utils");
33674
+ var import_joi12 = __toESM(require("joi"));
33675
+ function useDivisionController() {
33676
+ const { add: _add, updateById: _updateById } = useDivisionService();
33677
+ const {
33678
+ getAll: _getAll,
33679
+ getById: _getById,
33680
+ getByName: _getByName,
33681
+ updateFieldById: _updateFieldById,
33682
+ deleteById: _deleteById
33683
+ } = useDivisionRepo();
33684
+ async function add(req, res, next) {
33685
+ const value = req.body;
33686
+ const { error } = schemaDivision.validate(value);
33687
+ if (error) {
33688
+ next(new import_nodejs_utils21.BadRequestError(error.message));
33689
+ return;
33690
+ }
33691
+ try {
33692
+ const data = await _add(value);
33693
+ res.json({
33694
+ message: "Successfully created division.",
33695
+ data
33696
+ });
33697
+ return;
33698
+ } catch (error2) {
33699
+ next(error2);
33700
+ }
33701
+ }
33702
+ async function getAll(req, res, next) {
33703
+ const query = req.query;
33704
+ const validation = import_joi12.default.object({
33705
+ page: import_joi12.default.number().min(1).optional().allow("", null),
33706
+ limit: import_joi12.default.number().min(1).optional().allow("", null),
33707
+ search: import_joi12.default.string().optional().allow("", null),
33708
+ status: import_joi12.default.string().optional().allow("", null),
33709
+ region: import_joi12.default.string().hex().optional().allow("", null)
33710
+ });
33711
+ const { error } = validation.validate(query);
33712
+ const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
33713
+ const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
33714
+ const search = req.query.search ?? "";
33715
+ const status = req.query.status ?? "active";
33716
+ const region = req.query.region ?? "";
33717
+ const isPageNumber = isFinite(page);
33718
+ if (!isPageNumber) {
33719
+ next(new import_nodejs_utils21.BadRequestError("Invalid page number."));
33720
+ return;
33721
+ }
33722
+ const isLimitNumber = isFinite(limit);
33723
+ if (!isLimitNumber) {
33724
+ next(new import_nodejs_utils21.BadRequestError("Invalid limit number."));
33725
+ return;
33726
+ }
33727
+ if (error) {
33728
+ next(new import_nodejs_utils21.BadRequestError(error.message));
33729
+ return;
33730
+ }
33731
+ try {
33732
+ const data = await _getAll({ page, limit, search, status, region });
33733
+ res.json(data);
33734
+ return;
33735
+ } catch (error2) {
33736
+ next(error2);
33737
+ }
33738
+ }
33739
+ async function getById(req, res, next) {
33740
+ const id = req.params.id;
33741
+ const validation = import_joi12.default.object({
33742
+ id: import_joi12.default.string().hex().required()
33743
+ });
33744
+ const { error } = validation.validate({ id });
33745
+ if (error) {
33746
+ next(new import_nodejs_utils21.BadRequestError(error.message));
33747
+ return;
33748
+ }
33749
+ try {
33750
+ const data = await _getById(id);
33751
+ res.json({
33752
+ message: "Successfully retrieved division.",
33753
+ data
33754
+ });
33755
+ return;
33756
+ } catch (error2) {
33757
+ next(error2);
33758
+ }
33759
+ }
33760
+ async function getByName(req, res, next) {
33761
+ const name = req.params.name;
33762
+ const validation = import_joi12.default.object({
33763
+ name: import_joi12.default.string().required()
33764
+ });
33765
+ const { error } = validation.validate({ name });
33766
+ if (error) {
33767
+ next(new import_nodejs_utils21.BadRequestError(error.message));
33768
+ return;
33769
+ }
33770
+ try {
33771
+ const data = await _getByName(name);
33772
+ res.json({
33773
+ message: "Successfully retrieved division.",
33774
+ data
33775
+ });
33776
+ return;
33777
+ } catch (error2) {
33778
+ next(error2);
33779
+ }
33780
+ }
33781
+ async function updateField(req, res, next) {
33782
+ const _id = req.params.id;
33783
+ const { field, value } = req.body;
33784
+ const validation = import_joi12.default.object({
33785
+ _id: import_joi12.default.string().hex().required(),
33786
+ field: import_joi12.default.string().valid("name", "director", "directorName").required(),
33787
+ value: import_joi12.default.string().required()
33788
+ });
33789
+ const { error } = validation.validate({ _id, field, value });
33790
+ if (error) {
33791
+ next(new import_nodejs_utils21.BadRequestError(error.message));
33792
+ return;
33793
+ }
33794
+ try {
33795
+ const message = await _updateFieldById({ _id, field, value });
33796
+ res.json({ message });
33797
+ return;
33798
+ } catch (error2) {
33799
+ next(error2);
33800
+ }
33801
+ }
33802
+ async function updateById(req, res, next) {
33803
+ const _id = req.params.id;
33804
+ const payload = req.body;
33805
+ const { error } = schemaDivisionUpdate.validate({ _id, ...payload });
33806
+ if (error) {
33807
+ next(new import_nodejs_utils21.BadRequestError(error.message));
33808
+ return;
33809
+ }
33810
+ try {
33811
+ const message = await _updateById(_id, payload);
33812
+ res.json({ message });
33813
+ return;
33814
+ } catch (error2) {
33815
+ next(error2);
33816
+ }
33817
+ }
33818
+ async function deleteById(req, res, next) {
33819
+ const _id = req.params.id;
33820
+ const validation = import_joi12.default.object({
33821
+ _id: import_joi12.default.string().hex().required()
33822
+ });
33823
+ const { error } = validation.validate({ _id });
33824
+ if (error) {
33825
+ next(new import_nodejs_utils21.BadRequestError(error.message));
33826
+ return;
33827
+ }
33828
+ try {
33829
+ const message = await _deleteById(_id);
33830
+ res.json({ message });
33831
+ return;
33832
+ } catch (error2) {
33833
+ next(error2);
33834
+ }
33835
+ }
33836
+ return {
33837
+ add,
33838
+ getAll,
33839
+ getById,
33840
+ getByName,
33841
+ updateField,
33842
+ updateById,
33843
+ deleteById
33844
+ };
33845
+ }
33846
+
33748
33847
  // src/resources/asset/asset.model.ts
33749
33848
  var import_nodejs_utils22 = require("@eeplatform/nodejs-utils");
33750
33849
  var import_joi13 = __toESM(require("joi"));