@eeplatform/core 1.5.0 → 1.5.1

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
@@ -31293,14 +31293,20 @@ var schemaCurriculum = Joi45.object({
31293
31293
  _id: Joi45.string().hex().optional(),
31294
31294
  school: Joi45.string().hex().required(),
31295
31295
  code: Joi45.string().required(),
31296
- educationLevel: Joi45.string().required(),
31297
- gradeLevel: Joi45.string().required(),
31298
- subjectCode: Joi45.string().required(),
31299
- subjectName: Joi45.string().required(),
31300
- subjectType: Joi45.string().required(),
31301
- sessionFrequency: Joi45.number().integer().min(0).required(),
31302
- sessionDuration: Joi45.number().integer().min(0).required(),
31303
- totalMinutesPerWeek: Joi45.number().integer().min(0).required(),
31296
+ effectiveSchoolYear: Joi45.string().required(),
31297
+ maxTeachingHoursPerDay: Joi45.number().integer().min(1).required(),
31298
+ subjects: Joi45.array().items(
31299
+ Joi45.object({
31300
+ educationLevel: Joi45.string().required(),
31301
+ gradeLevel: Joi45.string().required(),
31302
+ subjectCode: Joi45.string().required(),
31303
+ subjectName: Joi45.string().required(),
31304
+ subjectType: Joi45.string().required(),
31305
+ sessionFrequency: Joi45.number().integer().min(0).required(),
31306
+ sessionDuration: Joi45.number().integer().min(0).required(),
31307
+ totalMinutesPerWeek: Joi45.number().integer().min(0).required()
31308
+ })
31309
+ ),
31304
31310
  curriculumMemoRef: Joi45.string().optional().allow("", null),
31305
31311
  status: Joi45.string().optional().allow("", null),
31306
31312
  createdAt: Joi45.date().optional().allow("", null),
@@ -31327,14 +31333,9 @@ function MCurriculum(value) {
31327
31333
  _id: value._id ?? void 0,
31328
31334
  school: value.school ?? "",
31329
31335
  code: value.code ?? "",
31330
- educationLevel: value.educationLevel ?? "",
31331
- gradeLevel: value.gradeLevel ?? "",
31332
- subjectCode: value.subjectCode ?? "",
31333
- subjectName: value.subjectName ?? "",
31334
- subjectType: value.subjectType ?? "",
31335
- sessionFrequency: value.sessionFrequency ?? 0,
31336
- sessionDuration: value.sessionDuration ?? 0,
31337
- totalMinutesPerWeek: value.totalMinutesPerWeek ?? 0,
31336
+ effectiveSchoolYear: value.effectiveSchoolYear ?? "",
31337
+ maxTeachingHoursPerDay: value.maxTeachingHoursPerDay ?? 0,
31338
+ subjects: value.subjects ?? [],
31338
31339
  curriculumMemoRef: value.curriculumMemoRef ?? "",
31339
31340
  status: value.status ?? "active",
31340
31341
  createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
@@ -31369,10 +31370,16 @@ function useCurriculumRepo() {
31369
31370
  async function createIndexes() {
31370
31371
  try {
31371
31372
  await collection.createIndexes([
31372
- { key: { code: 1 }, unique: true, name: "unique_code_index" },
31373
- { key: { educationLevel: 1 } },
31374
- { key: { gradeLevel: 1 } },
31375
- { key: { subjectCode: 1 } },
31373
+ {
31374
+ key: {
31375
+ school: 1,
31376
+ code: 1,
31377
+ status: 1
31378
+ },
31379
+ unique: true,
31380
+ partialFilterExpression: { status: "active" },
31381
+ name: "unique_code_index"
31382
+ },
31376
31383
  { key: { status: 1 } }
31377
31384
  ]);
31378
31385
  } catch (error) {
@@ -31432,9 +31439,6 @@ function useCurriculumRepo() {
31432
31439
  page = 1,
31433
31440
  limit = 10,
31434
31441
  sort = {},
31435
- educationLevel = "",
31436
- gradeLevel = "",
31437
- subjectCode = "",
31438
31442
  status = "active"
31439
31443
  } = {}) {
31440
31444
  page = page > 0 ? page - 1 : 0;
@@ -31449,15 +31453,6 @@ function useCurriculumRepo() {
31449
31453
  { subjectCode: { $regex: search, $options: "i" } }
31450
31454
  ];
31451
31455
  }
31452
- if (educationLevel) {
31453
- query.educationLevel = educationLevel;
31454
- }
31455
- if (gradeLevel) {
31456
- query.gradeLevel = gradeLevel;
31457
- }
31458
- if (subjectCode) {
31459
- query.subjectCode = subjectCode;
31460
- }
31461
31456
  const cacheParams = {
31462
31457
  page,
31463
31458
  limit,
@@ -31465,12 +31460,6 @@ function useCurriculumRepo() {
31465
31460
  };
31466
31461
  if (search)
31467
31462
  cacheParams.search = search;
31468
- if (educationLevel)
31469
- cacheParams.educationLevel = educationLevel;
31470
- if (gradeLevel)
31471
- cacheParams.gradeLevel = gradeLevel;
31472
- if (subjectCode)
31473
- cacheParams.subjectCode = subjectCode;
31474
31463
  if (status !== "active")
31475
31464
  cacheParams.status = status;
31476
31465
  const cacheKey = makeCacheKey25(namespace_collection, cacheParams);
@@ -31491,7 +31480,20 @@ function useCurriculumRepo() {
31491
31480
  { $match: query },
31492
31481
  { $sort: sort },
31493
31482
  { $skip: page * limit },
31494
- { $limit: limit }
31483
+ { $limit: limit },
31484
+ {
31485
+ $project: {
31486
+ _id: 1,
31487
+ school: 1,
31488
+ code: 1,
31489
+ effectiveSchoolYear: 1,
31490
+ maxTeachingHoursPerDay: 1,
31491
+ curriculumMemoRef: 1,
31492
+ status: 1,
31493
+ createdAt: 1,
31494
+ updatedAt: 1
31495
+ }
31496
+ }
31495
31497
  ]).toArray();
31496
31498
  const length = await collection.countDocuments(query);
31497
31499
  const data = paginate21(items, page, limit, length);
@@ -31612,21 +31614,7 @@ function useCurriculumController() {
31612
31614
  } = useCurriculumRepo();
31613
31615
  async function add(req, res, next) {
31614
31616
  const value = req.body;
31615
- const validation = Joi46.object({
31616
- code: Joi46.string().required(),
31617
- school: Joi46.string().hex().required(),
31618
- educationLevel: Joi46.string().required(),
31619
- gradeLevel: Joi46.string().required(),
31620
- subjectCode: Joi46.string().required(),
31621
- subjectName: Joi46.string().required(),
31622
- subjectType: Joi46.string().required(),
31623
- sessionFrequency: Joi46.number().integer().min(0).required(),
31624
- sessionDuration: Joi46.number().integer().min(0).required(),
31625
- totalMinutesPerWeek: Joi46.number().integer().min(0).required(),
31626
- curriculumMemoRef: Joi46.string().optional().allow("", null),
31627
- status: Joi46.string().optional().allow("", null)
31628
- });
31629
- const { error } = validation.validate(value);
31617
+ const { error } = schemaCurriculum.validate(value);
31630
31618
  if (error) {
31631
31619
  next(new BadRequestError84(error.message));
31632
31620
  logger42.info(`Controller: ${error.message}`);
@@ -31678,9 +31666,6 @@ function useCurriculumController() {
31678
31666
  page: Joi46.number().min(1).optional().allow("", null),
31679
31667
  limit: Joi46.number().min(1).optional().allow("", null),
31680
31668
  search: Joi46.string().optional().allow("", null),
31681
- educationLevel: Joi46.string().optional().allow("", null),
31682
- gradeLevel: Joi46.string().optional().allow("", null),
31683
- subjectCode: Joi46.string().optional().allow("", null),
31684
31669
  status: Joi46.string().optional().allow("", null)
31685
31670
  });
31686
31671
  const { error } = validation.validate(query);
@@ -31700,9 +31685,6 @@ function useCurriculumController() {
31700
31685
  });
31701
31686
  }
31702
31687
  const status = req.query.status ?? "active";
31703
- const educationLevel = req.query.educationLevel ?? "";
31704
- const gradeLevel = req.query.gradeLevel ?? "";
31705
- const subjectCode = req.query.subjectCode ?? "";
31706
31688
  const search = req.query.search ?? "";
31707
31689
  try {
31708
31690
  const curriculums = await _getAll({
@@ -31710,9 +31692,6 @@ function useCurriculumController() {
31710
31692
  limit,
31711
31693
  sort: sortObj,
31712
31694
  status,
31713
- educationLevel,
31714
- gradeLevel,
31715
- subjectCode,
31716
31695
  search
31717
31696
  });
31718
31697
  res.json(curriculums);
@@ -31771,6 +31750,631 @@ function useCurriculumController() {
31771
31750
  deleteById
31772
31751
  };
31773
31752
  }
31753
+
31754
+ // src/models/grade-level.model.ts
31755
+ import { BadRequestError as BadRequestError85, logger as logger43 } from "@eeplatform/nodejs-utils";
31756
+ import Joi47 from "joi";
31757
+ import { ObjectId as ObjectId52 } from "mongodb";
31758
+ var schemaGradeLevel = Joi47.object({
31759
+ _id: Joi47.string().hex().optional(),
31760
+ school: Joi47.string().hex().optional(),
31761
+ educationLevel: Joi47.string().required(),
31762
+ gradeLevel: Joi47.string().required(),
31763
+ teachingStyle: Joi47.string().required(),
31764
+ defaultStartTime: Joi47.string().optional().allow("", null),
31765
+ defaultEndTime: Joi47.string().optional().allow("", null),
31766
+ status: Joi47.string().optional().allow("", null),
31767
+ createdAt: Joi47.date().optional().allow("", null),
31768
+ updatedAt: Joi47.date().optional().allow("", null),
31769
+ deletedAt: Joi47.date().optional().allow("", null),
31770
+ createdBy: Joi47.string().optional().allow("", null),
31771
+ updatedBy: Joi47.string().optional().allow("", null),
31772
+ deletedBy: Joi47.string().optional().allow("", null)
31773
+ });
31774
+ function MGradeLevel(value) {
31775
+ const { error } = schemaGradeLevel.validate(value);
31776
+ if (error) {
31777
+ logger43.info(`Grade Level Model: ${error.message}`);
31778
+ throw new BadRequestError85(error.message);
31779
+ }
31780
+ if (value._id && typeof value._id === "string") {
31781
+ try {
31782
+ value._id = new ObjectId52(value._id);
31783
+ } catch (error2) {
31784
+ throw new BadRequestError85("Invalid _id format");
31785
+ }
31786
+ }
31787
+ if (value.school && typeof value.school === "string") {
31788
+ try {
31789
+ value.school = new ObjectId52(value.school);
31790
+ } catch (error2) {
31791
+ throw new BadRequestError85("Invalid school format");
31792
+ }
31793
+ }
31794
+ return {
31795
+ _id: value._id ?? void 0,
31796
+ school: value.school ?? void 0,
31797
+ educationLevel: value.educationLevel ?? "",
31798
+ gradeLevel: value.gradeLevel ?? "",
31799
+ teachingStyle: value.teachingStyle ?? "",
31800
+ defaultStartTime: value.defaultStartTime ?? "",
31801
+ defaultEndTime: value.defaultEndTime ?? "",
31802
+ status: value.status ?? "active",
31803
+ createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
31804
+ updatedAt: value.updatedAt ?? "",
31805
+ deletedAt: value.deletedAt ?? "",
31806
+ createdBy: value.createdBy ?? "",
31807
+ updatedBy: value.updatedBy ?? "",
31808
+ deletedBy: value.deletedBy ?? ""
31809
+ };
31810
+ }
31811
+
31812
+ // src/repositories/grade-level.repository.ts
31813
+ import {
31814
+ AppError as AppError21,
31815
+ BadRequestError as BadRequestError86,
31816
+ InternalServerError as InternalServerError31,
31817
+ logger as logger44,
31818
+ makeCacheKey as makeCacheKey26,
31819
+ paginate as paginate22,
31820
+ useAtlas as useAtlas43,
31821
+ useCache as useCache27
31822
+ } from "@eeplatform/nodejs-utils";
31823
+ import { ObjectId as ObjectId53 } from "mongodb";
31824
+ function useGradeLevelRepo() {
31825
+ const db = useAtlas43.getDb();
31826
+ if (!db) {
31827
+ throw new Error("Unable to connect to server.");
31828
+ }
31829
+ const namespace_collection = "school.grade-levels";
31830
+ const collection = db.collection(namespace_collection);
31831
+ const { getCache, setCache, delNamespace } = useCache27(namespace_collection);
31832
+ async function createIndexes() {
31833
+ try {
31834
+ await collection.createIndexes([
31835
+ { key: { educationLevel: 1 } },
31836
+ { key: { gradeLevel: 1 } },
31837
+ { key: { teachingStyle: 1 } },
31838
+ { key: { school: 1 } },
31839
+ { key: { status: 1 } },
31840
+ {
31841
+ key: { educationLevel: 1, gradeLevel: 1, school: 1 },
31842
+ unique: true,
31843
+ partialFilterExpression: { status: { $in: ["active", "suspended"] } },
31844
+ name: "unique_grade_level_per_school_index"
31845
+ }
31846
+ ]);
31847
+ } catch (error) {
31848
+ throw new Error("Failed to create index on grade levels.");
31849
+ }
31850
+ }
31851
+ async function add(value, session) {
31852
+ try {
31853
+ value = MGradeLevel(value);
31854
+ const res = await collection.insertOne(value, { session });
31855
+ delCachedData();
31856
+ return res.insertedId;
31857
+ } catch (error) {
31858
+ logger44.log({
31859
+ level: "error",
31860
+ message: error.message
31861
+ });
31862
+ if (error instanceof AppError21) {
31863
+ throw error;
31864
+ } else {
31865
+ const isDuplicated = error.message.includes("duplicate");
31866
+ if (isDuplicated) {
31867
+ throw new BadRequestError86(
31868
+ "Grade level already exists for this education level and school."
31869
+ );
31870
+ }
31871
+ throw new Error("Failed to create grade level.");
31872
+ }
31873
+ }
31874
+ }
31875
+ async function updateById(_id, value, session) {
31876
+ try {
31877
+ _id = new ObjectId53(_id);
31878
+ } catch (error) {
31879
+ throw new BadRequestError86("Invalid ID.");
31880
+ }
31881
+ if (value.school && typeof value.school === "string") {
31882
+ try {
31883
+ value.school = new ObjectId53(value.school);
31884
+ } catch (error) {
31885
+ throw new BadRequestError86("Invalid school ID format.");
31886
+ }
31887
+ }
31888
+ try {
31889
+ const res = await collection.updateOne(
31890
+ { _id },
31891
+ { $set: { ...value, updatedAt: /* @__PURE__ */ new Date() } },
31892
+ { session }
31893
+ );
31894
+ delCachedData();
31895
+ return res;
31896
+ } catch (error) {
31897
+ logger44.log({
31898
+ level: "error",
31899
+ message: error.message
31900
+ });
31901
+ if (error instanceof AppError21) {
31902
+ throw error;
31903
+ } else {
31904
+ const isDuplicated = error.message.includes("duplicate");
31905
+ if (isDuplicated) {
31906
+ throw new BadRequestError86(
31907
+ "Grade level already exists for this education level and school."
31908
+ );
31909
+ }
31910
+ throw new Error("Failed to update grade level.");
31911
+ }
31912
+ }
31913
+ }
31914
+ async function getAll({
31915
+ search = "",
31916
+ page = 1,
31917
+ limit = 10,
31918
+ sort = {},
31919
+ educationLevel = "",
31920
+ gradeLevel = "",
31921
+ teachingStyle = "",
31922
+ school = "",
31923
+ status = "active"
31924
+ } = {}) {
31925
+ page = page > 0 ? page - 1 : 0;
31926
+ const query = {
31927
+ status
31928
+ };
31929
+ sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
31930
+ if (search) {
31931
+ query.$or = [
31932
+ { educationLevel: { $regex: search, $options: "i" } },
31933
+ { gradeLevel: { $regex: search, $options: "i" } },
31934
+ { teachingStyle: { $regex: search, $options: "i" } }
31935
+ ];
31936
+ }
31937
+ if (educationLevel) {
31938
+ query.educationLevel = educationLevel;
31939
+ }
31940
+ if (gradeLevel) {
31941
+ query.gradeLevel = gradeLevel;
31942
+ }
31943
+ if (teachingStyle) {
31944
+ query.teachingStyle = teachingStyle;
31945
+ }
31946
+ if (school) {
31947
+ try {
31948
+ query.school = new ObjectId53(school);
31949
+ } catch (error) {
31950
+ throw new BadRequestError86("Invalid school ID format.");
31951
+ }
31952
+ }
31953
+ const cacheParams = {
31954
+ page,
31955
+ limit,
31956
+ sort: JSON.stringify(sort)
31957
+ };
31958
+ if (search)
31959
+ cacheParams.search = search;
31960
+ if (educationLevel)
31961
+ cacheParams.educationLevel = educationLevel;
31962
+ if (gradeLevel)
31963
+ cacheParams.gradeLevel = gradeLevel;
31964
+ if (teachingStyle)
31965
+ cacheParams.teachingStyle = teachingStyle;
31966
+ if (school)
31967
+ cacheParams.school = school;
31968
+ if (status !== "active")
31969
+ cacheParams.status = status;
31970
+ const cacheKey = makeCacheKey26(namespace_collection, cacheParams);
31971
+ logger44.log({
31972
+ level: "info",
31973
+ message: `Cache key for getAll grade levels: ${cacheKey}`
31974
+ });
31975
+ try {
31976
+ const cached = await getCache(cacheKey);
31977
+ if (cached) {
31978
+ logger44.log({
31979
+ level: "info",
31980
+ message: `Cache hit for getAll grade levels: ${cacheKey}`
31981
+ });
31982
+ return cached;
31983
+ }
31984
+ const items = await collection.aggregate([
31985
+ { $match: query },
31986
+ { $sort: sort },
31987
+ { $skip: page * limit },
31988
+ { $limit: limit },
31989
+ {
31990
+ $lookup: {
31991
+ from: "school.schools",
31992
+ localField: "school",
31993
+ foreignField: "_id",
31994
+ as: "schoolDetails"
31995
+ }
31996
+ },
31997
+ {
31998
+ $addFields: {
31999
+ schoolName: { $arrayElemAt: ["$schoolDetails.name", 0] }
32000
+ }
32001
+ },
32002
+ {
32003
+ $project: {
32004
+ schoolDetails: 0
32005
+ }
32006
+ }
32007
+ ]).toArray();
32008
+ const length = await collection.countDocuments(query);
32009
+ const data = paginate22(items, page, limit, length);
32010
+ setCache(cacheKey, data, 600).then(() => {
32011
+ logger44.log({
32012
+ level: "info",
32013
+ message: `Cache set for getAll grade levels: ${cacheKey}`
32014
+ });
32015
+ }).catch((err) => {
32016
+ logger44.log({
32017
+ level: "error",
32018
+ message: `Failed to set cache for getAll grade levels: ${err.message}`
32019
+ });
32020
+ });
32021
+ return data;
32022
+ } catch (error) {
32023
+ logger44.log({ level: "error", message: `${error}` });
32024
+ throw error;
32025
+ }
32026
+ }
32027
+ async function getById(_id) {
32028
+ try {
32029
+ _id = new ObjectId53(_id);
32030
+ } catch (error) {
32031
+ throw new BadRequestError86("Invalid ID.");
32032
+ }
32033
+ const cacheKey = makeCacheKey26(namespace_collection, { _id: String(_id) });
32034
+ try {
32035
+ const cached = await getCache(cacheKey);
32036
+ if (cached) {
32037
+ logger44.log({
32038
+ level: "info",
32039
+ message: `Cache hit for getById grade level: ${cacheKey}`
32040
+ });
32041
+ return cached;
32042
+ }
32043
+ const result = await collection.aggregate([
32044
+ { $match: { _id } },
32045
+ {
32046
+ $lookup: {
32047
+ from: "school.schools",
32048
+ localField: "school",
32049
+ foreignField: "_id",
32050
+ as: "schoolDetails"
32051
+ }
32052
+ },
32053
+ {
32054
+ $addFields: {
32055
+ schoolName: { $arrayElemAt: ["$schoolDetails.name", 0] }
32056
+ }
32057
+ },
32058
+ {
32059
+ $project: {
32060
+ schoolDetails: 0
32061
+ }
32062
+ }
32063
+ ]).toArray();
32064
+ const gradeLevel = result[0] || null;
32065
+ setCache(cacheKey, gradeLevel, 300).then(() => {
32066
+ logger44.log({
32067
+ level: "info",
32068
+ message: `Cache set for grade level by id: ${cacheKey}`
32069
+ });
32070
+ }).catch((err) => {
32071
+ logger44.log({
32072
+ level: "error",
32073
+ message: `Failed to set cache for grade level by id: ${err.message}`
32074
+ });
32075
+ });
32076
+ return gradeLevel;
32077
+ } catch (error) {
32078
+ if (error instanceof AppError21) {
32079
+ throw error;
32080
+ } else {
32081
+ throw new InternalServerError31("Failed to get grade level.");
32082
+ }
32083
+ }
32084
+ }
32085
+ async function deleteById(_id, session) {
32086
+ try {
32087
+ _id = new ObjectId53(_id);
32088
+ } catch (error) {
32089
+ throw new BadRequestError86("Invalid ID.");
32090
+ }
32091
+ try {
32092
+ const res = await collection.updateOne(
32093
+ { _id },
32094
+ { $set: { status: "deleted", deletedAt: /* @__PURE__ */ new Date() } },
32095
+ { session }
32096
+ );
32097
+ delCachedData();
32098
+ return res;
32099
+ } catch (error) {
32100
+ logger44.log({
32101
+ level: "error",
32102
+ message: error.message
32103
+ });
32104
+ if (error instanceof AppError21) {
32105
+ throw error;
32106
+ } else {
32107
+ throw new InternalServerError31("Failed to delete grade level.");
32108
+ }
32109
+ }
32110
+ }
32111
+ async function getByEducationLevel(educationLevel, school) {
32112
+ const query = {
32113
+ educationLevel,
32114
+ status: "active"
32115
+ };
32116
+ if (school) {
32117
+ try {
32118
+ query.school = new ObjectId53(school);
32119
+ } catch (error) {
32120
+ throw new BadRequestError86("Invalid school ID format.");
32121
+ }
32122
+ }
32123
+ const cacheKey = makeCacheKey26(namespace_collection, {
32124
+ educationLevel,
32125
+ school: school || "all",
32126
+ method: "getByEducationLevel"
32127
+ });
32128
+ try {
32129
+ const cached = await getCache(cacheKey);
32130
+ if (cached) {
32131
+ logger44.log({
32132
+ level: "info",
32133
+ message: `Cache hit for getByEducationLevel: ${cacheKey}`
32134
+ });
32135
+ return cached;
32136
+ }
32137
+ const result = await collection.find(query).sort({ gradeLevel: 1 }).toArray();
32138
+ setCache(cacheKey, result, 300).then(() => {
32139
+ logger44.log({
32140
+ level: "info",
32141
+ message: `Cache set for getByEducationLevel: ${cacheKey}`
32142
+ });
32143
+ }).catch((err) => {
32144
+ logger44.log({
32145
+ level: "error",
32146
+ message: `Failed to set cache for getByEducationLevel: ${err.message}`
32147
+ });
32148
+ });
32149
+ return result;
32150
+ } catch (error) {
32151
+ logger44.log({ level: "error", message: `${error}` });
32152
+ throw error;
32153
+ }
32154
+ }
32155
+ function delCachedData() {
32156
+ delNamespace().then(() => {
32157
+ logger44.log({
32158
+ level: "info",
32159
+ message: `Cache namespace cleared for ${namespace_collection}`
32160
+ });
32161
+ }).catch((err) => {
32162
+ logger44.log({
32163
+ level: "error",
32164
+ message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
32165
+ });
32166
+ });
32167
+ }
32168
+ return {
32169
+ createIndexes,
32170
+ add,
32171
+ getAll,
32172
+ getById,
32173
+ updateById,
32174
+ deleteById,
32175
+ getByEducationLevel
32176
+ };
32177
+ }
32178
+
32179
+ // src/controllers/grade-level.controller.ts
32180
+ import { BadRequestError as BadRequestError87, logger as logger45 } from "@eeplatform/nodejs-utils";
32181
+ import Joi48 from "joi";
32182
+ function useGradeLevelController() {
32183
+ const {
32184
+ getAll: _getAll,
32185
+ getById: _getById,
32186
+ add: _add,
32187
+ updateById: _updateById,
32188
+ deleteById: _deleteById,
32189
+ getByEducationLevel: _getByEducationLevel
32190
+ } = useGradeLevelRepo();
32191
+ async function add(req, res, next) {
32192
+ const value = req.body;
32193
+ const validation = Joi48.object({
32194
+ school: Joi48.string().hex().optional(),
32195
+ educationLevel: Joi48.string().required(),
32196
+ gradeLevel: Joi48.string().required(),
32197
+ teachingStyle: Joi48.string().required(),
32198
+ defaultStartTime: Joi48.string().optional().allow("", null),
32199
+ defaultEndTime: Joi48.string().optional().allow("", null),
32200
+ status: Joi48.string().optional().allow("", null)
32201
+ });
32202
+ const { error } = validation.validate(value);
32203
+ if (error) {
32204
+ next(new BadRequestError87(error.message));
32205
+ logger45.info(`Controller: ${error.message}`);
32206
+ return;
32207
+ }
32208
+ try {
32209
+ const result = await _add(value);
32210
+ res.json({
32211
+ message: "Successfully created grade level.",
32212
+ data: { _id: result }
32213
+ });
32214
+ return;
32215
+ } catch (error2) {
32216
+ next(error2);
32217
+ }
32218
+ }
32219
+ async function updateById(req, res, next) {
32220
+ const value = req.body;
32221
+ const id = req.params.id ?? "";
32222
+ const validation = Joi48.object({
32223
+ id: Joi48.string().hex().required(),
32224
+ value: Joi48.object({
32225
+ school: Joi48.string().hex().optional(),
32226
+ educationLevel: Joi48.string().optional(),
32227
+ gradeLevel: Joi48.string().optional(),
32228
+ teachingStyle: Joi48.string().optional(),
32229
+ maxTeachingHoursPerDay: Joi48.number().integer().min(0).optional(),
32230
+ maxTeachingHoursPerWeek: Joi48.number().integer().min(0).optional(),
32231
+ defaultStartTime: Joi48.string().optional().allow("", null),
32232
+ defaultEndTime: Joi48.string().optional().allow("", null)
32233
+ }).min(1)
32234
+ });
32235
+ const { error } = validation.validate({ id, value });
32236
+ if (error) {
32237
+ next(new BadRequestError87(error.message));
32238
+ logger45.info(`Controller: ${error.message}`);
32239
+ return;
32240
+ }
32241
+ try {
32242
+ const result = await _updateById(id, value);
32243
+ res.json({
32244
+ message: "Successfully updated grade level.",
32245
+ data: result
32246
+ });
32247
+ return;
32248
+ } catch (error2) {
32249
+ next(error2);
32250
+ }
32251
+ }
32252
+ async function getAll(req, res, next) {
32253
+ const query = req.query;
32254
+ const validation = Joi48.object({
32255
+ page: Joi48.number().min(1).optional().allow("", null),
32256
+ limit: Joi48.number().min(1).optional().allow("", null),
32257
+ search: Joi48.string().optional().allow("", null),
32258
+ educationLevel: Joi48.string().optional().allow("", null),
32259
+ gradeLevel: Joi48.string().optional().allow("", null),
32260
+ teachingStyle: Joi48.string().optional().allow("", null),
32261
+ school: Joi48.string().hex().optional().allow("", null),
32262
+ status: Joi48.string().optional().allow("", null)
32263
+ });
32264
+ const { error } = validation.validate(query);
32265
+ if (error) {
32266
+ next(new BadRequestError87(error.message));
32267
+ return;
32268
+ }
32269
+ const page = parseInt(req.query.page) ?? 1;
32270
+ let limit = parseInt(req.query.limit) ?? 20;
32271
+ limit = isNaN(limit) ? 20 : limit;
32272
+ const sort = req.query.sort ? String(req.query.sort).split(",") : "";
32273
+ const sortOrder = req.query.sortOrder ? String(req.query.sortOrder).split(",") : "";
32274
+ const sortObj = {};
32275
+ if (sort && Array.isArray(sort) && sort.length && sortOrder && Array.isArray(sortOrder) && sortOrder.length) {
32276
+ sort.forEach((field, index) => {
32277
+ sortObj[field] = sortOrder[index] === "desc" ? -1 : 1;
32278
+ });
32279
+ }
32280
+ const status = req.query.status ?? "active";
32281
+ const educationLevel = req.query.educationLevel ?? "";
32282
+ const gradeLevel = req.query.gradeLevel ?? "";
32283
+ const teachingStyle = req.query.teachingStyle ?? "";
32284
+ const school = req.query.school ?? "";
32285
+ const search = req.query.search ?? "";
32286
+ try {
32287
+ const gradeLevels = await _getAll({
32288
+ page,
32289
+ limit,
32290
+ sort: sortObj,
32291
+ status,
32292
+ educationLevel,
32293
+ gradeLevel,
32294
+ teachingStyle,
32295
+ school,
32296
+ search
32297
+ });
32298
+ res.json(gradeLevels);
32299
+ return;
32300
+ } catch (error2) {
32301
+ next(error2);
32302
+ }
32303
+ }
32304
+ async function getById(req, res, next) {
32305
+ const id = req.params.id;
32306
+ const validation = Joi48.object({
32307
+ id: Joi48.string().hex().required()
32308
+ });
32309
+ const { error } = validation.validate({ id });
32310
+ if (error) {
32311
+ next(new BadRequestError87(error.message));
32312
+ return;
32313
+ }
32314
+ try {
32315
+ const gradeLevel = await _getById(id);
32316
+ res.json({
32317
+ message: "Successfully retrieved grade level.",
32318
+ data: { gradeLevel }
32319
+ });
32320
+ return;
32321
+ } catch (error2) {
32322
+ next(error2);
32323
+ }
32324
+ }
32325
+ async function deleteById(req, res, next) {
32326
+ const id = req.params.id;
32327
+ const validation = Joi48.object({
32328
+ id: Joi48.string().hex().required()
32329
+ });
32330
+ const { error } = validation.validate({ id });
32331
+ if (error) {
32332
+ next(new BadRequestError87(error.message));
32333
+ return;
32334
+ }
32335
+ try {
32336
+ const result = await _deleteById(id);
32337
+ res.json({
32338
+ message: "Successfully deleted grade level.",
32339
+ data: result
32340
+ });
32341
+ return;
32342
+ } catch (error2) {
32343
+ next(error2);
32344
+ }
32345
+ }
32346
+ async function getByEducationLevel(req, res, next) {
32347
+ const educationLevel = req.params.educationLevel;
32348
+ const school = req.query.school;
32349
+ const validation = Joi48.object({
32350
+ educationLevel: Joi48.string().required(),
32351
+ school: Joi48.string().hex().optional().allow("", null)
32352
+ });
32353
+ const { error } = validation.validate({ educationLevel, school });
32354
+ if (error) {
32355
+ next(new BadRequestError87(error.message));
32356
+ return;
32357
+ }
32358
+ try {
32359
+ const gradeLevels = await _getByEducationLevel(educationLevel, school);
32360
+ res.json({
32361
+ message: "Successfully retrieved grade levels by education level.",
32362
+ data: { gradeLevels }
32363
+ });
32364
+ return;
32365
+ } catch (error2) {
32366
+ next(error2);
32367
+ }
32368
+ }
32369
+ return {
32370
+ add,
32371
+ getAll,
32372
+ getById,
32373
+ updateById,
32374
+ deleteById,
32375
+ getByEducationLevel
32376
+ };
32377
+ }
31774
32378
  export {
31775
32379
  ACCESS_TOKEN_EXPIRY,
31776
32380
  ACCESS_TOKEN_SECRET,
@@ -31796,6 +32400,7 @@ export {
31796
32400
  MDivision,
31797
32401
  MEntity,
31798
32402
  MFile,
32403
+ MGradeLevel,
31799
32404
  MMember,
31800
32405
  MONGO_DB,
31801
32406
  MONGO_URI,
@@ -31842,6 +32447,7 @@ export {
31842
32447
  schemaBuildingUnit,
31843
32448
  schemaCurriculum,
31844
32449
  schemaDivision,
32450
+ schemaGradeLevel,
31845
32451
  schemaOffice,
31846
32452
  schemaPlantilla,
31847
32453
  schemaRegion,
@@ -31871,6 +32477,8 @@ export {
31871
32477
  useFileRepo,
31872
32478
  useFileService,
31873
32479
  useGitHubService,
32480
+ useGradeLevelController,
32481
+ useGradeLevelRepo,
31874
32482
  useInvoiceController,
31875
32483
  useInvoiceModel,
31876
32484
  useInvoiceRepo,