@eeplatform/basic-edu 1.8.6 → 1.8.7

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.8.7
4
+
5
+ ### Patch Changes
6
+
7
+ - 24c7ff6: Fix enrollment status update
8
+
3
9
  ## 1.8.6
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1636,6 +1636,8 @@ declare function useProgramScreeningRepo(): {
1636
1636
  }, session?: ClientSession) => Promise<string>;
1637
1637
  deleteById: (_id: string | ObjectId) => Promise<string>;
1638
1638
  updateById: (_id: string | ObjectId, value: Pick<TProgramScreening, "specialProgram" | "specialProgramName" | "status">) => Promise<string>;
1639
+ updateStatusByApplicantId: (_id: string | ObjectId, status: string, session?: ClientSession) => Promise<string>;
1640
+ getByApplicantId: (_id: string | ObjectId) => Promise<TProgramScreening>;
1639
1641
  };
1640
1642
 
1641
1643
  declare function useProgramScreeningController(): {
package/dist/index.js CHANGED
@@ -2968,7 +2968,7 @@ var learnerInfoSchema = import_joi5.default.object({
2968
2968
  });
2969
2969
  var schemaUpdateStatus = import_joi5.default.object({
2970
2970
  _id: import_joi5.default.string().hex().length(24).required(),
2971
- status: import_joi5.default.string().valid("pending", "accepted", "rejected").required()
2971
+ status: import_joi5.default.string().valid("pending", "accepted", "rejected", "cancelled").required()
2972
2972
  });
2973
2973
  var gradeLevels = [
2974
2974
  "K1",
@@ -4390,6 +4390,51 @@ function useProgramScreeningRepo() {
4390
4390
  }
4391
4391
  }
4392
4392
  }
4393
+ async function getByApplicantId(_id) {
4394
+ try {
4395
+ _id = new import_mongodb10.ObjectId(_id);
4396
+ } catch (error) {
4397
+ throw new import_nodejs_utils15.BadRequestError("Invalid ID.");
4398
+ }
4399
+ const cacheKey = (0, import_nodejs_utils15.makeCacheKey)(namespace_collection, {
4400
+ applicant: String(_id)
4401
+ });
4402
+ try {
4403
+ const cached = await getCache(cacheKey);
4404
+ if (cached) {
4405
+ import_nodejs_utils15.logger.log({
4406
+ level: "info",
4407
+ message: `Cache hit for getById program screening: ${cacheKey}`
4408
+ });
4409
+ return cached;
4410
+ }
4411
+ const result = await collection.findOne({
4412
+ applicant: _id,
4413
+ status: { $ne: "deleted" }
4414
+ });
4415
+ if (!result) {
4416
+ throw new import_nodejs_utils15.BadRequestError("Program screening not found.");
4417
+ }
4418
+ setCache(cacheKey, result, 300).then(() => {
4419
+ import_nodejs_utils15.logger.log({
4420
+ level: "info",
4421
+ message: `Cache set for program screening by id: ${cacheKey}`
4422
+ });
4423
+ }).catch((err) => {
4424
+ import_nodejs_utils15.logger.log({
4425
+ level: "error",
4426
+ message: `Failed to set cache for program screening by id: ${err.message}`
4427
+ });
4428
+ });
4429
+ return result;
4430
+ } catch (error) {
4431
+ if (error instanceof import_nodejs_utils15.AppError) {
4432
+ throw error;
4433
+ } else {
4434
+ throw new import_nodejs_utils15.InternalServerError("Failed to get program screening.");
4435
+ }
4436
+ }
4437
+ }
4393
4438
  async function getByCode(code, school) {
4394
4439
  try {
4395
4440
  school = new import_mongodb10.ObjectId(school);
@@ -4513,6 +4558,26 @@ function useProgramScreeningRepo() {
4513
4558
  );
4514
4559
  }
4515
4560
  }
4561
+ async function updateStatusByApplicantId(_id, status, session) {
4562
+ try {
4563
+ _id = new import_mongodb10.ObjectId(_id);
4564
+ } catch (error) {
4565
+ throw new import_nodejs_utils15.BadRequestError("Invalid ID.");
4566
+ }
4567
+ try {
4568
+ await collection.updateOne(
4569
+ { applicant: _id },
4570
+ { $set: { status, updatedAt: /* @__PURE__ */ new Date() } },
4571
+ { session }
4572
+ );
4573
+ delCachedData();
4574
+ return "Successfully updated program screening status.";
4575
+ } catch (error) {
4576
+ throw new import_nodejs_utils15.InternalServerError(
4577
+ "Failed to update program screening status."
4578
+ );
4579
+ }
4580
+ }
4516
4581
  return {
4517
4582
  createIndexes,
4518
4583
  add,
@@ -4521,7 +4586,9 @@ function useProgramScreeningRepo() {
4521
4586
  getByCode,
4522
4587
  updateFieldById,
4523
4588
  deleteById,
4524
- updateById
4589
+ updateById,
4590
+ updateStatusByApplicantId,
4591
+ getByApplicantId
4525
4592
  };
4526
4593
  }
4527
4594
 
@@ -5408,7 +5475,11 @@ function useEnrollmentService() {
5408
5475
  getById: _getById,
5409
5476
  updateStatusById: _updateStatusById
5410
5477
  } = useEnrollmentRepo();
5411
- const { add: addProgramScreening } = useProgramScreeningRepo();
5478
+ const {
5479
+ add: addProgramScreening,
5480
+ updateStatusByApplicantId,
5481
+ getByApplicantId
5482
+ } = useProgramScreeningRepo();
5412
5483
  const { getById: getProgramById } = useProgramRepo();
5413
5484
  async function add(value) {
5414
5485
  const session = import_nodejs_utils21.useAtlas.getClient()?.startSession();
@@ -5539,6 +5610,11 @@ function useEnrollmentService() {
5539
5610
  if (status === "rejected" && enrollment.status == "accepted") {
5540
5611
  throw new import_nodejs_utils21.BadRequestError("Accepted enrollments cannot be rejected.");
5541
5612
  }
5613
+ if (status === "withdrawn" && enrollment.status !== "screening") {
5614
+ throw new import_nodejs_utils21.BadRequestError(
5615
+ "Only enrollments in screening status can be withdrawn."
5616
+ );
5617
+ }
5542
5618
  const result = await _updateStatusById(_id, status, session);
5543
5619
  if (result.modifiedCount === 0) {
5544
5620
  throw new import_nodejs_utils21.InternalServerError("Failed to accept enrollment");
@@ -5578,6 +5654,14 @@ function useEnrollmentService() {
5578
5654
  enrollment.createdBy = enrollment.createdBy?.toString();
5579
5655
  await addLearner(enrollment, session);
5580
5656
  }
5657
+ const programScreening = await getByApplicantId(_id);
5658
+ if (programScreening) {
5659
+ if (["cancelled", "withdrawn"].includes(status)) {
5660
+ await updateStatusByApplicantId(_id, "cancelled", session);
5661
+ } else {
5662
+ await updateStatusByApplicantId(_id, status, session);
5663
+ }
5664
+ }
5581
5665
  await session.commitTransaction();
5582
5666
  return "Enrollment accepted successfully";
5583
5667
  } catch (error2) {