-
Notifications
You must be signed in to change notification settings - Fork 0
/
vereinsflieger_nodejs_api.js
428 lines (390 loc) · 19.4 KB
/
vereinsflieger_nodejs_api.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* Copyright (c) 2021 robert1 @ kaider.info
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
var fetch = require('node-fetch');
var crypto = require('crypto')
var vfHostname = "https://vereinsflieger.de/"
class VereinsfliegerAPI {
constructor(appkey) {
this.AppKey = appkey;
this.Host = vfHostname;
this.accesstoken = ""; //access-token will be set during sign-in
}
//vereinsID only neccessary if user can access multiple Vereine
setVereinsID(id) {
this.VereinsID = id;
}
async setAccessToken() {
var vfp = new VereinsfliegerPromise('interface/rest/auth/accesstoken')
vfp.method = "GET";
return vfp.fetch()//.then(data => this.accesstoken = data['accesstoken']).catch(e => console.log(e));
}
async signIn(username, password) {
var vfp = new VereinsfliegerPromise('interface/rest/auth/signin');
await this.setAccessToken()
.then(data => {
this.accesstoken = data.accesstoken;
var hash = crypto.createHash('md5').update(password).digest('hex');
console.log("signInAccesstoken: " + this.accesstoken);
vfp.params.append('accesstoken', this.accesstoken);
vfp.params.append('appkey', this.AppKey);
vfp.params.append('username', username);
vfp.params.append('password', hash);
if (this.VereinsID) //vereinsID only neccessary if user can access multiple Vereine
params.append('cid', this.VereinsID);
return vfp.fetch();
},
error=>console.log("signIn: Error fetching Access-Token: ", error));
}
async signOut() {
var vfp = new VereinsfliegerPromise('interface/rest/auth/signout');
vfp.params.append('accesstoken', this.accesstoken);
vfp.method = "DELETE";
return vfp.fetch();//.then(d => data = d).catch(e => console.log("Error on Logout: " + e));
// return data;
}
getUser(accesstoken=this.accesstoken) {
var vfp = new VereinsfliegerPromise('interface/rest/auth/getuser')
//console.log("getUser Accesstoken: " + this.accesstoken);
vfp.params.append('accesstoken', accesstoken);
return vfp.fetch()//.then(data => user = data).catch(e => console.log(e));
//return user;
}
async addFlight(callsign, { pilotName = '', uidPilot = 0, attendantName = "", uidAttendant = 0, attendantName2 = "", uidAttendant2 = 0, attendantName3 = "", uidAttendant3 = 0, startType = "E", departureTime = '', departureLocation = '', arrivalTime = '', arrivalLocation = '', landingCount = 1, ftId = 10, km = 0, chargeMode = 0, uidCharge = 0, comment = '', wId = 0, towCallsign = '', towPilotName = '', towUidPilot = 0, towTime = '', towHeight = 0, offBlock = '', onBlock = '', motorStart = 0.0, motorEnd = 0.0 }) {
var vfp = new VereinsfliegerPromise('interface/rest/flight/add');
vfp.params.append('accesstoken', this.accesstoken);
vfp.params.append('callsign', callsign);
vfp.params.append('pilotname', pilotName);
vfp.params.append('uidpilot', uidPilot);
vfp.params.append('attendantname', attendantName);
vfp.params.append('uidattendant', uidAttendant);
vfp.params.append('attendantname2', attendantName2);
vfp.params.append('uidattendant2', uidAttendant2);
vfp.params.append('attendantname3', attendantName3);
vfp.params.append('uidattendant3', uidAttendant3);
vfp.params.append('starttype', startType);
vfp.params.append('departuretime', departureTime);
vfp.params.append('departurelocation', departureLocation);
vfp.params.append('arrivaltime', arrivalTime);
vfp.params.append('arrivallocation', arrivalLocation);
vfp.params.append('landingcount', landingCount);
vfp.params.append('ftid', ftId);
vfp.params.append('chargemode', chargeMode);
vfp.params.append('uidcharge', uidCharge);
vfp.params.append('comment', comment);
vfp.params.append('wid', wId);
vfp.params.append('towcallsign', towCallsign);
vfp.params.append('towpilotname', towPilotName);
vfp.params.append('towuidpilot', towUidPilot);
vfp.params.append('towtime', towTime);
vfp.params.append('towheight', towHeight);
vfp.params.append('offblock', offBlock);
vfp.params.append('onblock', onBlock);
vfp.params.append('motorstart', motorStart);
vfp.params.append('motorend', motorEnd);
var newflight;
await vfp.fetch().then(data => newflight = data).catch(e => console.log(e));
return newflight;
}
async editFlight(fligthId, { callsign = '', pilotName = '', uidPilot = 0, attendantName = "", uidAttendant = 0, attendantName2 = "", uidAttendant2 = 0, attendantName3 = "", uidAttendant3 = 0, startType = "E", departureTime = '', departureLocation = '', arrivalTime = '', arrivalLocation = '', landingCount = 1, ftId = 10, km = 0, chargeMode = 0, uidCharge = 0, comment = '', wId = 0, towCallsign = '', towPilotName = '', towUidPilot = 0, towTime = '', towHeight = 0, offBlock = '', onBlock = '', motorStart = 0.0, motorEnd = 0.0 }) {
var vfp = new VereinsfliegerPromise('interface/rest/flight/edit/' + fligthId)
vfp.method = "PUT"
vfp.params.append('accesstoken', this.accesstoken);
vfp.params.append('callsign', callsign);
vfp.params.append('pilotname', pilotName);
vfp.params.append('uidpilot', uidPilot);
vfp.params.append('attendantname', attendantName);
vfp.params.append('uidattendant', uidAttendant);
vfp.params.append('attendantname2', attendantName2);
vfp.params.append('uidattendant2', uidAttendant2);
vfp.params.append('attendantname3', attendantName3);
vfp.params.append('uidattendant3', uidAttendant3);
vfp.params.append('starttype', startType);
vfp.params.append('departuretime', departureTime);
vfp.params.append('departurelocation', departureLocation);
vfp.params.append('arrivaltime', arrivalTime);
vfp.params.append('arrivallocation', arrivalLocation);
vfp.params.append('landingcount', landingCount);
vfp.params.append('ftid', ftId);
vfp.params.append('chargemode', chargeMode);
vfp.params.append('uidcharge', uidCharge);
vfp.params.append('comment', comment);
vfp.params.append('towcallsign', towCallsign);
vfp.params.append('towpilotname', towPilotName);
vfp.params.append('towuidpilot', towUidPilot);
vfp.params.append('towtime', towTime);
vfp.params.append('towheight', towHeight);
vfp.params.append('offblock', offBlock);
vfp.params.append('onblock', onBlock);
vfp.params.append('motorstart', motorStart);
vfp.params.append('motorend', motorEnd);
var changedFlight;
await vfp.fetch().then(data => changedFlight = data).catch(e => console.log(e));
return changedFlight;
}
async deleteFlight( fligthId) {
var vfp = new VereinsfliegerPromise('interface/rest/flight/delete/' + fligthId)
vfp.params.append('accesstoken', this.accesstoken);
var result;
await vfp.fetch().then(r => result = r).catch(e => console.log(e));
return result;
}
async getFlight(accesstoken, fligthId) {
var vfp = new VereinsfliegerPromise('interface/rest/flight/get/' + fligthId)
vfp.params.append('accesstoken', accesstoken);
let flight;
await vfp.fetch().then(data => flight = data).catch(e => console.log(e));
return flight;
}
async getFlightListToday() {
var vfp = new VereinsfliegerPromise('interface/rest/flight/list/today');
vfp.params.append('accesstoken', this.accesstoken);
let flights;
await vfp.fetch().then(data => flights = data).catch(e => console.log(e));
return flights;
}
async getFlightListDate( dateParam) {
var vfp = new VereinsfliegerPromise('interface/rest/flight/list/date');
vfp.params.append('accesstoken', this.accesstoken);
vfp.params.append('dateparam', dateParam);
let flights;
await vfp.fetch().then(data => flights = data).catch(e => console.log(e));
return flights;
}
async getLastFlightsPlane( callsign, count) {
var vfp = new VereinsfliegerPromise('interface/rest/flight/list/plane');
vfp.params.append('accesstoken', this.accesstoken);
vfp.params.append('callsign', callsign);
vfp.params.append('count', count);
let flights;
await vfp.fetch().then(data => flights = data).catch(e => console.log(e));
return flights;
}
async getLastFlightsUser( count) {
var vfp = new VereinsfliegerPromise('interface/rest/flight/list/myflights');
vfp.params.append('accesstoken', this.accesstoken);
vfp.params.append('count', count);
let flights;
await vfp.fetch().then(data => flights = data).catch(e => console.log(e));
return flights;
}
async getLastFlightsPilot( uid, count) {
var vfp = new VereinsfliegerPromise('interface/rest/flight/list/user');
vfp.params.append('accesstoken',this.accesstoken);
vfp.params.append("uid", uid);
vfp.params.append('count', count);
let flights;
await vfp.fetch().then(data => flights = data).catch(e => console.log(e));
return flights;
}
async getLastModifiedFlights( days) {
var vfp = new VereinsfliegerPromise('interface/rest/flight/list/modified');
vfp.params.append('accesstoken', this.accesstoken);
vfp.params.append("days", days);
let flights;
await vfp.fetch().then(data => flights = data).catch(e => console.log(e));
return flights;
}
async getCalendarPublic(hpaccessCode) {
var vfp = new VereinsfliegerPromise('interface/rest/calendar/list/public');
//no login required, no accesstoken!!
vfp.params.append('hpaccesscode', hpaccessCode);
let appointments;
await vfp.fetch().then(data => appointments = data).catch(e => console.log(e));
return appointments;
}
async getCalendarUser() {
//warning: test of this function did not pass!!
var vfp = new VereinsfliegerPromise('interface/rest/calendar/list/mycalendar');
vfp.method = "GET";
vfp.params.append('accesstoken',token);
let appointments;
await vfp.fetch().then(data => appointments = data).catch(e => console.log(e));
return appointments;
}
async getPersonList(token=this.accesstoken) {
var vfp = new VereinsfliegerPromise('interface/rest/user/list');
vfp.params.append('accesstoken', token);
let persons;
return vfp.fetch();//.then(data => persons = data).catch(e => console.log(e));
//return persons;
}
async getReservationList() {
//warning: test of this function did not pass!!
var vfp = new VereinsfliegerPromise('interface/rest/reservation/list/actice');
vfp.params.append('accesstoken', this.accesstoken);
let rerservations;
await vfp.fetch().then(data => rerservations = data).catch(e => console.log(e));
return rerservations;
}
async getMaintenanceData( callSign) {
var vfp = new VereinsfliegerPromise('interface/rest/maintenance/airplane/' + callSign);
vfp.params.append('accesstoken', this.accesstoken);
let maintenanceData;
await vfp.fetch().then(data => maintenanceData = data).catch(e => console.log(e));
return maintenanceData;
}
async accountAddTransaction( bDate, value, salesTax, debitAccount, creditAccount, taxAccount, accountReference, accountReferenceId, bookingText) {
if (value <= 0)
return ("Betrag muss größer 0,00 sein");
var vfp = new VereinsfliegerPromise('interface/rest/account/add');
vfp.params.append('accesstoken', this.accesstoken);
vfp.params.append('bookingdate', bDate);
vfp.params.append('value', value);
vfp.params.append('salestax', salesTax);
vfp.params.append('debitaccount', debitAccount);
vfp.params.append('creditaccount', creditAccount);
vfp.params.append('taxaccount', taxAccount);
vfp.params.append('accountreference', accountReference);
vfp.params.append('accountreferenceid', accountReferenceId);
vfp.params.append('bookingtext', bookingText);
let newTransaction;
vfp.fetch().then(data => newTransaction = data).catch(e => console.log(e));
return newTransaction;
}
async getAccountTransaction(transactionId) {
var vfp = new VereinsfliegerPromise('interface/rest/account/get/' + transactionId);
vfp.params.append('accesstoken', this.accesstoken);
let transaction;
await vfp.fetch().then(data => transaction = data).catch(e => console.log(e));
return transaction;
}
async getAccountTransactionsToday() {
var vfp = new VereinsfliegerPromise('interface/rest/account/list/today');
vfp.params.append('accesstoken', this.accesstoken);
let transactions;
await vfp.fetch().then(data => transactions = data).catch(e => console.log(e));
return transactions;
}
async getAccountListYear(year) {
var vfp = new VereinsfliegerPromise('interface/rest/account/list/year');
vfp.params.append('accesstoken', this.accesstoken);
vfp.params.append('year', year);
let transactions;
await vfp.fetch().then(data => transactions = data).catch(e => console.log("Error in Routine getAccountListYear:" + e));
return transactions;
}
async getAccountTransactionsDaterange( dateFrom, dateTo) {
var vfp = new VereinsfliegerPromise('interface/rest/account/list/daterange');
vfp.params.append('accesstoken', this.accesstoken);
vfp.params.append('datefrom', dateFrom);
vfp.params.append('dateto', dateTo);
let transactions;
await vfp.fetch().then(data => transactions = data).catch(e => console.log(e));
return transactions;
}
async getWorkhoursDaterange( dateFrom, dateTo) {
var vfp = new VereinsfliegerPromise('interface/rest/workhours/list/daterange');
vfp.params.append('accesstoken', this.accesstoken);
vfp.params.append('datefrom', dateFrom);
vfp.params.append('dateto', dateTo);
let workhours;
await vfp.fetch().then(data => workhours = data).catch(e => console.log(e));
return workhours;
}
async workhoursAdd( uid, jobDate, jobText, hours, category, { timeFrom = '', timeTo = '', status = 0, comment = "" }) {
var vfp = new VereinsfliegerPromise('interface/rest/workhours/add');
vfp.params.append("accesstoken", this.accesstoken)
vfp.params.append('uid', uid);
vfp.params.append('jobdate', jobDate);
vfp.params.append('jobtext', jobText);
vfp.params.append('hours', hours);
vfp.params.append('category', category);
vfp.params.append('timefrom', timeFrom);
vfp.params.append('timeto', timeTo);
vfp.params.append('status', status);
vfp.params.append('comment', comment);
let newTransaction;
vfp.fetch().then(data => newTransaction = data).catch(e => console.log(e));
return newTransaction;
}
async getWorkhoursCategories() {
var vfp = new VereinsfliegerPromise('interface/rest/workhourcategories/list');
vfp.params.append("accesstoken", this.accesstoken)
let categories;
vfp.fetch().then(data => categories = data).catch(e => console.log(e));
return categories;
}
async getArticles() {
var vfp = new VereinsfliegerPromise('interface/rest/articles/list');
vfp.params.append("accesstoken", this.accesstoken)
let articles;
vfp.fetch().then(data => articles = data).catch(e => console.log(e));
return articles;
}
async addSale( bookingDate, articleId, { ammount = 0.0, memberId = 0, callsign = '', salesTax = 0.0, totalPrice = 0.0, counter = 0.0, comment = '', ccId = '' }) {
var vfp = new VereinsfliegerPromise('interface/rest/sale/add');
vfp.params.append("accesstoken", this.accesstoken)
vfp.params.append('bookingdate', bookingDate);
vfp.params.append('articleid', articleId);
vfp.params.append('amount', ammount);
vfp.params.append('memberid', memberId); //buyers memberId!
vfp.params.append('callsign', callsign);
vfp.params.append('salestax', salesTax);
vfp.params.append('totalprice', totalPrice);
vfp.params.append('counter', counter);
vfp.params.append('comment', comment);
vfp.params.append('ccid', ccId);
let sale;
vfp.fetch().then(data => sale = data).catch(e => console.log(e));
return sale;
}
}
class VereinsfliegerPromise {
constructor(resturi, method = "POST") {
this.Host = "https://vereinsflieger.de/";
this.params = new URLSearchParams();
this.resturi = resturi;
this.method = method;
}
fetch() {
//method 'GET' is not allowed to have a body
return new Promise((resolve, reject) => {
const that=this;
if (this.method == "GET")
fetch(this.Host + this.resturi)
.then(r => r.json())
.then(function (data) {
if (data.httpstatuscode == 200)
{
// data is enriched by accesstoken, if accesstoken is present
if(that.params.has("accesstoken"))
data.accesstoken=that.params.get("accesstoken");
resolve(data);
}
else
reject(data);
})
else //all other methods need a body
fetch(this.Host + this.resturi, { method: this.method, body: this.params })
.then(r => r.json())
.then(function (data) {
if (data.httpstatuscode == 200){
// data is enriched by accesstoken, if accesstoken is present
if(that.params.has("accesstoken"))
data.accesstoken=that.params.get("accesstoken");
resolve(data);
}
else
reject(data);
})
})
}
}
module.exports = { VereinsfliegerAPI }