Projekt

Obecné

Profil

Stáhnout (12.7 KB) Statistiky
| Větev: | Tag: | Revize:
1 ebfe6347 plundrichov
2 9f045397 plundrichov
const http = 'http://devcz.yoso.fi:8090/ymanager';
3
4
// ******************** GET DATA APP getCurrentProfile ********************
5 7495b9eb plundrichov
6
export const getCurrentProfile = async () => {
7
8 ebfe6347 plundrichov
  let response;
9
10
  try {
11
    response = await fetch(
12 9f045397 plundrichov
      `${http}/users/current/profile`, {
13 7495b9eb plundrichov
        headers: {
14
          Authorization: 1
15
        }
16
      }
17 ebfe6347 plundrichov
    );    
18
  } catch (e) {
19
    throw 'Server is not available'
20
    }
21 7495b9eb plundrichov
22 ebfe6347 plundrichov
  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 7495b9eb plundrichov
    }
33 ebfe6347 plundrichov
  } 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 7495b9eb plundrichov
    }
43
}
44
45
// ******************** LOAD DATA to CALENDAR - EMPLOYEE ********************
46
export const getUserCalendar = async (userName, fromDate ) => {
47 ebfe6347 plundrichov
48
  let response;
49
50 7495b9eb plundrichov
  try {
51 ebfe6347 plundrichov
    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 7495b9eb plundrichov
64
  if (response.ok) {
65 ebfe6347 plundrichov
    const data = await response.json();
66 7495b9eb plundrichov
  
67 ebfe6347 plundrichov
    return data.filter(day => {
68
      return day.status !== 'PENDING'
69
    }).map(day => {
70 7495b9eb plundrichov
71
    const newDate = day.date.split("/").join("-");
72
73
    return ({
74 ebfe6347 plundrichov
      title: userName.name,
75
      start: newDate,
76
      backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
77 7495b9eb plundrichov
    })
78
  })
79 ebfe6347 plundrichov
  } 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 7495b9eb plundrichov
    }
95
}
96
97
// ******************** LOAD DATA to CALENDAR - EMPLOYER ********************
98 ebfe6347 plundrichov
99 7495b9eb plundrichov
export const getAdminCalendar = async () => {
100 ebfe6347 plundrichov
101
  let response;
102
103
  try {
104
    response = await fetch(
105
      `${http}/users/requests/vacation?status=ACCEPTED`, {
106 7495b9eb plundrichov
        headers: {
107
          'Accept': 'application/json',
108
          Authorization: 1
109
        },
110
        method: 'GET',
111
      }
112
    );
113 ebfe6347 plundrichov
  } catch (e) {
114
    throw 'Server is not available'
115
  }
116 7495b9eb plundrichov
117 ebfe6347 plundrichov
  if (response.ok) {
118 7495b9eb plundrichov
    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 ebfe6347 plundrichov
      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 7495b9eb plundrichov
      }
142 9f045397 plundrichov
    }
143 7495b9eb plundrichov
}
144
145
// ******************** ADD EVENT to CALENDAR - EMPLOYEE ********************
146 ebfe6347 plundrichov
147 9f045397 plundrichov
export async function addEventApi(dataAddEventEmployee) {
148 ebfe6347 plundrichov
  let response;
149
150
  try {
151 7495b9eb plundrichov
  // send accepted request to server
152 ebfe6347 plundrichov
    response = await fetch(`${http}/user/calendar/create`, {
153 9f045397 plundrichov
      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 ebfe6347 plundrichov
  } catch (e) {
162
    throw 'Server is not available'
163
  }
164
165
  if (response.ok) {
166 9f045397 plundrichov
      
167 ebfe6347 plundrichov
    response = await fetch(
168
    `${http}/users/requests/vacation?status=PENDING`, {
169
      headers: {
170
        Authorization: 1
171
      },
172
    });
173
    const data = await response.json();
174 9f045397 plundrichov
      
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 7495b9eb plundrichov
179 9f045397 plundrichov
      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 ebfe6347 plundrichov
      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 9f045397 plundrichov
    }
204
}
205
      
206
// ******************** ADD EVENT to CALENDAR - EMPLOYER ********************
207 ebfe6347 plundrichov
208 9f045397 plundrichov
export async function addEventApiAdmin(dataAddEventAdmin) {
209 ebfe6347 plundrichov
  let response;
210
211 9f045397 plundrichov
  try {
212 ebfe6347 plundrichov
  // 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 9f045397 plundrichov
    // object which is sent to server
220 ebfe6347 plundrichov
      body: JSON.stringify(dataAddEventAdmin),
221
    });
222 9f045397 plundrichov
  } catch (e) {
223 ebfe6347 plundrichov
    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 9f045397 plundrichov
}
241 7495b9eb plundrichov
242 9f045397 plundrichov
// ******************** GET DATA to OVERVIEW - EMPLOYER ********************
243 ebfe6347 plundrichov
244 9f045397 plundrichov
export const getUsersOverview = async () => {
245 ebfe6347 plundrichov
  let response;
246
247 9f045397 plundrichov
  try {
248 ebfe6347 plundrichov
  response = await fetch (
249
    `${http}/users`, {
250 9f045397 plundrichov
      headers: {
251
        Authorization: 1          }
252
    }
253
  );
254 ebfe6347 plundrichov
  } catch (e) {
255
    throw 'Server is not available'
256
  }
257 7495b9eb plundrichov
  
258 ebfe6347 plundrichov
  if (response.ok) {
259 9f045397 plundrichov
260 ebfe6347 plundrichov
    const data = await response.json();
261
    return data.map(user => {
262 9f045397 plundrichov
263 ebfe6347 plundrichov
     return ({
264 9f045397 plundrichov
           name: user.firstName + ' ' + user.lastName,
265
           id: user.id,
266
           sickday: user.sickDayCount,
267
           holiday: user.vacationCount,
268 ebfe6347 plundrichov
           takenSickday: user.takenSickDayCount,
269 9f045397 plundrichov
           role: user.role
270 7495b9eb plundrichov
        })
271 ebfe6347 plundrichov
    })
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 9f045397 plundrichov
  }
286
}
287 7495b9eb plundrichov
288 9f045397 plundrichov
// ******************** SAVE DATA to OVERVIEW - EMPLOYER ********************
289 ebfe6347 plundrichov
290 9f045397 plundrichov
export async function saveDataOverview(dataOverviewObject) {
291 ebfe6347 plundrichov
  let response;
292
293 9f045397 plundrichov
  try {
294
    // send accepted request to server
295 ebfe6347 plundrichov
        response = await fetch(`${http}/user/settings`, {
296 9f045397 plundrichov
          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 ebfe6347 plundrichov
    throw 'Server is not available'
307 9f045397 plundrichov
  }
308 ebfe6347 plundrichov
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 9f045397 plundrichov
}
324
325
// ******************** LOAD DATA to SETTING - EMPLOYER ********************
326
export const getSettingData = async () =>  {
327 ebfe6347 plundrichov
  let response;
328
329 9f045397 plundrichov
  try {
330
    const response = await fetch(
331 ebfe6347 plundrichov
      `${http}/settings`, {
332 9f045397 plundrichov
        headers: {
333
          Authorization: 1
334
        }
335
      });
336
  } catch (e) {
337 ebfe6347 plundrichov
    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 9f045397 plundrichov
    }
353 ebfe6347 plundrichov
354 9f045397 plundrichov
}
355
356
// ******************** SAVE DATA to SETTING - EMPLOYER ********************
357
export async function saveDataSetting(dataSettingObject) {
358 ebfe6347 plundrichov
  let response;
359
360 9f045397 plundrichov
  try {
361 ebfe6347 plundrichov
    response = await fetch(`${http}/settings`, {
362 9f045397 plundrichov
      headers: {
363 ebfe6347 plundrichov
        'Authorization': 1,
364 9f045397 plundrichov
        'Content-Type': 'application/json'
365
      },
366
      method: 'POST',
367
      body: JSON.stringify(dataSettingObject),
368
    });
369 ebfe6347 plundrichov
  } catch (e) {
370
    throw 'Server is not available'
371
  }
372 9f045397 plundrichov
373 ebfe6347 plundrichov
  if (!response.ok) {
374 9f045397 plundrichov
    switch (response.status) {
375 ebfe6347 plundrichov
      case 401:
376
        throw new Error('Not authenticated.')
377
      case 403:
378
        throw new Error('Not authorized.')
379 9f045397 plundrichov
      case 500:
380 ebfe6347 plundrichov
        throw new Error('Internal server error.')
381 9f045397 plundrichov
      default:
382 ebfe6347 plundrichov
        throw new Error(response.statusText)
383
    }    
384
  }
385 9f045397 plundrichov
}
386
387
// ****************** LOAD DATA to YOUR REQUESTS - EMPLOYEE ******************
388
389
export async function loadYourRequests() {
390 ebfe6347 plundrichov
  let response;
391
  
392 9f045397 plundrichov
  try {
393 ebfe6347 plundrichov
    response = await fetch(
394
      `${http}/user/6/calendar?from=2020/06/24&status=PENDING`, {
395 9f045397 plundrichov
        headers: {
396
          Authorization: 6
397
        },
398
      }
399
    );
400 ebfe6347 plundrichov
  } catch (e) {
401
    throw 'Server is not available'
402
  }
403 9f045397 plundrichov
404
  if (response.ok) {
405
    const data = await response.json();
406
    return data;
407
  } else {
408 ebfe6347 plundrichov
      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 9f045397 plundrichov
}
422
423 ebfe6347 plundrichov
// ****************** LOAD DATA - UPCOMING REQUESTS - EMPLOYER ****************** //tady
424 9f045397 plundrichov
export async function loadAdminRequests() {
425 ebfe6347 plundrichov
  let response;
426
427 9f045397 plundrichov
  try {
428 ebfe6347 plundrichov
    response = await fetch(
429
      `${http}/users/requests/vacation?status=PENDING`, {
430 9f045397 plundrichov
        headers: {
431
          Authorization: 1
432
        }
433
      },
434
    );
435 ebfe6347 plundrichov
  } catch (e) {
436
    throw 'Server is not available'
437
    } 
438
  
439
  if (response.ok) {
440 9f045397 plundrichov
    const data = await response.json();
441
      return data;
442
  } else {
443 ebfe6347 plundrichov
      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 7495b9eb plundrichov
    }
456 9f045397 plundrichov
}
457
458
// ************** SEND ACCEPTED DATA - UPCOMING REQUESTS - EMPLOYER **************
459 ebfe6347 plundrichov
460 9f045397 plundrichov
export async function sendAcceptedRequest(acceptedRequests) {
461 ebfe6347 plundrichov
  let response;
462
463 9f045397 plundrichov
  try {
464 ebfe6347 plundrichov
    response = await fetch(`${http}/user/requests?type=VACATION`, {
465 9f045397 plundrichov
      headers: {
466
        Authorization: 1,
467
        'Content-Type': 'application/json',
468
      },
469
      method: 'PUT',
470
      body: JSON.stringify(acceptedRequests),
471
    });
472
  } catch (e) {
473 ebfe6347 plundrichov
    throw 'Server is not available'
474 9f045397 plundrichov
    }
475 ebfe6347 plundrichov
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 9f045397 plundrichov
}
493
494
// ************** SEND REJECTED DATA - UPCOMING REQUESTS - EMPLOYER **************
495
496
export async function sendRejectedRequest(rejectedRequest) {
497 ebfe6347 plundrichov
  let response;
498
  
499 9f045397 plundrichov
  try {
500 ebfe6347 plundrichov
    response = await fetch(`${http}/user/requests?type=VACATION`, {
501 9f045397 plundrichov
      headers: {
502
        Authorization: 1,
503
        'Content-Type': 'application/json',
504
      },
505
      method: 'PUT',
506
      body: JSON.stringify(rejectedRequest),
507
    });
508 ebfe6347 plundrichov
  } 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 9f045397 plundrichov
  }
528
}