Projekt

Obecné

Profil

Stáhnout (13.6 KB) Statistiky
| Větev: | Tag: | Revize:
1 1865a0be Pavel Fidransky
import * as HttpStatus from './HttpStatus';
2 a12a4e99 plundrichov
3 1865a0be Pavel Fidransky
/**
4
 * Log Out
5
 */
6
export async function logOut() {
7 a12a4e99 plundrichov
  let response;
8
9
  try {
10 1865a0be Pavel Fidransky
    response = await fetch(`${window.config.baseUrl}/logout`, {
11
      credentials: 'include',
12
    });
13 a12a4e99 plundrichov
  } catch (e) {
14 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
15
  }
16 a12a4e99 plundrichov
17
  if (response.ok) {
18 1865a0be Pavel Fidransky
    window.location.replace('/logout');
19 a12a4e99 plundrichov
  } else {
20 1865a0be Pavel Fidransky
    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 a12a4e99 plundrichov
    }
26 1865a0be Pavel Fidransky
  }
27 a12a4e99 plundrichov
}
28
29 1865a0be Pavel Fidransky
/**
30
 * GET DATA APP getCurrentProfile
31
 */
32
export async function getCurrentProfile() {
33 ebfe6347 plundrichov
  let response;
34
35
  try {
36 1865a0be Pavel Fidransky
    response = await fetch(`${window.config.baseUrl}/users/current/profile`, {
37
      credentials: 'include',
38
    });
39 ebfe6347 plundrichov
  } catch (e) {
40 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
41
  }
42 7495b9eb plundrichov
43 ebfe6347 plundrichov
  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 5bedee9e plundrichov
      photo: data.photo,
53 1865a0be Pavel Fidransky
      takenSickday: data.takenSickDayCount,
54
    };
55 ebfe6347 plundrichov
  } else {
56 1865a0be Pavel Fidransky
    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 7495b9eb plundrichov
    }
68 1865a0be Pavel Fidransky
  }
69 7495b9eb plundrichov
}
70
71 1865a0be Pavel Fidransky
/**
72
 * LOAD DATA to CALENDAR - EMPLOYEE
73
 */
74
export async function getUserCalendar(currentUser, fromDate ) {
75 ebfe6347 plundrichov
  let response;
76
77 7495b9eb plundrichov
  try {
78 1865a0be Pavel Fidransky
    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 ebfe6347 plundrichov
  } catch (e) {
86 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
87 ebfe6347 plundrichov
  }
88 7495b9eb plundrichov
89
  if (response.ok) {
90 ebfe6347 plundrichov
    const data = await response.json();
91 1865a0be Pavel Fidransky
92 ebfe6347 plundrichov
    return data.filter(day => {
93 1865a0be Pavel Fidransky
      return day.status !== 'PENDING';
94 ebfe6347 plundrichov
    }).map(day => {
95 1865a0be Pavel Fidransky
      const newDate = day.date.split('/').join('-');
96 7495b9eb plundrichov
97 1865a0be Pavel Fidransky
      return ({
98
        title: currentUser.name,
99
        start: newDate,
100
        backgroundColor: day.status === 'REJECTED' ? 'red' : 'green',
101
      });
102
    });
103 ebfe6347 plundrichov
  } else {
104 1865a0be Pavel Fidransky
    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 7495b9eb plundrichov
    }
120 1865a0be Pavel Fidransky
  }
121 7495b9eb plundrichov
}
122
123 1865a0be Pavel Fidransky
/**
124
 * LOAD DATA to CALENDAR - EMPLOYER
125
 */
126
export async function getAdminCalendar() {
127 ebfe6347 plundrichov
  let response;
128
129
  try {
130 1865a0be Pavel Fidransky
    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 ebfe6347 plundrichov
  } catch (e) {
138 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
139 ebfe6347 plundrichov
  }
140 7495b9eb plundrichov
141 ebfe6347 plundrichov
  if (response.ok) {
142 7495b9eb plundrichov
    const data = await response.json();
143 1865a0be Pavel Fidransky
144 7495b9eb plundrichov
    return data.map(day => {
145
146 1865a0be Pavel Fidransky
      const newDate = day.date.split('/').join('-');
147 7495b9eb plundrichov
148 1865a0be Pavel Fidransky
      return {
149
        title: day.firstName + ' ' + day.lastName,
150
        start: newDate,
151
      };
152
    });
153 7495b9eb plundrichov
  } else {
154 1865a0be Pavel Fidransky
    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 9f045397 plundrichov
    }
168 1865a0be Pavel Fidransky
  }
169 7495b9eb plundrichov
}
170
171 1865a0be Pavel Fidransky
/**
172
 * ADD EVENT to CALENDAR - EMPLOYEE
173
 */
174 5bedee9e plundrichov
export async function addEventApi(dataAddEventEmployee) {
175 ebfe6347 plundrichov
  let response;
176
177
  try {
178 7495b9eb plundrichov
  // send accepted request to server
179 faed7f5e plundrichov
    response = await fetch(`${window.config.baseUrl}/user/calendar/create`, {
180 9f045397 plundrichov
      headers: {
181
        'Content-Type': 'application/json',
182
      },
183 faed7f5e plundrichov
      credentials: 'include',
184 9f045397 plundrichov
      method: 'POST',
185 1865a0be Pavel Fidransky
      // object which is sent to server
186
      body: JSON.stringify(dataAddEventEmployee),
187 9f045397 plundrichov
    });
188 ebfe6347 plundrichov
  } catch (e) {
189 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
190 ebfe6347 plundrichov
  }
191
192 5bedee9e plundrichov
  if (!response.ok) {
193 1865a0be Pavel Fidransky
    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 9f045397 plundrichov
    }
207 1865a0be Pavel Fidransky
  }
208 9f045397 plundrichov
}
209 ebfe6347 plundrichov
210 1865a0be Pavel Fidransky
/**
211
 * ADD EVENT to CALENDAR - EMPLOYER
212
 */
213 9f045397 plundrichov
export async function addEventApiAdmin(dataAddEventAdmin) {
214 ebfe6347 plundrichov
  let response;
215
216 9f045397 plundrichov
  try {
217 1865a0be Pavel Fidransky
    // send accepted request to server
218 faed7f5e plundrichov
    response = await fetch(`${window.config.baseUrl}/user/calendar/create`, {
219 ebfe6347 plundrichov
      headers: {
220
        'Content-Type': 'application/json',
221 1865a0be Pavel Fidransky
      },
222
      credentials: 'include',
223 ebfe6347 plundrichov
      method: 'POST',
224 1865a0be Pavel Fidransky
      // object which is sent to server
225 ebfe6347 plundrichov
      body: JSON.stringify(dataAddEventAdmin),
226
    });
227 9f045397 plundrichov
  } catch (e) {
228 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
229 ebfe6347 plundrichov
  }
230
231
  if (!response.ok) {
232
    switch (response.status) {
233 1865a0be Pavel Fidransky
      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 ebfe6347 plundrichov
      default:
244 1865a0be Pavel Fidransky
        throw new Error(response.statusText);
245
    }
246 ebfe6347 plundrichov
  }
247 9f045397 plundrichov
}
248 7495b9eb plundrichov
249 1865a0be Pavel Fidransky
/**
250
 * GET DATA to OVERVIEW - EMPLOYER
251
 */
252
export async function getUsersOverview() {
253 ebfe6347 plundrichov
  let response;
254
255 9f045397 plundrichov
  try {
256 1865a0be Pavel Fidransky
    response = await fetch (`${window.config.baseUrl}/users`, {
257 faed7f5e plundrichov
      credentials: 'include',
258 1865a0be Pavel Fidransky
    });
259 ebfe6347 plundrichov
  } catch (e) {
260 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
261 ebfe6347 plundrichov
  }
262 9f045397 plundrichov
263 1865a0be Pavel Fidransky
  if (response.ok) {
264 ebfe6347 plundrichov
    const data = await response.json();
265
    return data.map(user => {
266 1865a0be Pavel Fidransky
      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 ebfe6347 plundrichov
  } else {
276
    switch (response.status) {
277 1865a0be Pavel Fidransky
      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 ebfe6347 plundrichov
      default:
288 1865a0be Pavel Fidransky
        throw new Error(response.statusText);
289
    }
290 9f045397 plundrichov
  }
291
}
292 7495b9eb plundrichov
293 1865a0be Pavel Fidransky
/**
294
 * SAVE DATA to OVERVIEW - EMPLOYER
295
 */
296 9f045397 plundrichov
export async function saveDataOverview(dataOverviewObject) {
297 ebfe6347 plundrichov
  let response;
298
299 9f045397 plundrichov
  try {
300
    // send accepted request to server
301 1865a0be Pavel Fidransky
    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 9f045397 plundrichov
  } catch (e) {
311 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
312 9f045397 plundrichov
  }
313 ebfe6347 plundrichov
314
  if (!response.ok) {
315
    switch (response.status) {
316 1865a0be Pavel Fidransky
      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 ebfe6347 plundrichov
      default:
327 1865a0be Pavel Fidransky
        throw new Error(response.statusText);
328 ebfe6347 plundrichov
    }
329 1865a0be Pavel Fidransky
  }
330 9f045397 plundrichov
}
331
332 1865a0be Pavel Fidransky
/**
333
 * LOAD DATA to SETTING - EMPLOYER
334
 */
335
export async function getSettingData() {
336 ebfe6347 plundrichov
  let response;
337
338 9f045397 plundrichov
  try {
339 1865a0be Pavel Fidransky
    response = await fetch(`${window.config.baseUrl}/settings`, {
340
      credentials: 'include',
341
    });
342 9f045397 plundrichov
  } catch (e) {
343 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
344
  }
345 ebfe6347 plundrichov
346
  if (response.ok) {
347
    const data = await response.json();
348
    return {
349
      sickday: data.sickDayCount,
350 1865a0be Pavel Fidransky
    };
351 ebfe6347 plundrichov
  } else {
352
    switch (response.status) {
353 1865a0be Pavel Fidransky
      case HttpStatus.INTERNAL_SERVER_ERROR:
354
        throw new Error('Internal server error.');
355 ebfe6347 plundrichov
      default:
356 1865a0be Pavel Fidransky
        throw new Error(response.statusText);
357 9f045397 plundrichov
    }
358 1865a0be Pavel Fidransky
  }
359 9f045397 plundrichov
}
360
361 1865a0be Pavel Fidransky
/**
362
 * SAVE DATA to SETTING - EMPLOYER
363
 */
364 9f045397 plundrichov
export async function saveDataSetting(dataSettingObject) {
365 ebfe6347 plundrichov
  let response;
366
367 9f045397 plundrichov
  try {
368 faed7f5e plundrichov
    response = await fetch(`${window.config.baseUrl}/settings`, {
369 9f045397 plundrichov
      headers: {
370 1865a0be Pavel Fidransky
        'Content-Type': 'application/json',
371 9f045397 plundrichov
      },
372 faed7f5e plundrichov
      credentials: 'include',
373 9f045397 plundrichov
      method: 'POST',
374
      body: JSON.stringify(dataSettingObject),
375
    });
376 ebfe6347 plundrichov
  } catch (e) {
377 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
378 ebfe6347 plundrichov
  }
379 9f045397 plundrichov
380 ebfe6347 plundrichov
  if (!response.ok) {
381 9f045397 plundrichov
    switch (response.status) {
382 1865a0be Pavel Fidransky
      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 9f045397 plundrichov
      default:
391 1865a0be Pavel Fidransky
        throw new Error(response.statusText);
392
    }
393 ebfe6347 plundrichov
  }
394 9f045397 plundrichov
}
395
396 1865a0be Pavel Fidransky
/**
397
 * LOAD DATA to YOUR REQUESTS - EMPLOYEE
398
 */
399 5bedee9e plundrichov
export async function loadYourRequests(currentUser) {
400 ebfe6347 plundrichov
  let response;
401 1865a0be Pavel Fidransky
402 9f045397 plundrichov
  try {
403 1865a0be Pavel Fidransky
    response = await fetch(`${window.config.baseUrl}/user/${currentUser.id}/calendar?from=2020/06/24&status=PENDING`, {
404
      credentials: 'include',
405
    });
406 ebfe6347 plundrichov
  } catch (e) {
407 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
408 ebfe6347 plundrichov
  }
409 9f045397 plundrichov
410
  if (response.ok) {
411
    const data = await response.json();
412
    return data;
413
  } else {
414 1865a0be Pavel Fidransky
    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 ebfe6347 plundrichov
    }
428 1865a0be Pavel Fidransky
  }
429 9f045397 plundrichov
}
430
431 1865a0be Pavel Fidransky
/**
432
 * LOAD DATA - UPCOMING REQUESTS - EMPLOYER
433
 */
434 9f045397 plundrichov
export async function loadAdminRequests() {
435 ebfe6347 plundrichov
  let response;
436
437 9f045397 plundrichov
  try {
438 1865a0be Pavel Fidransky
    response = await fetch(`${window.config.baseUrl}/users/requests/vacation?status=PENDING`, {
439
      credentials: 'include',
440
    });
441 ebfe6347 plundrichov
  } catch (e) {
442 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
443
  }
444
445 ebfe6347 plundrichov
  if (response.ok) {
446 1865a0be Pavel Fidransky
    return response.json();
447 9f045397 plundrichov
  } else {
448 1865a0be Pavel Fidransky
    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 7495b9eb plundrichov
    }
462 1865a0be Pavel Fidransky
  }
463 9f045397 plundrichov
}
464
465 1865a0be Pavel Fidransky
/**
466
 * SEND ACCEPTED DATA - UPCOMING REQUESTS - EMPLOYER
467
 */
468 9f045397 plundrichov
export async function sendAcceptedRequest(acceptedRequests) {
469 ebfe6347 plundrichov
  let response;
470
471 9f045397 plundrichov
  try {
472 faed7f5e plundrichov
    response = await fetch(`${window.config.baseUrl}/user/requests?type=VACATION`, {
473 9f045397 plundrichov
      headers: {
474
        'Content-Type': 'application/json',
475
      },
476 faed7f5e plundrichov
      credentials: 'include',
477 9f045397 plundrichov
      method: 'PUT',
478
      body: JSON.stringify(acceptedRequests),
479
    });
480
  } catch (e) {
481 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
482
  }
483 ebfe6347 plundrichov
484
  if (!response.ok) {
485
    switch (response.status) {
486 1865a0be Pavel Fidransky
      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 ebfe6347 plundrichov
      default:
499 1865a0be Pavel Fidransky
        throw new Error(response.statusText);
500
    }
501 ebfe6347 plundrichov
  }
502 9f045397 plundrichov
}
503
504 1865a0be Pavel Fidransky
/**
505
 * SEND REJECTED DATA - UPCOMING REQUESTS - EMPLOYER
506
 */
507 9f045397 plundrichov
export async function sendRejectedRequest(rejectedRequest) {
508 ebfe6347 plundrichov
  let response;
509 1865a0be Pavel Fidransky
510 9f045397 plundrichov
  try {
511 faed7f5e plundrichov
    response = await fetch(`${window.config.baseUrl}/user/requests?type=VACATION`, {
512 9f045397 plundrichov
      headers: {
513
        'Content-Type': 'application/json',
514
      },
515 faed7f5e plundrichov
      credentials: 'include',
516 9f045397 plundrichov
      method: 'PUT',
517
      body: JSON.stringify(rejectedRequest),
518
    });
519 ebfe6347 plundrichov
  } catch (e) {
520 1865a0be Pavel Fidransky
    throw new Error('Server is not available');
521
  }
522
523 ebfe6347 plundrichov
  if (!response.ok) {
524
    switch (response.status) {
525 1865a0be Pavel Fidransky
      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 ebfe6347 plundrichov
      default:
538 1865a0be Pavel Fidransky
        throw new Error(response.statusText);
539
    }
540 9f045397 plundrichov
  }
541
}