@eeplatform/basic-edu 1.3.1 → 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,272 +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
- cityMunicipality: import_joi11.default.string().max(100).optional().allow(null, ""),
4607
- province: import_joi11.default.string().max(100).optional().allow(null, ""),
4608
- provincePSGC: import_joi11.default.number().optional().allow(null, ""),
4609
- cityMunicipalityPSGC: import_joi11.default.number().optional().allow(null, ""),
4610
- postalCode: import_joi11.default.string().max(20).optional().allow(null, ""),
4611
- contactNumber: import_joi11.default.string().max(20).optional().allow(null, ""),
4612
- email: import_joi11.default.string().email().max(100).optional().allow(null, ""),
4613
- status: import_joi11.default.string().optional().allow(null, ""),
4614
- createdBy: import_joi11.default.string().optional().allow(null, ""),
4615
- createdAt: import_joi11.default.string().isoDate().optional().allow(null, ""),
4616
- updatedAt: import_joi11.default.string().isoDate().optional().allow(null, ""),
4617
- 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, "")
4618
4407
  });
4619
- var schemaSchoolUpdate = import_joi11.default.object({
4620
- id: import_joi11.default.string().min(1).max(50).required(),
4621
- name: import_joi11.default.string().min(1).max(100).required(),
4622
- region: import_joi11.default.string().hex().required(),
4623
- regionName: import_joi11.default.string().min(1).max(100).optional().allow(null, ""),
4624
- division: import_joi11.default.string().hex().required(),
4625
- divisionName: import_joi11.default.string().min(1).max(100).optional().allow(null, ""),
4626
- principal: import_joi11.default.string().hex().optional().allow(null, ""),
4627
- principalName: import_joi11.default.string().min(1).max(100).optional().allow(null, ""),
4628
- street: import_joi11.default.string().max(200).optional().allow(null, ""),
4629
- barangay: import_joi11.default.string().max(200).optional().allow(null, ""),
4630
- cityMunicipality: import_joi11.default.string().max(100).optional().allow(null, ""),
4631
- province: import_joi11.default.string().max(100).optional().allow(null, ""),
4632
- provincePSGC: import_joi11.default.number().optional().allow(null, ""),
4633
- cityMunicipalityPSGC: import_joi11.default.number().optional().allow(null, ""),
4634
- postalCode: import_joi11.default.string().max(20).optional().allow(null, ""),
4635
- contactNumber: import_joi11.default.string().max(20).optional().allow(null, ""),
4636
- 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, "")
4637
4426
  });
4638
4427
  function modelSchool(value) {
4639
4428
  const { error } = schemaSchool.validate(value);
4640
4429
  if (error) {
4641
- throw new import_nodejs_utils18.BadRequestError(`Invalid sdo data: ${error.message}`);
4430
+ throw new import_nodejs_utils16.BadRequestError(`Invalid sdo data: ${error.message}`);
4642
4431
  }
4643
4432
  if (value._id && typeof value._id === "string") {
4644
4433
  try {
@@ -4696,16 +4485,16 @@ function modelSchool(value) {
4696
4485
  }
4697
4486
 
4698
4487
  // src/resources/school/school.repository.ts
4699
- var import_nodejs_utils19 = require("@eeplatform/nodejs-utils");
4488
+ var import_nodejs_utils17 = require("@eeplatform/nodejs-utils");
4700
4489
  var import_mongodb12 = require("mongodb");
4701
4490
  function useSchoolRepo() {
4702
- const db = import_nodejs_utils19.useAtlas.getDb();
4491
+ const db = import_nodejs_utils17.useAtlas.getDb();
4703
4492
  if (!db) {
4704
4493
  throw new Error("Unable to connect to server.");
4705
4494
  }
4706
4495
  const namespace_collection = "deped.schools";
4707
4496
  const collection = db.collection(namespace_collection);
4708
- const { getCache, setCache, delNamespace } = (0, import_nodejs_utils19.useCache)(namespace_collection);
4497
+ const { getCache, setCache, delNamespace } = (0, import_nodejs_utils17.useCache)(namespace_collection);
4709
4498
  async function createIndexes() {
4710
4499
  try {
4711
4500
  await collection.createIndexes([
@@ -4720,12 +4509,12 @@ function useSchoolRepo() {
4720
4509
  }
4721
4510
  function delCachedData() {
4722
4511
  delNamespace().then(() => {
4723
- import_nodejs_utils19.logger.log({
4512
+ import_nodejs_utils17.logger.log({
4724
4513
  level: "info",
4725
4514
  message: `Cache namespace cleared for ${namespace_collection}`
4726
4515
  });
4727
4516
  }).catch((err) => {
4728
- import_nodejs_utils19.logger.log({
4517
+ import_nodejs_utils17.logger.log({
4729
4518
  level: "error",
4730
4519
  message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
4731
4520
  });
@@ -4740,7 +4529,7 @@ function useSchoolRepo() {
4740
4529
  }
4741
4530
  return res.insertedId;
4742
4531
  } catch (error) {
4743
- import_nodejs_utils19.logger.log({
4532
+ import_nodejs_utils17.logger.log({
4744
4533
  level: "error",
4745
4534
  message: `Failed to add school: ${error.message}`,
4746
4535
  metadata: {
@@ -4752,14 +4541,14 @@ function useSchoolRepo() {
4752
4541
  transactionNumber: error.errorResponse?.txnNumber || "unknown"
4753
4542
  }
4754
4543
  });
4755
- if (error instanceof import_nodejs_utils19.AppError) {
4544
+ if (error instanceof import_nodejs_utils17.AppError) {
4756
4545
  throw error;
4757
4546
  } else {
4758
4547
  const isDuplicated = error.message.includes("duplicate");
4759
4548
  if (isDuplicated) {
4760
- throw new import_nodejs_utils19.BadRequestError("Duplicate, school already exists.");
4549
+ throw new import_nodejs_utils17.BadRequestError("Duplicate, school already exists.");
4761
4550
  }
4762
- throw new import_nodejs_utils19.InternalServerError("Failed to add school.");
4551
+ throw new import_nodejs_utils17.InternalServerError("Failed to add school.");
4763
4552
  }
4764
4553
  }
4765
4554
  }
@@ -4786,15 +4575,15 @@ function useSchoolRepo() {
4786
4575
  query.$text = { $search: search };
4787
4576
  cacheKeyOptions.search = search;
4788
4577
  }
4789
- const cacheKey = (0, import_nodejs_utils19.makeCacheKey)(namespace_collection, cacheKeyOptions);
4790
- import_nodejs_utils19.logger.log({
4578
+ const cacheKey = (0, import_nodejs_utils17.makeCacheKey)(namespace_collection, cacheKeyOptions);
4579
+ import_nodejs_utils17.logger.log({
4791
4580
  level: "info",
4792
4581
  message: `Cache key for getAll schools: ${cacheKey}`
4793
4582
  });
4794
4583
  try {
4795
4584
  const cached = await getCache(cacheKey);
4796
4585
  if (cached) {
4797
- import_nodejs_utils19.logger.log({
4586
+ import_nodejs_utils17.logger.log({
4798
4587
  level: "info",
4799
4588
  message: `Cache hit for getAll schools: ${cacheKey}`
4800
4589
  });
@@ -4807,21 +4596,21 @@ function useSchoolRepo() {
4807
4596
  { $limit: limit }
4808
4597
  ]).toArray();
4809
4598
  const length = await collection.countDocuments(query);
4810
- const data = (0, import_nodejs_utils19.paginate)(items, page, limit, length);
4599
+ const data = (0, import_nodejs_utils17.paginate)(items, page, limit, length);
4811
4600
  setCache(cacheKey, data, 600).then(() => {
4812
- import_nodejs_utils19.logger.log({
4601
+ import_nodejs_utils17.logger.log({
4813
4602
  level: "info",
4814
4603
  message: `Cache set for getAll schools: ${cacheKey}`
4815
4604
  });
4816
4605
  }).catch((err) => {
4817
- import_nodejs_utils19.logger.log({
4606
+ import_nodejs_utils17.logger.log({
4818
4607
  level: "error",
4819
4608
  message: `Failed to set cache for getAll schools: ${err.message}`
4820
4609
  });
4821
4610
  });
4822
4611
  return data;
4823
4612
  } catch (error) {
4824
- import_nodejs_utils19.logger.log({ level: "error", message: `${error}` });
4613
+ import_nodejs_utils17.logger.log({ level: "error", message: `${error}` });
4825
4614
  throw error;
4826
4615
  }
4827
4616
  }
@@ -4829,7 +4618,7 @@ function useSchoolRepo() {
4829
4618
  try {
4830
4619
  _id = new import_mongodb12.ObjectId(_id);
4831
4620
  } catch (error) {
4832
- throw new import_nodejs_utils19.BadRequestError("Invalid ID.");
4621
+ throw new import_nodejs_utils17.BadRequestError("Invalid ID.");
4833
4622
  }
4834
4623
  const query = { _id };
4835
4624
  const cacheKeyOptions = {
@@ -4840,11 +4629,11 @@ function useSchoolRepo() {
4840
4629
  query.status = status;
4841
4630
  cacheKeyOptions.status = status;
4842
4631
  }
4843
- const cacheKey = (0, import_nodejs_utils19.makeCacheKey)(namespace_collection, cacheKeyOptions);
4632
+ const cacheKey = (0, import_nodejs_utils17.makeCacheKey)(namespace_collection, cacheKeyOptions);
4844
4633
  try {
4845
4634
  const cached = await getCache(cacheKey);
4846
4635
  if (cached) {
4847
- import_nodejs_utils19.logger.log({
4636
+ import_nodejs_utils17.logger.log({
4848
4637
  level: "info",
4849
4638
  message: `Cache hit for getById school: ${cacheKey}`
4850
4639
  });
@@ -4852,25 +4641,25 @@ function useSchoolRepo() {
4852
4641
  }
4853
4642
  const result = await collection.findOne(query);
4854
4643
  if (!result) {
4855
- throw new import_nodejs_utils19.BadRequestError("School not found.");
4644
+ throw new import_nodejs_utils17.BadRequestError("School not found.");
4856
4645
  }
4857
4646
  setCache(cacheKey, result, 300).then(() => {
4858
- import_nodejs_utils19.logger.log({
4647
+ import_nodejs_utils17.logger.log({
4859
4648
  level: "info",
4860
4649
  message: `Cache set for school by id: ${cacheKey}`
4861
4650
  });
4862
4651
  }).catch((err) => {
4863
- import_nodejs_utils19.logger.log({
4652
+ import_nodejs_utils17.logger.log({
4864
4653
  level: "error",
4865
4654
  message: `Failed to set cache for school by id: ${err.message}`
4866
4655
  });
4867
4656
  });
4868
4657
  return result;
4869
4658
  } catch (error) {
4870
- if (error instanceof import_nodejs_utils19.AppError) {
4659
+ if (error instanceof import_nodejs_utils17.AppError) {
4871
4660
  throw error;
4872
4661
  } else {
4873
- throw new import_nodejs_utils19.InternalServerError("Failed to get school.");
4662
+ throw new import_nodejs_utils17.InternalServerError("Failed to get school.");
4874
4663
  }
4875
4664
  }
4876
4665
  }
@@ -4878,13 +4667,13 @@ function useSchoolRepo() {
4878
4667
  try {
4879
4668
  createdBy = new import_mongodb12.ObjectId(createdBy);
4880
4669
  } catch (error) {
4881
- throw new import_nodejs_utils19.BadRequestError("Invalid ID.");
4670
+ throw new import_nodejs_utils17.BadRequestError("Invalid ID.");
4882
4671
  }
4883
- const cacheKey = (0, import_nodejs_utils19.makeCacheKey)(namespace_collection, { createdBy });
4672
+ const cacheKey = (0, import_nodejs_utils17.makeCacheKey)(namespace_collection, { createdBy });
4884
4673
  try {
4885
4674
  const cached = await getCache(cacheKey);
4886
4675
  if (cached) {
4887
- import_nodejs_utils19.logger.log({
4676
+ import_nodejs_utils17.logger.log({
4888
4677
  level: "info",
4889
4678
  message: `Cache hit for getById school: ${cacheKey}`
4890
4679
  });
@@ -4895,34 +4684,34 @@ function useSchoolRepo() {
4895
4684
  status: "pending"
4896
4685
  });
4897
4686
  if (!result) {
4898
- throw new import_nodejs_utils19.BadRequestError("School not found.");
4687
+ throw new import_nodejs_utils17.BadRequestError("School not found.");
4899
4688
  }
4900
4689
  setCache(cacheKey, result, 300).then(() => {
4901
- import_nodejs_utils19.logger.log({
4690
+ import_nodejs_utils17.logger.log({
4902
4691
  level: "info",
4903
4692
  message: `Cache set for school by id: ${cacheKey}`
4904
4693
  });
4905
4694
  }).catch((err) => {
4906
- import_nodejs_utils19.logger.log({
4695
+ import_nodejs_utils17.logger.log({
4907
4696
  level: "error",
4908
4697
  message: `Failed to set cache for school by id: ${err.message}`
4909
4698
  });
4910
4699
  });
4911
4700
  return result;
4912
4701
  } catch (error) {
4913
- if (error instanceof import_nodejs_utils19.AppError) {
4702
+ if (error instanceof import_nodejs_utils17.AppError) {
4914
4703
  throw error;
4915
4704
  } else {
4916
- throw new import_nodejs_utils19.InternalServerError("Failed to get school.");
4705
+ throw new import_nodejs_utils17.InternalServerError("Failed to get school.");
4917
4706
  }
4918
4707
  }
4919
4708
  }
4920
4709
  async function getByName(name) {
4921
- const cacheKey = (0, import_nodejs_utils19.makeCacheKey)(namespace_collection, { name });
4710
+ const cacheKey = (0, import_nodejs_utils17.makeCacheKey)(namespace_collection, { name });
4922
4711
  try {
4923
4712
  const cached = await getCache(cacheKey);
4924
4713
  if (cached) {
4925
- import_nodejs_utils19.logger.log({
4714
+ import_nodejs_utils17.logger.log({
4926
4715
  level: "info",
4927
4716
  message: `Cache hit for getByName school: ${cacheKey}`
4928
4717
  });
@@ -4933,25 +4722,25 @@ function useSchoolRepo() {
4933
4722
  deletedAt: { $in: ["", null] }
4934
4723
  });
4935
4724
  if (!result) {
4936
- throw new import_nodejs_utils19.BadRequestError("School not found.");
4725
+ throw new import_nodejs_utils17.BadRequestError("School not found.");
4937
4726
  }
4938
4727
  setCache(cacheKey, result, 300).then(() => {
4939
- import_nodejs_utils19.logger.log({
4728
+ import_nodejs_utils17.logger.log({
4940
4729
  level: "info",
4941
4730
  message: `Cache set for school by name: ${cacheKey}`
4942
4731
  });
4943
4732
  }).catch((err) => {
4944
- import_nodejs_utils19.logger.log({
4733
+ import_nodejs_utils17.logger.log({
4945
4734
  level: "error",
4946
4735
  message: `Failed to set cache for school by name: ${err.message}`
4947
4736
  });
4948
4737
  });
4949
4738
  return result;
4950
4739
  } catch (error) {
4951
- if (error instanceof import_nodejs_utils19.AppError) {
4740
+ if (error instanceof import_nodejs_utils17.AppError) {
4952
4741
  throw error;
4953
4742
  } else {
4954
- throw new import_nodejs_utils19.InternalServerError("Failed to get school.");
4743
+ throw new import_nodejs_utils17.InternalServerError("Failed to get school.");
4955
4744
  }
4956
4745
  }
4957
4746
  }
@@ -4959,7 +4748,7 @@ function useSchoolRepo() {
4959
4748
  try {
4960
4749
  _id = new import_mongodb12.ObjectId(_id);
4961
4750
  } catch (error) {
4962
- throw new import_nodejs_utils19.BadRequestError("Invalid ID.");
4751
+ throw new import_nodejs_utils17.BadRequestError("Invalid ID.");
4963
4752
  }
4964
4753
  try {
4965
4754
  await collection.updateOne(
@@ -4970,24 +4759,30 @@ function useSchoolRepo() {
4970
4759
  delCachedData();
4971
4760
  return `Successfully updated school status to ${status}.`;
4972
4761
  } catch (error) {
4973
- if (error instanceof import_nodejs_utils19.AppError) {
4762
+ if (error instanceof import_nodejs_utils17.AppError) {
4974
4763
  throw error;
4975
4764
  } else {
4976
- throw new import_nodejs_utils19.InternalServerError("Failed to update school status.");
4765
+ throw new import_nodejs_utils17.InternalServerError("Failed to update school status.");
4977
4766
  }
4978
4767
  }
4979
4768
  }
4980
4769
  async function updateFieldById({ _id, field, value } = {}, session) {
4981
- const allowedFields = ["name"];
4770
+ const allowedFields = [
4771
+ "name",
4772
+ "division",
4773
+ "divisionName",
4774
+ "region",
4775
+ "regionName"
4776
+ ];
4982
4777
  if (!allowedFields.includes(field)) {
4983
- throw new import_nodejs_utils19.BadRequestError(
4778
+ throw new import_nodejs_utils17.BadRequestError(
4984
4779
  `Field "${field}" is not allowed to be updated.`
4985
4780
  );
4986
4781
  }
4987
4782
  try {
4988
4783
  _id = new import_mongodb12.ObjectId(_id);
4989
4784
  } catch (error) {
4990
- throw new import_nodejs_utils19.BadRequestError("Invalid ID.");
4785
+ throw new import_nodejs_utils17.BadRequestError("Invalid ID.");
4991
4786
  }
4992
4787
  try {
4993
4788
  await collection.updateOne(
@@ -4998,32 +4793,56 @@ function useSchoolRepo() {
4998
4793
  delCachedData();
4999
4794
  return `Successfully updated school ${field}.`;
5000
4795
  } catch (error) {
5001
- 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.`);
5002
4821
  }
5003
4822
  }
5004
4823
  async function updateById(_id, options, session) {
5005
4824
  const { error } = schemaSchoolUpdate.validate(options);
5006
4825
  if (error) {
5007
- throw new import_nodejs_utils19.BadRequestError(`Invalid school data: ${error.message}`);
4826
+ throw new import_nodejs_utils17.BadRequestError(`Invalid school data: ${error.message}`);
5008
4827
  }
5009
4828
  try {
5010
4829
  _id = new import_mongodb12.ObjectId(_id);
5011
4830
  } catch (error2) {
5012
- throw new import_nodejs_utils19.BadRequestError("Invalid ID.");
4831
+ throw new import_nodejs_utils17.BadRequestError("Invalid ID.");
5013
4832
  }
5014
4833
  try {
5015
4834
  await collection.updateOne({ _id }, { $set: options }, { session });
5016
4835
  delCachedData();
5017
4836
  return `Successfully updated school.`;
5018
4837
  } catch (error2) {
5019
- throw new import_nodejs_utils19.InternalServerError(`Failed to update school.`);
4838
+ throw new import_nodejs_utils17.InternalServerError(`Failed to update school.`);
5020
4839
  }
5021
4840
  }
5022
4841
  async function deleteById(_id) {
5023
4842
  try {
5024
4843
  _id = new import_mongodb12.ObjectId(_id);
5025
4844
  } catch (error) {
5026
- throw new import_nodejs_utils19.BadRequestError("Invalid ID.");
4845
+ throw new import_nodejs_utils17.BadRequestError("Invalid ID.");
5027
4846
  }
5028
4847
  try {
5029
4848
  await collection.updateOne(
@@ -5033,7 +4852,7 @@ function useSchoolRepo() {
5033
4852
  delCachedData();
5034
4853
  return "Successfully deleted school.";
5035
4854
  } catch (error) {
5036
- throw new import_nodejs_utils19.InternalServerError("Failed to delete school.");
4855
+ throw new import_nodejs_utils17.InternalServerError("Failed to delete school.");
5037
4856
  }
5038
4857
  }
5039
4858
  return {
@@ -5047,13 +4866,14 @@ function useSchoolRepo() {
5047
4866
  updateById,
5048
4867
  deleteById,
5049
4868
  getByName,
5050
- delCachedData
4869
+ delCachedData,
4870
+ updateDivisionNameByDivision
5051
4871
  };
5052
4872
  }
5053
4873
 
5054
4874
  // src/resources/school/school.service.ts
5055
- var import_nodejs_utils20 = require("@eeplatform/nodejs-utils");
5056
- var import_core2 = require("@eeplatform/core");
4875
+ var import_nodejs_utils18 = require("@eeplatform/nodejs-utils");
4876
+ var import_core = require("@eeplatform/core");
5057
4877
 
5058
4878
  // node_modules/xlsx/xlsx.mjs
5059
4879
  var XLSX = {};
@@ -33221,17 +33041,17 @@ function useSchoolService() {
33221
33041
  getById,
33222
33042
  delCachedData: delCachedSchoolData
33223
33043
  } = useSchoolRepo();
33224
- const { addRole, delCachedData: delCachedRoleData } = (0, import_core2.useRoleRepo)();
33225
- const { getUserById } = (0, import_core2.useUserRepo)();
33226
- 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)();
33227
33047
  async function register(value) {
33228
33048
  const { error } = schemaSchool.validate(value);
33229
33049
  if (error) {
33230
- throw new import_nodejs_utils20.BadRequestError(error.message);
33050
+ throw new import_nodejs_utils18.BadRequestError(error.message);
33231
33051
  }
33232
33052
  const existingSchool = await getPendingByCreatedBy(value.createdBy ?? "");
33233
33053
  if (existingSchool) {
33234
- throw new import_nodejs_utils20.BadRequestError(
33054
+ throw new import_nodejs_utils18.BadRequestError(
33235
33055
  "You already have a pending school registration."
33236
33056
  );
33237
33057
  }
@@ -33246,9 +33066,9 @@ function useSchoolService() {
33246
33066
  async function approve(id) {
33247
33067
  const school = await getById(id, "pending");
33248
33068
  if (!school) {
33249
- throw new import_nodejs_utils20.BadRequestError("School registration not found.");
33069
+ throw new import_nodejs_utils18.BadRequestError("School registration not found.");
33250
33070
  }
33251
- const session = import_nodejs_utils20.useAtlas.getClient()?.startSession();
33071
+ const session = import_nodejs_utils18.useAtlas.getClient()?.startSession();
33252
33072
  if (!session) {
33253
33073
  throw new Error("Unable to start session for school service.");
33254
33074
  }
@@ -33270,11 +33090,11 @@ function useSchoolService() {
33270
33090
  session
33271
33091
  );
33272
33092
  if (!school.createdBy) {
33273
- throw new import_nodejs_utils20.BadRequestError("School must have a creator.");
33093
+ throw new import_nodejs_utils18.BadRequestError("School must have a creator.");
33274
33094
  }
33275
33095
  const user = await getUserById(school.createdBy ?? "");
33276
33096
  if (!user) {
33277
- 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.");
33278
33098
  }
33279
33099
  await addMember(
33280
33100
  {
@@ -33291,7 +33111,7 @@ function useSchoolService() {
33291
33111
  await session.commitTransaction();
33292
33112
  return "School registration has been approved.";
33293
33113
  } catch (error) {
33294
- import_nodejs_utils20.logger.log({
33114
+ import_nodejs_utils18.logger.log({
33295
33115
  level: "error",
33296
33116
  message: `Error approving school registration: ${error.message}`
33297
33117
  });
@@ -33304,9 +33124,9 @@ function useSchoolService() {
33304
33124
  async function add(value) {
33305
33125
  const { error } = schemaSchool.validate(value);
33306
33126
  if (error) {
33307
- throw new import_nodejs_utils20.BadRequestError(error.message);
33127
+ throw new import_nodejs_utils18.BadRequestError(error.message);
33308
33128
  }
33309
- const session = import_nodejs_utils20.useAtlas.getClient()?.startSession();
33129
+ const session = import_nodejs_utils18.useAtlas.getClient()?.startSession();
33310
33130
  if (!session) {
33311
33131
  throw new Error("Unable to start session for school service.");
33312
33132
  }
@@ -33328,11 +33148,11 @@ function useSchoolService() {
33328
33148
  session
33329
33149
  );
33330
33150
  if (!value.createdBy) {
33331
- throw new import_nodejs_utils20.BadRequestError("School must have a creator.");
33151
+ throw new import_nodejs_utils18.BadRequestError("School must have a creator.");
33332
33152
  }
33333
33153
  const user = await getUserById(value.createdBy ?? "");
33334
33154
  if (!user) {
33335
- 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.");
33336
33156
  }
33337
33157
  await addMember(
33338
33158
  {
@@ -33349,7 +33169,7 @@ function useSchoolService() {
33349
33169
  await session.commitTransaction();
33350
33170
  return "School has been added and activated successfully.";
33351
33171
  } catch (error2) {
33352
- import_nodejs_utils20.logger.log({
33172
+ import_nodejs_utils18.logger.log({
33353
33173
  level: "error",
33354
33174
  message: `Error adding school: ${error2.message}`
33355
33175
  });
@@ -33363,7 +33183,7 @@ function useSchoolService() {
33363
33183
  const isCSV = file.mimetype.includes("csv") || file.originalname.endsWith(".csv");
33364
33184
  const isExcel = file.mimetype.includes("sheet") || file.originalname.endsWith(".xlsx") || file.originalname.endsWith(".xls");
33365
33185
  if (!isCSV && !isExcel) {
33366
- 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");
33367
33187
  }
33368
33188
  let rawData = [];
33369
33189
  try {
@@ -33380,17 +33200,17 @@ function useSchoolService() {
33380
33200
  transformHeader: (header) => header.trim()
33381
33201
  });
33382
33202
  if (parseResult.errors.length > 0) {
33383
- throw new import_nodejs_utils20.BadRequestError(
33203
+ throw new import_nodejs_utils18.BadRequestError(
33384
33204
  `CSV parsing error: ${parseResult.errors[0].message}`
33385
33205
  );
33386
33206
  }
33387
33207
  rawData = parseResult.data;
33388
33208
  }
33389
33209
  } catch (error) {
33390
- throw new import_nodejs_utils20.BadRequestError(`File parsing error: ${error.message}`);
33210
+ throw new import_nodejs_utils18.BadRequestError(`File parsing error: ${error.message}`);
33391
33211
  }
33392
33212
  if (!rawData.length) {
33393
- throw new import_nodejs_utils20.BadRequestError("No data found in file");
33213
+ throw new import_nodejs_utils18.BadRequestError("No data found in file");
33394
33214
  }
33395
33215
  const schools = [];
33396
33216
  const errors = [];
@@ -33436,7 +33256,7 @@ function useSchoolService() {
33436
33256
  schools.push(school);
33437
33257
  }
33438
33258
  if (errors.length > 0) {
33439
- throw new import_nodejs_utils20.BadRequestError(
33259
+ throw new import_nodejs_utils18.BadRequestError(
33440
33260
  `Validation errors:
33441
33261
  ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33442
33262
  ...and ${errors.length - 5} more` : ""}`
@@ -33448,9 +33268,9 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33448
33268
  skipped: 0,
33449
33269
  errors: []
33450
33270
  };
33451
- const { getByName: getPSGCByName } = (0, import_core2.usePSGCRepo)();
33271
+ const { getByName: getPSGCByName } = (0, import_core.usePSGCRepo)();
33452
33272
  for (let i = 0; i < schools.length; i++) {
33453
- const session = import_nodejs_utils20.useAtlas.getClient()?.startSession();
33273
+ const session = import_nodejs_utils18.useAtlas.getClient()?.startSession();
33454
33274
  if (!session) {
33455
33275
  throw new Error("Unable to start MongoDB session");
33456
33276
  }
@@ -33462,7 +33282,7 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33462
33282
  type: "Prov"
33463
33283
  });
33464
33284
  if (!provincePSGC) {
33465
- throw new import_nodejs_utils20.BadRequestError(
33285
+ throw new import_nodejs_utils18.BadRequestError(
33466
33286
  `Province '${school.province}' not found in PSGC data.`
33467
33287
  );
33468
33288
  }
@@ -33472,7 +33292,7 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33472
33292
  code: provincePSGC.code
33473
33293
  });
33474
33294
  if (!cityMunPSGC) {
33475
- throw new import_nodejs_utils20.BadRequestError(
33295
+ throw new import_nodejs_utils18.BadRequestError(
33476
33296
  `City/Municipality '${school.cityMunicipality}' not found in PSGC data.`
33477
33297
  );
33478
33298
  }
@@ -33524,8 +33344,8 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33524
33344
  }
33525
33345
 
33526
33346
  // src/resources/school/school.controller.ts
33527
- var import_nodejs_utils21 = require("@eeplatform/nodejs-utils");
33528
- var import_joi12 = __toESM(require("joi"));
33347
+ var import_nodejs_utils19 = require("@eeplatform/nodejs-utils");
33348
+ var import_joi11 = __toESM(require("joi"));
33529
33349
  function useSchoolController() {
33530
33350
  const {
33531
33351
  getAll: _getAll,
@@ -33545,7 +33365,7 @@ function useSchoolController() {
33545
33365
  const payload = req.body;
33546
33366
  const { error } = schemaSchool.validate(payload);
33547
33367
  if (error) {
33548
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33368
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33549
33369
  return;
33550
33370
  }
33551
33371
  try {
@@ -33558,19 +33378,19 @@ function useSchoolController() {
33558
33378
  }
33559
33379
  }
33560
33380
  async function getAll(req, res, next) {
33561
- const validation = import_joi12.default.object({
33562
- page: import_joi12.default.number().optional().allow(null, ""),
33563
- limit: import_joi12.default.number().optional().allow(null, ""),
33564
- sort: import_joi12.default.string().optional().allow(null, ""),
33565
- sortOrder: import_joi12.default.string().optional().allow(null, ""),
33566
- status: import_joi12.default.string().optional().allow(null, ""),
33567
- org: import_joi12.default.string().hex().optional().allow(null, ""),
33568
- app: import_joi12.default.string().optional().allow(null, ""),
33569
- 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, "")
33570
33390
  });
33571
33391
  const { error } = validation.validate(req.query);
33572
33392
  if (error) {
33573
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33393
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33574
33394
  return;
33575
33395
  }
33576
33396
  const page = parseInt(req.query.page) ?? 1;
@@ -33602,10 +33422,10 @@ function useSchoolController() {
33602
33422
  }
33603
33423
  async function getByCreatedBy(req, res, next) {
33604
33424
  const createdBy = req.params.createdBy;
33605
- const validation = import_joi12.default.string().hex().required();
33425
+ const validation = import_joi11.default.string().hex().required();
33606
33426
  const { error } = validation.validate(createdBy);
33607
33427
  if (error) {
33608
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33428
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33609
33429
  return;
33610
33430
  }
33611
33431
  try {
@@ -33619,13 +33439,13 @@ function useSchoolController() {
33619
33439
  async function updateStatusById(req, res, next) {
33620
33440
  const schoolId = req.params.id;
33621
33441
  const status = req.params.status;
33622
- const validation = import_joi12.default.object({
33623
- id: import_joi12.default.string().hex().required(),
33624
- 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()
33625
33445
  });
33626
33446
  const { error } = validation.validate({ id: schoolId, status });
33627
33447
  if (error) {
33628
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33448
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33629
33449
  return;
33630
33450
  }
33631
33451
  try {
@@ -33640,7 +33460,7 @@ function useSchoolController() {
33640
33460
  const payload = req.body;
33641
33461
  const { error } = schemaSchool.validate(payload);
33642
33462
  if (error) {
33643
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33463
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33644
33464
  return;
33645
33465
  }
33646
33466
  try {
@@ -33654,12 +33474,12 @@ function useSchoolController() {
33654
33474
  }
33655
33475
  async function approveSchool(req, res, next) {
33656
33476
  const schoolId = req.params.id;
33657
- const validation = import_joi12.default.object({
33658
- id: import_joi12.default.string().hex().required()
33477
+ const validation = import_joi11.default.object({
33478
+ id: import_joi11.default.string().hex().required()
33659
33479
  });
33660
33480
  const { error } = validation.validate({ id: schoolId });
33661
33481
  if (error) {
33662
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33482
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33663
33483
  return;
33664
33484
  }
33665
33485
  try {
@@ -33678,11 +33498,11 @@ function useSchoolController() {
33678
33498
  return;
33679
33499
  }
33680
33500
  const { region, regionName, division, divisionName } = req.body;
33681
- const validation = import_joi12.default.object({
33682
- region: import_joi12.default.string().hex().required(),
33683
- regionName: import_joi12.default.string().min(1).required(),
33684
- division: import_joi12.default.string().hex().required(),
33685
- 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()
33686
33506
  });
33687
33507
  const { error } = validation.validate({
33688
33508
  region,
@@ -33691,7 +33511,7 @@ function useSchoolController() {
33691
33511
  divisionName
33692
33512
  });
33693
33513
  if (error) {
33694
- next(new import_nodejs_utils21.BadRequestError(`Validation error: ${error.message}`));
33514
+ next(new import_nodejs_utils19.BadRequestError(`Validation error: ${error.message}`));
33695
33515
  return;
33696
33516
  }
33697
33517
  try {
@@ -33711,14 +33531,14 @@ function useSchoolController() {
33711
33531
  async function updateFieldById(req, res, next) {
33712
33532
  const _id = req.params.id;
33713
33533
  const { field, value } = req.body;
33714
- const validation = import_joi12.default.object({
33715
- _id: import_joi12.default.string().hex().required(),
33716
- field: import_joi12.default.string().valid("name", "director", "directorName").required(),
33717
- 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()
33718
33538
  });
33719
33539
  const { error } = validation.validate({ _id, field, value });
33720
33540
  if (error) {
33721
- next(new import_nodejs_utils21.BadRequestError(error.message));
33541
+ next(new import_nodejs_utils19.BadRequestError(error.message));
33722
33542
  return;
33723
33543
  }
33724
33544
  try {
@@ -33732,17 +33552,17 @@ function useSchoolController() {
33732
33552
  async function updateById(req, res, next) {
33733
33553
  const id = req.params.id;
33734
33554
  const payload = req.body;
33735
- const validation = import_joi12.default.object({
33736
- id: import_joi12.default.string().hex().required()
33555
+ const validation = import_joi11.default.object({
33556
+ id: import_joi11.default.string().hex().required()
33737
33557
  });
33738
33558
  const { error: idError } = validation.validate({ id });
33739
33559
  if (idError) {
33740
- next(new import_nodejs_utils21.BadRequestError(idError.message));
33560
+ next(new import_nodejs_utils19.BadRequestError(idError.message));
33741
33561
  return;
33742
33562
  }
33743
33563
  const { error } = schemaSchoolUpdate.validate(payload);
33744
33564
  if (error) {
33745
- next(new import_nodejs_utils21.BadRequestError(error.message));
33565
+ next(new import_nodejs_utils19.BadRequestError(error.message));
33746
33566
  return;
33747
33567
  }
33748
33568
  try {
@@ -33755,12 +33575,12 @@ function useSchoolController() {
33755
33575
  }
33756
33576
  async function deleteById(req, res, next) {
33757
33577
  const _id = req.params.id;
33758
- const validation = import_joi12.default.object({
33759
- _id: import_joi12.default.string().hex().required()
33578
+ const validation = import_joi11.default.object({
33579
+ _id: import_joi11.default.string().hex().required()
33760
33580
  });
33761
33581
  const { error } = validation.validate({ _id });
33762
33582
  if (error) {
33763
- next(new import_nodejs_utils21.BadRequestError(error.message));
33583
+ next(new import_nodejs_utils19.BadRequestError(error.message));
33764
33584
  return;
33765
33585
  }
33766
33586
  try {
@@ -33785,6 +33605,245 @@ function useSchoolController() {
33785
33605
  };
33786
33606
  }
33787
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
+
33788
33847
  // src/resources/asset/asset.model.ts
33789
33848
  var import_nodejs_utils22 = require("@eeplatform/nodejs-utils");
33790
33849
  var import_joi13 = __toESM(require("joi"));