Moodle Chron wrapped in tryCatch

This commit is contained in:
PxlLoewe
2025-07-11 23:26:01 -07:00
parent d2b287abdc
commit 280393b307

View File

@@ -32,51 +32,55 @@ const syncMoodleIds = async () => {
}; };
const updateParticipantMoodleResults = async () => { const updateParticipantMoodleResults = async () => {
const participantsMoodlePending = await prisma.participant.findMany({ try {
where: { const participantsMoodlePending = await prisma.participant.findMany({
finisherMoodleCurseCompleted: false, where: {
Event: { finisherMoodleCurseCompleted: false,
finisherMoodleCourseId: { Event: {
not: null, finisherMoodleCourseId: {
not: null,
},
}, },
}, },
}, include: {
include: { Event: true,
Event: true, User: true,
User: true, },
}, });
}); await Promise.all(
await Promise.all( participantsMoodlePending.map(async (p) => {
participantsMoodlePending.map(async (p) => { if (!p.User) return;
if (!p.User) return; if (!p.User.moodleId) return;
if (!p.User.moodleId) return;
const quizzResult = await getMoodleCourseCompletionStatus( const quizzResult = await getMoodleCourseCompletionStatus(
p.User.moodleId.toString(), p.User.moodleId.toString(),
p.Event.finisherMoodleCourseId!, p.Event.finisherMoodleCourseId!,
); );
if (quizzResult?.completionstatus?.completed === true) { if (quizzResult?.completionstatus?.completed === true) {
await handleParticipantFinished(p.Event, p, p.User); await handleParticipantFinished(p.Event, p, p.User);
return prisma.participant.update({ return prisma.participant.update({
where: { where: {
id: p.id, id: p.id,
}, },
data: { data: {
finisherMoodleCurseCompleted: true, finisherMoodleCurseCompleted: true,
statusLog: { statusLog: {
push: { push: {
event: "Moodle-Kurs abgeschlossen", event: "Moodle-Kurs abgeschlossen",
timestamp: new Date(), timestamp: new Date(),
user: "system", user: "system",
},
}, },
}, },
}, });
}); }
} }),
}), );
); } catch (error) {
console.error("Error updating participant Moodle results:", error);
}
}; };
CronJob.from({ cronTime: "0 * * * *", onTick: syncMoodleIds, start: true }); CronJob.from({ cronTime: "0 * * * *", onTick: syncMoodleIds, start: true });