summaryrefslogtreecommitdiff
path: root/school/node_modules/pronote-api/src/server/context.js
blob: 9f3444285b5edb1498655ecb83e792e310acb5a9 (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
function common(session) {
    return {
        params: () => session.params,
        user: () => session.user,

        keepAlive: async () => {
            await session.keepAlive();
            return true;
        },
        logout: async () => {
            await session.logout();
            return true;
        },

        setKeepAlive: async ({ enabled }) => {
            await session.setKeepAlive(enabled);
            return enabled;
        }
    };
}

function student(session) {
    return {
        timetable: ({ from, to }) => session.timetable(from, to),
        marks: ({ period }) => session.marks(period),
        evaluations: ({ period }) => session.evaluations(period),
        absences: ({ period, from, to }) => session.absences(period, from, to),
        infos: () => session.infos(),
        contents: ({ from, to }) => session.contents(from, to),
        homeworks: ({ from, to }) => session.homeworks(from, to),
        menu: ({ from, to }) => session.menu(from, to),
        files: () => session.files()
    };
}

function parent(session) {
    function getStudent(student) {
        for (const s of session.user.students) {
            if (s.id === student || s.name === student) {
                return s;
            }
        }

        return null;
    }

    return {
        timetable: ({ student, from, to }) => session.timetable(getStudent(student), from, to),
        marks: ({ student, period }) => session.marks(getStudent(student), period),
        evaluations: ({ student, period }) => session.evaluations(getStudent(student), period),
        absences: ({ student, period, from, to }) => session.absences(getStudent(student), period, from, to),
        infos: ({ student }) => session.infos(getStudent(student)),
        contents: ({ student, from, to }) => session.contents(getStudent(student), from, to),
        homeworks: ({ student, from, to }) => session.homeworks(getStudent(student), from, to),
        menu: ({ student, from, to }) => session.menu(getStudent(student), from, to),
        files: ({ student }) => session.files(getStudent(student))
    };
}

function getContext(session) {
    const result = common(session);

    switch (session.type.name) {
    case 'student':
        return { ...result, ...student(session) };
    case 'parent':
        return { ...result, ...parent(session) };
    default:
        return result;
    }
}

module.exports = getContext;