From d228a55f2f5c36c39bc4c4438df975067703a9b8 Mon Sep 17 00:00:00 2001 From: Bidyashish Date: Wed, 9 Oct 2024 10:21:23 -0700 Subject: [PATCH] #2979 - Email notification - PD/PPD Student reminder email 8 weeks before end date Part 3 (#3778) - [x] E2E Tests - [x] Ensure the email will be sent once for the same assessment. - [x] Ensure the email will be sent again for different assessments for the same application. --- ...cation-notifications.scheduler.e2e-spec.ts | 191 +++++++++++++++++- 1 file changed, 187 insertions(+), 4 deletions(-) diff --git a/sources/packages/backend/apps/queue-consumers/src/processors/schedulers/student-application-notifications/_tests_/student-application-notifications.scheduler.e2e-spec.ts b/sources/packages/backend/apps/queue-consumers/src/processors/schedulers/student-application-notifications/_tests_/student-application-notifications.scheduler.e2e-spec.ts index 05f2cfc3be..f5e7395b31 100644 --- a/sources/packages/backend/apps/queue-consumers/src/processors/schedulers/student-application-notifications/_tests_/student-application-notifications.scheduler.e2e-spec.ts +++ b/sources/packages/backend/apps/queue-consumers/src/processors/schedulers/student-application-notifications/_tests_/student-application-notifications.scheduler.e2e-spec.ts @@ -1,21 +1,25 @@ -import { createMock } from "@golevelup/ts-jest"; import { INestApplication } from "@nestjs/common"; import { QueueNames } from "@sims/utilities"; import { createTestingAppModule, describeProcessorRootTest, + mockBullJob, } from "../../../../../test/helpers"; import { E2EDataSources, createE2EDataSources, + createFakeDisbursementSchedule, + createFakeNotification, + createFakeStudentAssessment, saveFakeApplicationDisbursements, saveFakeStudent, } from "@sims/test-utils"; -import { Job } from "bull"; import { ApplicationStatus, + AssessmentTriggerType, DisabilityStatus, DisbursementScheduleStatus, + NotificationMessage, NotificationMessageType, WorkflowData, } from "@sims/sims-db"; @@ -77,13 +81,19 @@ describe( }, }, ); + // Queued job. - const job = createMock>(); + const mockedJob = mockBullJob(); // Act - await processor.studentApplicationNotifications(job); + await processor.studentApplicationNotifications(mockedJob.job); // Assert + expect( + mockedJob.containLogMessages([ + `PD/PPD mismatch assessments that generated notifications: ${application.currentAssessment.id}`, + ]), + ).toBe(true); const notification = await db.notification.findOne({ select: { id: true, @@ -110,5 +120,178 @@ describe( }); }, ); + + it( + "Should not generate a notification for PD/PPD student mismatch close to the offering end date " + + "when the application is completed and email will be sent once for the same assessment.", + async () => { + // Arrange + // Create a student with a non-approved disability. + const student = await saveFakeStudent(db.dataSource, undefined, { + initialValue: { disabilityStatus: DisabilityStatus.Requested }, + }); + // Create an application with the disability as true. + const application = await saveFakeApplicationDisbursements( + db.dataSource, + { student }, + { + applicationStatus: ApplicationStatus.Completed, + currentAssessmentInitialValues: { + workflowData: { + calculatedData: { + pdppdStatus: true, + }, + } as WorkflowData, + }, + firstDisbursementInitialValues: { + disbursementScheduleStatus: DisbursementScheduleStatus.Pending, + }, + }, + ); + + const studentNotification = createFakeNotification( + { + user: student.user, + notificationMessage: { + id: NotificationMessageType.StudentPDPPDApplicationNotification, + } as NotificationMessage, + }, + { + initialValue: { + dateSent: new Date(), + metadata: { + assessmentId: application.currentAssessment.id, + }, + }, + }, + ); + + await db.notification.save(studentNotification); + + // Queued job. + const mockedJob = mockBullJob(); + + // Act + await processor.studentApplicationNotifications(mockedJob.job); + + // Assert + expect( + mockedJob.containLogMessages([ + "No assessments found to generate PD/PPD mismatch notifications.", + ]), + ).toBe(true); + const notificationExists = await db.notification.exists({ + relations: { notificationMessage: true }, + where: { + notificationMessage: { + id: NotificationMessageType.StudentPDPPDApplicationNotification, + }, + metadata: { assessmentId: application.currentAssessment.id }, + user: { id: student.user.id }, + dateSent: IsNull(), + }, + }); + expect(notificationExists).toBe(false); + }, + ); + + it( + "Should generate a notification for PD/PPD student mismatch close to the offering end date " + + "when the application is completed and email will be sent again for different assessments for the same application.", + async () => { + // Arrange + // Create a student with a non-approved disability. + const student = await saveFakeStudent(db.dataSource, undefined, { + initialValue: { disabilityStatus: DisabilityStatus.Requested }, + }); + // Create an application with the disability as true. + const application = await saveFakeApplicationDisbursements( + db.dataSource, + { student }, + { + applicationStatus: ApplicationStatus.Completed, + currentAssessmentInitialValues: { + workflowData: { + calculatedData: { + pdppdStatus: true, + }, + } as WorkflowData, + }, + firstDisbursementInitialValues: { + disbursementScheduleStatus: DisbursementScheduleStatus.Pending, + }, + }, + ); + + const studentNotification = createFakeNotification( + { + user: student.user, + notificationMessage: { + id: NotificationMessageType.StudentPDPPDApplicationNotification, + } as NotificationMessage, + }, + { + initialValue: { + dateSent: new Date(), + metadata: { + assessmentId: application.currentAssessment.id, + }, + }, + }, + ); + + await db.notification.save(studentNotification); + + // Create a new assessment for the same application. + application.currentAssessment = createFakeStudentAssessment( + { + auditUser: application.student.user, + application: application, + offering: application.currentAssessment.offering, + }, + { + initialValue: { + triggerType: AssessmentTriggerType.ManualReassessment, + workflowData: { + calculatedData: { + pdppdStatus: true, + }, + } as WorkflowData, + }, + }, + ); + await db.application.save(application); + + const newAssessmentDisbursement = createFakeDisbursementSchedule({ + studentAssessment: application.currentAssessment, + }); + await db.disbursementSchedule.save(newAssessmentDisbursement); + + // Queued job. + const mockedJob = mockBullJob(); + + // Act + await processor.studentApplicationNotifications(mockedJob.job); + + // Assert + expect( + mockedJob.containLogMessages([ + `PD/PPD mismatch assessments that generated notifications: ${application.currentAssessment.id}`, + ]), + ).toBe(true); + const notificationExists = await db.notification.exists({ + relations: { notificationMessage: true }, + where: { + notificationMessage: { + id: NotificationMessageType.StudentPDPPDApplicationNotification, + }, + metadata: { assessmentId: application.currentAssessment.id }, + user: { id: student.user.id }, + dateSent: IsNull(), + }, + }); + expect(notificationExists).toBe(true); + }, + ); }, );