summaryrefslogtreecommitdiff
path: root/school/node_modules/pronote-api/src/fetch/marks.js
blob: 1e57e85c538135e027b3894a2b80c48e6fd8ce88 (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
const { getPeriodBy } = require('../data/periods');
const { withId, checkDuplicates } = require('../data/id');

const getMarks = require('./pronote/marks');

async function marks(session, user, period = null, type = null)
{
    const marks = await getMarks(session, user, getPeriodBy(session, period, type));
    if (!marks) {
        return null;
    }

    const result = {
        subjects: [],
        averages: {}
    };

    if (marks.studentAverage) {
        result.averages.student = Number((marks.studentAverage / marks.studentAverageScale * 20).toFixed(2));
    }
    if (marks.studentClassAverage) {
        result.averages.studentClass = Number(marks.studentClassAverage.toFixed(2));
    }

    for (const subject of marks.subjects.sort((a, b) => a.order - b.order)) {
        result.subjects.push({
            name: subject.name,
            averages: {
                student: subject.studentAverage / subject.studentAverageScale * 20,
                studentClass: subject.studentClassAverage,
                max: subject.maxAverage,
                min: subject.minAverage
            },
            color: subject.color,
            marks: []
        });
    }

    for (const mark of marks.marks) {
        const subject = result.subjects.find(s => s.name === mark.subject.name);
        if (!subject) {
            continue;
        }

        const res = {
            isAway: mark.value < 0
        };

        if (!res.isAway) {
            res.value = mark.value;
        }

        if (mark.average >= 0) {
            res.min = mark.min;
            res.max = mark.max;
            res.average = mark.average;
        }

        subject.marks.push(withId({
            title: mark.title,
            ...res,
            scale: mark.scale,
            coefficient: mark.coefficient,
            date: mark.date
        }, ['title', 'date'], subject.name));
    }

    result.subjects.forEach(s => checkDuplicates(s.marks));

    return result;
}

module.exports = marks;