Projekt

Obecné

Profil

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