@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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +436 -377
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +395 -336
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -4374,272 +4374,61 @@ function useDivisionRepo() {
|
|
|
4374
4374
|
}
|
|
4375
4375
|
|
|
4376
4376
|
// src/resources/division/division.service.ts
|
|
4377
|
-
import { useAtlas as
|
|
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/
|
|
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 =
|
|
4596
|
-
_id:
|
|
4597
|
-
id:
|
|
4598
|
-
name:
|
|
4599
|
-
region:
|
|
4600
|
-
regionName:
|
|
4601
|
-
division:
|
|
4602
|
-
divisionName:
|
|
4603
|
-
principal:
|
|
4604
|
-
principalName:
|
|
4605
|
-
street:
|
|
4606
|
-
barangay:
|
|
4607
|
-
cityMunicipality:
|
|
4608
|
-
province:
|
|
4609
|
-
provincePSGC:
|
|
4610
|
-
cityMunicipalityPSGC:
|
|
4611
|
-
postalCode:
|
|
4612
|
-
contactNumber:
|
|
4613
|
-
email:
|
|
4614
|
-
status:
|
|
4615
|
-
createdBy:
|
|
4616
|
-
createdAt:
|
|
4617
|
-
updatedAt:
|
|
4618
|
-
deletedAt:
|
|
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, "")
|
|
4619
4408
|
});
|
|
4620
|
-
var schemaSchoolUpdate =
|
|
4621
|
-
id:
|
|
4622
|
-
name:
|
|
4623
|
-
region:
|
|
4624
|
-
regionName:
|
|
4625
|
-
division:
|
|
4626
|
-
divisionName:
|
|
4627
|
-
principal:
|
|
4628
|
-
principalName:
|
|
4629
|
-
street:
|
|
4630
|
-
barangay:
|
|
4631
|
-
cityMunicipality:
|
|
4632
|
-
province:
|
|
4633
|
-
provincePSGC:
|
|
4634
|
-
cityMunicipalityPSGC:
|
|
4635
|
-
postalCode:
|
|
4636
|
-
contactNumber:
|
|
4637
|
-
email:
|
|
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, "")
|
|
4638
4427
|
});
|
|
4639
4428
|
function modelSchool(value) {
|
|
4640
4429
|
const { error } = schemaSchool.validate(value);
|
|
4641
4430
|
if (error) {
|
|
4642
|
-
throw new
|
|
4431
|
+
throw new BadRequestError16(`Invalid sdo data: ${error.message}`);
|
|
4643
4432
|
}
|
|
4644
4433
|
if (value._id && typeof value._id === "string") {
|
|
4645
4434
|
try {
|
|
@@ -4699,17 +4488,17 @@ function modelSchool(value) {
|
|
|
4699
4488
|
// src/resources/school/school.repository.ts
|
|
4700
4489
|
import {
|
|
4701
4490
|
AppError as AppError6,
|
|
4702
|
-
BadRequestError as
|
|
4491
|
+
BadRequestError as BadRequestError17,
|
|
4703
4492
|
InternalServerError as InternalServerError7,
|
|
4704
4493
|
logger as logger13,
|
|
4705
4494
|
makeCacheKey as makeCacheKey6,
|
|
4706
4495
|
paginate as paginate6,
|
|
4707
|
-
useAtlas as
|
|
4496
|
+
useAtlas as useAtlas7,
|
|
4708
4497
|
useCache as useCache6
|
|
4709
4498
|
} from "@eeplatform/nodejs-utils";
|
|
4710
4499
|
import { ObjectId as ObjectId12 } from "mongodb";
|
|
4711
4500
|
function useSchoolRepo() {
|
|
4712
|
-
const db =
|
|
4501
|
+
const db = useAtlas7.getDb();
|
|
4713
4502
|
if (!db) {
|
|
4714
4503
|
throw new Error("Unable to connect to server.");
|
|
4715
4504
|
}
|
|
@@ -4767,7 +4556,7 @@ function useSchoolRepo() {
|
|
|
4767
4556
|
} else {
|
|
4768
4557
|
const isDuplicated = error.message.includes("duplicate");
|
|
4769
4558
|
if (isDuplicated) {
|
|
4770
|
-
throw new
|
|
4559
|
+
throw new BadRequestError17("Duplicate, school already exists.");
|
|
4771
4560
|
}
|
|
4772
4561
|
throw new InternalServerError7("Failed to add school.");
|
|
4773
4562
|
}
|
|
@@ -4839,7 +4628,7 @@ function useSchoolRepo() {
|
|
|
4839
4628
|
try {
|
|
4840
4629
|
_id = new ObjectId12(_id);
|
|
4841
4630
|
} catch (error) {
|
|
4842
|
-
throw new
|
|
4631
|
+
throw new BadRequestError17("Invalid ID.");
|
|
4843
4632
|
}
|
|
4844
4633
|
const query = { _id };
|
|
4845
4634
|
const cacheKeyOptions = {
|
|
@@ -4862,7 +4651,7 @@ function useSchoolRepo() {
|
|
|
4862
4651
|
}
|
|
4863
4652
|
const result = await collection.findOne(query);
|
|
4864
4653
|
if (!result) {
|
|
4865
|
-
throw new
|
|
4654
|
+
throw new BadRequestError17("School not found.");
|
|
4866
4655
|
}
|
|
4867
4656
|
setCache(cacheKey, result, 300).then(() => {
|
|
4868
4657
|
logger13.log({
|
|
@@ -4888,7 +4677,7 @@ function useSchoolRepo() {
|
|
|
4888
4677
|
try {
|
|
4889
4678
|
createdBy = new ObjectId12(createdBy);
|
|
4890
4679
|
} catch (error) {
|
|
4891
|
-
throw new
|
|
4680
|
+
throw new BadRequestError17("Invalid ID.");
|
|
4892
4681
|
}
|
|
4893
4682
|
const cacheKey = makeCacheKey6(namespace_collection, { createdBy });
|
|
4894
4683
|
try {
|
|
@@ -4905,7 +4694,7 @@ function useSchoolRepo() {
|
|
|
4905
4694
|
status: "pending"
|
|
4906
4695
|
});
|
|
4907
4696
|
if (!result) {
|
|
4908
|
-
throw new
|
|
4697
|
+
throw new BadRequestError17("School not found.");
|
|
4909
4698
|
}
|
|
4910
4699
|
setCache(cacheKey, result, 300).then(() => {
|
|
4911
4700
|
logger13.log({
|
|
@@ -4943,7 +4732,7 @@ function useSchoolRepo() {
|
|
|
4943
4732
|
deletedAt: { $in: ["", null] }
|
|
4944
4733
|
});
|
|
4945
4734
|
if (!result) {
|
|
4946
|
-
throw new
|
|
4735
|
+
throw new BadRequestError17("School not found.");
|
|
4947
4736
|
}
|
|
4948
4737
|
setCache(cacheKey, result, 300).then(() => {
|
|
4949
4738
|
logger13.log({
|
|
@@ -4969,7 +4758,7 @@ function useSchoolRepo() {
|
|
|
4969
4758
|
try {
|
|
4970
4759
|
_id = new ObjectId12(_id);
|
|
4971
4760
|
} catch (error) {
|
|
4972
|
-
throw new
|
|
4761
|
+
throw new BadRequestError17("Invalid ID.");
|
|
4973
4762
|
}
|
|
4974
4763
|
try {
|
|
4975
4764
|
await collection.updateOne(
|
|
@@ -4988,16 +4777,22 @@ function useSchoolRepo() {
|
|
|
4988
4777
|
}
|
|
4989
4778
|
}
|
|
4990
4779
|
async function updateFieldById({ _id, field, value } = {}, session) {
|
|
4991
|
-
const allowedFields = [
|
|
4780
|
+
const allowedFields = [
|
|
4781
|
+
"name",
|
|
4782
|
+
"division",
|
|
4783
|
+
"divisionName",
|
|
4784
|
+
"region",
|
|
4785
|
+
"regionName"
|
|
4786
|
+
];
|
|
4992
4787
|
if (!allowedFields.includes(field)) {
|
|
4993
|
-
throw new
|
|
4788
|
+
throw new BadRequestError17(
|
|
4994
4789
|
`Field "${field}" is not allowed to be updated.`
|
|
4995
4790
|
);
|
|
4996
4791
|
}
|
|
4997
4792
|
try {
|
|
4998
4793
|
_id = new ObjectId12(_id);
|
|
4999
4794
|
} catch (error) {
|
|
5000
|
-
throw new
|
|
4795
|
+
throw new BadRequestError17("Invalid ID.");
|
|
5001
4796
|
}
|
|
5002
4797
|
try {
|
|
5003
4798
|
await collection.updateOne(
|
|
@@ -5011,15 +4806,39 @@ function useSchoolRepo() {
|
|
|
5011
4806
|
throw new InternalServerError7(`Failed to update school ${field}.`);
|
|
5012
4807
|
}
|
|
5013
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
|
+
}
|
|
5014
4833
|
async function updateById(_id, options, session) {
|
|
5015
4834
|
const { error } = schemaSchoolUpdate.validate(options);
|
|
5016
4835
|
if (error) {
|
|
5017
|
-
throw new
|
|
4836
|
+
throw new BadRequestError17(`Invalid school data: ${error.message}`);
|
|
5018
4837
|
}
|
|
5019
4838
|
try {
|
|
5020
4839
|
_id = new ObjectId12(_id);
|
|
5021
4840
|
} catch (error2) {
|
|
5022
|
-
throw new
|
|
4841
|
+
throw new BadRequestError17("Invalid ID.");
|
|
5023
4842
|
}
|
|
5024
4843
|
try {
|
|
5025
4844
|
await collection.updateOne({ _id }, { $set: options }, { session });
|
|
@@ -5033,7 +4852,7 @@ function useSchoolRepo() {
|
|
|
5033
4852
|
try {
|
|
5034
4853
|
_id = new ObjectId12(_id);
|
|
5035
4854
|
} catch (error) {
|
|
5036
|
-
throw new
|
|
4855
|
+
throw new BadRequestError17("Invalid ID.");
|
|
5037
4856
|
}
|
|
5038
4857
|
try {
|
|
5039
4858
|
await collection.updateOne(
|
|
@@ -5057,14 +4876,15 @@ function useSchoolRepo() {
|
|
|
5057
4876
|
updateById,
|
|
5058
4877
|
deleteById,
|
|
5059
4878
|
getByName,
|
|
5060
|
-
delCachedData
|
|
4879
|
+
delCachedData,
|
|
4880
|
+
updateDivisionNameByDivision
|
|
5061
4881
|
};
|
|
5062
4882
|
}
|
|
5063
4883
|
|
|
5064
4884
|
// src/resources/school/school.service.ts
|
|
5065
|
-
import { BadRequestError as
|
|
4885
|
+
import { BadRequestError as BadRequestError18, useAtlas as useAtlas8, logger as logger14 } from "@eeplatform/nodejs-utils";
|
|
5066
4886
|
import {
|
|
5067
|
-
useRoleRepo
|
|
4887
|
+
useRoleRepo,
|
|
5068
4888
|
useUserRepo,
|
|
5069
4889
|
useMemberRepo,
|
|
5070
4890
|
usePSGCRepo
|
|
@@ -33236,17 +33056,17 @@ function useSchoolService() {
|
|
|
33236
33056
|
getById,
|
|
33237
33057
|
delCachedData: delCachedSchoolData
|
|
33238
33058
|
} = useSchoolRepo();
|
|
33239
|
-
const { addRole, delCachedData: delCachedRoleData } =
|
|
33059
|
+
const { addRole, delCachedData: delCachedRoleData } = useRoleRepo();
|
|
33240
33060
|
const { getUserById } = useUserRepo();
|
|
33241
33061
|
const { add: addMember } = useMemberRepo();
|
|
33242
33062
|
async function register(value) {
|
|
33243
33063
|
const { error } = schemaSchool.validate(value);
|
|
33244
33064
|
if (error) {
|
|
33245
|
-
throw new
|
|
33065
|
+
throw new BadRequestError18(error.message);
|
|
33246
33066
|
}
|
|
33247
33067
|
const existingSchool = await getPendingByCreatedBy(value.createdBy ?? "");
|
|
33248
33068
|
if (existingSchool) {
|
|
33249
|
-
throw new
|
|
33069
|
+
throw new BadRequestError18(
|
|
33250
33070
|
"You already have a pending school registration."
|
|
33251
33071
|
);
|
|
33252
33072
|
}
|
|
@@ -33261,9 +33081,9 @@ function useSchoolService() {
|
|
|
33261
33081
|
async function approve(id) {
|
|
33262
33082
|
const school = await getById(id, "pending");
|
|
33263
33083
|
if (!school) {
|
|
33264
|
-
throw new
|
|
33084
|
+
throw new BadRequestError18("School registration not found.");
|
|
33265
33085
|
}
|
|
33266
|
-
const session =
|
|
33086
|
+
const session = useAtlas8.getClient()?.startSession();
|
|
33267
33087
|
if (!session) {
|
|
33268
33088
|
throw new Error("Unable to start session for school service.");
|
|
33269
33089
|
}
|
|
@@ -33285,11 +33105,11 @@ function useSchoolService() {
|
|
|
33285
33105
|
session
|
|
33286
33106
|
);
|
|
33287
33107
|
if (!school.createdBy) {
|
|
33288
|
-
throw new
|
|
33108
|
+
throw new BadRequestError18("School must have a creator.");
|
|
33289
33109
|
}
|
|
33290
33110
|
const user = await getUserById(school.createdBy ?? "");
|
|
33291
33111
|
if (!user) {
|
|
33292
|
-
throw new
|
|
33112
|
+
throw new BadRequestError18("User not found for the school creator.");
|
|
33293
33113
|
}
|
|
33294
33114
|
await addMember(
|
|
33295
33115
|
{
|
|
@@ -33319,9 +33139,9 @@ function useSchoolService() {
|
|
|
33319
33139
|
async function add(value) {
|
|
33320
33140
|
const { error } = schemaSchool.validate(value);
|
|
33321
33141
|
if (error) {
|
|
33322
|
-
throw new
|
|
33142
|
+
throw new BadRequestError18(error.message);
|
|
33323
33143
|
}
|
|
33324
|
-
const session =
|
|
33144
|
+
const session = useAtlas8.getClient()?.startSession();
|
|
33325
33145
|
if (!session) {
|
|
33326
33146
|
throw new Error("Unable to start session for school service.");
|
|
33327
33147
|
}
|
|
@@ -33343,11 +33163,11 @@ function useSchoolService() {
|
|
|
33343
33163
|
session
|
|
33344
33164
|
);
|
|
33345
33165
|
if (!value.createdBy) {
|
|
33346
|
-
throw new
|
|
33166
|
+
throw new BadRequestError18("School must have a creator.");
|
|
33347
33167
|
}
|
|
33348
33168
|
const user = await getUserById(value.createdBy ?? "");
|
|
33349
33169
|
if (!user) {
|
|
33350
|
-
throw new
|
|
33170
|
+
throw new BadRequestError18("User not found for the school creator.");
|
|
33351
33171
|
}
|
|
33352
33172
|
await addMember(
|
|
33353
33173
|
{
|
|
@@ -33378,7 +33198,7 @@ function useSchoolService() {
|
|
|
33378
33198
|
const isCSV = file.mimetype.includes("csv") || file.originalname.endsWith(".csv");
|
|
33379
33199
|
const isExcel = file.mimetype.includes("sheet") || file.originalname.endsWith(".xlsx") || file.originalname.endsWith(".xls");
|
|
33380
33200
|
if (!isCSV && !isExcel) {
|
|
33381
|
-
throw new
|
|
33201
|
+
throw new BadRequestError18("Only CSV and Excel files are supported");
|
|
33382
33202
|
}
|
|
33383
33203
|
let rawData = [];
|
|
33384
33204
|
try {
|
|
@@ -33395,17 +33215,17 @@ function useSchoolService() {
|
|
|
33395
33215
|
transformHeader: (header) => header.trim()
|
|
33396
33216
|
});
|
|
33397
33217
|
if (parseResult.errors.length > 0) {
|
|
33398
|
-
throw new
|
|
33218
|
+
throw new BadRequestError18(
|
|
33399
33219
|
`CSV parsing error: ${parseResult.errors[0].message}`
|
|
33400
33220
|
);
|
|
33401
33221
|
}
|
|
33402
33222
|
rawData = parseResult.data;
|
|
33403
33223
|
}
|
|
33404
33224
|
} catch (error) {
|
|
33405
|
-
throw new
|
|
33225
|
+
throw new BadRequestError18(`File parsing error: ${error.message}`);
|
|
33406
33226
|
}
|
|
33407
33227
|
if (!rawData.length) {
|
|
33408
|
-
throw new
|
|
33228
|
+
throw new BadRequestError18("No data found in file");
|
|
33409
33229
|
}
|
|
33410
33230
|
const schools = [];
|
|
33411
33231
|
const errors = [];
|
|
@@ -33451,7 +33271,7 @@ function useSchoolService() {
|
|
|
33451
33271
|
schools.push(school);
|
|
33452
33272
|
}
|
|
33453
33273
|
if (errors.length > 0) {
|
|
33454
|
-
throw new
|
|
33274
|
+
throw new BadRequestError18(
|
|
33455
33275
|
`Validation errors:
|
|
33456
33276
|
${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
|
|
33457
33277
|
...and ${errors.length - 5} more` : ""}`
|
|
@@ -33465,7 +33285,7 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
|
|
|
33465
33285
|
};
|
|
33466
33286
|
const { getByName: getPSGCByName } = usePSGCRepo();
|
|
33467
33287
|
for (let i = 0; i < schools.length; i++) {
|
|
33468
|
-
const session =
|
|
33288
|
+
const session = useAtlas8.getClient()?.startSession();
|
|
33469
33289
|
if (!session) {
|
|
33470
33290
|
throw new Error("Unable to start MongoDB session");
|
|
33471
33291
|
}
|
|
@@ -33477,7 +33297,7 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
|
|
|
33477
33297
|
type: "Prov"
|
|
33478
33298
|
});
|
|
33479
33299
|
if (!provincePSGC) {
|
|
33480
|
-
throw new
|
|
33300
|
+
throw new BadRequestError18(
|
|
33481
33301
|
`Province '${school.province}' not found in PSGC data.`
|
|
33482
33302
|
);
|
|
33483
33303
|
}
|
|
@@ -33487,7 +33307,7 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
|
|
|
33487
33307
|
code: provincePSGC.code
|
|
33488
33308
|
});
|
|
33489
33309
|
if (!cityMunPSGC) {
|
|
33490
|
-
throw new
|
|
33310
|
+
throw new BadRequestError18(
|
|
33491
33311
|
`City/Municipality '${school.cityMunicipality}' not found in PSGC data.`
|
|
33492
33312
|
);
|
|
33493
33313
|
}
|
|
@@ -33539,8 +33359,8 @@ ${errors.slice(0, 5).join("\n")}${errors.length > 5 ? `
|
|
|
33539
33359
|
}
|
|
33540
33360
|
|
|
33541
33361
|
// src/resources/school/school.controller.ts
|
|
33542
|
-
import { BadRequestError as
|
|
33543
|
-
import
|
|
33362
|
+
import { BadRequestError as BadRequestError19 } from "@eeplatform/nodejs-utils";
|
|
33363
|
+
import Joi11 from "joi";
|
|
33544
33364
|
function useSchoolController() {
|
|
33545
33365
|
const {
|
|
33546
33366
|
getAll: _getAll,
|
|
@@ -33560,7 +33380,7 @@ function useSchoolController() {
|
|
|
33560
33380
|
const payload = req.body;
|
|
33561
33381
|
const { error } = schemaSchool.validate(payload);
|
|
33562
33382
|
if (error) {
|
|
33563
|
-
next(new
|
|
33383
|
+
next(new BadRequestError19(`Validation error: ${error.message}`));
|
|
33564
33384
|
return;
|
|
33565
33385
|
}
|
|
33566
33386
|
try {
|
|
@@ -33573,19 +33393,19 @@ function useSchoolController() {
|
|
|
33573
33393
|
}
|
|
33574
33394
|
}
|
|
33575
33395
|
async function getAll(req, res, next) {
|
|
33576
|
-
const validation =
|
|
33577
|
-
page:
|
|
33578
|
-
limit:
|
|
33579
|
-
sort:
|
|
33580
|
-
sortOrder:
|
|
33581
|
-
status:
|
|
33582
|
-
org:
|
|
33583
|
-
app:
|
|
33584
|
-
search:
|
|
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, "")
|
|
33585
33405
|
});
|
|
33586
33406
|
const { error } = validation.validate(req.query);
|
|
33587
33407
|
if (error) {
|
|
33588
|
-
next(new
|
|
33408
|
+
next(new BadRequestError19(`Validation error: ${error.message}`));
|
|
33589
33409
|
return;
|
|
33590
33410
|
}
|
|
33591
33411
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -33617,10 +33437,10 @@ function useSchoolController() {
|
|
|
33617
33437
|
}
|
|
33618
33438
|
async function getByCreatedBy(req, res, next) {
|
|
33619
33439
|
const createdBy = req.params.createdBy;
|
|
33620
|
-
const validation =
|
|
33440
|
+
const validation = Joi11.string().hex().required();
|
|
33621
33441
|
const { error } = validation.validate(createdBy);
|
|
33622
33442
|
if (error) {
|
|
33623
|
-
next(new
|
|
33443
|
+
next(new BadRequestError19(`Validation error: ${error.message}`));
|
|
33624
33444
|
return;
|
|
33625
33445
|
}
|
|
33626
33446
|
try {
|
|
@@ -33634,13 +33454,13 @@ function useSchoolController() {
|
|
|
33634
33454
|
async function updateStatusById(req, res, next) {
|
|
33635
33455
|
const schoolId = req.params.id;
|
|
33636
33456
|
const status = req.params.status;
|
|
33637
|
-
const validation =
|
|
33638
|
-
id:
|
|
33639
|
-
status:
|
|
33457
|
+
const validation = Joi11.object({
|
|
33458
|
+
id: Joi11.string().hex().required(),
|
|
33459
|
+
status: Joi11.string().valid("active", "deleted", "suspended").required()
|
|
33640
33460
|
});
|
|
33641
33461
|
const { error } = validation.validate({ id: schoolId, status });
|
|
33642
33462
|
if (error) {
|
|
33643
|
-
next(new
|
|
33463
|
+
next(new BadRequestError19(`Validation error: ${error.message}`));
|
|
33644
33464
|
return;
|
|
33645
33465
|
}
|
|
33646
33466
|
try {
|
|
@@ -33655,7 +33475,7 @@ function useSchoolController() {
|
|
|
33655
33475
|
const payload = req.body;
|
|
33656
33476
|
const { error } = schemaSchool.validate(payload);
|
|
33657
33477
|
if (error) {
|
|
33658
|
-
next(new
|
|
33478
|
+
next(new BadRequestError19(`Validation error: ${error.message}`));
|
|
33659
33479
|
return;
|
|
33660
33480
|
}
|
|
33661
33481
|
try {
|
|
@@ -33669,12 +33489,12 @@ function useSchoolController() {
|
|
|
33669
33489
|
}
|
|
33670
33490
|
async function approveSchool(req, res, next) {
|
|
33671
33491
|
const schoolId = req.params.id;
|
|
33672
|
-
const validation =
|
|
33673
|
-
id:
|
|
33492
|
+
const validation = Joi11.object({
|
|
33493
|
+
id: Joi11.string().hex().required()
|
|
33674
33494
|
});
|
|
33675
33495
|
const { error } = validation.validate({ id: schoolId });
|
|
33676
33496
|
if (error) {
|
|
33677
|
-
next(new
|
|
33497
|
+
next(new BadRequestError19(`Validation error: ${error.message}`));
|
|
33678
33498
|
return;
|
|
33679
33499
|
}
|
|
33680
33500
|
try {
|
|
@@ -33693,11 +33513,11 @@ function useSchoolController() {
|
|
|
33693
33513
|
return;
|
|
33694
33514
|
}
|
|
33695
33515
|
const { region, regionName, division, divisionName } = req.body;
|
|
33696
|
-
const validation =
|
|
33697
|
-
region:
|
|
33698
|
-
regionName:
|
|
33699
|
-
division:
|
|
33700
|
-
divisionName:
|
|
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()
|
|
33701
33521
|
});
|
|
33702
33522
|
const { error } = validation.validate({
|
|
33703
33523
|
region,
|
|
@@ -33706,7 +33526,7 @@ function useSchoolController() {
|
|
|
33706
33526
|
divisionName
|
|
33707
33527
|
});
|
|
33708
33528
|
if (error) {
|
|
33709
|
-
next(new
|
|
33529
|
+
next(new BadRequestError19(`Validation error: ${error.message}`));
|
|
33710
33530
|
return;
|
|
33711
33531
|
}
|
|
33712
33532
|
try {
|
|
@@ -33726,14 +33546,14 @@ function useSchoolController() {
|
|
|
33726
33546
|
async function updateFieldById(req, res, next) {
|
|
33727
33547
|
const _id = req.params.id;
|
|
33728
33548
|
const { field, value } = req.body;
|
|
33729
|
-
const validation =
|
|
33730
|
-
_id:
|
|
33731
|
-
field:
|
|
33732
|
-
value:
|
|
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()
|
|
33733
33553
|
});
|
|
33734
33554
|
const { error } = validation.validate({ _id, field, value });
|
|
33735
33555
|
if (error) {
|
|
33736
|
-
next(new
|
|
33556
|
+
next(new BadRequestError19(error.message));
|
|
33737
33557
|
return;
|
|
33738
33558
|
}
|
|
33739
33559
|
try {
|
|
@@ -33747,17 +33567,17 @@ function useSchoolController() {
|
|
|
33747
33567
|
async function updateById(req, res, next) {
|
|
33748
33568
|
const id = req.params.id;
|
|
33749
33569
|
const payload = req.body;
|
|
33750
|
-
const validation =
|
|
33751
|
-
id:
|
|
33570
|
+
const validation = Joi11.object({
|
|
33571
|
+
id: Joi11.string().hex().required()
|
|
33752
33572
|
});
|
|
33753
33573
|
const { error: idError } = validation.validate({ id });
|
|
33754
33574
|
if (idError) {
|
|
33755
|
-
next(new
|
|
33575
|
+
next(new BadRequestError19(idError.message));
|
|
33756
33576
|
return;
|
|
33757
33577
|
}
|
|
33758
33578
|
const { error } = schemaSchoolUpdate.validate(payload);
|
|
33759
33579
|
if (error) {
|
|
33760
|
-
next(new
|
|
33580
|
+
next(new BadRequestError19(error.message));
|
|
33761
33581
|
return;
|
|
33762
33582
|
}
|
|
33763
33583
|
try {
|
|
@@ -33770,12 +33590,12 @@ function useSchoolController() {
|
|
|
33770
33590
|
}
|
|
33771
33591
|
async function deleteById(req, res, next) {
|
|
33772
33592
|
const _id = req.params.id;
|
|
33773
|
-
const validation =
|
|
33774
|
-
_id:
|
|
33593
|
+
const validation = Joi11.object({
|
|
33594
|
+
_id: Joi11.string().hex().required()
|
|
33775
33595
|
});
|
|
33776
33596
|
const { error } = validation.validate({ _id });
|
|
33777
33597
|
if (error) {
|
|
33778
|
-
next(new
|
|
33598
|
+
next(new BadRequestError19(error.message));
|
|
33779
33599
|
return;
|
|
33780
33600
|
}
|
|
33781
33601
|
try {
|
|
@@ -33800,6 +33620,245 @@ function useSchoolController() {
|
|
|
33800
33620
|
};
|
|
33801
33621
|
}
|
|
33802
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
|
+
|
|
33803
33862
|
// src/resources/asset/asset.model.ts
|
|
33804
33863
|
import { BadRequestError as BadRequestError21 } from "@eeplatform/nodejs-utils";
|
|
33805
33864
|
import Joi13 from "joi";
|