edtech / packages /database /sync-days.ts
CognxSafeTrack
refactor(prod): unify currentDay exclusively in Enrollment model to fix scheduler sync
8cd83e2
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function syncDays() {
console.log('🔄 Démarrage de la synchronisation des jours (UserProgress -> Enrollment)...');
const enrollments = await prisma.enrollment.findMany({
where: { status: 'ACTIVE' },
include: { user: { include: { progress: true } } }
});
let updatedCount = 0;
for (const enrollment of enrollments) {
// Find the corresponding UserProgress for this active track
const progress = enrollment.user.progress.find(p => p.trackId === enrollment.trackId);
if (progress) {
// If UserProgress has a higher internal currentDay than Enrollment,
// the scheduler bug affected this user. We need to align Enrollment.
// (Note: Since we haven't removed currentDay from UserProgress yet, we can access it via 'any')
const progressAny = progress as any;
if (progressAny.currentDay && progressAny.currentDay > enrollment.currentDay) {
console.log(`➡️ Mise à jour: User ${enrollment.userId} Track ${enrollment.trackId}: Jour ${enrollment.currentDay} -> ${progressAny.currentDay}`);
await prisma.enrollment.update({
where: { id: enrollment.id },
data: { currentDay: progressAny.currentDay }
});
updatedCount++;
}
}
}
console.log(`✅ Synchronisation terminée. ${updatedCount} inscriptions synchronisées.`);
}
syncDays()
.catch(console.error)
.finally(() => prisma.$disconnect());