@@ -9,8 +9,11 @@
|
||||
* For each ready row it:
|
||||
* 1. Grants the course_completed_<uuid> achievement (the key
|
||||
* MyCertificates / Profile use to display certificate cards).
|
||||
* 2. Sends a 'certificate_issued' UserNotification.
|
||||
* 3. Marks the row processed_at = NOW() so it never fires again.
|
||||
* 2. Persists the certificate record (cert_no/ref_no) via
|
||||
* services/certificate-record.service.js, so course.certificate
|
||||
* is populated immediately instead of only on first PDF download.
|
||||
* 3. Sends a 'certificate_issued' UserNotification.
|
||||
* 4. Marks the row processed_at = NOW() so it never fires again.
|
||||
*
|
||||
* Safety pattern: processed_at is set only after both step 1 and
|
||||
* step 2 succeed. If the process restarts mid-run the row will be
|
||||
@@ -28,9 +31,15 @@ const { Op } = require('sequelize');
|
||||
const PendingCertificate = require('../../models/courses/pending_certificate.mdl');
|
||||
const mdl_Achievements = require('../../models/users/achievements.mdl');
|
||||
const UserNotification = require('../../models/notifications/user_notification.mdl');
|
||||
const CronNotificationSetting = require('../../models/notifications/cron_notification_setting.mdl');
|
||||
const { NOTIFICATION_REGISTRY } = require('../../data/notifications.data');
|
||||
const { ensureCertificateRecord } = require('../../services/certificate-record.service');
|
||||
|
||||
async function run() {
|
||||
// Certificate/achievement issuance always happens — only the notification step is skippable.
|
||||
const settings = await CronNotificationSetting.findOne({ where: { job_name: 'issueCertificates' } });
|
||||
const notificationsEnabled = !settings || settings.enabled;
|
||||
|
||||
// ── 1. Fetch all rows ready to process ────────────────────────────────────
|
||||
let rows;
|
||||
try {
|
||||
@@ -69,16 +78,23 @@ async function run() {
|
||||
});
|
||||
}
|
||||
|
||||
// ── 3. Send certificate_issued notification ────────────────────────
|
||||
await UserNotification.create({
|
||||
user_id,
|
||||
...NOTIFICATION_REGISTRY.certificate_issued.build({
|
||||
courseTitle: course_title ?? '',
|
||||
courseUuid: course_uuid,
|
||||
}),
|
||||
});
|
||||
// ── 3. Persist the actual certificate record (cert_no/ref_no) so
|
||||
// course.certificate is populated immediately, instead of only
|
||||
// lazily on first PDF download ────────────────────────────────
|
||||
await ensureCertificateRecord({ userId: user_id, courseId: row.course_id });
|
||||
|
||||
// ── 4. Mark row processed ─────────────────────────────────────────
|
||||
// ── 4. Send certificate_issued notification ─────────────────────────
|
||||
if (notificationsEnabled) {
|
||||
await UserNotification.create({
|
||||
user_id,
|
||||
...NOTIFICATION_REGISTRY.certificate_issued.build({
|
||||
courseTitle: course_title ?? '',
|
||||
courseUuid: course_uuid,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
// ── 5. Mark row processed ─────────────────────────────────────────
|
||||
await PendingCertificate.update(
|
||||
{ processed_at: new Date() },
|
||||
{ where: { pending_id } }
|
||||
|
||||
Reference in New Issue
Block a user