@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.js CHANGED
@@ -11349,6 +11349,7 @@ __export(src_exports, {
11349
11349
  MDivision: () => MDivision,
11350
11350
  MEntity: () => MEntity,
11351
11351
  MFile: () => MFile,
11352
+ MGradeLevel: () => MGradeLevel,
11352
11353
  MMember: () => MMember,
11353
11354
  MONGO_DB: () => MONGO_DB,
11354
11355
  MONGO_URI: () => MONGO_URI,
@@ -11395,6 +11396,7 @@ __export(src_exports, {
11395
11396
  schemaBuildingUnit: () => schemaBuildingUnit,
11396
11397
  schemaCurriculum: () => schemaCurriculum,
11397
11398
  schemaDivision: () => schemaDivision,
11399
+ schemaGradeLevel: () => schemaGradeLevel,
11398
11400
  schemaOffice: () => schemaOffice,
11399
11401
  schemaPlantilla: () => schemaPlantilla,
11400
11402
  schemaRegion: () => schemaRegion,
@@ -11424,6 +11426,8 @@ __export(src_exports, {
11424
11426
  useFileRepo: () => useFileRepo,
11425
11427
  useFileService: () => useFileService,
11426
11428
  useGitHubService: () => useGitHubService,
11429
+ useGradeLevelController: () => useGradeLevelController,
11430
+ useGradeLevelRepo: () => useGradeLevelRepo,
11427
11431
  useInvoiceController: () => useInvoiceController,
11428
11432
  useInvoiceModel: () => useInvoiceModel,
11429
11433
  useInvoiceRepo: () => useInvoiceRepo,
@@ -31179,14 +31183,20 @@ var schemaCurriculum = import_joi45.default.object({
31179
31183
  _id: import_joi45.default.string().hex().optional(),
31180
31184
  school: import_joi45.default.string().hex().required(),
31181
31185
  code: import_joi45.default.string().required(),
31182
- educationLevel: import_joi45.default.string().required(),
31183
- gradeLevel: import_joi45.default.string().required(),
31184
- subjectCode: import_joi45.default.string().required(),
31185
- subjectName: import_joi45.default.string().required(),
31186
- subjectType: import_joi45.default.string().required(),
31187
- sessionFrequency: import_joi45.default.number().integer().min(0).required(),
31188
- sessionDuration: import_joi45.default.number().integer().min(0).required(),
31189
- totalMinutesPerWeek: import_joi45.default.number().integer().min(0).required(),
31186
+ effectiveSchoolYear: import_joi45.default.string().required(),
31187
+ maxTeachingHoursPerDay: import_joi45.default.number().integer().min(1).required(),
31188
+ subjects: import_joi45.default.array().items(
31189
+ import_joi45.default.object({
31190
+ educationLevel: import_joi45.default.string().required(),
31191
+ gradeLevel: import_joi45.default.string().required(),
31192
+ subjectCode: import_joi45.default.string().required(),
31193
+ subjectName: import_joi45.default.string().required(),
31194
+ subjectType: import_joi45.default.string().required(),
31195
+ sessionFrequency: import_joi45.default.number().integer().min(0).required(),
31196
+ sessionDuration: import_joi45.default.number().integer().min(0).required(),
31197
+ totalMinutesPerWeek: import_joi45.default.number().integer().min(0).required()
31198
+ })
31199
+ ),
31190
31200
  curriculumMemoRef: import_joi45.default.string().optional().allow("", null),
31191
31201
  status: import_joi45.default.string().optional().allow("", null),
31192
31202
  createdAt: import_joi45.default.date().optional().allow("", null),
@@ -31213,14 +31223,9 @@ function MCurriculum(value) {
31213
31223
  _id: value._id ?? void 0,
31214
31224
  school: value.school ?? "",
31215
31225
  code: value.code ?? "",
31216
- educationLevel: value.educationLevel ?? "",
31217
- gradeLevel: value.gradeLevel ?? "",
31218
- subjectCode: value.subjectCode ?? "",
31219
- subjectName: value.subjectName ?? "",
31220
- subjectType: value.subjectType ?? "",
31221
- sessionFrequency: value.sessionFrequency ?? 0,
31222
- sessionDuration: value.sessionDuration ?? 0,
31223
- totalMinutesPerWeek: value.totalMinutesPerWeek ?? 0,
31226
+ effectiveSchoolYear: value.effectiveSchoolYear ?? "",
31227
+ maxTeachingHoursPerDay: value.maxTeachingHoursPerDay ?? 0,
31228
+ subjects: value.subjects ?? [],
31224
31229
  curriculumMemoRef: value.curriculumMemoRef ?? "",
31225
31230
  status: value.status ?? "active",
31226
31231
  createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
@@ -31246,10 +31251,16 @@ function useCurriculumRepo() {
31246
31251
  async function createIndexes() {
31247
31252
  try {
31248
31253
  await collection.createIndexes([
31249
- { key: { code: 1 }, unique: true, name: "unique_code_index" },
31250
- { key: { educationLevel: 1 } },
31251
- { key: { gradeLevel: 1 } },
31252
- { key: { subjectCode: 1 } },
31254
+ {
31255
+ key: {
31256
+ school: 1,
31257
+ code: 1,
31258
+ status: 1
31259
+ },
31260
+ unique: true,
31261
+ partialFilterExpression: { status: "active" },
31262
+ name: "unique_code_index"
31263
+ },
31253
31264
  { key: { status: 1 } }
31254
31265
  ]);
31255
31266
  } catch (error) {
@@ -31309,9 +31320,6 @@ function useCurriculumRepo() {
31309
31320
  page = 1,
31310
31321
  limit = 10,
31311
31322
  sort = {},
31312
- educationLevel = "",
31313
- gradeLevel = "",
31314
- subjectCode = "",
31315
31323
  status = "active"
31316
31324
  } = {}) {
31317
31325
  page = page > 0 ? page - 1 : 0;
@@ -31326,15 +31334,6 @@ function useCurriculumRepo() {
31326
31334
  { subjectCode: { $regex: search, $options: "i" } }
31327
31335
  ];
31328
31336
  }
31329
- if (educationLevel) {
31330
- query.educationLevel = educationLevel;
31331
- }
31332
- if (gradeLevel) {
31333
- query.gradeLevel = gradeLevel;
31334
- }
31335
- if (subjectCode) {
31336
- query.subjectCode = subjectCode;
31337
- }
31338
31337
  const cacheParams = {
31339
31338
  page,
31340
31339
  limit,
@@ -31342,12 +31341,6 @@ function useCurriculumRepo() {
31342
31341
  };
31343
31342
  if (search)
31344
31343
  cacheParams.search = search;
31345
- if (educationLevel)
31346
- cacheParams.educationLevel = educationLevel;
31347
- if (gradeLevel)
31348
- cacheParams.gradeLevel = gradeLevel;
31349
- if (subjectCode)
31350
- cacheParams.subjectCode = subjectCode;
31351
31344
  if (status !== "active")
31352
31345
  cacheParams.status = status;
31353
31346
  const cacheKey = (0, import_nodejs_utils91.makeCacheKey)(namespace_collection, cacheParams);
@@ -31368,7 +31361,20 @@ function useCurriculumRepo() {
31368
31361
  { $match: query },
31369
31362
  { $sort: sort },
31370
31363
  { $skip: page * limit },
31371
- { $limit: limit }
31364
+ { $limit: limit },
31365
+ {
31366
+ $project: {
31367
+ _id: 1,
31368
+ school: 1,
31369
+ code: 1,
31370
+ effectiveSchoolYear: 1,
31371
+ maxTeachingHoursPerDay: 1,
31372
+ curriculumMemoRef: 1,
31373
+ status: 1,
31374
+ createdAt: 1,
31375
+ updatedAt: 1
31376
+ }
31377
+ }
31372
31378
  ]).toArray();
31373
31379
  const length = await collection.countDocuments(query);
31374
31380
  const data = (0, import_nodejs_utils91.paginate)(items, page, limit, length);
@@ -31489,21 +31495,7 @@ function useCurriculumController() {
31489
31495
  } = useCurriculumRepo();
31490
31496
  async function add(req, res, next) {
31491
31497
  const value = req.body;
31492
- const validation = import_joi46.default.object({
31493
- code: import_joi46.default.string().required(),
31494
- school: import_joi46.default.string().hex().required(),
31495
- educationLevel: import_joi46.default.string().required(),
31496
- gradeLevel: import_joi46.default.string().required(),
31497
- subjectCode: import_joi46.default.string().required(),
31498
- subjectName: import_joi46.default.string().required(),
31499
- subjectType: import_joi46.default.string().required(),
31500
- sessionFrequency: import_joi46.default.number().integer().min(0).required(),
31501
- sessionDuration: import_joi46.default.number().integer().min(0).required(),
31502
- totalMinutesPerWeek: import_joi46.default.number().integer().min(0).required(),
31503
- curriculumMemoRef: import_joi46.default.string().optional().allow("", null),
31504
- status: import_joi46.default.string().optional().allow("", null)
31505
- });
31506
- const { error } = validation.validate(value);
31498
+ const { error } = schemaCurriculum.validate(value);
31507
31499
  if (error) {
31508
31500
  next(new import_nodejs_utils92.BadRequestError(error.message));
31509
31501
  import_nodejs_utils92.logger.info(`Controller: ${error.message}`);
@@ -31555,9 +31547,6 @@ function useCurriculumController() {
31555
31547
  page: import_joi46.default.number().min(1).optional().allow("", null),
31556
31548
  limit: import_joi46.default.number().min(1).optional().allow("", null),
31557
31549
  search: import_joi46.default.string().optional().allow("", null),
31558
- educationLevel: import_joi46.default.string().optional().allow("", null),
31559
- gradeLevel: import_joi46.default.string().optional().allow("", null),
31560
- subjectCode: import_joi46.default.string().optional().allow("", null),
31561
31550
  status: import_joi46.default.string().optional().allow("", null)
31562
31551
  });
31563
31552
  const { error } = validation.validate(query);
@@ -31577,9 +31566,6 @@ function useCurriculumController() {
31577
31566
  });
31578
31567
  }
31579
31568
  const status = req.query.status ?? "active";
31580
- const educationLevel = req.query.educationLevel ?? "";
31581
- const gradeLevel = req.query.gradeLevel ?? "";
31582
- const subjectCode = req.query.subjectCode ?? "";
31583
31569
  const search = req.query.search ?? "";
31584
31570
  try {
31585
31571
  const curriculums = await _getAll({
@@ -31587,9 +31573,6 @@ function useCurriculumController() {
31587
31573
  limit,
31588
31574
  sort: sortObj,
31589
31575
  status,
31590
- educationLevel,
31591
- gradeLevel,
31592
- subjectCode,
31593
31576
  search
31594
31577
  });
31595
31578
  res.json(curriculums);
@@ -31648,6 +31631,622 @@ function useCurriculumController() {
31648
31631
  deleteById
31649
31632
  };
31650
31633
  }
31634
+
31635
+ // src/models/grade-level.model.ts
31636
+ var import_nodejs_utils93 = require("@eeplatform/nodejs-utils");
31637
+ var import_joi47 = __toESM(require("joi"));
31638
+ var import_mongodb52 = require("mongodb");
31639
+ var schemaGradeLevel = import_joi47.default.object({
31640
+ _id: import_joi47.default.string().hex().optional(),
31641
+ school: import_joi47.default.string().hex().optional(),
31642
+ educationLevel: import_joi47.default.string().required(),
31643
+ gradeLevel: import_joi47.default.string().required(),
31644
+ teachingStyle: import_joi47.default.string().required(),
31645
+ defaultStartTime: import_joi47.default.string().optional().allow("", null),
31646
+ defaultEndTime: import_joi47.default.string().optional().allow("", null),
31647
+ status: import_joi47.default.string().optional().allow("", null),
31648
+ createdAt: import_joi47.default.date().optional().allow("", null),
31649
+ updatedAt: import_joi47.default.date().optional().allow("", null),
31650
+ deletedAt: import_joi47.default.date().optional().allow("", null),
31651
+ createdBy: import_joi47.default.string().optional().allow("", null),
31652
+ updatedBy: import_joi47.default.string().optional().allow("", null),
31653
+ deletedBy: import_joi47.default.string().optional().allow("", null)
31654
+ });
31655
+ function MGradeLevel(value) {
31656
+ const { error } = schemaGradeLevel.validate(value);
31657
+ if (error) {
31658
+ import_nodejs_utils93.logger.info(`Grade Level Model: ${error.message}`);
31659
+ throw new import_nodejs_utils93.BadRequestError(error.message);
31660
+ }
31661
+ if (value._id && typeof value._id === "string") {
31662
+ try {
31663
+ value._id = new import_mongodb52.ObjectId(value._id);
31664
+ } catch (error2) {
31665
+ throw new import_nodejs_utils93.BadRequestError("Invalid _id format");
31666
+ }
31667
+ }
31668
+ if (value.school && typeof value.school === "string") {
31669
+ try {
31670
+ value.school = new import_mongodb52.ObjectId(value.school);
31671
+ } catch (error2) {
31672
+ throw new import_nodejs_utils93.BadRequestError("Invalid school format");
31673
+ }
31674
+ }
31675
+ return {
31676
+ _id: value._id ?? void 0,
31677
+ school: value.school ?? void 0,
31678
+ educationLevel: value.educationLevel ?? "",
31679
+ gradeLevel: value.gradeLevel ?? "",
31680
+ teachingStyle: value.teachingStyle ?? "",
31681
+ defaultStartTime: value.defaultStartTime ?? "",
31682
+ defaultEndTime: value.defaultEndTime ?? "",
31683
+ status: value.status ?? "active",
31684
+ createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
31685
+ updatedAt: value.updatedAt ?? "",
31686
+ deletedAt: value.deletedAt ?? "",
31687
+ createdBy: value.createdBy ?? "",
31688
+ updatedBy: value.updatedBy ?? "",
31689
+ deletedBy: value.deletedBy ?? ""
31690
+ };
31691
+ }
31692
+
31693
+ // src/repositories/grade-level.repository.ts
31694
+ var import_nodejs_utils94 = require("@eeplatform/nodejs-utils");
31695
+ var import_mongodb53 = require("mongodb");
31696
+ function useGradeLevelRepo() {
31697
+ const db = import_nodejs_utils94.useAtlas.getDb();
31698
+ if (!db) {
31699
+ throw new Error("Unable to connect to server.");
31700
+ }
31701
+ const namespace_collection = "school.grade-levels";
31702
+ const collection = db.collection(namespace_collection);
31703
+ const { getCache, setCache, delNamespace } = (0, import_nodejs_utils94.useCache)(namespace_collection);
31704
+ async function createIndexes() {
31705
+ try {
31706
+ await collection.createIndexes([
31707
+ { key: { educationLevel: 1 } },
31708
+ { key: { gradeLevel: 1 } },
31709
+ { key: { teachingStyle: 1 } },
31710
+ { key: { school: 1 } },
31711
+ { key: { status: 1 } },
31712
+ {
31713
+ key: { educationLevel: 1, gradeLevel: 1, school: 1 },
31714
+ unique: true,
31715
+ partialFilterExpression: { status: { $in: ["active", "suspended"] } },
31716
+ name: "unique_grade_level_per_school_index"
31717
+ }
31718
+ ]);
31719
+ } catch (error) {
31720
+ throw new Error("Failed to create index on grade levels.");
31721
+ }
31722
+ }
31723
+ async function add(value, session) {
31724
+ try {
31725
+ value = MGradeLevel(value);
31726
+ const res = await collection.insertOne(value, { session });
31727
+ delCachedData();
31728
+ return res.insertedId;
31729
+ } catch (error) {
31730
+ import_nodejs_utils94.logger.log({
31731
+ level: "error",
31732
+ message: error.message
31733
+ });
31734
+ if (error instanceof import_nodejs_utils94.AppError) {
31735
+ throw error;
31736
+ } else {
31737
+ const isDuplicated = error.message.includes("duplicate");
31738
+ if (isDuplicated) {
31739
+ throw new import_nodejs_utils94.BadRequestError(
31740
+ "Grade level already exists for this education level and school."
31741
+ );
31742
+ }
31743
+ throw new Error("Failed to create grade level.");
31744
+ }
31745
+ }
31746
+ }
31747
+ async function updateById(_id, value, session) {
31748
+ try {
31749
+ _id = new import_mongodb53.ObjectId(_id);
31750
+ } catch (error) {
31751
+ throw new import_nodejs_utils94.BadRequestError("Invalid ID.");
31752
+ }
31753
+ if (value.school && typeof value.school === "string") {
31754
+ try {
31755
+ value.school = new import_mongodb53.ObjectId(value.school);
31756
+ } catch (error) {
31757
+ throw new import_nodejs_utils94.BadRequestError("Invalid school ID format.");
31758
+ }
31759
+ }
31760
+ try {
31761
+ const res = await collection.updateOne(
31762
+ { _id },
31763
+ { $set: { ...value, updatedAt: /* @__PURE__ */ new Date() } },
31764
+ { session }
31765
+ );
31766
+ delCachedData();
31767
+ return res;
31768
+ } catch (error) {
31769
+ import_nodejs_utils94.logger.log({
31770
+ level: "error",
31771
+ message: error.message
31772
+ });
31773
+ if (error instanceof import_nodejs_utils94.AppError) {
31774
+ throw error;
31775
+ } else {
31776
+ const isDuplicated = error.message.includes("duplicate");
31777
+ if (isDuplicated) {
31778
+ throw new import_nodejs_utils94.BadRequestError(
31779
+ "Grade level already exists for this education level and school."
31780
+ );
31781
+ }
31782
+ throw new Error("Failed to update grade level.");
31783
+ }
31784
+ }
31785
+ }
31786
+ async function getAll({
31787
+ search = "",
31788
+ page = 1,
31789
+ limit = 10,
31790
+ sort = {},
31791
+ educationLevel = "",
31792
+ gradeLevel = "",
31793
+ teachingStyle = "",
31794
+ school = "",
31795
+ status = "active"
31796
+ } = {}) {
31797
+ page = page > 0 ? page - 1 : 0;
31798
+ const query = {
31799
+ status
31800
+ };
31801
+ sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
31802
+ if (search) {
31803
+ query.$or = [
31804
+ { educationLevel: { $regex: search, $options: "i" } },
31805
+ { gradeLevel: { $regex: search, $options: "i" } },
31806
+ { teachingStyle: { $regex: search, $options: "i" } }
31807
+ ];
31808
+ }
31809
+ if (educationLevel) {
31810
+ query.educationLevel = educationLevel;
31811
+ }
31812
+ if (gradeLevel) {
31813
+ query.gradeLevel = gradeLevel;
31814
+ }
31815
+ if (teachingStyle) {
31816
+ query.teachingStyle = teachingStyle;
31817
+ }
31818
+ if (school) {
31819
+ try {
31820
+ query.school = new import_mongodb53.ObjectId(school);
31821
+ } catch (error) {
31822
+ throw new import_nodejs_utils94.BadRequestError("Invalid school ID format.");
31823
+ }
31824
+ }
31825
+ const cacheParams = {
31826
+ page,
31827
+ limit,
31828
+ sort: JSON.stringify(sort)
31829
+ };
31830
+ if (search)
31831
+ cacheParams.search = search;
31832
+ if (educationLevel)
31833
+ cacheParams.educationLevel = educationLevel;
31834
+ if (gradeLevel)
31835
+ cacheParams.gradeLevel = gradeLevel;
31836
+ if (teachingStyle)
31837
+ cacheParams.teachingStyle = teachingStyle;
31838
+ if (school)
31839
+ cacheParams.school = school;
31840
+ if (status !== "active")
31841
+ cacheParams.status = status;
31842
+ const cacheKey = (0, import_nodejs_utils94.makeCacheKey)(namespace_collection, cacheParams);
31843
+ import_nodejs_utils94.logger.log({
31844
+ level: "info",
31845
+ message: `Cache key for getAll grade levels: ${cacheKey}`
31846
+ });
31847
+ try {
31848
+ const cached = await getCache(cacheKey);
31849
+ if (cached) {
31850
+ import_nodejs_utils94.logger.log({
31851
+ level: "info",
31852
+ message: `Cache hit for getAll grade levels: ${cacheKey}`
31853
+ });
31854
+ return cached;
31855
+ }
31856
+ const items = await collection.aggregate([
31857
+ { $match: query },
31858
+ { $sort: sort },
31859
+ { $skip: page * limit },
31860
+ { $limit: limit },
31861
+ {
31862
+ $lookup: {
31863
+ from: "school.schools",
31864
+ localField: "school",
31865
+ foreignField: "_id",
31866
+ as: "schoolDetails"
31867
+ }
31868
+ },
31869
+ {
31870
+ $addFields: {
31871
+ schoolName: { $arrayElemAt: ["$schoolDetails.name", 0] }
31872
+ }
31873
+ },
31874
+ {
31875
+ $project: {
31876
+ schoolDetails: 0
31877
+ }
31878
+ }
31879
+ ]).toArray();
31880
+ const length = await collection.countDocuments(query);
31881
+ const data = (0, import_nodejs_utils94.paginate)(items, page, limit, length);
31882
+ setCache(cacheKey, data, 600).then(() => {
31883
+ import_nodejs_utils94.logger.log({
31884
+ level: "info",
31885
+ message: `Cache set for getAll grade levels: ${cacheKey}`
31886
+ });
31887
+ }).catch((err) => {
31888
+ import_nodejs_utils94.logger.log({
31889
+ level: "error",
31890
+ message: `Failed to set cache for getAll grade levels: ${err.message}`
31891
+ });
31892
+ });
31893
+ return data;
31894
+ } catch (error) {
31895
+ import_nodejs_utils94.logger.log({ level: "error", message: `${error}` });
31896
+ throw error;
31897
+ }
31898
+ }
31899
+ async function getById(_id) {
31900
+ try {
31901
+ _id = new import_mongodb53.ObjectId(_id);
31902
+ } catch (error) {
31903
+ throw new import_nodejs_utils94.BadRequestError("Invalid ID.");
31904
+ }
31905
+ const cacheKey = (0, import_nodejs_utils94.makeCacheKey)(namespace_collection, { _id: String(_id) });
31906
+ try {
31907
+ const cached = await getCache(cacheKey);
31908
+ if (cached) {
31909
+ import_nodejs_utils94.logger.log({
31910
+ level: "info",
31911
+ message: `Cache hit for getById grade level: ${cacheKey}`
31912
+ });
31913
+ return cached;
31914
+ }
31915
+ const result = await collection.aggregate([
31916
+ { $match: { _id } },
31917
+ {
31918
+ $lookup: {
31919
+ from: "school.schools",
31920
+ localField: "school",
31921
+ foreignField: "_id",
31922
+ as: "schoolDetails"
31923
+ }
31924
+ },
31925
+ {
31926
+ $addFields: {
31927
+ schoolName: { $arrayElemAt: ["$schoolDetails.name", 0] }
31928
+ }
31929
+ },
31930
+ {
31931
+ $project: {
31932
+ schoolDetails: 0
31933
+ }
31934
+ }
31935
+ ]).toArray();
31936
+ const gradeLevel = result[0] || null;
31937
+ setCache(cacheKey, gradeLevel, 300).then(() => {
31938
+ import_nodejs_utils94.logger.log({
31939
+ level: "info",
31940
+ message: `Cache set for grade level by id: ${cacheKey}`
31941
+ });
31942
+ }).catch((err) => {
31943
+ import_nodejs_utils94.logger.log({
31944
+ level: "error",
31945
+ message: `Failed to set cache for grade level by id: ${err.message}`
31946
+ });
31947
+ });
31948
+ return gradeLevel;
31949
+ } catch (error) {
31950
+ if (error instanceof import_nodejs_utils94.AppError) {
31951
+ throw error;
31952
+ } else {
31953
+ throw new import_nodejs_utils94.InternalServerError("Failed to get grade level.");
31954
+ }
31955
+ }
31956
+ }
31957
+ async function deleteById(_id, session) {
31958
+ try {
31959
+ _id = new import_mongodb53.ObjectId(_id);
31960
+ } catch (error) {
31961
+ throw new import_nodejs_utils94.BadRequestError("Invalid ID.");
31962
+ }
31963
+ try {
31964
+ const res = await collection.updateOne(
31965
+ { _id },
31966
+ { $set: { status: "deleted", deletedAt: /* @__PURE__ */ new Date() } },
31967
+ { session }
31968
+ );
31969
+ delCachedData();
31970
+ return res;
31971
+ } catch (error) {
31972
+ import_nodejs_utils94.logger.log({
31973
+ level: "error",
31974
+ message: error.message
31975
+ });
31976
+ if (error instanceof import_nodejs_utils94.AppError) {
31977
+ throw error;
31978
+ } else {
31979
+ throw new import_nodejs_utils94.InternalServerError("Failed to delete grade level.");
31980
+ }
31981
+ }
31982
+ }
31983
+ async function getByEducationLevel(educationLevel, school) {
31984
+ const query = {
31985
+ educationLevel,
31986
+ status: "active"
31987
+ };
31988
+ if (school) {
31989
+ try {
31990
+ query.school = new import_mongodb53.ObjectId(school);
31991
+ } catch (error) {
31992
+ throw new import_nodejs_utils94.BadRequestError("Invalid school ID format.");
31993
+ }
31994
+ }
31995
+ const cacheKey = (0, import_nodejs_utils94.makeCacheKey)(namespace_collection, {
31996
+ educationLevel,
31997
+ school: school || "all",
31998
+ method: "getByEducationLevel"
31999
+ });
32000
+ try {
32001
+ const cached = await getCache(cacheKey);
32002
+ if (cached) {
32003
+ import_nodejs_utils94.logger.log({
32004
+ level: "info",
32005
+ message: `Cache hit for getByEducationLevel: ${cacheKey}`
32006
+ });
32007
+ return cached;
32008
+ }
32009
+ const result = await collection.find(query).sort({ gradeLevel: 1 }).toArray();
32010
+ setCache(cacheKey, result, 300).then(() => {
32011
+ import_nodejs_utils94.logger.log({
32012
+ level: "info",
32013
+ message: `Cache set for getByEducationLevel: ${cacheKey}`
32014
+ });
32015
+ }).catch((err) => {
32016
+ import_nodejs_utils94.logger.log({
32017
+ level: "error",
32018
+ message: `Failed to set cache for getByEducationLevel: ${err.message}`
32019
+ });
32020
+ });
32021
+ return result;
32022
+ } catch (error) {
32023
+ import_nodejs_utils94.logger.log({ level: "error", message: `${error}` });
32024
+ throw error;
32025
+ }
32026
+ }
32027
+ function delCachedData() {
32028
+ delNamespace().then(() => {
32029
+ import_nodejs_utils94.logger.log({
32030
+ level: "info",
32031
+ message: `Cache namespace cleared for ${namespace_collection}`
32032
+ });
32033
+ }).catch((err) => {
32034
+ import_nodejs_utils94.logger.log({
32035
+ level: "error",
32036
+ message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
32037
+ });
32038
+ });
32039
+ }
32040
+ return {
32041
+ createIndexes,
32042
+ add,
32043
+ getAll,
32044
+ getById,
32045
+ updateById,
32046
+ deleteById,
32047
+ getByEducationLevel
32048
+ };
32049
+ }
32050
+
32051
+ // src/controllers/grade-level.controller.ts
32052
+ var import_nodejs_utils95 = require("@eeplatform/nodejs-utils");
32053
+ var import_joi48 = __toESM(require("joi"));
32054
+ function useGradeLevelController() {
32055
+ const {
32056
+ getAll: _getAll,
32057
+ getById: _getById,
32058
+ add: _add,
32059
+ updateById: _updateById,
32060
+ deleteById: _deleteById,
32061
+ getByEducationLevel: _getByEducationLevel
32062
+ } = useGradeLevelRepo();
32063
+ async function add(req, res, next) {
32064
+ const value = req.body;
32065
+ const validation = import_joi48.default.object({
32066
+ school: import_joi48.default.string().hex().optional(),
32067
+ educationLevel: import_joi48.default.string().required(),
32068
+ gradeLevel: import_joi48.default.string().required(),
32069
+ teachingStyle: import_joi48.default.string().required(),
32070
+ defaultStartTime: import_joi48.default.string().optional().allow("", null),
32071
+ defaultEndTime: import_joi48.default.string().optional().allow("", null),
32072
+ status: import_joi48.default.string().optional().allow("", null)
32073
+ });
32074
+ const { error } = validation.validate(value);
32075
+ if (error) {
32076
+ next(new import_nodejs_utils95.BadRequestError(error.message));
32077
+ import_nodejs_utils95.logger.info(`Controller: ${error.message}`);
32078
+ return;
32079
+ }
32080
+ try {
32081
+ const result = await _add(value);
32082
+ res.json({
32083
+ message: "Successfully created grade level.",
32084
+ data: { _id: result }
32085
+ });
32086
+ return;
32087
+ } catch (error2) {
32088
+ next(error2);
32089
+ }
32090
+ }
32091
+ async function updateById(req, res, next) {
32092
+ const value = req.body;
32093
+ const id = req.params.id ?? "";
32094
+ const validation = import_joi48.default.object({
32095
+ id: import_joi48.default.string().hex().required(),
32096
+ value: import_joi48.default.object({
32097
+ school: import_joi48.default.string().hex().optional(),
32098
+ educationLevel: import_joi48.default.string().optional(),
32099
+ gradeLevel: import_joi48.default.string().optional(),
32100
+ teachingStyle: import_joi48.default.string().optional(),
32101
+ maxTeachingHoursPerDay: import_joi48.default.number().integer().min(0).optional(),
32102
+ maxTeachingHoursPerWeek: import_joi48.default.number().integer().min(0).optional(),
32103
+ defaultStartTime: import_joi48.default.string().optional().allow("", null),
32104
+ defaultEndTime: import_joi48.default.string().optional().allow("", null)
32105
+ }).min(1)
32106
+ });
32107
+ const { error } = validation.validate({ id, value });
32108
+ if (error) {
32109
+ next(new import_nodejs_utils95.BadRequestError(error.message));
32110
+ import_nodejs_utils95.logger.info(`Controller: ${error.message}`);
32111
+ return;
32112
+ }
32113
+ try {
32114
+ const result = await _updateById(id, value);
32115
+ res.json({
32116
+ message: "Successfully updated grade level.",
32117
+ data: result
32118
+ });
32119
+ return;
32120
+ } catch (error2) {
32121
+ next(error2);
32122
+ }
32123
+ }
32124
+ async function getAll(req, res, next) {
32125
+ const query = req.query;
32126
+ const validation = import_joi48.default.object({
32127
+ page: import_joi48.default.number().min(1).optional().allow("", null),
32128
+ limit: import_joi48.default.number().min(1).optional().allow("", null),
32129
+ search: import_joi48.default.string().optional().allow("", null),
32130
+ educationLevel: import_joi48.default.string().optional().allow("", null),
32131
+ gradeLevel: import_joi48.default.string().optional().allow("", null),
32132
+ teachingStyle: import_joi48.default.string().optional().allow("", null),
32133
+ school: import_joi48.default.string().hex().optional().allow("", null),
32134
+ status: import_joi48.default.string().optional().allow("", null)
32135
+ });
32136
+ const { error } = validation.validate(query);
32137
+ if (error) {
32138
+ next(new import_nodejs_utils95.BadRequestError(error.message));
32139
+ return;
32140
+ }
32141
+ const page = parseInt(req.query.page) ?? 1;
32142
+ let limit = parseInt(req.query.limit) ?? 20;
32143
+ limit = isNaN(limit) ? 20 : limit;
32144
+ const sort = req.query.sort ? String(req.query.sort).split(",") : "";
32145
+ const sortOrder = req.query.sortOrder ? String(req.query.sortOrder).split(",") : "";
32146
+ const sortObj = {};
32147
+ if (sort && Array.isArray(sort) && sort.length && sortOrder && Array.isArray(sortOrder) && sortOrder.length) {
32148
+ sort.forEach((field, index) => {
32149
+ sortObj[field] = sortOrder[index] === "desc" ? -1 : 1;
32150
+ });
32151
+ }
32152
+ const status = req.query.status ?? "active";
32153
+ const educationLevel = req.query.educationLevel ?? "";
32154
+ const gradeLevel = req.query.gradeLevel ?? "";
32155
+ const teachingStyle = req.query.teachingStyle ?? "";
32156
+ const school = req.query.school ?? "";
32157
+ const search = req.query.search ?? "";
32158
+ try {
32159
+ const gradeLevels = await _getAll({
32160
+ page,
32161
+ limit,
32162
+ sort: sortObj,
32163
+ status,
32164
+ educationLevel,
32165
+ gradeLevel,
32166
+ teachingStyle,
32167
+ school,
32168
+ search
32169
+ });
32170
+ res.json(gradeLevels);
32171
+ return;
32172
+ } catch (error2) {
32173
+ next(error2);
32174
+ }
32175
+ }
32176
+ async function getById(req, res, next) {
32177
+ const id = req.params.id;
32178
+ const validation = import_joi48.default.object({
32179
+ id: import_joi48.default.string().hex().required()
32180
+ });
32181
+ const { error } = validation.validate({ id });
32182
+ if (error) {
32183
+ next(new import_nodejs_utils95.BadRequestError(error.message));
32184
+ return;
32185
+ }
32186
+ try {
32187
+ const gradeLevel = await _getById(id);
32188
+ res.json({
32189
+ message: "Successfully retrieved grade level.",
32190
+ data: { gradeLevel }
32191
+ });
32192
+ return;
32193
+ } catch (error2) {
32194
+ next(error2);
32195
+ }
32196
+ }
32197
+ async function deleteById(req, res, next) {
32198
+ const id = req.params.id;
32199
+ const validation = import_joi48.default.object({
32200
+ id: import_joi48.default.string().hex().required()
32201
+ });
32202
+ const { error } = validation.validate({ id });
32203
+ if (error) {
32204
+ next(new import_nodejs_utils95.BadRequestError(error.message));
32205
+ return;
32206
+ }
32207
+ try {
32208
+ const result = await _deleteById(id);
32209
+ res.json({
32210
+ message: "Successfully deleted grade level.",
32211
+ data: result
32212
+ });
32213
+ return;
32214
+ } catch (error2) {
32215
+ next(error2);
32216
+ }
32217
+ }
32218
+ async function getByEducationLevel(req, res, next) {
32219
+ const educationLevel = req.params.educationLevel;
32220
+ const school = req.query.school;
32221
+ const validation = import_joi48.default.object({
32222
+ educationLevel: import_joi48.default.string().required(),
32223
+ school: import_joi48.default.string().hex().optional().allow("", null)
32224
+ });
32225
+ const { error } = validation.validate({ educationLevel, school });
32226
+ if (error) {
32227
+ next(new import_nodejs_utils95.BadRequestError(error.message));
32228
+ return;
32229
+ }
32230
+ try {
32231
+ const gradeLevels = await _getByEducationLevel(educationLevel, school);
32232
+ res.json({
32233
+ message: "Successfully retrieved grade levels by education level.",
32234
+ data: { gradeLevels }
32235
+ });
32236
+ return;
32237
+ } catch (error2) {
32238
+ next(error2);
32239
+ }
32240
+ }
32241
+ return {
32242
+ add,
32243
+ getAll,
32244
+ getById,
32245
+ updateById,
32246
+ deleteById,
32247
+ getByEducationLevel
32248
+ };
32249
+ }
31651
32250
  // Annotate the CommonJS export names for ESM import in node:
31652
32251
  0 && (module.exports = {
31653
32252
  ACCESS_TOKEN_EXPIRY,
@@ -31674,6 +32273,7 @@ function useCurriculumController() {
31674
32273
  MDivision,
31675
32274
  MEntity,
31676
32275
  MFile,
32276
+ MGradeLevel,
31677
32277
  MMember,
31678
32278
  MONGO_DB,
31679
32279
  MONGO_URI,
@@ -31720,6 +32320,7 @@ function useCurriculumController() {
31720
32320
  schemaBuildingUnit,
31721
32321
  schemaCurriculum,
31722
32322
  schemaDivision,
32323
+ schemaGradeLevel,
31723
32324
  schemaOffice,
31724
32325
  schemaPlantilla,
31725
32326
  schemaRegion,
@@ -31749,6 +32350,8 @@ function useCurriculumController() {
31749
32350
  useFileRepo,
31750
32351
  useFileService,
31751
32352
  useGitHubService,
32353
+ useGradeLevelController,
32354
+ useGradeLevelRepo,
31752
32355
  useInvoiceController,
31753
32356
  useInvoiceModel,
31754
32357
  useInvoiceRepo,