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