diff --git a/jobs/periodic/process-expired-coc/index.ts b/jobs/periodic/process-expired-coc/index.ts index 23802e4a7..0e270918f 100644 --- a/jobs/periodic/process-expired-coc/index.ts +++ b/jobs/periodic/process-expired-coc/index.ts @@ -2,12 +2,24 @@ import { prisma } from '../../../common/prisma'; import { getLogger } from '../../../common/logger/logger'; import moment from 'moment'; import { scheduleCoCReminders } from '../../../common/student/screening'; +import { predictedHookActionDate } from '../../../common/notification'; +import { userForStudent } from '../../../common/user'; const logger = getLogger(); export default async function execute() { const thirtyThreeMonthsAgo = moment().subtract(33, 'months').toDate(); const twoMonthsAgo = moment().subtract(2, 'months').toDate(); + const activeStudentsWithoutCoC = await prisma.student.findMany({ + where: { + active: true, + lastLogin: { + gt: twoMonthsAgo, + }, + certificate_of_conduct: { is: null }, + OR: [{ screening: { status: 'success' } }, { instructor_screening: { status: 'success' } }], + }, + }); const studentsWithStaleCoC = await prisma.student.findMany({ where: { active: true, @@ -37,4 +49,17 @@ export default async function execute() { await scheduleCoCReminders(student, /** ignoreAccCreationDate: */ false, /** isRenewal: */ true); } logger.info(`Scheduled CoC reminders for ${studentsWithStaleCoC.length} students.`); + + let scheduledForMissingCoC = 0; + for (const student of activeStudentsWithoutCoC) { + // Rescheduling cancels the pending deactivation, so students already in the countdown must be skipped + const deactivationAlreadyScheduled = await predictedHookActionDate('coc_reminder', 'deactivate-student', userForStudent(student)); + if (deactivationAlreadyScheduled) { + continue; + } + // We also treat these cases as renewals + await scheduleCoCReminders(student, /** ignoreAccCreationDate: */ false, /** isRenewal: */ true); + scheduledForMissingCoC++; + } + logger.info(`Scheduled CoC reminders for ${scheduledForMissingCoC} students without a CoC.`); }