@eeplatform/basic-edu 1.4.0 → 1.4.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/dist/index.mjs CHANGED
@@ -1940,6 +1940,27 @@ function MLearner(value) {
1940
1940
  throw new BadRequestError4("Invalid school format");
1941
1941
  }
1942
1942
  }
1943
+ if (value.createdBy && typeof value.createdBy === "string") {
1944
+ try {
1945
+ value.createdBy = new ObjectId3(value.createdBy);
1946
+ } catch (error2) {
1947
+ throw new BadRequestError4("Invalid createdBy format");
1948
+ }
1949
+ }
1950
+ if (value.updatedBy && typeof value.updatedBy === "string") {
1951
+ try {
1952
+ value.updatedBy = new ObjectId3(value.updatedBy);
1953
+ } catch (error2) {
1954
+ throw new BadRequestError4("Invalid updatedBy format");
1955
+ }
1956
+ }
1957
+ if (value.deletedBy && typeof value.deletedBy === "string") {
1958
+ try {
1959
+ value.deletedBy = new ObjectId3(value.deletedBy);
1960
+ } catch (error2) {
1961
+ throw new BadRequestError4("Invalid deletedBy format");
1962
+ }
1963
+ }
1943
1964
  return {
1944
1965
  _id: value._id ?? void 0,
1945
1966
  region: value.region,
@@ -2799,12 +2820,31 @@ function useBasicEduCountRepo() {
2799
2820
  throw new Error("Failed to increment counter status.");
2800
2821
  }
2801
2822
  }
2823
+ async function resetById(_id, session) {
2824
+ try {
2825
+ _id = new ObjectId6(_id);
2826
+ } catch (error) {
2827
+ throw new BadRequestError9("Invalid ID.");
2828
+ }
2829
+ try {
2830
+ const result = await collection.updateOne(
2831
+ { _id },
2832
+ { $set: { updatedAt: /* @__PURE__ */ new Date(), count: 0 } },
2833
+ { session }
2834
+ );
2835
+ delCachedData();
2836
+ return result;
2837
+ } catch (error) {
2838
+ throw new Error("Failed to reset counter to zero.");
2839
+ }
2840
+ }
2802
2841
  return {
2803
2842
  createIndexes,
2804
2843
  add,
2805
2844
  getById,
2806
2845
  incrementById,
2807
- getByName
2846
+ getByName,
2847
+ resetById
2808
2848
  };
2809
2849
  }
2810
2850
 
@@ -2880,7 +2920,7 @@ function useEnrollmentService() {
2880
2920
  }
2881
2921
  }
2882
2922
  const { add: addLearner } = useLearnerRepo();
2883
- const { getByName, incrementById } = useBasicEduCountRepo();
2923
+ const { getByName, incrementById, resetById } = useBasicEduCountRepo();
2884
2924
  function padNumber(num) {
2885
2925
  const str = String(num);
2886
2926
  const pad = 4 - str.length;
@@ -2924,10 +2964,20 @@ function useEnrollmentService() {
2924
2964
  "Failed to retrieve enrollment counter"
2925
2965
  );
2926
2966
  }
2967
+ const counterId = String(counter._id);
2968
+ if (counter.updatedAt) {
2969
+ const countUpdatedAtYear = new Date(
2970
+ counter.updatedAt
2971
+ ).getFullYear();
2972
+ const currentYear = (/* @__PURE__ */ new Date()).getFullYear();
2973
+ if (countUpdatedAtYear < currentYear) {
2974
+ counter.count = 0;
2975
+ await resetById(counterId, session);
2976
+ }
2977
+ }
2927
2978
  const newCount = padNumber(counter.count + 1);
2928
2979
  const lrn = `${enrollment.schoolId}-${(/* @__PURE__ */ new Date()).getFullYear().toString().slice(2, 4)}-${newCount}`;
2929
2980
  enrollment.learnerInfo.lrn = lrn;
2930
- const counterId = String(counter._id);
2931
2981
  await incrementById(counterId, session);
2932
2982
  }
2933
2983
  await addLearner(enrollment, session);