-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
178 lines (152 loc) · 4.42 KB
/
node_helper.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const { GraphQLClient } = require("graphql-request");
const fetch = require("node-fetch");
const NodeHelper = require("node_helper");
const apiUrl = "https://m.mensamax.de/graphql/";
const graphQLClient = new GraphQLClient(apiUrl);
const Log = require("logger");
// console.error() will log to stderr at Docker Log.
module.exports = NodeHelper.create({
socketNotificationReceived: function (notification, payload) {
const self = this;
if (notification === "GET_TOKEN") {
getToken(self, payload.users)
}
if (notification === "GET_BENUTZER_DATEN") {
addRequestHeaders(payload.token, payload.mensamaxSuperglue);
queryBenutzerDaten(self, payload.benutzername);
}
if (notification === "GET_KONTOSTAND") {
addRequestHeaders(payload.token, payload.mensamaxSuperglue)
queryKontostand(self, payload.benutzername)
}
if (notification === "GET_SPEISEPLAN") {
addRequestHeaders(payload.token, payload.mensamaxSuperglue)
querySpeiseplan(self, payload.benutzername, payload.config)
}
if (notification === "GET_BESTELLUEBERSICHT") {
addRequestHeaders(payload.token, payload.mensamaxSuperglue)
queryBestellübersicht(self, payload.benutzername, payload.config)
}
}
});
function getToken(nodeHelper, users) {
users.forEach(async user => {
const url = apiUrl + 'auth/login';
const loginData = {
projekt: user.projekt,
benutzername: user.benutzername,
passwort: user.passwort
};
const options = {
method: 'POST',
body: JSON.stringify(loginData),
headers: {
'Content-Type': 'application/json'
}
}
try {
// Extract "mensamax_superglue" Cookie needed for following requests.
options.redirect = 'manual';
const response = await fetch(url, options);
const mensamaxSuperglue = response.headers.raw()['set-cookie']
.at(-1)
.split(';')
.at(0);
options.headers.Cookie = mensamaxSuperglue;
// Get token.
options.redirect = 'follow';
const response2 = await fetch(url, options);
const data = await response2.json();
nodeHelper.sendSocketNotification("RECEIVED_TOKEN", {
benutzername: user.benutzername,
token: data.text,
mensamaxSuperglue: mensamaxSuperglue
})
} catch (error) {
Log.error(error.type, error.message);
}
});
}
function addRequestHeaders(token, mensamaxSuperglue) {
graphQLClient.setHeaders({
'Content-Type': 'application/json',
'Cookie': `${mensamaxSuperglue};token=` + token
});
}
function queryBenutzerDaten(self, benutzername) {
const query = `
{
meineDaten { angemeldetePerson { vorname, nachname }}
}
`
graphQLClient.request(query)
.then((data) => self.sendSocketNotification("RECEIVED_BENUTZER_DATEN", {
benutzername,
benutzerdaten: data.meineDaten.angemeldetePerson
}))
.catch(error => console.error(error));
}
function queryKontostand(self, benutzername) {
const query = `
{
meinKontostand { gesamtKontostandAktuell }
}
`
graphQLClient.request(query)
.then((data) => self.sendSocketNotification("RECEIVED_KONTOSTAND", {
benutzername,
kontostand: data.meinKontostand.gesamtKontostandAktuell
}))
.catch(error => console.error(error));
}
function querySpeiseplan(self, benutzername, config) {
const variables = {
startTime: config.monday,
endTime: config.friday
};
const query = `
query ($startTime: DateTime!, $endTime: DateTime!) {
meinSpeiseplan(von: $startTime, bis: $endTime) {
menues {
meineBestellung { anzahl },
hauptspeisen { bezeichnung },
vorspeisen { bezeichnung },
nachspeisen { bezeichnung }
}
}
}
`
graphQLClient.request(query, variables)
.then((data) => self.sendSocketNotification("RECEIVED_SPEISEPLAN", {
benutzername,
speiseplan: data.meinSpeiseplan
}))
.catch(error => console.error(error));
}
function queryBestellübersicht(self, benutzername, config) {
const variables = {
kw: config.week,
year: config.year
};
const query = `
query ($kw: Int!, $year: Int!) {
bestelluebersicht(kw: $kw, year: $year) {
date,
bestellungen {
menue {
id
vorspeisen { bezeichnung }
hauptspeisen { bezeichnung }
nachspeisen { bezeichnung }
}
}
}
}
`
graphQLClient.request(query, variables)
.then((data) => self.sendSocketNotification("RECEIVED_BESTELLUEBERSICHT", {
benutzername,
bestelluebersicht: data.bestelluebersicht
}))
.catch(error => console.error(error));
}