summaryrefslogtreecommitdiff
path: root/alarm/node_modules/pronote-api/src/fetch/absences.js
blob: 9c1ac02b465f0c199a2b706c170f7ee862f4be95 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const { getPeriodBy } = require('../data/periods');
const { withId, checkDuplicates } = require('../data/id');

const getAbsences = require('./pronote/absences');

// eslint-disable-next-line complexity
async function absences(session, user, period = null, from = null, to = null, type = null)
{
    const result = {
        absences: [],
        delays: [],
        punishments: [],
        other: [],
        totals: []
    };

    const p = getPeriodBy(session, !period && !type && from && to ? 'Trimestre 1' : period, type);
    const absences = await getAbsences(session, user, p, from || p.from, to || p.to);
    if (!absences) {
        return null;
    }

    for (const event of absences.events) {
        // eslint-disable-next-line default-case
        switch (event.type) {
        case 'absence':
            result.absences.push(withId({
                from: event.from,
                to: event.to,
                justified: event.justified,
                solved: event.solved,
                hours: event.hours,
                reason: event.reasons.length && event.reasons[0].name || ''
            }, ['from', 'to']));
            break;
        case 'delay':
            result.delays.push(withId({
                date: event.date,
                justified: event.justified,
                solved: event.solved,
                justification: event.justification,
                minutesMissed: event.duration,
                reason: event.reasons.length && event.reasons[0].name || ''
            }, ['data', 'minutesMissed']));
            break;
        case 'punishment':
            // eslint-disable-next-line no-case-declarations
            let detention = null;
            if (event.nature.type === 1) {
                const schedule = event.schedule[0];
                const hour = session.params.firstHour.getHours() + schedule.position / session.params.ticksPerHour;

                const from = new Date(schedule.date.getTime());
                const to = new Date(schedule.date.getTime());

                from.setHours(from.getHours() + hour);
                to.setHours(to.getHours() + hour);
                to.setMinutes(to.getMinutes() + schedule.duration);

                detention = { from, to };
            }

            result.punishments.push(withId({
                date: event.date,
                isExclusion: event.isExclusion,
                isDuringLesson: !event.isNotDuringLesson,
                homework: event.homework,
                circumstances: event.circumstances,
                giver: event.giver.name,
                reason: event.reasons.length && event.reasons[0].name || '',
                detention
            }, ['data']));
            break;
        case 'other':
            result.other.push(withId({
                kind: event.name,
                date: event.date,
                giver: event.giver.name,
                comment: event.comment,
                subject: event.subject && event.subject.name || null
            }, ['kind', 'date']));
            break;
        }
    }

    Object.values(result).forEach(checkDuplicates);

    if (absences.subjects) {
        for (const subject of absences.subjects) {
            if (subject.inGroup) {
                continue;
            }

            const res = parseSubject(subject);
            if (subject.group) {
                res.subs = absences.subjects.filter(s => s.inGroup === subject.group).map(s => parseSubject(s));
            }

            result.totals.push(res);
        }
    }

    return result;
}

function parseSubject(subject) {
    return {
        subject: subject.name,
        hoursAssisted: subject.hoursAssisted,
        hoursMissed: subject.hoursMissed
    };
}

module.exports = absences;