File size: 1,645 Bytes
8cd83e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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());