1
|
import * as HttpStatus from './HttpStatus';
|
2
|
|
3
|
/**
|
4
|
* Log Out
|
5
|
*/
|
6
|
export async function logOut() {
|
7
|
let response;
|
8
|
|
9
|
try {
|
10
|
response = await fetch(`${window.config.baseUrl}/logout`, {
|
11
|
credentials: 'include',
|
12
|
});
|
13
|
} catch (e) {
|
14
|
throw new Error('Server is not available');
|
15
|
}
|
16
|
|
17
|
if (response.ok) {
|
18
|
window.location.replace('/logout');
|
19
|
} else {
|
20
|
switch (response.status) {
|
21
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
22
|
throw new Error('Internal server error.');
|
23
|
default:
|
24
|
throw new Error(response.statusText);
|
25
|
}
|
26
|
}
|
27
|
}
|
28
|
|
29
|
/**
|
30
|
* GET DATA APP getCurrentProfile
|
31
|
*/
|
32
|
export async function getCurrentProfile() {
|
33
|
let response;
|
34
|
|
35
|
try {
|
36
|
response = await fetch(`${window.config.baseUrl}/users/current/profile`, {
|
37
|
credentials: 'include',
|
38
|
});
|
39
|
} catch (e) {
|
40
|
throw new Error('Server is not available');
|
41
|
}
|
42
|
|
43
|
if (response.ok) {
|
44
|
const data = await response.json();
|
45
|
|
46
|
return {
|
47
|
name: data.firstName + ' ' + data.lastName,
|
48
|
role: data.role,
|
49
|
id: data.id,
|
50
|
holiday: data.vacationCount,
|
51
|
sickday: data.sickDayCount,
|
52
|
photo: data.photo,
|
53
|
takenSickday: data.takenSickDayCount,
|
54
|
};
|
55
|
} else {
|
56
|
switch (response.status) {
|
57
|
case HttpStatus.UNAUTHORIZED:
|
58
|
window.location.replace('/login');
|
59
|
break;
|
60
|
case HttpStatus.FORBIDDEN:
|
61
|
window.location.replace('/login');
|
62
|
break;
|
63
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
64
|
throw new Error('Internal server error.');
|
65
|
default:
|
66
|
throw new Error(response.statusText);
|
67
|
}
|
68
|
}
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* LOAD DATA to CALENDAR - EMPLOYEE
|
73
|
*/
|
74
|
export async function getUserCalendar(currentUser, fromDate ) {
|
75
|
let response;
|
76
|
|
77
|
try {
|
78
|
response = await fetch(`${window.config.baseUrl}/user/${currentUser.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
|
79
|
headers: {
|
80
|
'Accept': 'application/json',
|
81
|
},
|
82
|
credentials: 'include',
|
83
|
method: 'GET',
|
84
|
});
|
85
|
} catch (e) {
|
86
|
throw new Error('Server is not available');
|
87
|
}
|
88
|
|
89
|
if (response.ok) {
|
90
|
const data = await response.json();
|
91
|
|
92
|
return data.filter(day => {
|
93
|
return day.status !== 'PENDING';
|
94
|
}).map(day => {
|
95
|
const newDate = day.date.split('/').join('-');
|
96
|
|
97
|
return ({
|
98
|
title: currentUser.name,
|
99
|
start: newDate,
|
100
|
backgroundColor: day.status === 'REJECTED' ? 'red' : 'green',
|
101
|
});
|
102
|
});
|
103
|
} else {
|
104
|
switch (response.status) {
|
105
|
case HttpStatus.BAD_REQUEST:
|
106
|
throw new Error('Bad request. Check query parameters.');
|
107
|
case HttpStatus.UNAUTHORIZED:
|
108
|
window.location.replace('/login');
|
109
|
break;
|
110
|
case HttpStatus.FORBIDDEN:
|
111
|
window.location.replace('/login');
|
112
|
break;
|
113
|
case HttpStatus.NOT_FOUND:
|
114
|
throw new Error('User with given ID does not exist.');
|
115
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
116
|
throw new Error('Internal server error.');
|
117
|
default:
|
118
|
throw new Error(response.statusText);
|
119
|
}
|
120
|
}
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* LOAD DATA to CALENDAR - EMPLOYER
|
125
|
*/
|
126
|
export async function getAdminCalendar() {
|
127
|
let response;
|
128
|
|
129
|
try {
|
130
|
response = await fetch(`${window.config.baseUrl}/users/requests/vacation?status=ACCEPTED`, {
|
131
|
headers: {
|
132
|
'Accept': 'application/json',
|
133
|
},
|
134
|
credentials: 'include',
|
135
|
method: 'GET',
|
136
|
});
|
137
|
} catch (e) {
|
138
|
throw new Error('Server is not available');
|
139
|
}
|
140
|
|
141
|
if (response.ok) {
|
142
|
const data = await response.json();
|
143
|
|
144
|
return data.map(day => {
|
145
|
|
146
|
const newDate = day.date.split('/').join('-');
|
147
|
|
148
|
return {
|
149
|
title: day.firstName + ' ' + day.lastName,
|
150
|
start: newDate,
|
151
|
};
|
152
|
});
|
153
|
} else {
|
154
|
switch (response.status) {
|
155
|
case HttpStatus.BAD_REQUEST:
|
156
|
throw new Error('Bad request. Check query parameters.');
|
157
|
case HttpStatus.UNAUTHORIZED:
|
158
|
window.location.replace('/login');
|
159
|
break;
|
160
|
case HttpStatus.FORBIDDEN:
|
161
|
window.location.replace('/login');
|
162
|
break;
|
163
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
164
|
throw new Error('Internal server error.');
|
165
|
default:
|
166
|
throw new Error(response.statusText);
|
167
|
}
|
168
|
}
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* ADD EVENT to CALENDAR - EMPLOYEE
|
173
|
*/
|
174
|
export async function addEventApi(dataAddEventEmployee) {
|
175
|
let response;
|
176
|
|
177
|
try {
|
178
|
// send accepted request to server
|
179
|
response = await fetch(`${window.config.baseUrl}/user/calendar/create`, {
|
180
|
headers: {
|
181
|
'Content-Type': 'application/json',
|
182
|
},
|
183
|
credentials: 'include',
|
184
|
method: 'POST',
|
185
|
// object which is sent to server
|
186
|
body: JSON.stringify(dataAddEventEmployee),
|
187
|
});
|
188
|
} catch (e) {
|
189
|
throw new Error('Server is not available');
|
190
|
}
|
191
|
|
192
|
if (!response.ok) {
|
193
|
switch (response.status) {
|
194
|
case HttpStatus.BAD_REQUEST:
|
195
|
throw new Error('Bad request. Check request body.');
|
196
|
case HttpStatus.UNAUTHORIZED:
|
197
|
window.location.replace('/login');
|
198
|
break;
|
199
|
case HttpStatus.FORBIDDEN:
|
200
|
window.location.replace('/login');
|
201
|
break;
|
202
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
203
|
throw new Error('Internal server error.');
|
204
|
default:
|
205
|
throw new Error(response.statusText);
|
206
|
}
|
207
|
}
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* ADD EVENT to CALENDAR - EMPLOYER
|
212
|
*/
|
213
|
export async function addEventApiAdmin(dataAddEventAdmin) {
|
214
|
let response;
|
215
|
|
216
|
try {
|
217
|
// send accepted request to server
|
218
|
response = await fetch(`${window.config.baseUrl}/user/calendar/create`, {
|
219
|
headers: {
|
220
|
'Content-Type': 'application/json',
|
221
|
},
|
222
|
credentials: 'include',
|
223
|
method: 'POST',
|
224
|
// object which is sent to server
|
225
|
body: JSON.stringify(dataAddEventAdmin),
|
226
|
});
|
227
|
} catch (e) {
|
228
|
throw new Error('Server is not available');
|
229
|
}
|
230
|
|
231
|
if (!response.ok) {
|
232
|
switch (response.status) {
|
233
|
case HttpStatus.BAD_REQUEST:
|
234
|
throw new Error('Bad request. Check request body.');
|
235
|
case HttpStatus.UNAUTHORIZED:
|
236
|
window.location.replace('/login');
|
237
|
break;
|
238
|
case HttpStatus.FORBIDDEN:
|
239
|
window.location.replace('/login');
|
240
|
break;
|
241
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
242
|
throw new Error('Internal server error.');
|
243
|
default:
|
244
|
throw new Error(response.statusText);
|
245
|
}
|
246
|
}
|
247
|
}
|
248
|
|
249
|
/**
|
250
|
* GET DATA to OVERVIEW - EMPLOYER
|
251
|
*/
|
252
|
export async function getUsersOverview() {
|
253
|
let response;
|
254
|
|
255
|
try {
|
256
|
response = await fetch (`${window.config.baseUrl}/users`, {
|
257
|
credentials: 'include',
|
258
|
});
|
259
|
} catch (e) {
|
260
|
throw new Error('Server is not available');
|
261
|
}
|
262
|
|
263
|
if (response.ok) {
|
264
|
const data = await response.json();
|
265
|
return data.map(user => {
|
266
|
return {
|
267
|
name: user.firstName + ' ' + user.lastName,
|
268
|
id: user.id,
|
269
|
sickday: user.sickDayCount,
|
270
|
holiday: user.vacationCount,
|
271
|
takenSickday: user.takenSickDayCount,
|
272
|
role: user.role,
|
273
|
};
|
274
|
});
|
275
|
} else {
|
276
|
switch (response.status) {
|
277
|
case HttpStatus.BAD_REQUEST:
|
278
|
throw new Error('Bad request. Check query parameters.');
|
279
|
case HttpStatus.UNAUTHORIZED:
|
280
|
window.location.replace('/login');
|
281
|
break;
|
282
|
case HttpStatus.FORBIDDEN:
|
283
|
window.location.replace('/login');
|
284
|
break;
|
285
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
286
|
throw new Error('Internal server error.');
|
287
|
default:
|
288
|
throw new Error(response.statusText);
|
289
|
}
|
290
|
}
|
291
|
}
|
292
|
|
293
|
/**
|
294
|
* SAVE DATA to OVERVIEW - EMPLOYER
|
295
|
*/
|
296
|
export async function saveDataOverview(dataOverviewObject) {
|
297
|
let response;
|
298
|
|
299
|
try {
|
300
|
// send accepted request to server
|
301
|
response = await fetch(`${window.config.baseUrl}/user/settings`, {
|
302
|
headers: {
|
303
|
'Content-Type': 'application/json',
|
304
|
},
|
305
|
credentials: 'include',
|
306
|
method: 'PUT',
|
307
|
// object which is sent to server
|
308
|
body: JSON.stringify(dataOverviewObject),
|
309
|
});
|
310
|
} catch (e) {
|
311
|
throw new Error('Server is not available');
|
312
|
}
|
313
|
|
314
|
if (!response.ok) {
|
315
|
switch (response.status) {
|
316
|
case HttpStatus.BAD_REQUEST:
|
317
|
throw new Error('Bad request. Check query parameters.');
|
318
|
case HttpStatus.UNAUTHORIZED:
|
319
|
window.location.replace('/login');
|
320
|
break;
|
321
|
case HttpStatus.FORBIDDEN:
|
322
|
window.location.replace('/login');
|
323
|
break;
|
324
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
325
|
throw new Error('Internal server error.');
|
326
|
default:
|
327
|
throw new Error(response.statusText);
|
328
|
}
|
329
|
}
|
330
|
}
|
331
|
|
332
|
/**
|
333
|
* LOAD DATA to SETTING - EMPLOYER
|
334
|
*/
|
335
|
export async function getSettingData() {
|
336
|
let response;
|
337
|
|
338
|
try {
|
339
|
response = await fetch(`${window.config.baseUrl}/settings`, {
|
340
|
credentials: 'include',
|
341
|
});
|
342
|
} catch (e) {
|
343
|
throw new Error('Server is not available');
|
344
|
}
|
345
|
|
346
|
if (response.ok) {
|
347
|
const data = await response.json();
|
348
|
return {
|
349
|
sickday: data.sickDayCount,
|
350
|
};
|
351
|
} else {
|
352
|
switch (response.status) {
|
353
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
354
|
throw new Error('Internal server error.');
|
355
|
default:
|
356
|
throw new Error(response.statusText);
|
357
|
}
|
358
|
}
|
359
|
}
|
360
|
|
361
|
/**
|
362
|
* SAVE DATA to SETTING - EMPLOYER
|
363
|
*/
|
364
|
export async function saveDataSetting(dataSettingObject) {
|
365
|
let response;
|
366
|
|
367
|
try {
|
368
|
response = await fetch(`${window.config.baseUrl}/settings`, {
|
369
|
headers: {
|
370
|
'Content-Type': 'application/json',
|
371
|
},
|
372
|
credentials: 'include',
|
373
|
method: 'POST',
|
374
|
body: JSON.stringify(dataSettingObject),
|
375
|
});
|
376
|
} catch (e) {
|
377
|
throw new Error('Server is not available');
|
378
|
}
|
379
|
|
380
|
if (!response.ok) {
|
381
|
switch (response.status) {
|
382
|
case HttpStatus.UNAUTHORIZED:
|
383
|
window.location.replace('/login');
|
384
|
break;
|
385
|
case HttpStatus.FORBIDDEN:
|
386
|
window.location.replace('/login');
|
387
|
break;
|
388
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
389
|
throw new Error('Internal server error.');
|
390
|
default:
|
391
|
throw new Error(response.statusText);
|
392
|
}
|
393
|
}
|
394
|
}
|
395
|
|
396
|
/**
|
397
|
* LOAD DATA to YOUR REQUESTS - EMPLOYEE
|
398
|
*/
|
399
|
export async function loadYourRequests(currentUser) {
|
400
|
let response;
|
401
|
|
402
|
try {
|
403
|
response = await fetch(`${window.config.baseUrl}/user/${currentUser.id}/calendar?from=2020/06/24&status=PENDING`, {
|
404
|
credentials: 'include',
|
405
|
});
|
406
|
} catch (e) {
|
407
|
throw new Error('Server is not available');
|
408
|
}
|
409
|
|
410
|
if (response.ok) {
|
411
|
const data = await response.json();
|
412
|
return data;
|
413
|
} else {
|
414
|
switch (response.status) {
|
415
|
case HttpStatus.BAD_REQUEST:
|
416
|
throw new Error('Bad request. Check query parameters.');
|
417
|
case HttpStatus.UNAUTHORIZED:
|
418
|
window.location.replace('/login');
|
419
|
break;
|
420
|
case HttpStatus.FORBIDDEN:
|
421
|
window.location.replace('/login');
|
422
|
break;
|
423
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
424
|
throw new Error('Internal server error.');
|
425
|
default:
|
426
|
throw new Error(response.statusText);
|
427
|
}
|
428
|
}
|
429
|
}
|
430
|
|
431
|
/**
|
432
|
* LOAD DATA - UPCOMING REQUESTS - EMPLOYER
|
433
|
*/
|
434
|
export async function loadAdminRequests() {
|
435
|
let response;
|
436
|
|
437
|
try {
|
438
|
response = await fetch(`${window.config.baseUrl}/users/requests/vacation?status=PENDING`, {
|
439
|
credentials: 'include',
|
440
|
});
|
441
|
} catch (e) {
|
442
|
throw new Error('Server is not available');
|
443
|
}
|
444
|
|
445
|
if (response.ok) {
|
446
|
return response.json();
|
447
|
} else {
|
448
|
switch (response.status) {
|
449
|
case HttpStatus.BAD_REQUEST:
|
450
|
throw new Error('Bad request. Check query parameters.');
|
451
|
case HttpStatus.UNAUTHORIZED:
|
452
|
window.location.replace('/login');
|
453
|
break;
|
454
|
case HttpStatus.FORBIDDEN:
|
455
|
window.location.replace('/login');
|
456
|
break;
|
457
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
458
|
throw new Error('Internal server error.');
|
459
|
default:
|
460
|
throw new Error(response.statusText);
|
461
|
}
|
462
|
}
|
463
|
}
|
464
|
|
465
|
/**
|
466
|
* SEND ACCEPTED DATA - UPCOMING REQUESTS - EMPLOYER
|
467
|
*/
|
468
|
export async function sendAcceptedRequest(acceptedRequests) {
|
469
|
let response;
|
470
|
|
471
|
try {
|
472
|
response = await fetch(`${window.config.baseUrl}/user/requests?type=VACATION`, {
|
473
|
headers: {
|
474
|
'Content-Type': 'application/json',
|
475
|
},
|
476
|
credentials: 'include',
|
477
|
method: 'PUT',
|
478
|
body: JSON.stringify(acceptedRequests),
|
479
|
});
|
480
|
} catch (e) {
|
481
|
throw new Error('Server is not available');
|
482
|
}
|
483
|
|
484
|
if (!response.ok) {
|
485
|
switch (response.status) {
|
486
|
case HttpStatus.BAD_REQUEST:
|
487
|
throw new Error('Bad request. Check query parameters and request body.');
|
488
|
case HttpStatus.UNAUTHORIZED:
|
489
|
window.location.replace('/login');
|
490
|
break;
|
491
|
case HttpStatus.FORBIDDEN:
|
492
|
window.location.replace('/login');
|
493
|
break;
|
494
|
case HttpStatus.NOT_FOUND:
|
495
|
throw new Error('Neither vacation nor authorization request with given ID exists.');
|
496
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
497
|
throw new Error('Internal server error.');
|
498
|
default:
|
499
|
throw new Error(response.statusText);
|
500
|
}
|
501
|
}
|
502
|
}
|
503
|
|
504
|
/**
|
505
|
* SEND REJECTED DATA - UPCOMING REQUESTS - EMPLOYER
|
506
|
*/
|
507
|
export async function sendRejectedRequest(rejectedRequest) {
|
508
|
let response;
|
509
|
|
510
|
try {
|
511
|
response = await fetch(`${window.config.baseUrl}/user/requests?type=VACATION`, {
|
512
|
headers: {
|
513
|
'Content-Type': 'application/json',
|
514
|
},
|
515
|
credentials: 'include',
|
516
|
method: 'PUT',
|
517
|
body: JSON.stringify(rejectedRequest),
|
518
|
});
|
519
|
} catch (e) {
|
520
|
throw new Error('Server is not available');
|
521
|
}
|
522
|
|
523
|
if (!response.ok) {
|
524
|
switch (response.status) {
|
525
|
case HttpStatus.BAD_REQUEST:
|
526
|
throw new Error('Bad request. Check query parameters and request body.');
|
527
|
case HttpStatus.UNAUTHORIZED:
|
528
|
window.location.replace('/login');
|
529
|
break;
|
530
|
case HttpStatus.FORBIDDEN:
|
531
|
window.location.replace('/login');
|
532
|
break;
|
533
|
case HttpStatus.NOT_FOUND:
|
534
|
throw new Error('Neither vacation nor authorization request with given ID exists.');
|
535
|
case HttpStatus.INTERNAL_SERVER_ERROR:
|
536
|
throw new Error('Internal server error.');
|
537
|
default:
|
538
|
throw new Error(response.statusText);
|
539
|
}
|
540
|
}
|
541
|
}
|