summaryrefslogtreecommitdiff
path: root/school/node_modules/pronote-api/src/fetch/timetable.js
blob: 059b8497e15aac4eea23a5df9f2788ea9956d77f (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const { toPronoteWeek } = require('../data/dates');
const { withId, checkDuplicates } = require('../data/id');

const { getFilledDaysAndWeeks, getTimetable } = require('./pronote/timetable');

async function timetable(session, user, from = new Date(), to = null)
{
    if (!to || to < from) {
        to = new Date(from.getTime());
        to.setDate(to.getDate() + 1);
    }

    const filled = await getFilledDaysAndWeeks(session, user);
    if (!filled) {
        return null;
    }

    const fromWeek = toPronoteWeek(session, from);
    const toWeek = toPronoteWeek(session, to);

    const weeks = [];
    for (let i = fromWeek; i <= toWeek; i++) {
        weeks.push(i);
    }

    const result = [];
    for (const week of weeks) {
        const timetable = await getTimetable(session, user, week);
        const lessons = getTimetableWeek(session, timetable);

        if (lessons) {
            lessons.filter(l => l.from >= from && l.from <= to).forEach(lesson => {
                if (!filled.filledWeeks.includes(week)) {
                    lesson.isCancelled = true;
                }

                result.push(lesson);
            });
        }
    }

    return result.sort((a, b) => a.from - b.from);
}

function getTimetableWeek(session, table) {
    const result = [];
    if (!table || !table.lessons) {
        return
    }

    for (const lesson of table.lessons) {
        const from = lesson.date;
        const to = new Date(from.getTime() + (lesson.duration / session.params.ticksPerHour * 3600000));

        const res = {
            from,
            to,
            isDetention: lesson.isDetention,
            remoteLesson: lesson.remoteLesson,
            status: lesson.status,
            hasDuplicate: !!table.lessons.find(l => l.date.getTime() === from.getTime() && l !== lesson)
        };

        let room, subject, teacher;
        if (lesson.isDetention) {
            subject = lesson.content[0];
            teacher = lesson.content[1];
            room = lesson.content[2];
        } else {
            if (lesson.content) {
                subject = lesson.content.find(o => o.type === 16);
                teacher = lesson.content.find(o => o.type === 3);
                room = lesson.content.find(o => o.type === 17);
            } else {
                subject = 'Non défini';
                room = 'Non défini';
                teacher = 'Non défini';
            }

            res.isAway = (lesson.status || false) && !!lesson.status.match(/(.+)?prof(.+)?absent(.+)?/giu);
            res.isCancelled = !res.isAway && lesson.isCancelled;
            res.color = lesson.color;
        }

        res.subject = subject && subject.name || null;
        res.teacher = teacher && teacher.name || null;
        res.room = room && room.name || null;

        result.push(withId(res, ['from', 'to', 'subject']));
    }

    return checkDuplicates(result);
}

module.exports = timetable;