@eeplatform/basic-edu 1.10.7 → 1.10.8

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @eeplatform/basic-edu
2
2
 
3
+ ## 1.10.8
4
+
5
+ ### Patch Changes
6
+
7
+ - cf34525: Section management - fix section generation
8
+
3
9
  ## 1.10.7
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1012,6 +1012,7 @@ declare const schemaGenerateSections: Joi.ObjectSchema<any>;
1012
1012
  declare function modelSection(value: TSection): TSection;
1013
1013
 
1014
1014
  declare function useSectionRepo(): {
1015
+ delCachedData: () => void;
1015
1016
  createIndexes: () => Promise<void>;
1016
1017
  add: (value: TSection, session?: ClientSession) => Promise<string>;
1017
1018
  getAll: ({ search, page, limit, sort, status, school, schoolYear, gradeLevel, }?: {
@@ -1023,7 +1024,7 @@ declare function useSectionRepo(): {
1023
1024
  school?: string | undefined;
1024
1025
  schoolYear?: string | undefined;
1025
1026
  gradeLevel?: string | undefined;
1026
- }) => Promise<Record<string, any> | {
1027
+ }, session?: ClientSession) => Promise<Record<string, any> | {
1027
1028
  items: any[];
1028
1029
  pages: number;
1029
1030
  pageRange: string;
@@ -1044,8 +1045,9 @@ declare function useSectionRepo(): {
1044
1045
  }, session?: ClientSession) => Promise<string>;
1045
1046
  addStudentToSection: (_id: string | ObjectId, studentId: string, session?: ClientSession) => Promise<string>;
1046
1047
  removeStudentFromSection: (_id: string | ObjectId, studentId: string, session?: ClientSession) => Promise<string>;
1047
- deleteById: (_id: string | ObjectId) => Promise<string>;
1048
+ deleteById: (_id: string | ObjectId, session?: ClientSession) => Promise<string>;
1048
1049
  updateSizeById: (_id: string | ObjectId, size: number, session?: ClientSession) => Promise<string>;
1050
+ deleteByIds: (_id: string[] | ObjectId[], session?: ClientSession) => Promise<string>;
1049
1051
  };
1050
1052
 
1051
1053
  declare function useSectionController(): {
@@ -1095,7 +1097,11 @@ declare function useSectionStudentRepo(): {
1095
1097
  gradeLevel?: string;
1096
1098
  section?: string | ObjectId;
1097
1099
  schoolYear?: string;
1098
- }) => Promise<{}>;
1100
+ }, session?: ClientSession) => Promise<{
1101
+ items: any[];
1102
+ pages: number;
1103
+ pageRange: string;
1104
+ }>;
1099
1105
  getStudent: (options: {
1100
1106
  schoolYear?: string;
1101
1107
  section?: string | ObjectId;
package/dist/index.js CHANGED
@@ -40747,7 +40747,7 @@ function useSectionRepo() {
40747
40747
  school = "",
40748
40748
  schoolYear = "",
40749
40749
  gradeLevel = ""
40750
- } = {}) {
40750
+ } = {}, session) {
40751
40751
  page = page > 0 ? page - 1 : 0;
40752
40752
  const query = {
40753
40753
  deletedAt: { $in: ["", null] },
@@ -40794,12 +40794,15 @@ function useSectionRepo() {
40794
40794
  });
40795
40795
  return cached;
40796
40796
  }
40797
- const items = await collection.aggregate([
40798
- { $match: query },
40799
- { $sort: sort },
40800
- { $skip: page * limit },
40801
- { $limit: limit }
40802
- ]).toArray();
40797
+ const items = await collection.aggregate(
40798
+ [
40799
+ { $match: query },
40800
+ { $sort: sort },
40801
+ { $skip: page * limit },
40802
+ { $limit: limit }
40803
+ ],
40804
+ { session }
40805
+ ).toArray();
40803
40806
  const length = await collection.countDocuments(query);
40804
40807
  const data = (0, import_nodejs_utils59.paginate)(items, page, limit, length);
40805
40808
  setCache(cacheKey, data, 600).then(() => {
@@ -40864,7 +40867,10 @@ function useSectionRepo() {
40864
40867
  }
40865
40868
  async function getSection(options) {
40866
40869
  const query = {};
40867
- const cacheKeyOptions = { tag: "getSection" };
40870
+ const cacheKeyOptions = {
40871
+ tag: "getSection",
40872
+ status: "notDeleted"
40873
+ };
40868
40874
  try {
40869
40875
  query.school = new import_mongodb34.ObjectId(options.school);
40870
40876
  cacheKeyOptions.school = String(options.school);
@@ -41019,7 +41025,7 @@ function useSectionRepo() {
41019
41025
  const updateValue = field === "adviser" ? new import_mongodb34.ObjectId(value) : value;
41020
41026
  await collection.updateOne(
41021
41027
  { _id, deletedAt: { $in: ["", null] } },
41022
- { $set: { [field]: updateValue, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
41028
+ { $set: { [field]: updateValue, updatedAt: /* @__PURE__ */ new Date() } },
41023
41029
  { session }
41024
41030
  );
41025
41031
  delCachedData();
@@ -41039,7 +41045,7 @@ function useSectionRepo() {
41039
41045
  { _id, deletedAt: { $in: ["", null] } },
41040
41046
  {
41041
41047
  $addToSet: { students: studentId },
41042
- $set: { updatedAt: (/* @__PURE__ */ new Date()).toISOString() }
41048
+ $set: { updatedAt: /* @__PURE__ */ new Date() }
41043
41049
  },
41044
41050
  { session }
41045
41051
  );
@@ -41061,7 +41067,7 @@ function useSectionRepo() {
41061
41067
  {
41062
41068
  // @ts-ignore
41063
41069
  $pull: { students: studentId },
41064
- $set: { updatedAt: (/* @__PURE__ */ new Date()).toISOString() }
41070
+ $set: { updatedAt: /* @__PURE__ */ new Date() }
41065
41071
  },
41066
41072
  { session }
41067
41073
  );
@@ -41071,23 +41077,42 @@ function useSectionRepo() {
41071
41077
  throw new import_nodejs_utils59.InternalServerError("Failed to remove student from section.");
41072
41078
  }
41073
41079
  }
41074
- async function deleteById(_id) {
41080
+ async function deleteById(_id, session) {
41075
41081
  try {
41076
41082
  _id = new import_mongodb34.ObjectId(_id);
41077
41083
  } catch (error) {
41078
41084
  throw new import_nodejs_utils59.BadRequestError(namespace_collection + " Invalid ID.");
41079
41085
  }
41080
41086
  try {
41081
- await collection.updateOne(
41082
- { _id },
41083
- { $set: { status: "deleted", deletedAt: (/* @__PURE__ */ new Date()).toISOString() } }
41084
- );
41087
+ await collection.deleteOne({ _id }, { session });
41085
41088
  delCachedData();
41086
41089
  return "Successfully deleted section.";
41087
41090
  } catch (error) {
41088
41091
  throw new import_nodejs_utils59.InternalServerError("Failed to delete section.");
41089
41092
  }
41090
41093
  }
41094
+ async function deleteByIds(_id, session) {
41095
+ if (_id && Array.isArray(_id)) {
41096
+ _id = _id.map((id) => {
41097
+ try {
41098
+ return new import_mongodb34.ObjectId(id);
41099
+ } catch (error) {
41100
+ throw new import_nodejs_utils59.BadRequestError(namespace_collection + " Invalid ID.");
41101
+ }
41102
+ });
41103
+ }
41104
+ try {
41105
+ await collection.updateOne(
41106
+ { _id: { $in: _id } },
41107
+ { $set: { status: "deleted", deletedAt: /* @__PURE__ */ new Date() } },
41108
+ { session }
41109
+ );
41110
+ delCachedData();
41111
+ return "Successfully deleted sections.";
41112
+ } catch (error) {
41113
+ throw new import_nodejs_utils59.InternalServerError("Failed to delete sections.");
41114
+ }
41115
+ }
41091
41116
  async function updateSizeById(_id, size, session) {
41092
41117
  try {
41093
41118
  _id = new import_mongodb34.ObjectId(_id);
@@ -41097,7 +41122,7 @@ function useSectionRepo() {
41097
41122
  try {
41098
41123
  await collection.updateOne(
41099
41124
  { _id },
41100
- { $set: { student: size, updateAt: /* @__PURE__ */ new Date() } },
41125
+ { $set: { students: size, updateAt: /* @__PURE__ */ new Date() } },
41101
41126
  { session }
41102
41127
  );
41103
41128
  delCachedData();
@@ -41107,6 +41132,7 @@ function useSectionRepo() {
41107
41132
  }
41108
41133
  }
41109
41134
  return {
41135
+ delCachedData,
41110
41136
  createIndexes,
41111
41137
  add,
41112
41138
  getAll,
@@ -41118,7 +41144,8 @@ function useSectionRepo() {
41118
41144
  addStudentToSection,
41119
41145
  removeStudentFromSection,
41120
41146
  deleteById,
41121
- updateSizeById
41147
+ updateSizeById,
41148
+ deleteByIds
41122
41149
  };
41123
41150
  }
41124
41151
 
@@ -41284,7 +41311,7 @@ function useSectionStudentRepo() {
41284
41311
  gradeLevel = "",
41285
41312
  section = "",
41286
41313
  schoolYear = ""
41287
- } = {}) {
41314
+ } = {}, session) {
41288
41315
  const { error } = import_joi37.default.object({
41289
41316
  page: import_joi37.default.number().min(1).default(1),
41290
41317
  limit: import_joi37.default.number().min(1).max(100).default(10),
@@ -41346,11 +41373,10 @@ function useSectionStudentRepo() {
41346
41373
  return cachedData;
41347
41374
  }
41348
41375
  page = page > 0 ? page - 1 : 0;
41349
- const items = await collection.aggregate([
41350
- { $match: query },
41351
- { $skip: page * limit },
41352
- { $limit: limit }
41353
- ]).toArray();
41376
+ const items = await collection.aggregate(
41377
+ [{ $match: query }, { $skip: page * limit }, { $limit: limit }],
41378
+ { session }
41379
+ ).toArray();
41354
41380
  const length = await collection.countDocuments(query);
41355
41381
  const data = (0, import_nodejs_utils61.paginate)(items, page, limit, length);
41356
41382
  setCache(cacheKey, data, 600).then(() => {
@@ -41425,6 +41451,13 @@ function useSectionStudentRepo() {
41425
41451
  } catch (error) {
41426
41452
  throw new import_nodejs_utils61.BadRequestError("Invalid section student ID.");
41427
41453
  }
41454
+ try {
41455
+ if (typeof options.section === "string") {
41456
+ options.section = new import_mongodb36.ObjectId(options.section);
41457
+ }
41458
+ } catch (error) {
41459
+ throw new import_nodejs_utils61.BadRequestError("Invalid section ID.");
41460
+ }
41428
41461
  try {
41429
41462
  await collection.updateMany({ _id }, { $set: options }, { session });
41430
41463
  delCachedData();
@@ -43308,11 +43341,20 @@ function usePersonnelController() {
43308
43341
  function useSectionService() {
43309
43342
  const { getCountByGradeLevel, getByGradeLevel: getLeanerByGradeLevel } = useLearnerRepo();
43310
43343
  const { getByGradeLevel } = useGradeLevelRepo();
43311
- const { add: createSection, getSection, updateSizeById } = useSectionRepo();
43344
+ const {
43345
+ add: createSection,
43346
+ getSection,
43347
+ updateSizeById,
43348
+ getAll: getAllSection,
43349
+ deleteById: deleteSectionById,
43350
+ delCachedData: delCachedSectionData
43351
+ } = useSectionRepo();
43312
43352
  const {
43313
43353
  add: assignStudent,
43314
43354
  getStudent,
43315
- updateSectionById
43355
+ updateSectionById,
43356
+ getAll: getAllStudents,
43357
+ delCachedData: delCachedSectionStudentData
43316
43358
  } = useSectionStudentRepo();
43317
43359
  const { getById: getSchoolById } = useSchoolRepo();
43318
43360
  const { getAll: getAllPersonnel } = usePersonnelRepo();
@@ -43416,6 +43458,9 @@ function useSectionService() {
43416
43458
  let studentSkipped = 0;
43417
43459
  let subjectsAssigned = 0;
43418
43460
  let subjectsSkipped = 0;
43461
+ await delCachedSectionData();
43462
+ await delCachedSectionStudentData();
43463
+ console.log("SECTION SIZES:", sectionSizes);
43419
43464
  for (let i = 0; i < sectionSizes.length; i++) {
43420
43465
  const size = sectionSizes[i];
43421
43466
  const sectionName = String(i + 1);
@@ -43430,11 +43475,7 @@ function useSectionService() {
43430
43475
  sectionId = existingSection._id?.toString() || "";
43431
43476
  sectionsSkipped++;
43432
43477
  if (size !== existingSection.students) {
43433
- await updateSizeById(
43434
- existingSection._id?.toString() || "",
43435
- size,
43436
- session
43437
- );
43478
+ await updateSizeById(sectionId, size, session);
43438
43479
  }
43439
43480
  } else {
43440
43481
  sectionId = await createSection(
@@ -43476,14 +43517,13 @@ function useSectionService() {
43476
43517
  const existingStudent = await getStudent({
43477
43518
  student: student._id.toString(),
43478
43519
  schoolYear: value.schoolYear,
43479
- section: sectionId,
43480
43520
  gradeLevel: value.gradeLevel
43481
43521
  });
43482
43522
  if (existingStudent) {
43483
43523
  studentSkipped++;
43484
- if (existingStudent.section !== sectionId) {
43524
+ if (String(existingStudent.section) !== sectionId) {
43485
43525
  await updateSectionById(
43486
- sectionId,
43526
+ existingStudent._id?.toString() ?? "",
43487
43527
  { section: sectionId, sectionName },
43488
43528
  session
43489
43529
  );
@@ -43557,6 +43597,35 @@ function useSectionService() {
43557
43597
  }
43558
43598
  }
43559
43599
  await session.commitTransaction();
43600
+ await session.startTransaction();
43601
+ await delCachedSectionData();
43602
+ await delCachedSectionStudentData();
43603
+ const sections = await getAllSection(
43604
+ {
43605
+ school: value.school,
43606
+ schoolYear: value.schoolYear,
43607
+ gradeLevel: value.gradeLevel
43608
+ },
43609
+ session
43610
+ );
43611
+ if (sections && sections.items && sections.items.length) {
43612
+ for (const section of sections.items) {
43613
+ const sectionId = section._id?.toString() || "";
43614
+ const students = await getAllStudents(
43615
+ {
43616
+ school: value.school,
43617
+ schoolYear: value.schoolYear,
43618
+ section: sectionId
43619
+ },
43620
+ session
43621
+ );
43622
+ if (students && students.items && students.items.length) {
43623
+ continue;
43624
+ }
43625
+ await deleteSectionById(sectionId, session);
43626
+ }
43627
+ }
43628
+ await session.commitTransaction();
43560
43629
  return {
43561
43630
  message: "Sections generated successfully.",
43562
43631
  summary: {
@@ -43574,6 +43643,7 @@ function useSectionService() {
43574
43643
  }
43575
43644
  };
43576
43645
  } catch (error2) {
43646
+ console.log(error2);
43577
43647
  await session.abortTransaction();
43578
43648
  if (error2 instanceof import_nodejs_utils72.AppError) {
43579
43649
  throw error2;