@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.mjs CHANGED
@@ -4374,268 +4374,61 @@ function useDivisionRepo() {
4374
4374
  }
4375
4375
 
4376
4376
  // src/resources/division/division.service.ts
4377
- import { useAtlas as useAtlas7 } from "@eeplatform/nodejs-utils";
4378
- import { useRoleRepo } from "@eeplatform/core";
4379
- function useDivisionService() {
4380
- const { add: _add } = useDivisionRepo();
4381
- const { addRole } = useRoleRepo();
4382
- async function add(value) {
4383
- const session = useAtlas7.getClient()?.startSession();
4384
- if (!session) {
4385
- throw new Error("Unable to start session for division service.");
4386
- }
4387
- try {
4388
- session.startTransaction();
4389
- const division = await _add(value, session);
4390
- await addRole(
4391
- {
4392
- id: division.toString(),
4393
- name: "Admin",
4394
- type: "basic-edu-sdo",
4395
- permissions: ["*"],
4396
- status: "active",
4397
- default: true
4398
- },
4399
- session
4400
- );
4401
- await session.commitTransaction();
4402
- return "Division and admin role created successfully.";
4403
- } catch (error) {
4404
- session.abortTransaction();
4405
- throw error;
4406
- } finally {
4407
- session.endSession();
4408
- }
4409
- }
4410
- return {
4411
- add
4412
- };
4413
- }
4377
+ import { useAtlas as useAtlas9 } from "@eeplatform/nodejs-utils";
4378
+ import { useRoleRepo as useRoleRepo2 } from "@eeplatform/core";
4414
4379
 
4415
- // src/resources/division/division.controller.ts
4380
+ // src/resources/school/school.model.ts
4416
4381
  import { BadRequestError as BadRequestError16 } from "@eeplatform/nodejs-utils";
4417
4382
  import Joi10 from "joi";
4418
- function useDivisionController() {
4419
- const { add: _add } = useDivisionService();
4420
- const {
4421
- getAll: _getAll,
4422
- getById: _getById,
4423
- getByName: _getByName,
4424
- updateFieldById: _updateFieldById,
4425
- updateById: _updateById,
4426
- deleteById: _deleteById
4427
- } = useDivisionRepo();
4428
- async function add(req, res, next) {
4429
- const value = req.body;
4430
- const { error } = schemaDivision.validate(value);
4431
- if (error) {
4432
- next(new BadRequestError16(error.message));
4433
- return;
4434
- }
4435
- try {
4436
- const data = await _add(value);
4437
- res.json({
4438
- message: "Successfully created division.",
4439
- data
4440
- });
4441
- return;
4442
- } catch (error2) {
4443
- next(error2);
4444
- }
4445
- }
4446
- async function getAll(req, res, next) {
4447
- const query = req.query;
4448
- const validation = Joi10.object({
4449
- page: Joi10.number().min(1).optional().allow("", null),
4450
- limit: Joi10.number().min(1).optional().allow("", null),
4451
- search: Joi10.string().optional().allow("", null),
4452
- status: Joi10.string().optional().allow("", null),
4453
- region: Joi10.string().hex().optional().allow("", null)
4454
- });
4455
- const { error } = validation.validate(query);
4456
- const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
4457
- const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
4458
- const search = req.query.search ?? "";
4459
- const status = req.query.status ?? "active";
4460
- const region = req.query.region ?? "";
4461
- const isPageNumber = isFinite(page);
4462
- if (!isPageNumber) {
4463
- next(new BadRequestError16("Invalid page number."));
4464
- return;
4465
- }
4466
- const isLimitNumber = isFinite(limit);
4467
- if (!isLimitNumber) {
4468
- next(new BadRequestError16("Invalid limit number."));
4469
- return;
4470
- }
4471
- if (error) {
4472
- next(new BadRequestError16(error.message));
4473
- return;
4474
- }
4475
- try {
4476
- const data = await _getAll({ page, limit, search, status, region });
4477
- res.json(data);
4478
- return;
4479
- } catch (error2) {
4480
- next(error2);
4481
- }
4482
- }
4483
- async function getById(req, res, next) {
4484
- const id = req.params.id;
4485
- const validation = Joi10.object({
4486
- id: Joi10.string().hex().required()
4487
- });
4488
- const { error } = validation.validate({ id });
4489
- if (error) {
4490
- next(new BadRequestError16(error.message));
4491
- return;
4492
- }
4493
- try {
4494
- const data = await _getById(id);
4495
- res.json({
4496
- message: "Successfully retrieved division.",
4497
- data
4498
- });
4499
- return;
4500
- } catch (error2) {
4501
- next(error2);
4502
- }
4503
- }
4504
- async function getByName(req, res, next) {
4505
- const name = req.params.name;
4506
- const validation = Joi10.object({
4507
- name: Joi10.string().required()
4508
- });
4509
- const { error } = validation.validate({ name });
4510
- if (error) {
4511
- next(new BadRequestError16(error.message));
4512
- return;
4513
- }
4514
- try {
4515
- const data = await _getByName(name);
4516
- res.json({
4517
- message: "Successfully retrieved division.",
4518
- data
4519
- });
4520
- return;
4521
- } catch (error2) {
4522
- next(error2);
4523
- }
4524
- }
4525
- async function updateField(req, res, next) {
4526
- const _id = req.params.id;
4527
- const { field, value } = req.body;
4528
- const validation = Joi10.object({
4529
- _id: Joi10.string().hex().required(),
4530
- field: Joi10.string().valid("name", "director", "directorName").required(),
4531
- value: Joi10.string().required()
4532
- });
4533
- const { error } = validation.validate({ _id, field, value });
4534
- if (error) {
4535
- next(new BadRequestError16(error.message));
4536
- return;
4537
- }
4538
- try {
4539
- const message = await _updateFieldById({ _id, field, value });
4540
- res.json({ message });
4541
- return;
4542
- } catch (error2) {
4543
- next(error2);
4544
- }
4545
- }
4546
- async function updateById(req, res, next) {
4547
- const _id = req.params.id;
4548
- const payload = req.body;
4549
- const { error } = schemaDivisionUpdate.validate({ _id, ...payload });
4550
- if (error) {
4551
- next(new BadRequestError16(error.message));
4552
- return;
4553
- }
4554
- try {
4555
- const message = await _updateById(_id, payload);
4556
- res.json({ message });
4557
- return;
4558
- } catch (error2) {
4559
- next(error2);
4560
- }
4561
- }
4562
- async function deleteById(req, res, next) {
4563
- const _id = req.params.id;
4564
- const validation = Joi10.object({
4565
- _id: Joi10.string().hex().required()
4566
- });
4567
- const { error } = validation.validate({ _id });
4568
- if (error) {
4569
- next(new BadRequestError16(error.message));
4570
- return;
4571
- }
4572
- try {
4573
- const message = await _deleteById(_id);
4574
- res.json({ message });
4575
- return;
4576
- } catch (error2) {
4577
- next(error2);
4578
- }
4579
- }
4580
- return {
4581
- add,
4582
- getAll,
4583
- getById,
4584
- getByName,
4585
- updateField,
4586
- updateById,
4587
- deleteById
4588
- };
4589
- }
4590
-
4591
- // src/resources/school/school.model.ts
4592
- import { BadRequestError as BadRequestError17 } from "@eeplatform/nodejs-utils";
4593
- import Joi11 from "joi";
4594
4383
  import { ObjectId as ObjectId11 } from "mongodb";
4595
- var schemaSchool = Joi11.object({
4596
- _id: Joi11.string().hex().optional().allow(null, ""),
4597
- id: Joi11.string().min(1).max(50).required(),
4598
- name: Joi11.string().min(1).max(100).required(),
4599
- region: Joi11.string().hex().required(),
4600
- regionName: Joi11.string().min(1).max(100).optional().allow(null, ""),
4601
- division: Joi11.string().hex().required(),
4602
- divisionName: Joi11.string().min(1).max(100).optional().allow(null, ""),
4603
- principal: Joi11.string().hex().optional().allow(null, ""),
4604
- principalName: Joi11.string().min(1).max(100).optional().allow(null, ""),
4605
- street: Joi11.string().max(200).optional().allow(null, ""),
4606
- barangay: Joi11.string().max(200).optional().allow(null, ""),
4607
- city: Joi11.string().max(100).optional().allow(null, ""),
4608
- province: Joi11.string().max(100).optional().allow(null, ""),
4609
- postalCode: Joi11.string().max(20).optional().allow(null, ""),
4610
- contactNumber: Joi11.string().max(20).optional().allow(null, ""),
4611
- email: Joi11.string().email().max(100).optional().allow(null, ""),
4612
- status: Joi11.string().optional().allow(null, ""),
4613
- createdBy: Joi11.string().optional().allow(null, ""),
4614
- createdAt: Joi11.string().isoDate().optional().allow(null, ""),
4615
- updatedAt: Joi11.string().isoDate().optional().allow(null, ""),
4616
- deletedAt: Joi11.string().isoDate().optional().allow(null, "")
4384
+ var schemaSchool = Joi10.object({
4385
+ _id: Joi10.string().hex().optional().allow(null, ""),
4386
+ id: Joi10.string().min(1).max(50).required(),
4387
+ name: Joi10.string().min(1).max(100).required(),
4388
+ region: Joi10.string().hex().required(),
4389
+ regionName: Joi10.string().min(1).max(100).optional().allow(null, ""),
4390
+ division: Joi10.string().hex().required(),
4391
+ divisionName: Joi10.string().min(1).max(100).optional().allow(null, ""),
4392
+ principal: Joi10.string().hex().optional().allow(null, ""),
4393
+ principalName: Joi10.string().min(1).max(100).optional().allow(null, ""),
4394
+ street: Joi10.string().max(200).optional().allow(null, ""),
4395
+ barangay: Joi10.string().max(200).optional().allow(null, ""),
4396
+ cityMunicipality: Joi10.string().max(100).optional().allow(null, ""),
4397
+ province: Joi10.string().max(100).optional().allow(null, ""),
4398
+ provincePSGC: Joi10.number().optional().allow(null, ""),
4399
+ cityMunicipalityPSGC: Joi10.number().optional().allow(null, ""),
4400
+ postalCode: Joi10.string().max(20).optional().allow(null, ""),
4401
+ contactNumber: Joi10.string().max(20).optional().allow(null, ""),
4402
+ email: Joi10.string().email().max(100).optional().allow(null, ""),
4403
+ status: Joi10.string().optional().allow(null, ""),
4404
+ createdBy: Joi10.string().optional().allow(null, ""),
4405
+ createdAt: Joi10.string().isoDate().optional().allow(null, ""),
4406
+ updatedAt: Joi10.string().isoDate().optional().allow(null, ""),
4407
+ deletedAt: Joi10.string().isoDate().optional().allow(null, "")
4617
4408
  });
4618
- var schemaSchoolUpdate = Joi11.object({
4619
- id: Joi11.string().min(1).max(50).required(),
4620
- name: Joi11.string().min(1).max(100).required(),
4621
- region: Joi11.string().hex().required(),
4622
- regionName: Joi11.string().min(1).max(100).optional().allow(null, ""),
4623
- division: Joi11.string().hex().required(),
4624
- divisionName: Joi11.string().min(1).max(100).optional().allow(null, ""),
4625
- principal: Joi11.string().hex().optional().allow(null, ""),
4626
- principalName: Joi11.string().min(1).max(100).optional().allow(null, ""),
4627
- street: Joi11.string().max(200).optional().allow(null, ""),
4628
- barangay: Joi11.string().max(200).optional().allow(null, ""),
4629
- city: Joi11.string().max(100).optional().allow(null, ""),
4630
- province: Joi11.string().max(100).optional().allow(null, ""),
4631
- postalCode: Joi11.string().max(20).optional().allow(null, ""),
4632
- contactNumber: Joi11.string().max(20).optional().allow(null, ""),
4633
- email: Joi11.string().email().max(100).optional().allow(null, "")
4409
+ var schemaSchoolUpdate = Joi10.object({
4410
+ id: Joi10.string().min(1).max(50).required(),
4411
+ name: Joi10.string().min(1).max(100).required(),
4412
+ region: Joi10.string().hex().required(),
4413
+ regionName: Joi10.string().min(1).max(100).optional().allow(null, ""),
4414
+ division: Joi10.string().hex().required(),
4415
+ divisionName: Joi10.string().min(1).max(100).optional().allow(null, ""),
4416
+ principal: Joi10.string().hex().optional().allow(null, ""),
4417
+ principalName: Joi10.string().min(1).max(100).optional().allow(null, ""),
4418
+ street: Joi10.string().max(200).optional().allow(null, ""),
4419
+ barangay: Joi10.string().max(200).optional().allow(null, ""),
4420
+ cityMunicipality: Joi10.string().max(100).optional().allow(null, ""),
4421
+ province: Joi10.string().max(100).optional().allow(null, ""),
4422
+ provincePSGC: Joi10.number().optional().allow(null, ""),
4423
+ cityMunicipalityPSGC: Joi10.number().optional().allow(null, ""),
4424
+ postalCode: Joi10.string().max(20).optional().allow(null, ""),
4425
+ contactNumber: Joi10.string().max(20).optional().allow(null, ""),
4426
+ email: Joi10.string().email().max(100).optional().allow(null, "")
4634
4427
  });
4635
4428
  function modelSchool(value) {
4636
4429
  const { error } = schemaSchool.validate(value);
4637
4430
  if (error) {
4638
- throw new BadRequestError17(`Invalid sdo data: ${error.message}`);
4431
+ throw new BadRequestError16(`Invalid sdo data: ${error.message}`);
4639
4432
  }
4640
4433
  if (value._id && typeof value._id === "string") {
4641
4434
  try {
@@ -4677,12 +4470,14 @@ function modelSchool(value) {
4677
4470
  principalName: value.principalName ?? "",
4678
4471
  street: value.street ?? "",
4679
4472
  barangay: value.barangay ?? "",
4680
- city: value.city ?? "",
4473
+ cityMunicipality: value.cityMunicipality ?? "",
4681
4474
  province: value.province ?? "",
4682
4475
  postalCode: value.postalCode ?? "",
4683
4476
  contactNumber: value.contactNumber ?? "",
4684
4477
  email: value.email ?? "",
4685
4478
  status: value.status ?? "active",
4479
+ provincePSGC: value.provincePSGC ?? 0,
4480
+ cityMunicipalityPSGC: value.cityMunicipalityPSGC ?? 0,
4686
4481
  createdBy: value.createdBy ?? "",
4687
4482
  createdAt: value.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
4688
4483
  updatedAt: value.updatedAt ?? "",
@@ -4693,17 +4488,17 @@ function modelSchool(value) {
4693
4488
  // src/resources/school/school.repository.ts
4694
4489
  import {
4695
4490
  AppError as AppError6,
4696
- BadRequestError as BadRequestError18,
4491
+ BadRequestError as BadRequestError17,
4697
4492
  InternalServerError as InternalServerError7,
4698
4493
  logger as logger13,
4699
4494
  makeCacheKey as makeCacheKey6,
4700
4495
  paginate as paginate6,
4701
- useAtlas as useAtlas8,
4496
+ useAtlas as useAtlas7,
4702
4497
  useCache as useCache6
4703
4498
  } from "@eeplatform/nodejs-utils";
4704
4499
  import { ObjectId as ObjectId12 } from "mongodb";
4705
4500
  function useSchoolRepo() {
4706
- const db = useAtlas8.getDb();
4501
+ const db = useAtlas7.getDb();
4707
4502
  if (!db) {
4708
4503
  throw new Error("Unable to connect to server.");
4709
4504
  }
@@ -4761,7 +4556,7 @@ function useSchoolRepo() {
4761
4556
  } else {
4762
4557
  const isDuplicated = error.message.includes("duplicate");
4763
4558
  if (isDuplicated) {
4764
- throw new BadRequestError18("Duplicate, school already exists.");
4559
+ throw new BadRequestError17("Duplicate, school already exists.");
4765
4560
  }
4766
4561
  throw new InternalServerError7("Failed to add school.");
4767
4562
  }
@@ -4833,7 +4628,7 @@ function useSchoolRepo() {
4833
4628
  try {
4834
4629
  _id = new ObjectId12(_id);
4835
4630
  } catch (error) {
4836
- throw new BadRequestError18("Invalid ID.");
4631
+ throw new BadRequestError17("Invalid ID.");
4837
4632
  }
4838
4633
  const query = { _id };
4839
4634
  const cacheKeyOptions = {
@@ -4856,7 +4651,7 @@ function useSchoolRepo() {
4856
4651
  }
4857
4652
  const result = await collection.findOne(query);
4858
4653
  if (!result) {
4859
- throw new BadRequestError18("School not found.");
4654
+ throw new BadRequestError17("School not found.");
4860
4655
  }
4861
4656
  setCache(cacheKey, result, 300).then(() => {
4862
4657
  logger13.log({
@@ -4882,7 +4677,7 @@ function useSchoolRepo() {
4882
4677
  try {
4883
4678
  createdBy = new ObjectId12(createdBy);
4884
4679
  } catch (error) {
4885
- throw new BadRequestError18("Invalid ID.");
4680
+ throw new BadRequestError17("Invalid ID.");
4886
4681
  }
4887
4682
  const cacheKey = makeCacheKey6(namespace_collection, { createdBy });
4888
4683
  try {
@@ -4899,7 +4694,7 @@ function useSchoolRepo() {
4899
4694
  status: "pending"
4900
4695
  });
4901
4696
  if (!result) {
4902
- throw new BadRequestError18("School not found.");
4697
+ throw new BadRequestError17("School not found.");
4903
4698
  }
4904
4699
  setCache(cacheKey, result, 300).then(() => {
4905
4700
  logger13.log({
@@ -4937,7 +4732,7 @@ function useSchoolRepo() {
4937
4732
  deletedAt: { $in: ["", null] }
4938
4733
  });
4939
4734
  if (!result) {
4940
- throw new BadRequestError18("School not found.");
4735
+ throw new BadRequestError17("School not found.");
4941
4736
  }
4942
4737
  setCache(cacheKey, result, 300).then(() => {
4943
4738
  logger13.log({
@@ -4963,7 +4758,7 @@ function useSchoolRepo() {
4963
4758
  try {
4964
4759
  _id = new ObjectId12(_id);
4965
4760
  } catch (error) {
4966
- throw new BadRequestError18("Invalid ID.");
4761
+ throw new BadRequestError17("Invalid ID.");
4967
4762
  }
4968
4763
  try {
4969
4764
  await collection.updateOne(
@@ -4982,16 +4777,22 @@ function useSchoolRepo() {
4982
4777
  }
4983
4778
  }
4984
4779
  async function updateFieldById({ _id, field, value } = {}, session) {
4985
- const allowedFields = ["name"];
4780
+ const allowedFields = [
4781
+ "name",
4782
+ "division",
4783
+ "divisionName",
4784
+ "region",
4785
+ "regionName"
4786
+ ];
4986
4787
  if (!allowedFields.includes(field)) {
4987
- throw new BadRequestError18(
4788
+ throw new BadRequestError17(
4988
4789
  `Field "${field}" is not allowed to be updated.`
4989
4790
  );
4990
4791
  }
4991
4792
  try {
4992
4793
  _id = new ObjectId12(_id);
4993
4794
  } catch (error) {
4994
- throw new BadRequestError18("Invalid ID.");
4795
+ throw new BadRequestError17("Invalid ID.");
4995
4796
  }
4996
4797
  try {
4997
4798
  await collection.updateOne(
@@ -5005,15 +4806,39 @@ function useSchoolRepo() {
5005
4806
  throw new InternalServerError7(`Failed to update school ${field}.`);
5006
4807
  }
5007
4808
  }
4809
+ async function updateDivisionNameByDivision(division, name, session) {
4810
+ try {
4811
+ division = new ObjectId12(division);
4812
+ } catch (error) {
4813
+ throw new BadRequestError17("Invalid division.");
4814
+ }
4815
+ try {
4816
+ const result = await collection.updateMany(
4817
+ { division },
4818
+ { $set: { divisionName: name, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
4819
+ { session }
4820
+ );
4821
+ if (result.modifiedCount === 0) {
4822
+ logger13.log({
4823
+ level: "info",
4824
+ message: `No schools found for division ${division} to update divisionName.`
4825
+ });
4826
+ }
4827
+ delCachedData();
4828
+ return `Successfully updated school divisionName.`;
4829
+ } catch (error) {
4830
+ throw new InternalServerError7(`Failed to update school divisionName.`);
4831
+ }
4832
+ }
5008
4833
  async function updateById(_id, options, session) {
5009
4834
  const { error } = schemaSchoolUpdate.validate(options);
5010
4835
  if (error) {
5011
- throw new BadRequestError18(`Invalid school data: ${error.message}`);
4836
+ throw new BadRequestError17(`Invalid school data: ${error.message}`);
5012
4837
  }
5013
4838
  try {
5014
4839
  _id = new ObjectId12(_id);
5015
4840
  } catch (error2) {
5016
- throw new BadRequestError18("Invalid ID.");
4841
+ throw new BadRequestError17("Invalid ID.");
5017
4842
  }
5018
4843
  try {
5019
4844
  await collection.updateOne({ _id }, { $set: options }, { session });
@@ -5027,7 +4852,7 @@ function useSchoolRepo() {
5027
4852
  try {
5028
4853
  _id = new ObjectId12(_id);
5029
4854
  } catch (error) {
5030
- throw new BadRequestError18("Invalid ID.");
4855
+ throw new BadRequestError17("Invalid ID.");
5031
4856
  }
5032
4857
  try {
5033
4858
  await collection.updateOne(
@@ -5051,13 +4876,19 @@ function useSchoolRepo() {
5051
4876
  updateById,
5052
4877
  deleteById,
5053
4878
  getByName,
5054
- delCachedData
4879
+ delCachedData,
4880
+ updateDivisionNameByDivision
5055
4881
  };
5056
4882
  }
5057
4883
 
5058
4884
  // src/resources/school/school.service.ts
5059
- import { BadRequestError as BadRequestError19, useAtlas as useAtlas9, logger as logger14 } from "@eeplatform/nodejs-utils";
5060
- import { useRoleRepo as useRoleRepo2, useUserRepo, useMemberRepo } from "@eeplatform/core";
4885
+ import { BadRequestError as BadRequestError18, useAtlas as useAtlas8, logger as logger14 } from "@eeplatform/nodejs-utils";
4886
+ import {
4887
+ useRoleRepo,
4888
+ useUserRepo,
4889
+ useMemberRepo,
4890
+ usePSGCRepo
4891
+ } from "@eeplatform/core";
5061
4892
 
5062
4893
  // node_modules/xlsx/xlsx.mjs
5063
4894
  var XLSX = {};
@@ -33225,17 +33056,17 @@ function useSchoolService() {
33225
33056
  getById,
33226
33057
  delCachedData: delCachedSchoolData
33227
33058
  } = useSchoolRepo();
33228
- const { addRole, delCachedData: delCachedRoleData } = useRoleRepo2();
33059
+ const { addRole, delCachedData: delCachedRoleData } = useRoleRepo();
33229
33060
  const { getUserById } = useUserRepo();
33230
33061
  const { add: addMember } = useMemberRepo();
33231
33062
  async function register(value) {
33232
33063
  const { error } = schemaSchool.validate(value);
33233
33064
  if (error) {
33234
- throw new BadRequestError19(error.message);
33065
+ throw new BadRequestError18(error.message);
33235
33066
  }
33236
33067
  const existingSchool = await getPendingByCreatedBy(value.createdBy ?? "");
33237
33068
  if (existingSchool) {
33238
- throw new BadRequestError19(
33069
+ throw new BadRequestError18(
33239
33070
  "You already have a pending school registration."
33240
33071
  );
33241
33072
  }
@@ -33250,9 +33081,9 @@ function useSchoolService() {
33250
33081
  async function approve(id) {
33251
33082
  const school = await getById(id, "pending");
33252
33083
  if (!school) {
33253
- throw new BadRequestError19("School registration not found.");
33084
+ throw new BadRequestError18("School registration not found.");
33254
33085
  }
33255
- const session = useAtlas9.getClient()?.startSession();
33086
+ const session = useAtlas8.getClient()?.startSession();
33256
33087
  if (!session) {
33257
33088
  throw new Error("Unable to start session for school service.");
33258
33089
  }
@@ -33274,11 +33105,11 @@ function useSchoolService() {
33274
33105
  session
33275
33106
  );
33276
33107
  if (!school.createdBy) {
33277
- throw new BadRequestError19("School must have a creator.");
33108
+ throw new BadRequestError18("School must have a creator.");
33278
33109
  }
33279
33110
  const user = await getUserById(school.createdBy ?? "");
33280
33111
  if (!user) {
33281
- throw new BadRequestError19("User not found for the school creator.");
33112
+ throw new BadRequestError18("User not found for the school creator.");
33282
33113
  }
33283
33114
  await addMember(
33284
33115
  {
@@ -33308,9 +33139,9 @@ function useSchoolService() {
33308
33139
  async function add(value) {
33309
33140
  const { error } = schemaSchool.validate(value);
33310
33141
  if (error) {
33311
- throw new BadRequestError19(error.message);
33142
+ throw new BadRequestError18(error.message);
33312
33143
  }
33313
- const session = useAtlas9.getClient()?.startSession();
33144
+ const session = useAtlas8.getClient()?.startSession();
33314
33145
  if (!session) {
33315
33146
  throw new Error("Unable to start session for school service.");
33316
33147
  }
@@ -33332,11 +33163,11 @@ function useSchoolService() {
33332
33163
  session
33333
33164
  );
33334
33165
  if (!value.createdBy) {
33335
- throw new BadRequestError19("School must have a creator.");
33166
+ throw new BadRequestError18("School must have a creator.");
33336
33167
  }
33337
33168
  const user = await getUserById(value.createdBy ?? "");
33338
33169
  if (!user) {
33339
- throw new BadRequestError19("User not found for the school creator.");
33170
+ throw new BadRequestError18("User not found for the school creator.");
33340
33171
  }
33341
33172
  await addMember(
33342
33173
  {
@@ -33367,7 +33198,7 @@ function useSchoolService() {
33367
33198
  const isCSV = file.mimetype.includes("csv") || file.originalname.endsWith(".csv");
33368
33199
  const isExcel = file.mimetype.includes("sheet") || file.originalname.endsWith(".xlsx") || file.originalname.endsWith(".xls");
33369
33200
  if (!isCSV && !isExcel) {
33370
- throw new BadRequestError19("Only CSV and Excel files are supported");
33201
+ throw new BadRequestError18("Only CSV and Excel files are supported");
33371
33202
  }
33372
33203
  let rawData = [];
33373
33204
  try {
@@ -33384,17 +33215,17 @@ function useSchoolService() {
33384
33215
  transformHeader: (header) => header.trim()
33385
33216
  });
33386
33217
  if (parseResult.errors.length > 0) {
33387
- throw new BadRequestError19(
33218
+ throw new BadRequestError18(
33388
33219
  `CSV parsing error: ${parseResult.errors[0].message}`
33389
33220
  );
33390
33221
  }
33391
33222
  rawData = parseResult.data;
33392
33223
  }
33393
33224
  } catch (error) {
33394
- throw new BadRequestError19(`File parsing error: ${error.message}`);
33225
+ throw new BadRequestError18(`File parsing error: ${error.message}`);
33395
33226
  }
33396
33227
  if (!rawData.length) {
33397
- throw new BadRequestError19("No data found in file");
33228
+ throw new BadRequestError18("No data found in file");
33398
33229
  }
33399
33230
  const schools = [];
33400
33231
  const errors = [];
@@ -33403,6 +33234,8 @@ function useSchoolService() {
33403
33234
  const rowNum = i + 1;
33404
33235
  const schoolId = (row.schoolId || row.id || "").toString().trim();
33405
33236
  const name = (row.name || "").toString().trim();
33237
+ const province = (row.province || "").toString().trim();
33238
+ const cityMunicipality = (row.cityMunicipality || "").toString().trim();
33406
33239
  if (!schoolId) {
33407
33240
  errors.push(`Row ${rowNum}: schoolId is required`);
33408
33241
  continue;
@@ -33411,6 +33244,14 @@ function useSchoolService() {
33411
33244
  errors.push(`Row ${rowNum}: name is required`);
33412
33245
  continue;
33413
33246
  }
33247
+ if (!province) {
33248
+ errors.push(`Row ${rowNum}: province is required`);
33249
+ continue;
33250
+ }
33251
+ if (!cityMunicipality) {
33252
+ errors.push(`Row ${rowNum}: city/municipality is required`);
33253
+ continue;
33254
+ }
33414
33255
  const school = {
33415
33256
  id: schoolId,
33416
33257
  name,
@@ -33418,6 +33259,8 @@ function useSchoolService() {
33418
33259
  regionName: payload.regionName,
33419
33260
  division: payload.division,
33420
33261
  divisionName: payload.divisionName,
33262
+ province,
33263
+ cityMunicipality,
33421
33264
  status: "active"
33422
33265
  };
33423
33266
  const { error } = schemaSchool.validate(school);
@@ -33428,7 +33271,7 @@ function useSchoolService() {
33428
33271
  schools.push(school);
33429
33272
  }
33430
33273
  if (errors.length > 0) {
33431
- throw new BadRequestError19(
33274
+ throw new BadRequestError18(
33432
33275
  `Validation errors:
33433
33276
  ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33434
33277
  ...and ${errors.length - 5} more` : ""}`
@@ -33440,14 +33283,36 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33440
33283
  skipped: 0,
33441
33284
  errors: []
33442
33285
  };
33286
+ const { getByName: getPSGCByName } = usePSGCRepo();
33443
33287
  for (let i = 0; i < schools.length; i++) {
33444
- const session = useAtlas9.getClient()?.startSession();
33288
+ const session = useAtlas8.getClient()?.startSession();
33445
33289
  if (!session) {
33446
33290
  throw new Error("Unable to start MongoDB session");
33447
33291
  }
33448
33292
  session.startTransaction();
33449
33293
  const school = schools[i];
33450
33294
  try {
33295
+ const provincePSGC = await getPSGCByName({
33296
+ name: school.province ?? "",
33297
+ type: "Prov"
33298
+ });
33299
+ if (!provincePSGC) {
33300
+ throw new BadRequestError18(
33301
+ `Province '${school.province}' not found in PSGC data.`
33302
+ );
33303
+ }
33304
+ const cityMunPSGC = await getPSGCByName({
33305
+ name: school.cityMunicipality ?? "",
33306
+ cityMunicipality: true,
33307
+ code: provincePSGC.code
33308
+ });
33309
+ if (!cityMunPSGC) {
33310
+ throw new BadRequestError18(
33311
+ `City/Municipality '${school.cityMunicipality}' not found in PSGC data.`
33312
+ );
33313
+ }
33314
+ school.provincePSGC = provincePSGC.code ?? 0;
33315
+ school.cityMunicipalityPSGC = cityMunPSGC.code ?? 0;
33451
33316
  const schoolId = await addSchool(school, session, false);
33452
33317
  await addRole(
33453
33318
  {
@@ -33494,8 +33359,8 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33494
33359
  }
33495
33360
 
33496
33361
  // src/resources/school/school.controller.ts
33497
- import { BadRequestError as BadRequestError20 } from "@eeplatform/nodejs-utils";
33498
- import Joi12 from "joi";
33362
+ import { BadRequestError as BadRequestError19 } from "@eeplatform/nodejs-utils";
33363
+ import Joi11 from "joi";
33499
33364
  function useSchoolController() {
33500
33365
  const {
33501
33366
  getAll: _getAll,
@@ -33515,7 +33380,7 @@ function useSchoolController() {
33515
33380
  const payload = req.body;
33516
33381
  const { error } = schemaSchool.validate(payload);
33517
33382
  if (error) {
33518
- next(new BadRequestError20(`Validation error: ${error.message}`));
33383
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33519
33384
  return;
33520
33385
  }
33521
33386
  try {
@@ -33528,19 +33393,19 @@ function useSchoolController() {
33528
33393
  }
33529
33394
  }
33530
33395
  async function getAll(req, res, next) {
33531
- const validation = Joi12.object({
33532
- page: Joi12.number().optional().allow(null, ""),
33533
- limit: Joi12.number().optional().allow(null, ""),
33534
- sort: Joi12.string().optional().allow(null, ""),
33535
- sortOrder: Joi12.string().optional().allow(null, ""),
33536
- status: Joi12.string().optional().allow(null, ""),
33537
- org: Joi12.string().hex().optional().allow(null, ""),
33538
- app: Joi12.string().optional().allow(null, ""),
33539
- search: Joi12.string().optional().allow(null, "")
33396
+ const validation = Joi11.object({
33397
+ page: Joi11.number().optional().allow(null, ""),
33398
+ limit: Joi11.number().optional().allow(null, ""),
33399
+ sort: Joi11.string().optional().allow(null, ""),
33400
+ sortOrder: Joi11.string().optional().allow(null, ""),
33401
+ status: Joi11.string().optional().allow(null, ""),
33402
+ org: Joi11.string().hex().optional().allow(null, ""),
33403
+ app: Joi11.string().optional().allow(null, ""),
33404
+ search: Joi11.string().optional().allow(null, "")
33540
33405
  });
33541
33406
  const { error } = validation.validate(req.query);
33542
33407
  if (error) {
33543
- next(new BadRequestError20(`Validation error: ${error.message}`));
33408
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33544
33409
  return;
33545
33410
  }
33546
33411
  const page = parseInt(req.query.page) ?? 1;
@@ -33572,10 +33437,10 @@ function useSchoolController() {
33572
33437
  }
33573
33438
  async function getByCreatedBy(req, res, next) {
33574
33439
  const createdBy = req.params.createdBy;
33575
- const validation = Joi12.string().hex().required();
33440
+ const validation = Joi11.string().hex().required();
33576
33441
  const { error } = validation.validate(createdBy);
33577
33442
  if (error) {
33578
- next(new BadRequestError20(`Validation error: ${error.message}`));
33443
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33579
33444
  return;
33580
33445
  }
33581
33446
  try {
@@ -33589,13 +33454,13 @@ function useSchoolController() {
33589
33454
  async function updateStatusById(req, res, next) {
33590
33455
  const schoolId = req.params.id;
33591
33456
  const status = req.params.status;
33592
- const validation = Joi12.object({
33593
- id: Joi12.string().hex().required(),
33594
- status: Joi12.string().valid("active", "deleted", "suspended").required()
33457
+ const validation = Joi11.object({
33458
+ id: Joi11.string().hex().required(),
33459
+ status: Joi11.string().valid("active", "deleted", "suspended").required()
33595
33460
  });
33596
33461
  const { error } = validation.validate({ id: schoolId, status });
33597
33462
  if (error) {
33598
- next(new BadRequestError20(`Validation error: ${error.message}`));
33463
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33599
33464
  return;
33600
33465
  }
33601
33466
  try {
@@ -33610,7 +33475,7 @@ function useSchoolController() {
33610
33475
  const payload = req.body;
33611
33476
  const { error } = schemaSchool.validate(payload);
33612
33477
  if (error) {
33613
- next(new BadRequestError20(`Validation error: ${error.message}`));
33478
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33614
33479
  return;
33615
33480
  }
33616
33481
  try {
@@ -33624,12 +33489,12 @@ function useSchoolController() {
33624
33489
  }
33625
33490
  async function approveSchool(req, res, next) {
33626
33491
  const schoolId = req.params.id;
33627
- const validation = Joi12.object({
33628
- id: Joi12.string().hex().required()
33492
+ const validation = Joi11.object({
33493
+ id: Joi11.string().hex().required()
33629
33494
  });
33630
33495
  const { error } = validation.validate({ id: schoolId });
33631
33496
  if (error) {
33632
- next(new BadRequestError20(`Validation error: ${error.message}`));
33497
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33633
33498
  return;
33634
33499
  }
33635
33500
  try {
@@ -33648,11 +33513,11 @@ function useSchoolController() {
33648
33513
  return;
33649
33514
  }
33650
33515
  const { region, regionName, division, divisionName } = req.body;
33651
- const validation = Joi12.object({
33652
- region: Joi12.string().hex().required(),
33653
- regionName: Joi12.string().min(1).required(),
33654
- division: Joi12.string().hex().required(),
33655
- divisionName: Joi12.string().min(1).required()
33516
+ const validation = Joi11.object({
33517
+ region: Joi11.string().hex().required(),
33518
+ regionName: Joi11.string().min(1).required(),
33519
+ division: Joi11.string().hex().required(),
33520
+ divisionName: Joi11.string().min(1).required()
33656
33521
  });
33657
33522
  const { error } = validation.validate({
33658
33523
  region,
@@ -33661,7 +33526,7 @@ function useSchoolController() {
33661
33526
  divisionName
33662
33527
  });
33663
33528
  if (error) {
33664
- next(new BadRequestError20(`Validation error: ${error.message}`));
33529
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33665
33530
  return;
33666
33531
  }
33667
33532
  try {
@@ -33681,14 +33546,14 @@ function useSchoolController() {
33681
33546
  async function updateFieldById(req, res, next) {
33682
33547
  const _id = req.params.id;
33683
33548
  const { field, value } = req.body;
33684
- const validation = Joi12.object({
33685
- _id: Joi12.string().hex().required(),
33686
- field: Joi12.string().valid("name", "director", "directorName").required(),
33687
- value: Joi12.string().required()
33549
+ const validation = Joi11.object({
33550
+ _id: Joi11.string().hex().required(),
33551
+ field: Joi11.string().valid("name", "director", "directorName").required(),
33552
+ value: Joi11.string().required()
33688
33553
  });
33689
33554
  const { error } = validation.validate({ _id, field, value });
33690
33555
  if (error) {
33691
- next(new BadRequestError20(error.message));
33556
+ next(new BadRequestError19(error.message));
33692
33557
  return;
33693
33558
  }
33694
33559
  try {
@@ -33702,17 +33567,17 @@ function useSchoolController() {
33702
33567
  async function updateById(req, res, next) {
33703
33568
  const id = req.params.id;
33704
33569
  const payload = req.body;
33705
- const validation = Joi12.object({
33706
- id: Joi12.string().hex().required()
33570
+ const validation = Joi11.object({
33571
+ id: Joi11.string().hex().required()
33707
33572
  });
33708
33573
  const { error: idError } = validation.validate({ id });
33709
33574
  if (idError) {
33710
- next(new BadRequestError20(idError.message));
33575
+ next(new BadRequestError19(idError.message));
33711
33576
  return;
33712
33577
  }
33713
33578
  const { error } = schemaSchoolUpdate.validate(payload);
33714
33579
  if (error) {
33715
- next(new BadRequestError20(error.message));
33580
+ next(new BadRequestError19(error.message));
33716
33581
  return;
33717
33582
  }
33718
33583
  try {
@@ -33725,12 +33590,12 @@ function useSchoolController() {
33725
33590
  }
33726
33591
  async function deleteById(req, res, next) {
33727
33592
  const _id = req.params.id;
33728
- const validation = Joi12.object({
33729
- _id: Joi12.string().hex().required()
33593
+ const validation = Joi11.object({
33594
+ _id: Joi11.string().hex().required()
33730
33595
  });
33731
33596
  const { error } = validation.validate({ _id });
33732
33597
  if (error) {
33733
- next(new BadRequestError20(error.message));
33598
+ next(new BadRequestError19(error.message));
33734
33599
  return;
33735
33600
  }
33736
33601
  try {
@@ -33755,6 +33620,245 @@ function useSchoolController() {
33755
33620
  };
33756
33621
  }
33757
33622
 
33623
+ // src/resources/division/division.service.ts
33624
+ function useDivisionService() {
33625
+ const {
33626
+ add: _add,
33627
+ updateById: _updateById,
33628
+ getById: _getById
33629
+ } = useDivisionRepo();
33630
+ const { addRole } = useRoleRepo2();
33631
+ const { updateDivisionNameByDivision } = useSchoolRepo();
33632
+ async function add(value) {
33633
+ const session = useAtlas9.getClient()?.startSession();
33634
+ if (!session) {
33635
+ throw new Error("Unable to start session for division service.");
33636
+ }
33637
+ try {
33638
+ session.startTransaction();
33639
+ const division = await _add(value, session);
33640
+ await addRole(
33641
+ {
33642
+ id: division.toString(),
33643
+ name: "Admin",
33644
+ type: "basic-edu-sdo",
33645
+ permissions: ["*"],
33646
+ status: "active",
33647
+ default: true
33648
+ },
33649
+ session
33650
+ );
33651
+ await session.commitTransaction();
33652
+ return "Division and admin role created successfully.";
33653
+ } catch (error) {
33654
+ session.abortTransaction();
33655
+ throw error;
33656
+ } finally {
33657
+ session.endSession();
33658
+ }
33659
+ }
33660
+ async function updateById(id, value) {
33661
+ const session = useAtlas9.getClient()?.startSession();
33662
+ if (!session) {
33663
+ throw new Error("Unable to start session for division service.");
33664
+ }
33665
+ try {
33666
+ session.startTransaction();
33667
+ const division = await _getById(id);
33668
+ await _updateById(id, value, session);
33669
+ if (division && division._id && division.name !== value.name) {
33670
+ await updateDivisionNameByDivision(division._id, value.name, session);
33671
+ }
33672
+ await session.commitTransaction();
33673
+ return "Division and admin role created successfully.";
33674
+ } catch (error) {
33675
+ session.abortTransaction();
33676
+ throw error;
33677
+ } finally {
33678
+ session.endSession();
33679
+ }
33680
+ }
33681
+ return {
33682
+ add,
33683
+ updateById
33684
+ };
33685
+ }
33686
+
33687
+ // src/resources/division/division.controller.ts
33688
+ import { BadRequestError as BadRequestError20 } from "@eeplatform/nodejs-utils";
33689
+ import Joi12 from "joi";
33690
+ function useDivisionController() {
33691
+ const { add: _add, updateById: _updateById } = useDivisionService();
33692
+ const {
33693
+ getAll: _getAll,
33694
+ getById: _getById,
33695
+ getByName: _getByName,
33696
+ updateFieldById: _updateFieldById,
33697
+ deleteById: _deleteById
33698
+ } = useDivisionRepo();
33699
+ async function add(req, res, next) {
33700
+ const value = req.body;
33701
+ const { error } = schemaDivision.validate(value);
33702
+ if (error) {
33703
+ next(new BadRequestError20(error.message));
33704
+ return;
33705
+ }
33706
+ try {
33707
+ const data = await _add(value);
33708
+ res.json({
33709
+ message: "Successfully created division.",
33710
+ data
33711
+ });
33712
+ return;
33713
+ } catch (error2) {
33714
+ next(error2);
33715
+ }
33716
+ }
33717
+ async function getAll(req, res, next) {
33718
+ const query = req.query;
33719
+ const validation = Joi12.object({
33720
+ page: Joi12.number().min(1).optional().allow("", null),
33721
+ limit: Joi12.number().min(1).optional().allow("", null),
33722
+ search: Joi12.string().optional().allow("", null),
33723
+ status: Joi12.string().optional().allow("", null),
33724
+ region: Joi12.string().hex().optional().allow("", null)
33725
+ });
33726
+ const { error } = validation.validate(query);
33727
+ const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
33728
+ const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
33729
+ const search = req.query.search ?? "";
33730
+ const status = req.query.status ?? "active";
33731
+ const region = req.query.region ?? "";
33732
+ const isPageNumber = isFinite(page);
33733
+ if (!isPageNumber) {
33734
+ next(new BadRequestError20("Invalid page number."));
33735
+ return;
33736
+ }
33737
+ const isLimitNumber = isFinite(limit);
33738
+ if (!isLimitNumber) {
33739
+ next(new BadRequestError20("Invalid limit number."));
33740
+ return;
33741
+ }
33742
+ if (error) {
33743
+ next(new BadRequestError20(error.message));
33744
+ return;
33745
+ }
33746
+ try {
33747
+ const data = await _getAll({ page, limit, search, status, region });
33748
+ res.json(data);
33749
+ return;
33750
+ } catch (error2) {
33751
+ next(error2);
33752
+ }
33753
+ }
33754
+ async function getById(req, res, next) {
33755
+ const id = req.params.id;
33756
+ const validation = Joi12.object({
33757
+ id: Joi12.string().hex().required()
33758
+ });
33759
+ const { error } = validation.validate({ id });
33760
+ if (error) {
33761
+ next(new BadRequestError20(error.message));
33762
+ return;
33763
+ }
33764
+ try {
33765
+ const data = await _getById(id);
33766
+ res.json({
33767
+ message: "Successfully retrieved division.",
33768
+ data
33769
+ });
33770
+ return;
33771
+ } catch (error2) {
33772
+ next(error2);
33773
+ }
33774
+ }
33775
+ async function getByName(req, res, next) {
33776
+ const name = req.params.name;
33777
+ const validation = Joi12.object({
33778
+ name: Joi12.string().required()
33779
+ });
33780
+ const { error } = validation.validate({ name });
33781
+ if (error) {
33782
+ next(new BadRequestError20(error.message));
33783
+ return;
33784
+ }
33785
+ try {
33786
+ const data = await _getByName(name);
33787
+ res.json({
33788
+ message: "Successfully retrieved division.",
33789
+ data
33790
+ });
33791
+ return;
33792
+ } catch (error2) {
33793
+ next(error2);
33794
+ }
33795
+ }
33796
+ async function updateField(req, res, next) {
33797
+ const _id = req.params.id;
33798
+ const { field, value } = req.body;
33799
+ const validation = Joi12.object({
33800
+ _id: Joi12.string().hex().required(),
33801
+ field: Joi12.string().valid("name", "director", "directorName").required(),
33802
+ value: Joi12.string().required()
33803
+ });
33804
+ const { error } = validation.validate({ _id, field, value });
33805
+ if (error) {
33806
+ next(new BadRequestError20(error.message));
33807
+ return;
33808
+ }
33809
+ try {
33810
+ const message = await _updateFieldById({ _id, field, value });
33811
+ res.json({ message });
33812
+ return;
33813
+ } catch (error2) {
33814
+ next(error2);
33815
+ }
33816
+ }
33817
+ async function updateById(req, res, next) {
33818
+ const _id = req.params.id;
33819
+ const payload = req.body;
33820
+ const { error } = schemaDivisionUpdate.validate({ _id, ...payload });
33821
+ if (error) {
33822
+ next(new BadRequestError20(error.message));
33823
+ return;
33824
+ }
33825
+ try {
33826
+ const message = await _updateById(_id, payload);
33827
+ res.json({ message });
33828
+ return;
33829
+ } catch (error2) {
33830
+ next(error2);
33831
+ }
33832
+ }
33833
+ async function deleteById(req, res, next) {
33834
+ const _id = req.params.id;
33835
+ const validation = Joi12.object({
33836
+ _id: Joi12.string().hex().required()
33837
+ });
33838
+ const { error } = validation.validate({ _id });
33839
+ if (error) {
33840
+ next(new BadRequestError20(error.message));
33841
+ return;
33842
+ }
33843
+ try {
33844
+ const message = await _deleteById(_id);
33845
+ res.json({ message });
33846
+ return;
33847
+ } catch (error2) {
33848
+ next(error2);
33849
+ }
33850
+ }
33851
+ return {
33852
+ add,
33853
+ getAll,
33854
+ getById,
33855
+ getByName,
33856
+ updateField,
33857
+ updateById,
33858
+ deleteById
33859
+ };
33860
+ }
33861
+
33758
33862
  // src/resources/asset/asset.model.ts
33759
33863
  import { BadRequestError as BadRequestError21 } from "@eeplatform/nodejs-utils";
33760
33864
  import Joi13 from "joi";