Projekt

Obecné

Profil

Stáhnout (13.8 KB) Statistiky
| Větev: | Tag: | Revize:
1
// ******************** GET DATA APP getCurrentProfile ********************
2

    
3
export const getCurrentProfile = async () => {
4

    
5
  let response;
6

    
7
  try {
8
    response = await fetch(
9
      `${window.config.baseUrl}/users/current/profile`, {
10
        headers: {
11
          Authorization: 1
12

    
13
        },
14
        credentials: 'include'
15
      }
16
    );    
17
  } catch (e) {
18
    throw 'Server is not available'
19
    }
20

    
21
  if (response.ok) {
22
    const data = await response.json();
23

    
24
    return {
25
      name: data.firstName + ' ' + data.lastName,
26
      role: data.role,
27
      id: data.id,
28
      holiday: data.vacationCount,
29
      sickday: data.sickDayCount,
30
      takenSickday: data.takenSickDayCount
31
    }
32
  } else {
33
      switch (response.status) {
34
        case 401:
35
          window.location.replace("http://localhost:3000/login")
36
          break;
37
        case 403:
38
          window.location.replace("http://localhost:3000/login")
39
          break;
40
        case 500:
41
          throw new Error('Internal server error.')
42
        default:
43
          throw new Error(response.statusText)
44
      }
45
    }
46
}
47

    
48
// ******************** LOAD DATA to CALENDAR - EMPLOYEE ********************
49
export const getUserCalendar = async (userName, fromDate ) => {
50

    
51
  let response;
52

    
53
  try {
54
    response = await fetch(
55
      `${window.config.baseUrl}/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
56
        headers: {
57
          'Accept': 'application/json',
58
          Authorization: 6
59
        },
60
        credentials: 'include',
61
        method: 'GET',
62
      }
63
    );
64
  } catch (e) {
65
    throw 'Server is not available'
66
  }
67

    
68
  if (response.ok) {
69
    const data = await response.json();
70
  
71
    return data.filter(day => {
72
      return day.status !== 'PENDING'
73
    }).map(day => {
74

    
75
    const newDate = day.date.split("/").join("-");
76

    
77
    return ({
78
      title: userName.name,
79
      start: newDate,
80
      backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
81
    })
82
  })
83
  } else {
84
      switch (response.status) {
85
        case 400:
86
          throw new Error('Bad request. Check query parameters.')
87
        case 401:
88
          window.location.replace("http://localhost:3000/login")
89
        case 403:
90
          window.location.replace("http://localhost:3000/login")
91
        case 404:
92
          throw new Error('User with given ID does not exist.')
93
        case 500:
94
          throw new Error('Internal server error.')
95
        default:
96
          throw new Error(response.statusText)
97
      }
98
    }
99
}
100

    
101
// ******************** LOAD DATA to CALENDAR - EMPLOYER ********************
102

    
103
export const getAdminCalendar = async () => {
104

    
105
  let response;
106

    
107
  try {
108
    response = await fetch(
109
      `${window.config.baseUrl}/users/requests/vacation?status=ACCEPTED`, {
110
        headers: {
111
          'Accept': 'application/json',
112
          Authorization: 6
113
        },
114
        credentials: 'include',
115
        method: 'GET',
116
      }
117
    );
118
  } catch (e) {
119
    throw 'Server is not available'
120
  }
121

    
122
  if (response.ok) {
123
    const data = await response.json();
124
    
125
    return data.map(day => {
126

    
127
      const newDate = day.date.split("/").join("-");
128

    
129
      return ( {
130
      title: day.firstName + ' ' + day.lastName,
131
      start: newDate
132
      })
133
    })
134
  } else {
135
      switch (response.status) {
136
        case 400:
137
          throw new Error('Bad request. Check query parameters.')
138
        case 401:
139
          window.location.replace("http://localhost:3000/login")
140
        case 403:
141
          window.location.replace("http://localhost:3000/login")
142
        case 500:
143
          throw new Error('Internal server error.')
144
        default:
145
          throw new Error(response.statusText)
146
      }
147
    }
148
}
149

    
150
// ******************** ADD EVENT to CALENDAR - EMPLOYEE ********************
151

    
152
export async function addEventApi(userName, dataAddEventEmployee) {
153
  let response;
154

    
155
  try {
156
  // send accepted request to server
157
    response = await fetch(`${window.config.baseUrl}/user/calendar/create`, {
158
      headers: {
159
        Authorization: 6,
160
        'Content-Type': 'application/json',
161
      },
162
      credentials: 'include',
163
      method: 'POST',
164
  // object which is sent to server
165
  body: JSON.stringify(dataAddEventEmployee),
166
    });
167
  } catch (e) {
168
    throw 'Server is not available'
169
  }
170

    
171
  if (response.ok) {
172
      
173
    response = await fetch(
174
    `${window.config.baseUrl}/users/requests/vacation?status=PENDING`, {
175
      headers: {
176
        Authorization: 1
177
      },
178
      credentials: 'include',
179
    });
180
    const data = await response.json();
181
      
182
    return data.map(request => {
183
      const a = request.date;
184
      const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
185

    
186
      return (
187
        {
188
          title: request.firstName + ' ' + request.lastName,
189
          id: request.id,
190
          type: request.type,
191
          start: b,
192
          end: null,
193
          status: request.status
194
        })
195
    })
196

    
197
  } else {
198
      switch (response.status) {
199
        case 400:
200
          throw new Error('Bad request. Check request body.')
201
        case 401:
202
          window.location.replace("http://localhost:3000/login")
203
        case 403:
204
          window.location.replace("http://localhost:3000/login")
205
        case 500:
206
          throw new Error('Internal server error.')
207
        default:
208
          throw new Error(response.statusText)
209
      }    
210
    }
211
}
212
      
213
// ******************** ADD EVENT to CALENDAR - EMPLOYER ********************
214

    
215
export async function addEventApiAdmin(dataAddEventAdmin) {
216
  let response;
217

    
218
  try {
219
  // send accepted request to server
220
    response = await fetch(`${window.config.baseUrl}/user/calendar/create`, {
221
      headers: {
222
        Authorization: 1,
223
        'Content-Type': 'application/json',
224
    },
225
    credentials: 'include',
226
      method: 'POST',
227
    // object which is sent to server
228
      body: JSON.stringify(dataAddEventAdmin),
229
    });
230
  } catch (e) {
231
    throw 'Server is not available'
232
  }
233

    
234
  if (!response.ok) {
235
    switch (response.status) {
236
      case 400:
237
        throw new Error('Bad request. Check request body.')
238
      case 401:
239
        window.location.replace("http://localhost:3000/login")
240
      case 403:
241
        window.location.replace("http://localhost:3000/login")
242
      case 500:
243
        throw new Error('Internal server error.')
244
      default:
245
        throw new Error(response.statusText)
246
    }    
247
  }
248
}
249

    
250
// ******************** GET DATA to OVERVIEW - EMPLOYER ********************
251

    
252
export const getUsersOverview = async () => {
253
  let response;
254

    
255
  try {
256
  response = await fetch (
257
    `${window.config.baseUrl}/users`, {
258
      headers: {
259
        Authorization: 1          
260
      },
261
      credentials: 'include',
262
    }
263
  );
264
  } catch (e) {
265
    throw 'Server is not available'
266
  }
267
  
268
  if (response.ok) {
269

    
270
    const data = await response.json();
271
    return data.map(user => {
272

    
273
     return ({
274
           name: user.firstName + ' ' + user.lastName,
275
           id: user.id,
276
           sickday: user.sickDayCount,
277
           holiday: user.vacationCount,
278
           takenSickday: user.takenSickDayCount,
279
           role: user.role
280
        })
281
    })
282
  } else {
283
    switch (response.status) {
284
      case 400:
285
        throw new Error('Bad request. Check query parameters.')
286
      case 401:
287
        window.location.replace("http://localhost:3000/login")
288
      case 403:
289
        window.location.replace("http://localhost:3000/login")
290
      case 500:
291
        throw new Error('Internal server error.')
292
      default:
293
        throw new Error(response.statusText)
294
    }    
295
  }
296
}
297

    
298
// ******************** SAVE DATA to OVERVIEW - EMPLOYER ********************
299

    
300
export async function saveDataOverview(dataOverviewObject) {
301
  let response;
302

    
303
  try {
304
    // send accepted request to server
305
        response = await fetch(`${window.config.baseUrl}/user/settings`, {
306
          headers: {
307
            Authorization: 1,
308
            'Content-Type': 'application/json',
309
          },
310
          credentials: 'include',
311
          method: 'PUT',
312
 
313
    // object which is sent to server
314
          body: JSON.stringify(dataOverviewObject),        
315
        });
316
  } catch (e) {
317
    throw 'Server is not available'
318
  }
319

    
320
  if (!response.ok) {
321
    switch (response.status) {
322
      case 400:
323
        throw new Error('Bad request. Check query parameters.')
324
      case 401:
325
        window.location.replace("http://localhost:3000/login")
326
      case 403:
327
        window.location.replace("http://localhost:3000/login")
328
      case 500:
329
        throw new Error('Internal server error.')
330
      default:
331
        throw new Error(response.statusText)
332
    }
333
  }    
334
}
335

    
336
// ******************** LOAD DATA to SETTING - EMPLOYER ********************
337
export const getSettingData = async () =>  {
338
  let response;
339

    
340
  try {
341
    response = await fetch(
342
      `${window.config.baseUrl}/settings`, {
343
        headers: {
344
          Authorization: 1
345
        },
346
        credentials: 'include',
347
      }
348
    );
349

    
350
  } catch (e) {
351
    throw 'Server is not available'
352
    }
353

    
354
  if (response.ok) {
355
    const data = await response.json();
356
    return {
357
      sickday: data.sickDayCount,
358
    }
359
  } else {
360
    switch (response.status) {
361
      case 500:
362
        throw new Error('Internal server error.')
363
      default:
364
        throw new Error(response.statusText)
365
      }            
366
    }
367

    
368
}
369

    
370
// ******************** SAVE DATA to SETTING - EMPLOYER ********************
371
export async function saveDataSetting(dataSettingObject) {
372
  let response;
373

    
374
  try {
375
    response = await fetch(`${window.config.baseUrl}/settings`, {
376
      headers: {
377
        'Authorization': 1,
378
        'Content-Type': 'application/json'
379
      },
380
      credentials: 'include',
381
      method: 'POST',
382
      body: JSON.stringify(dataSettingObject),
383
    });
384
  } catch (e) {
385
    throw 'Server is not available'
386
  }
387

    
388
  if (!response.ok) {
389
    switch (response.status) {
390
      case 401:
391
        window.location.replace("http://localhost:3000/login")
392
      case 403:
393
        window.location.replace("http://localhost:3000/login")
394
      case 500:
395
        throw new Error('Internal server error.')
396
      default:
397
        throw new Error(response.statusText)
398
    }    
399
  }
400
}
401

    
402
// ****************** LOAD DATA to YOUR REQUESTS - EMPLOYEE ******************
403

    
404
export async function loadYourRequests(userName) {
405
  let response;
406
  
407
  try {
408
    response = await fetch(
409
      `${window.config.baseUrl}/user/${userName.id}/calendar?from=2020/06/24&status=PENDING`, {
410
        headers: {
411
          Authorization: 6
412
        },
413
        credentials: 'include',
414
      }
415
    );
416
  } catch (e) {
417
    throw 'Server is not available'
418
  }
419

    
420
  if (response.ok) {
421
    const data = await response.json();
422
    return data;
423
  } else {
424
      switch (response.status) {
425
        case 400:
426
          throw new Error('Bad request. Check query parameters.')
427
        case 401:
428
          window.location.replace("http://localhost:3000/login")
429
        case 403:
430
          window.location.replace("http://localhost:3000/login")
431
        case 500:
432
          throw new Error('Internal server error.')
433
        default:
434
          throw new Error(response.statusText)
435
      }    
436
    }
437
}
438

    
439
// ****************** LOAD DATA - UPCOMING REQUESTS - EMPLOYER ****************** //tady
440
export async function loadAdminRequests() {
441
  let response;
442

    
443
  try {
444
    response = await fetch(
445
      `${window.config.baseUrl}/users/requests/vacation?status=PENDING`, {
446
        headers: {
447
          Authorization: 1
448
        },
449
        credentials: 'include',
450
      },
451
    );
452
  } catch (e) {
453
    throw 'Server is not available'
454
    } 
455
  
456
  if (response.ok) {
457
    const data = await response.json();
458
      return data;
459
  } else {
460
      switch (response.status) {
461
        case 400:
462
          throw new Error('Bad request. Check query parameters.')
463
        case 401:
464
          window.location.replace("http://localhost:3000/login")
465
        case 403:
466
          window.location.replace("http://localhost:3000/login")
467
        case 500:
468
          throw new Error('Internal server error.')
469
        default:
470
          throw new Error(response.statusText)
471
      }   
472
    }
473
}
474

    
475
// ************** SEND ACCEPTED DATA - UPCOMING REQUESTS - EMPLOYER **************
476

    
477
export async function sendAcceptedRequest(acceptedRequests) {
478
  let response;
479

    
480
  try {
481
    response = await fetch(`${window.config.baseUrl}/user/requests?type=VACATION`, {
482
      headers: {
483
        Authorization: 1,
484
        'Content-Type': 'application/json',
485
      },
486
      credentials: 'include',
487
      method: 'PUT',
488
      body: JSON.stringify(acceptedRequests),
489
    });
490
  } catch (e) {
491
    throw 'Server is not available'
492
    }
493

    
494
  if (!response.ok) {
495
    switch (response.status) {
496
      case 400:
497
        throw new Error('Bad request. Check query parameters and request body.')
498
      case 401:
499
        window.location.replace("http://localhost:3000/login")
500
      case 403:
501
        window.location.replace("http://localhost:3000/login")
502
      case 404:
503
        throw new Error('Neither vacation nor authorization request with given ID exists.')
504
      case 500:
505
        throw new Error('Internal server error.')
506
      default:
507
        throw new Error(response.statusText)
508
    }   
509
  }
510
}
511

    
512
// ************** SEND REJECTED DATA - UPCOMING REQUESTS - EMPLOYER **************
513

    
514
export async function sendRejectedRequest(rejectedRequest) {
515
  let response;
516
  
517
  try {
518
    response = await fetch(`${window.config.baseUrl}/user/requests?type=VACATION`, {
519
      headers: {
520
        Authorization: 1,
521
        'Content-Type': 'application/json',
522
      },
523
      credentials: 'include',
524
      method: 'PUT',
525
      body: JSON.stringify(rejectedRequest),
526
    });
527
  } catch (e) {
528
    throw 'Server is not available'
529
    }
530
  
531
  if (!response.ok) {
532
    switch (response.status) {
533
      case 400:
534
        throw new Error('Bad request. Check query parameters and request body.')
535
      case 401:
536
        window.location.replace("http://localhost:3000/login")
537
      case 403:
538
        window.location.replace("http://localhost:3000/login")
539
      case 404:
540
        throw new Error('Neither vacation nor authorization request with given ID exists.')
541
      case 500:
542
        throw new Error('Internal server error.')
543
      default:
544
        throw new Error(response.statusText)
545
    }    
546
  }
547
}
(13-13/17)