@eeplatform/basic-edu 1.3.1 → 1.3.3

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
@@ -1832,8 +1832,9 @@ var learnerInfoSchema = Joi3.object({
1832
1832
  hasDisability: Joi3.boolean().required(),
1833
1833
  disabilityTypes: Joi3.array().items(
1834
1834
  Joi3.string().valid(
1835
- "Visual Impairment (Blind)",
1836
- "Visual Impairment (Low Vision)",
1835
+ "Visual Impairment",
1836
+ "Visual Impairment(Blind)",
1837
+ "Visual Impairment(Low Vision)",
1837
1838
  "Hearing Impairment",
1838
1839
  "Learning Disability",
1839
1840
  "Intellectual Disability",
@@ -1844,8 +1845,7 @@ var learnerInfoSchema = Joi3.object({
1844
1845
  "Cerebral Palsy",
1845
1846
  "Special Health Problem/Chronic Disease",
1846
1847
  "Multiple Disorder",
1847
- "Cancer",
1848
- "Other"
1848
+ "Cancer"
1849
1849
  )
1850
1850
  ).optional(),
1851
1851
  otherDisabilityDetails: Joi3.string().optional().allow("", null),
@@ -4336,6 +4336,13 @@ function useDivisionRepo() {
4336
4336
  } catch (error2) {
4337
4337
  throw new BadRequestError15("Invalid ID.");
4338
4338
  }
4339
+ if (options.region) {
4340
+ try {
4341
+ options.region = new ObjectId10(options.region);
4342
+ } catch (error2) {
4343
+ throw new BadRequestError15("Invalid region ID.");
4344
+ }
4345
+ }
4339
4346
  try {
4340
4347
  await collection.updateOne({ _id }, { $set: options }, { session });
4341
4348
  delCachedData();
@@ -4374,272 +4381,61 @@ function useDivisionRepo() {
4374
4381
  }
4375
4382
 
4376
4383
  // 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
- }
4384
+ import { useAtlas as useAtlas9 } from "@eeplatform/nodejs-utils";
4385
+ import { useRoleRepo as useRoleRepo2 } from "@eeplatform/core";
4414
4386
 
4415
- // src/resources/division/division.controller.ts
4387
+ // src/resources/school/school.model.ts
4416
4388
  import { BadRequestError as BadRequestError16 } from "@eeplatform/nodejs-utils";
4417
4389
  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
4390
  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
- cityMunicipality: Joi11.string().max(100).optional().allow(null, ""),
4608
- province: Joi11.string().max(100).optional().allow(null, ""),
4609
- provincePSGC: Joi11.number().optional().allow(null, ""),
4610
- cityMunicipalityPSGC: Joi11.number().optional().allow(null, ""),
4611
- postalCode: Joi11.string().max(20).optional().allow(null, ""),
4612
- contactNumber: Joi11.string().max(20).optional().allow(null, ""),
4613
- email: Joi11.string().email().max(100).optional().allow(null, ""),
4614
- status: Joi11.string().optional().allow(null, ""),
4615
- createdBy: Joi11.string().optional().allow(null, ""),
4616
- createdAt: Joi11.string().isoDate().optional().allow(null, ""),
4617
- updatedAt: Joi11.string().isoDate().optional().allow(null, ""),
4618
- deletedAt: Joi11.string().isoDate().optional().allow(null, "")
4391
+ var schemaSchool = Joi10.object({
4392
+ _id: Joi10.string().hex().optional().allow(null, ""),
4393
+ id: Joi10.string().min(1).max(50).required(),
4394
+ name: Joi10.string().min(1).max(100).required(),
4395
+ region: Joi10.string().hex().required(),
4396
+ regionName: Joi10.string().min(1).max(100).optional().allow(null, ""),
4397
+ division: Joi10.string().hex().required(),
4398
+ divisionName: Joi10.string().min(1).max(100).optional().allow(null, ""),
4399
+ principal: Joi10.string().hex().optional().allow(null, ""),
4400
+ principalName: Joi10.string().min(1).max(100).optional().allow(null, ""),
4401
+ street: Joi10.string().max(200).optional().allow(null, ""),
4402
+ barangay: Joi10.string().max(200).optional().allow(null, ""),
4403
+ cityMunicipality: Joi10.string().max(100).optional().allow(null, ""),
4404
+ province: Joi10.string().max(100).optional().allow(null, ""),
4405
+ provincePSGC: Joi10.string().length(10).optional().allow(null, ""),
4406
+ cityMunicipalityPSGC: Joi10.string().length(10).optional().allow(null, ""),
4407
+ postalCode: Joi10.string().max(20).optional().allow(null, ""),
4408
+ contactNumber: Joi10.string().max(20).optional().allow(null, ""),
4409
+ email: Joi10.string().email().max(100).optional().allow(null, ""),
4410
+ status: Joi10.string().optional().allow(null, ""),
4411
+ createdBy: Joi10.string().optional().allow(null, ""),
4412
+ createdAt: Joi10.string().isoDate().optional().allow(null, ""),
4413
+ updatedAt: Joi10.string().isoDate().optional().allow(null, ""),
4414
+ deletedAt: Joi10.string().isoDate().optional().allow(null, "")
4619
4415
  });
4620
- var schemaSchoolUpdate = Joi11.object({
4621
- id: Joi11.string().min(1).max(50).required(),
4622
- name: Joi11.string().min(1).max(100).required(),
4623
- region: Joi11.string().hex().required(),
4624
- regionName: Joi11.string().min(1).max(100).optional().allow(null, ""),
4625
- division: Joi11.string().hex().required(),
4626
- divisionName: Joi11.string().min(1).max(100).optional().allow(null, ""),
4627
- principal: Joi11.string().hex().optional().allow(null, ""),
4628
- principalName: Joi11.string().min(1).max(100).optional().allow(null, ""),
4629
- street: Joi11.string().max(200).optional().allow(null, ""),
4630
- barangay: Joi11.string().max(200).optional().allow(null, ""),
4631
- cityMunicipality: Joi11.string().max(100).optional().allow(null, ""),
4632
- province: Joi11.string().max(100).optional().allow(null, ""),
4633
- provincePSGC: Joi11.number().optional().allow(null, ""),
4634
- cityMunicipalityPSGC: Joi11.number().optional().allow(null, ""),
4635
- postalCode: Joi11.string().max(20).optional().allow(null, ""),
4636
- contactNumber: Joi11.string().max(20).optional().allow(null, ""),
4637
- email: Joi11.string().email().max(100).optional().allow(null, "")
4416
+ var schemaSchoolUpdate = Joi10.object({
4417
+ id: Joi10.string().min(1).max(50).required(),
4418
+ name: Joi10.string().min(1).max(100).required(),
4419
+ region: Joi10.string().hex().required(),
4420
+ regionName: Joi10.string().min(1).max(100).optional().allow(null, ""),
4421
+ division: Joi10.string().hex().required(),
4422
+ divisionName: Joi10.string().min(1).max(100).optional().allow(null, ""),
4423
+ principal: Joi10.string().hex().optional().allow(null, ""),
4424
+ principalName: Joi10.string().min(1).max(100).optional().allow(null, ""),
4425
+ street: Joi10.string().max(200).optional().allow(null, ""),
4426
+ barangay: Joi10.string().max(200).optional().allow(null, ""),
4427
+ cityMunicipality: Joi10.string().max(100).optional().allow(null, ""),
4428
+ province: Joi10.string().max(100).optional().allow(null, ""),
4429
+ provincePSGC: Joi10.string().length(10).optional().allow(null, ""),
4430
+ cityMunicipalityPSGC: Joi10.string().length(10).optional().allow(null, ""),
4431
+ postalCode: Joi10.string().max(20).optional().allow(null, ""),
4432
+ contactNumber: Joi10.string().max(20).optional().allow(null, ""),
4433
+ email: Joi10.string().email().max(100).optional().allow(null, "")
4638
4434
  });
4639
4435
  function modelSchool(value) {
4640
4436
  const { error } = schemaSchool.validate(value);
4641
4437
  if (error) {
4642
- throw new BadRequestError17(`Invalid sdo data: ${error.message}`);
4438
+ throw new BadRequestError16(`Invalid sdo data: ${error.message}`);
4643
4439
  }
4644
4440
  if (value._id && typeof value._id === "string") {
4645
4441
  try {
@@ -4687,8 +4483,8 @@ function modelSchool(value) {
4687
4483
  contactNumber: value.contactNumber ?? "",
4688
4484
  email: value.email ?? "",
4689
4485
  status: value.status ?? "active",
4690
- provincePSGC: value.provincePSGC ?? 0,
4691
- cityMunicipalityPSGC: value.cityMunicipalityPSGC ?? 0,
4486
+ provincePSGC: value.provincePSGC ?? "",
4487
+ cityMunicipalityPSGC: value.cityMunicipalityPSGC ?? "",
4692
4488
  createdBy: value.createdBy ?? "",
4693
4489
  createdAt: value.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
4694
4490
  updatedAt: value.updatedAt ?? "",
@@ -4699,17 +4495,17 @@ function modelSchool(value) {
4699
4495
  // src/resources/school/school.repository.ts
4700
4496
  import {
4701
4497
  AppError as AppError6,
4702
- BadRequestError as BadRequestError18,
4498
+ BadRequestError as BadRequestError17,
4703
4499
  InternalServerError as InternalServerError7,
4704
4500
  logger as logger13,
4705
4501
  makeCacheKey as makeCacheKey6,
4706
4502
  paginate as paginate6,
4707
- useAtlas as useAtlas8,
4503
+ useAtlas as useAtlas7,
4708
4504
  useCache as useCache6
4709
4505
  } from "@eeplatform/nodejs-utils";
4710
4506
  import { ObjectId as ObjectId12 } from "mongodb";
4711
4507
  function useSchoolRepo() {
4712
- const db = useAtlas8.getDb();
4508
+ const db = useAtlas7.getDb();
4713
4509
  if (!db) {
4714
4510
  throw new Error("Unable to connect to server.");
4715
4511
  }
@@ -4767,7 +4563,7 @@ function useSchoolRepo() {
4767
4563
  } else {
4768
4564
  const isDuplicated = error.message.includes("duplicate");
4769
4565
  if (isDuplicated) {
4770
- throw new BadRequestError18("Duplicate, school already exists.");
4566
+ throw new BadRequestError17("Duplicate, school already exists.");
4771
4567
  }
4772
4568
  throw new InternalServerError7("Failed to add school.");
4773
4569
  }
@@ -4839,7 +4635,7 @@ function useSchoolRepo() {
4839
4635
  try {
4840
4636
  _id = new ObjectId12(_id);
4841
4637
  } catch (error) {
4842
- throw new BadRequestError18("Invalid ID.");
4638
+ throw new BadRequestError17("Invalid ID.");
4843
4639
  }
4844
4640
  const query = { _id };
4845
4641
  const cacheKeyOptions = {
@@ -4862,7 +4658,7 @@ function useSchoolRepo() {
4862
4658
  }
4863
4659
  const result = await collection.findOne(query);
4864
4660
  if (!result) {
4865
- throw new BadRequestError18("School not found.");
4661
+ throw new BadRequestError17("School not found.");
4866
4662
  }
4867
4663
  setCache(cacheKey, result, 300).then(() => {
4868
4664
  logger13.log({
@@ -4888,7 +4684,7 @@ function useSchoolRepo() {
4888
4684
  try {
4889
4685
  createdBy = new ObjectId12(createdBy);
4890
4686
  } catch (error) {
4891
- throw new BadRequestError18("Invalid ID.");
4687
+ throw new BadRequestError17("Invalid ID.");
4892
4688
  }
4893
4689
  const cacheKey = makeCacheKey6(namespace_collection, { createdBy });
4894
4690
  try {
@@ -4905,7 +4701,7 @@ function useSchoolRepo() {
4905
4701
  status: "pending"
4906
4702
  });
4907
4703
  if (!result) {
4908
- throw new BadRequestError18("School not found.");
4704
+ throw new BadRequestError17("School not found.");
4909
4705
  }
4910
4706
  setCache(cacheKey, result, 300).then(() => {
4911
4707
  logger13.log({
@@ -4943,7 +4739,7 @@ function useSchoolRepo() {
4943
4739
  deletedAt: { $in: ["", null] }
4944
4740
  });
4945
4741
  if (!result) {
4946
- throw new BadRequestError18("School not found.");
4742
+ throw new BadRequestError17("School not found.");
4947
4743
  }
4948
4744
  setCache(cacheKey, result, 300).then(() => {
4949
4745
  logger13.log({
@@ -4969,7 +4765,7 @@ function useSchoolRepo() {
4969
4765
  try {
4970
4766
  _id = new ObjectId12(_id);
4971
4767
  } catch (error) {
4972
- throw new BadRequestError18("Invalid ID.");
4768
+ throw new BadRequestError17("Invalid ID.");
4973
4769
  }
4974
4770
  try {
4975
4771
  await collection.updateOne(
@@ -4988,16 +4784,22 @@ function useSchoolRepo() {
4988
4784
  }
4989
4785
  }
4990
4786
  async function updateFieldById({ _id, field, value } = {}, session) {
4991
- const allowedFields = ["name"];
4787
+ const allowedFields = [
4788
+ "name",
4789
+ "division",
4790
+ "divisionName",
4791
+ "region",
4792
+ "regionName"
4793
+ ];
4992
4794
  if (!allowedFields.includes(field)) {
4993
- throw new BadRequestError18(
4795
+ throw new BadRequestError17(
4994
4796
  `Field "${field}" is not allowed to be updated.`
4995
4797
  );
4996
4798
  }
4997
4799
  try {
4998
4800
  _id = new ObjectId12(_id);
4999
4801
  } catch (error) {
5000
- throw new BadRequestError18("Invalid ID.");
4802
+ throw new BadRequestError17("Invalid ID.");
5001
4803
  }
5002
4804
  try {
5003
4805
  await collection.updateOne(
@@ -5011,15 +4813,39 @@ function useSchoolRepo() {
5011
4813
  throw new InternalServerError7(`Failed to update school ${field}.`);
5012
4814
  }
5013
4815
  }
4816
+ async function updateDivisionNameByDivision(division, name, session) {
4817
+ try {
4818
+ division = new ObjectId12(division);
4819
+ } catch (error) {
4820
+ throw new BadRequestError17("Invalid division.");
4821
+ }
4822
+ try {
4823
+ const result = await collection.updateMany(
4824
+ { division },
4825
+ { $set: { divisionName: name, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
4826
+ { session }
4827
+ );
4828
+ if (result.modifiedCount === 0) {
4829
+ logger13.log({
4830
+ level: "info",
4831
+ message: `No schools found for division ${division} to update divisionName.`
4832
+ });
4833
+ }
4834
+ delCachedData();
4835
+ return `Successfully updated school divisionName.`;
4836
+ } catch (error) {
4837
+ throw new InternalServerError7(`Failed to update school divisionName.`);
4838
+ }
4839
+ }
5014
4840
  async function updateById(_id, options, session) {
5015
4841
  const { error } = schemaSchoolUpdate.validate(options);
5016
4842
  if (error) {
5017
- throw new BadRequestError18(`Invalid school data: ${error.message}`);
4843
+ throw new BadRequestError17(`Invalid school data: ${error.message}`);
5018
4844
  }
5019
4845
  try {
5020
4846
  _id = new ObjectId12(_id);
5021
4847
  } catch (error2) {
5022
- throw new BadRequestError18("Invalid ID.");
4848
+ throw new BadRequestError17("Invalid ID.");
5023
4849
  }
5024
4850
  try {
5025
4851
  await collection.updateOne({ _id }, { $set: options }, { session });
@@ -5033,7 +4859,7 @@ function useSchoolRepo() {
5033
4859
  try {
5034
4860
  _id = new ObjectId12(_id);
5035
4861
  } catch (error) {
5036
- throw new BadRequestError18("Invalid ID.");
4862
+ throw new BadRequestError17("Invalid ID.");
5037
4863
  }
5038
4864
  try {
5039
4865
  await collection.updateOne(
@@ -5057,14 +4883,15 @@ function useSchoolRepo() {
5057
4883
  updateById,
5058
4884
  deleteById,
5059
4885
  getByName,
5060
- delCachedData
4886
+ delCachedData,
4887
+ updateDivisionNameByDivision
5061
4888
  };
5062
4889
  }
5063
4890
 
5064
4891
  // src/resources/school/school.service.ts
5065
- import { BadRequestError as BadRequestError19, useAtlas as useAtlas9, logger as logger14 } from "@eeplatform/nodejs-utils";
4892
+ import { BadRequestError as BadRequestError18, useAtlas as useAtlas8, logger as logger14 } from "@eeplatform/nodejs-utils";
5066
4893
  import {
5067
- useRoleRepo as useRoleRepo2,
4894
+ useRoleRepo,
5068
4895
  useUserRepo,
5069
4896
  useMemberRepo,
5070
4897
  usePSGCRepo
@@ -33236,17 +33063,17 @@ function useSchoolService() {
33236
33063
  getById,
33237
33064
  delCachedData: delCachedSchoolData
33238
33065
  } = useSchoolRepo();
33239
- const { addRole, delCachedData: delCachedRoleData } = useRoleRepo2();
33066
+ const { addRole, delCachedData: delCachedRoleData } = useRoleRepo();
33240
33067
  const { getUserById } = useUserRepo();
33241
33068
  const { add: addMember } = useMemberRepo();
33242
33069
  async function register(value) {
33243
33070
  const { error } = schemaSchool.validate(value);
33244
33071
  if (error) {
33245
- throw new BadRequestError19(error.message);
33072
+ throw new BadRequestError18(error.message);
33246
33073
  }
33247
33074
  const existingSchool = await getPendingByCreatedBy(value.createdBy ?? "");
33248
33075
  if (existingSchool) {
33249
- throw new BadRequestError19(
33076
+ throw new BadRequestError18(
33250
33077
  "You already have a pending school registration."
33251
33078
  );
33252
33079
  }
@@ -33261,9 +33088,9 @@ function useSchoolService() {
33261
33088
  async function approve(id) {
33262
33089
  const school = await getById(id, "pending");
33263
33090
  if (!school) {
33264
- throw new BadRequestError19("School registration not found.");
33091
+ throw new BadRequestError18("School registration not found.");
33265
33092
  }
33266
- const session = useAtlas9.getClient()?.startSession();
33093
+ const session = useAtlas8.getClient()?.startSession();
33267
33094
  if (!session) {
33268
33095
  throw new Error("Unable to start session for school service.");
33269
33096
  }
@@ -33285,11 +33112,11 @@ function useSchoolService() {
33285
33112
  session
33286
33113
  );
33287
33114
  if (!school.createdBy) {
33288
- throw new BadRequestError19("School must have a creator.");
33115
+ throw new BadRequestError18("School must have a creator.");
33289
33116
  }
33290
33117
  const user = await getUserById(school.createdBy ?? "");
33291
33118
  if (!user) {
33292
- throw new BadRequestError19("User not found for the school creator.");
33119
+ throw new BadRequestError18("User not found for the school creator.");
33293
33120
  }
33294
33121
  await addMember(
33295
33122
  {
@@ -33319,9 +33146,9 @@ function useSchoolService() {
33319
33146
  async function add(value) {
33320
33147
  const { error } = schemaSchool.validate(value);
33321
33148
  if (error) {
33322
- throw new BadRequestError19(error.message);
33149
+ throw new BadRequestError18(error.message);
33323
33150
  }
33324
- const session = useAtlas9.getClient()?.startSession();
33151
+ const session = useAtlas8.getClient()?.startSession();
33325
33152
  if (!session) {
33326
33153
  throw new Error("Unable to start session for school service.");
33327
33154
  }
@@ -33343,11 +33170,11 @@ function useSchoolService() {
33343
33170
  session
33344
33171
  );
33345
33172
  if (!value.createdBy) {
33346
- throw new BadRequestError19("School must have a creator.");
33173
+ throw new BadRequestError18("School must have a creator.");
33347
33174
  }
33348
33175
  const user = await getUserById(value.createdBy ?? "");
33349
33176
  if (!user) {
33350
- throw new BadRequestError19("User not found for the school creator.");
33177
+ throw new BadRequestError18("User not found for the school creator.");
33351
33178
  }
33352
33179
  await addMember(
33353
33180
  {
@@ -33378,7 +33205,7 @@ function useSchoolService() {
33378
33205
  const isCSV = file.mimetype.includes("csv") || file.originalname.endsWith(".csv");
33379
33206
  const isExcel = file.mimetype.includes("sheet") || file.originalname.endsWith(".xlsx") || file.originalname.endsWith(".xls");
33380
33207
  if (!isCSV && !isExcel) {
33381
- throw new BadRequestError19("Only CSV and Excel files are supported");
33208
+ throw new BadRequestError18("Only CSV and Excel files are supported");
33382
33209
  }
33383
33210
  let rawData = [];
33384
33211
  try {
@@ -33395,17 +33222,17 @@ function useSchoolService() {
33395
33222
  transformHeader: (header) => header.trim()
33396
33223
  });
33397
33224
  if (parseResult.errors.length > 0) {
33398
- throw new BadRequestError19(
33225
+ throw new BadRequestError18(
33399
33226
  `CSV parsing error: ${parseResult.errors[0].message}`
33400
33227
  );
33401
33228
  }
33402
33229
  rawData = parseResult.data;
33403
33230
  }
33404
33231
  } catch (error) {
33405
- throw new BadRequestError19(`File parsing error: ${error.message}`);
33232
+ throw new BadRequestError18(`File parsing error: ${error.message}`);
33406
33233
  }
33407
33234
  if (!rawData.length) {
33408
- throw new BadRequestError19("No data found in file");
33235
+ throw new BadRequestError18("No data found in file");
33409
33236
  }
33410
33237
  const schools = [];
33411
33238
  const errors = [];
@@ -33451,7 +33278,7 @@ function useSchoolService() {
33451
33278
  schools.push(school);
33452
33279
  }
33453
33280
  if (errors.length > 0) {
33454
- throw new BadRequestError19(
33281
+ throw new BadRequestError18(
33455
33282
  `Validation errors:
33456
33283
  ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33457
33284
  ...and ${errors.length - 5} more` : ""}`
@@ -33465,7 +33292,7 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33465
33292
  };
33466
33293
  const { getByName: getPSGCByName } = usePSGCRepo();
33467
33294
  for (let i = 0; i < schools.length; i++) {
33468
- const session = useAtlas9.getClient()?.startSession();
33295
+ const session = useAtlas8.getClient()?.startSession();
33469
33296
  if (!session) {
33470
33297
  throw new Error("Unable to start MongoDB session");
33471
33298
  }
@@ -33477,22 +33304,22 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33477
33304
  type: "Prov"
33478
33305
  });
33479
33306
  if (!provincePSGC) {
33480
- throw new BadRequestError19(
33307
+ throw new BadRequestError18(
33481
33308
  `Province '${school.province}' not found in PSGC data.`
33482
33309
  );
33483
33310
  }
33484
33311
  const cityMunPSGC = await getPSGCByName({
33485
33312
  name: school.cityMunicipality ?? "",
33486
- cityMunicipality: true,
33487
- code: provincePSGC.code
33313
+ type: "City",
33314
+ prefix: provincePSGC.code.toString().slice(0, 5)
33488
33315
  });
33489
33316
  if (!cityMunPSGC) {
33490
- throw new BadRequestError19(
33317
+ throw new BadRequestError18(
33491
33318
  `City/Municipality '${school.cityMunicipality}' not found in PSGC data.`
33492
33319
  );
33493
33320
  }
33494
- school.provincePSGC = provincePSGC.code ?? 0;
33495
- school.cityMunicipalityPSGC = cityMunPSGC.code ?? 0;
33321
+ school.provincePSGC = provincePSGC.code ?? "";
33322
+ school.cityMunicipalityPSGC = cityMunPSGC.code ?? "";
33496
33323
  const schoolId = await addSchool(school, session, false);
33497
33324
  await addRole(
33498
33325
  {
@@ -33539,8 +33366,8 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
33539
33366
  }
33540
33367
 
33541
33368
  // src/resources/school/school.controller.ts
33542
- import { BadRequestError as BadRequestError20 } from "@eeplatform/nodejs-utils";
33543
- import Joi12 from "joi";
33369
+ import { BadRequestError as BadRequestError19 } from "@eeplatform/nodejs-utils";
33370
+ import Joi11 from "joi";
33544
33371
  function useSchoolController() {
33545
33372
  const {
33546
33373
  getAll: _getAll,
@@ -33560,7 +33387,7 @@ function useSchoolController() {
33560
33387
  const payload = req.body;
33561
33388
  const { error } = schemaSchool.validate(payload);
33562
33389
  if (error) {
33563
- next(new BadRequestError20(`Validation error: ${error.message}`));
33390
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33564
33391
  return;
33565
33392
  }
33566
33393
  try {
@@ -33573,19 +33400,19 @@ function useSchoolController() {
33573
33400
  }
33574
33401
  }
33575
33402
  async function getAll(req, res, next) {
33576
- const validation = Joi12.object({
33577
- page: Joi12.number().optional().allow(null, ""),
33578
- limit: Joi12.number().optional().allow(null, ""),
33579
- sort: Joi12.string().optional().allow(null, ""),
33580
- sortOrder: Joi12.string().optional().allow(null, ""),
33581
- status: Joi12.string().optional().allow(null, ""),
33582
- org: Joi12.string().hex().optional().allow(null, ""),
33583
- app: Joi12.string().optional().allow(null, ""),
33584
- search: Joi12.string().optional().allow(null, "")
33403
+ const validation = Joi11.object({
33404
+ page: Joi11.number().optional().allow(null, ""),
33405
+ limit: Joi11.number().optional().allow(null, ""),
33406
+ sort: Joi11.string().optional().allow(null, ""),
33407
+ sortOrder: Joi11.string().optional().allow(null, ""),
33408
+ status: Joi11.string().optional().allow(null, ""),
33409
+ org: Joi11.string().hex().optional().allow(null, ""),
33410
+ app: Joi11.string().optional().allow(null, ""),
33411
+ search: Joi11.string().optional().allow(null, "")
33585
33412
  });
33586
33413
  const { error } = validation.validate(req.query);
33587
33414
  if (error) {
33588
- next(new BadRequestError20(`Validation error: ${error.message}`));
33415
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33589
33416
  return;
33590
33417
  }
33591
33418
  const page = parseInt(req.query.page) ?? 1;
@@ -33617,10 +33444,10 @@ function useSchoolController() {
33617
33444
  }
33618
33445
  async function getByCreatedBy(req, res, next) {
33619
33446
  const createdBy = req.params.createdBy;
33620
- const validation = Joi12.string().hex().required();
33447
+ const validation = Joi11.string().hex().required();
33621
33448
  const { error } = validation.validate(createdBy);
33622
33449
  if (error) {
33623
- next(new BadRequestError20(`Validation error: ${error.message}`));
33450
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33624
33451
  return;
33625
33452
  }
33626
33453
  try {
@@ -33634,13 +33461,13 @@ function useSchoolController() {
33634
33461
  async function updateStatusById(req, res, next) {
33635
33462
  const schoolId = req.params.id;
33636
33463
  const status = req.params.status;
33637
- const validation = Joi12.object({
33638
- id: Joi12.string().hex().required(),
33639
- status: Joi12.string().valid("active", "deleted", "suspended").required()
33464
+ const validation = Joi11.object({
33465
+ id: Joi11.string().hex().required(),
33466
+ status: Joi11.string().valid("active", "deleted", "suspended").required()
33640
33467
  });
33641
33468
  const { error } = validation.validate({ id: schoolId, status });
33642
33469
  if (error) {
33643
- next(new BadRequestError20(`Validation error: ${error.message}`));
33470
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33644
33471
  return;
33645
33472
  }
33646
33473
  try {
@@ -33655,7 +33482,7 @@ function useSchoolController() {
33655
33482
  const payload = req.body;
33656
33483
  const { error } = schemaSchool.validate(payload);
33657
33484
  if (error) {
33658
- next(new BadRequestError20(`Validation error: ${error.message}`));
33485
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33659
33486
  return;
33660
33487
  }
33661
33488
  try {
@@ -33669,12 +33496,12 @@ function useSchoolController() {
33669
33496
  }
33670
33497
  async function approveSchool(req, res, next) {
33671
33498
  const schoolId = req.params.id;
33672
- const validation = Joi12.object({
33673
- id: Joi12.string().hex().required()
33499
+ const validation = Joi11.object({
33500
+ id: Joi11.string().hex().required()
33674
33501
  });
33675
33502
  const { error } = validation.validate({ id: schoolId });
33676
33503
  if (error) {
33677
- next(new BadRequestError20(`Validation error: ${error.message}`));
33504
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33678
33505
  return;
33679
33506
  }
33680
33507
  try {
@@ -33693,11 +33520,11 @@ function useSchoolController() {
33693
33520
  return;
33694
33521
  }
33695
33522
  const { region, regionName, division, divisionName } = req.body;
33696
- const validation = Joi12.object({
33697
- region: Joi12.string().hex().required(),
33698
- regionName: Joi12.string().min(1).required(),
33699
- division: Joi12.string().hex().required(),
33700
- divisionName: Joi12.string().min(1).required()
33523
+ const validation = Joi11.object({
33524
+ region: Joi11.string().hex().required(),
33525
+ regionName: Joi11.string().min(1).required(),
33526
+ division: Joi11.string().hex().required(),
33527
+ divisionName: Joi11.string().min(1).required()
33701
33528
  });
33702
33529
  const { error } = validation.validate({
33703
33530
  region,
@@ -33706,7 +33533,7 @@ function useSchoolController() {
33706
33533
  divisionName
33707
33534
  });
33708
33535
  if (error) {
33709
- next(new BadRequestError20(`Validation error: ${error.message}`));
33536
+ next(new BadRequestError19(`Validation error: ${error.message}`));
33710
33537
  return;
33711
33538
  }
33712
33539
  try {
@@ -33726,14 +33553,14 @@ function useSchoolController() {
33726
33553
  async function updateFieldById(req, res, next) {
33727
33554
  const _id = req.params.id;
33728
33555
  const { field, value } = req.body;
33729
- const validation = Joi12.object({
33730
- _id: Joi12.string().hex().required(),
33731
- field: Joi12.string().valid("name", "director", "directorName").required(),
33732
- value: Joi12.string().required()
33556
+ const validation = Joi11.object({
33557
+ _id: Joi11.string().hex().required(),
33558
+ field: Joi11.string().valid("name", "director", "directorName").required(),
33559
+ value: Joi11.string().required()
33733
33560
  });
33734
33561
  const { error } = validation.validate({ _id, field, value });
33735
33562
  if (error) {
33736
- next(new BadRequestError20(error.message));
33563
+ next(new BadRequestError19(error.message));
33737
33564
  return;
33738
33565
  }
33739
33566
  try {
@@ -33747,17 +33574,17 @@ function useSchoolController() {
33747
33574
  async function updateById(req, res, next) {
33748
33575
  const id = req.params.id;
33749
33576
  const payload = req.body;
33750
- const validation = Joi12.object({
33751
- id: Joi12.string().hex().required()
33577
+ const validation = Joi11.object({
33578
+ id: Joi11.string().hex().required()
33752
33579
  });
33753
33580
  const { error: idError } = validation.validate({ id });
33754
33581
  if (idError) {
33755
- next(new BadRequestError20(idError.message));
33582
+ next(new BadRequestError19(idError.message));
33756
33583
  return;
33757
33584
  }
33758
33585
  const { error } = schemaSchoolUpdate.validate(payload);
33759
33586
  if (error) {
33760
- next(new BadRequestError20(error.message));
33587
+ next(new BadRequestError19(error.message));
33761
33588
  return;
33762
33589
  }
33763
33590
  try {
@@ -33770,12 +33597,12 @@ function useSchoolController() {
33770
33597
  }
33771
33598
  async function deleteById(req, res, next) {
33772
33599
  const _id = req.params.id;
33773
- const validation = Joi12.object({
33774
- _id: Joi12.string().hex().required()
33600
+ const validation = Joi11.object({
33601
+ _id: Joi11.string().hex().required()
33775
33602
  });
33776
33603
  const { error } = validation.validate({ _id });
33777
33604
  if (error) {
33778
- next(new BadRequestError20(error.message));
33605
+ next(new BadRequestError19(error.message));
33779
33606
  return;
33780
33607
  }
33781
33608
  try {
@@ -33800,6 +33627,245 @@ function useSchoolController() {
33800
33627
  };
33801
33628
  }
33802
33629
 
33630
+ // src/resources/division/division.service.ts
33631
+ function useDivisionService() {
33632
+ const {
33633
+ add: _add,
33634
+ updateById: _updateById,
33635
+ getById: _getById
33636
+ } = useDivisionRepo();
33637
+ const { addRole } = useRoleRepo2();
33638
+ const { updateDivisionNameByDivision } = useSchoolRepo();
33639
+ async function add(value) {
33640
+ const session = useAtlas9.getClient()?.startSession();
33641
+ if (!session) {
33642
+ throw new Error("Unable to start session for division service.");
33643
+ }
33644
+ try {
33645
+ session.startTransaction();
33646
+ const division = await _add(value, session);
33647
+ await addRole(
33648
+ {
33649
+ id: division.toString(),
33650
+ name: "Admin",
33651
+ type: "basic-edu-sdo",
33652
+ permissions: ["*"],
33653
+ status: "active",
33654
+ default: true
33655
+ },
33656
+ session
33657
+ );
33658
+ await session.commitTransaction();
33659
+ return "Division and admin role created successfully.";
33660
+ } catch (error) {
33661
+ session.abortTransaction();
33662
+ throw error;
33663
+ } finally {
33664
+ session.endSession();
33665
+ }
33666
+ }
33667
+ async function updateById(id, value) {
33668
+ const session = useAtlas9.getClient()?.startSession();
33669
+ if (!session) {
33670
+ throw new Error("Unable to start session for division service.");
33671
+ }
33672
+ try {
33673
+ session.startTransaction();
33674
+ const division = await _getById(id);
33675
+ await _updateById(id, value, session);
33676
+ if (division && division._id && division.name !== value.name) {
33677
+ await updateDivisionNameByDivision(division._id, value.name, session);
33678
+ }
33679
+ await session.commitTransaction();
33680
+ return "Division and admin role created successfully.";
33681
+ } catch (error) {
33682
+ session.abortTransaction();
33683
+ throw error;
33684
+ } finally {
33685
+ session.endSession();
33686
+ }
33687
+ }
33688
+ return {
33689
+ add,
33690
+ updateById
33691
+ };
33692
+ }
33693
+
33694
+ // src/resources/division/division.controller.ts
33695
+ import { BadRequestError as BadRequestError20 } from "@eeplatform/nodejs-utils";
33696
+ import Joi12 from "joi";
33697
+ function useDivisionController() {
33698
+ const { add: _add, updateById: _updateById } = useDivisionService();
33699
+ const {
33700
+ getAll: _getAll,
33701
+ getById: _getById,
33702
+ getByName: _getByName,
33703
+ updateFieldById: _updateFieldById,
33704
+ deleteById: _deleteById
33705
+ } = useDivisionRepo();
33706
+ async function add(req, res, next) {
33707
+ const value = req.body;
33708
+ const { error } = schemaDivision.validate(value);
33709
+ if (error) {
33710
+ next(new BadRequestError20(error.message));
33711
+ return;
33712
+ }
33713
+ try {
33714
+ const data = await _add(value);
33715
+ res.json({
33716
+ message: "Successfully created division.",
33717
+ data
33718
+ });
33719
+ return;
33720
+ } catch (error2) {
33721
+ next(error2);
33722
+ }
33723
+ }
33724
+ async function getAll(req, res, next) {
33725
+ const query = req.query;
33726
+ const validation = Joi12.object({
33727
+ page: Joi12.number().min(1).optional().allow("", null),
33728
+ limit: Joi12.number().min(1).optional().allow("", null),
33729
+ search: Joi12.string().optional().allow("", null),
33730
+ status: Joi12.string().optional().allow("", null),
33731
+ region: Joi12.string().hex().optional().allow("", null)
33732
+ });
33733
+ const { error } = validation.validate(query);
33734
+ const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
33735
+ const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
33736
+ const search = req.query.search ?? "";
33737
+ const status = req.query.status ?? "active";
33738
+ const region = req.query.region ?? "";
33739
+ const isPageNumber = isFinite(page);
33740
+ if (!isPageNumber) {
33741
+ next(new BadRequestError20("Invalid page number."));
33742
+ return;
33743
+ }
33744
+ const isLimitNumber = isFinite(limit);
33745
+ if (!isLimitNumber) {
33746
+ next(new BadRequestError20("Invalid limit number."));
33747
+ return;
33748
+ }
33749
+ if (error) {
33750
+ next(new BadRequestError20(error.message));
33751
+ return;
33752
+ }
33753
+ try {
33754
+ const data = await _getAll({ page, limit, search, status, region });
33755
+ res.json(data);
33756
+ return;
33757
+ } catch (error2) {
33758
+ next(error2);
33759
+ }
33760
+ }
33761
+ async function getById(req, res, next) {
33762
+ const id = req.params.id;
33763
+ const validation = Joi12.object({
33764
+ id: Joi12.string().hex().required()
33765
+ });
33766
+ const { error } = validation.validate({ id });
33767
+ if (error) {
33768
+ next(new BadRequestError20(error.message));
33769
+ return;
33770
+ }
33771
+ try {
33772
+ const data = await _getById(id);
33773
+ res.json({
33774
+ message: "Successfully retrieved division.",
33775
+ data
33776
+ });
33777
+ return;
33778
+ } catch (error2) {
33779
+ next(error2);
33780
+ }
33781
+ }
33782
+ async function getByName(req, res, next) {
33783
+ const name = req.params.name;
33784
+ const validation = Joi12.object({
33785
+ name: Joi12.string().required()
33786
+ });
33787
+ const { error } = validation.validate({ name });
33788
+ if (error) {
33789
+ next(new BadRequestError20(error.message));
33790
+ return;
33791
+ }
33792
+ try {
33793
+ const data = await _getByName(name);
33794
+ res.json({
33795
+ message: "Successfully retrieved division.",
33796
+ data
33797
+ });
33798
+ return;
33799
+ } catch (error2) {
33800
+ next(error2);
33801
+ }
33802
+ }
33803
+ async function updateField(req, res, next) {
33804
+ const _id = req.params.id;
33805
+ const { field, value } = req.body;
33806
+ const validation = Joi12.object({
33807
+ _id: Joi12.string().hex().required(),
33808
+ field: Joi12.string().valid("name", "director", "directorName").required(),
33809
+ value: Joi12.string().required()
33810
+ });
33811
+ const { error } = validation.validate({ _id, field, value });
33812
+ if (error) {
33813
+ next(new BadRequestError20(error.message));
33814
+ return;
33815
+ }
33816
+ try {
33817
+ const message = await _updateFieldById({ _id, field, value });
33818
+ res.json({ message });
33819
+ return;
33820
+ } catch (error2) {
33821
+ next(error2);
33822
+ }
33823
+ }
33824
+ async function updateById(req, res, next) {
33825
+ const _id = req.params.id;
33826
+ const payload = req.body;
33827
+ const { error } = schemaDivisionUpdate.validate({ _id, ...payload });
33828
+ if (error) {
33829
+ next(new BadRequestError20(error.message));
33830
+ return;
33831
+ }
33832
+ try {
33833
+ const message = await _updateById(_id, payload);
33834
+ res.json({ message });
33835
+ return;
33836
+ } catch (error2) {
33837
+ next(error2);
33838
+ }
33839
+ }
33840
+ async function deleteById(req, res, next) {
33841
+ const _id = req.params.id;
33842
+ const validation = Joi12.object({
33843
+ _id: Joi12.string().hex().required()
33844
+ });
33845
+ const { error } = validation.validate({ _id });
33846
+ if (error) {
33847
+ next(new BadRequestError20(error.message));
33848
+ return;
33849
+ }
33850
+ try {
33851
+ const message = await _deleteById(_id);
33852
+ res.json({ message });
33853
+ return;
33854
+ } catch (error2) {
33855
+ next(error2);
33856
+ }
33857
+ }
33858
+ return {
33859
+ add,
33860
+ getAll,
33861
+ getById,
33862
+ getByName,
33863
+ updateField,
33864
+ updateById,
33865
+ deleteById
33866
+ };
33867
+ }
33868
+
33803
33869
  // src/resources/asset/asset.model.ts
33804
33870
  import { BadRequestError as BadRequestError21 } from "@eeplatform/nodejs-utils";
33805
33871
  import Joi13 from "joi";