Projekt

Obecné

Profil

Stáhnout (12.7 KB) Statistiky
| Větev: | Tag: | Revize:
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
    );    
19
  } catch (e) {
20
    throw 'Server is not available'
21
    }
22

    
23
  if (response.ok) {
24
    const data = await response.json();
25

    
26
    return {
27
      name: data.firstName + ' ' + data.lastName,
28
      role: data.role,
29
      id: data.id,
30
      holiday: data.vacationCount,
31
      sickday: data.sickDayCount,
32
      takenSickday: data.takenSickDayCount
33
    }
34
  } else {
35
      switch (response.status) {
36
        case 401:
37
          throw new Error('Not authenticated.')
38
        case 500:
39
          throw new Error('Internal server error.')
40
        default:
41
          throw new Error(response.statusText)
42
      }
43
    }
44
}
45

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

    
49
  let response;
50

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

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

    
72
    const newDate = day.date.split("/").join("-");
73

    
74
    return ({
75
      title: userName.name,
76
      start: newDate,
77
      backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
78
    })
79
  })
80
  } else {
81
      switch (response.status) {
82
        case 400:
83
          throw new Error('Bad request. Check query parameters.')
84
        case 401:
85
          throw new Error('Not authenticated.')
86
        case 403:
87
          throw new Error('Not authorized.')
88
        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
    }
96
}
97

    
98
// ******************** LOAD DATA to CALENDAR - EMPLOYER ********************
99

    
100
export const getAdminCalendar = async () => {
101

    
102
  let response;
103

    
104
  try {
105
    response = await fetch(
106
      `${http}/users/requests/vacation?status=ACCEPTED`, {
107
        headers: {
108
          'Accept': 'application/json',
109
          Authorization: 1
110
        },
111
        method: 'GET',
112
      }
113
    );
114
  } catch (e) {
115
    throw 'Server is not available'
116
  }
117

    
118
  if (response.ok) {
119
    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
      switch (response.status) {
132
        case 400:
133
          throw new Error('Bad request. Check query parameters.')
134
        case 401:
135
          throw new Error('Not authenticated.')
136
        case 403:
137
          throw new Error('Not authorized.')
138
        case 500:
139
          throw new Error('Internal server error.')
140
        default:
141
          throw new Error(response.statusText)
142
      }
143
    }
144
}
145

    
146
// ******************** ADD EVENT to CALENDAR - EMPLOYEE ********************
147

    
148
export async function addEventApi(dataAddEventEmployee) {
149
  let response;
150

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

    
166
  if (response.ok) {
167
      
168
    response = await fetch(
169
    `${http}/users/requests/vacation?status=PENDING`, {
170
      headers: {
171
        Authorization: 1
172
      },
173
    });
174
    const data = await response.json();
175
      
176
    return data.map(request => {
177
      const a = request.date;
178
      const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
179

    
180
      return (
181
        {
182
          title: request.firstName + ' ' + request.lastName,
183
          id: request.id,
184
          type: request.type,
185
          start: b,
186
          end: null,
187
          status: request.status
188
        })
189
    })
190

    
191
  } else {
192
      switch (response.status) {
193
        case 400:
194
          throw new Error('Bad request. Check request body.')
195
        case 401:
196
          throw new Error('Not authenticated.')
197
        case 403:
198
          throw new Error('Not authorized.')
199
        case 500:
200
          throw new Error('Internal server error.')
201
        default:
202
          throw new Error(response.statusText)
203
      }    
204
    }
205
}
206
      
207
// ******************** ADD EVENT to CALENDAR - EMPLOYER ********************
208

    
209
export async function addEventApiAdmin(dataAddEventAdmin) {
210
  let response;
211

    
212
  try {
213
  // send accepted request to server
214
    response = await fetch(`${http}/user/calendar/create`, {
215
      headers: {
216
        Authorization: 1,
217
        'Content-Type': 'application/json',
218
    },
219
      method: 'POST',
220
    // object which is sent to server
221
      body: JSON.stringify(dataAddEventAdmin),
222
    });
223
  } catch (e) {
224
    throw 'Server is not available'
225
  }
226

    
227
  if (!response.ok) {
228
    switch (response.status) {
229
      case 400:
230
        throw new Error('Bad request. Check request body.')
231
      case 401:
232
        throw new Error('Not authenticated.')
233
      case 403:
234
        throw new Error('Not authorized.')
235
      case 500:
236
        throw new Error('Internal server error.')
237
      default:
238
        throw new Error(response.statusText)
239
    }    
240
  }
241
}
242

    
243
// ******************** GET DATA to OVERVIEW - EMPLOYER ********************
244

    
245
export const getUsersOverview = async () => {
246
  let response;
247

    
248
  try {
249
  response = await fetch (
250
    `${http}/users`, {
251
      headers: {
252
        Authorization: 1          }
253
    }
254
  );
255
  } catch (e) {
256
    throw 'Server is not available'
257
  }
258
  
259
  if (response.ok) {
260

    
261
    const data = await response.json();
262
    return data.map(user => {
263

    
264
     return ({
265
           name: user.firstName + ' ' + user.lastName,
266
           id: user.id,
267
           sickday: user.sickDayCount,
268
           holiday: user.vacationCount,
269
           takenSickday: user.takenSickDayCount,
270
           role: user.role
271
        })
272
    })
273
  } else {
274
    switch (response.status) {
275
      case 400:
276
        throw new Error('Bad request. Check query parameters.')
277
      case 401:
278
        throw new Error('Not authenticated.')
279
      case 403:
280
        throw new Error('Not authorized.')
281
      case 500:
282
        throw new Error('Internal server error.')
283
      default:
284
        throw new Error(response.statusText)
285
    }    
286
  }
287
}
288

    
289
// ******************** SAVE DATA to OVERVIEW - EMPLOYER ********************
290

    
291
export async function saveDataOverview(dataOverviewObject) {
292
  let response;
293

    
294
  try {
295
    // send accepted request to server
296
        response = await fetch(`${http}/user/settings`, {
297
          headers: {
298
            Authorization: 1,
299
            'Content-Type': 'application/json',
300
          },
301
          method: 'PUT',
302
 
303
    // object which is sent to server
304
          body: JSON.stringify(dataOverviewObject),        
305
        });
306
  } catch (e) {
307
    throw 'Server is not available'
308
  }
309

    
310
  if (!response.ok) {
311
    switch (response.status) {
312
      case 400:
313
        throw new Error('Bad request. Check query parameters.')
314
      case 401:
315
        throw new Error('Not authenticated.')
316
      case 403:
317
        throw new Error('Not authorized.')
318
      case 500:
319
        throw new Error('Internal server error.')
320
      default:
321
        throw new Error(response.statusText)
322
    }
323
  }    
324
}
325

    
326
// ******************** LOAD DATA to SETTING - EMPLOYER ********************
327
export const getSettingData = async () =>  {
328
  let response;
329

    
330
  try {
331
    response = await fetch(
332
      `${http}/settings`, {
333
        headers: {
334
          Authorization: 1
335

    
336
        }
337
      });
338
  } catch (e) {
339
    throw 'Server is not available'
340
    }
341

    
342
  if (response.ok) {
343
    const data = await response.json();
344
    return {
345
      sickday: data.sickDayCount,
346
    }
347
  } else {
348
    switch (response.status) {
349
      case 500:
350
        throw new Error('Internal server error.')
351
      default:
352
        throw new Error(response.statusText)
353
      }            
354
    }
355

    
356
}
357

    
358
// ******************** SAVE DATA to SETTING - EMPLOYER ********************
359
export async function saveDataSetting(dataSettingObject) {
360
  let response;
361

    
362
  try {
363
    response = await fetch(`${http}/settings`, {
364
      headers: {
365
        'Authorization': 1,
366
        'Content-Type': 'application/json'
367
      },
368
      method: 'POST',
369
      body: JSON.stringify(dataSettingObject),
370
    });
371
  } catch (e) {
372
    throw 'Server is not available'
373
  }
374

    
375
  if (!response.ok) {
376
    switch (response.status) {
377
      case 401:
378
        throw new Error('Not authenticated.')
379
      case 403:
380
        throw new Error('Not authorized.')
381
      case 500:
382
        throw new Error('Internal server error.')
383
      default:
384
        throw new Error(response.statusText)
385
    }    
386
  }
387
}
388

    
389
// ****************** LOAD DATA to YOUR REQUESTS - EMPLOYEE ******************
390

    
391
export async function loadYourRequests() {
392
  let response;
393
  
394
  try {
395
    response = await fetch(
396
      `${http}/user/6/calendar?from=2020/06/24&status=PENDING`, {
397
        headers: {
398
          Authorization: 6
399
        },
400
      }
401
    );
402
  } catch (e) {
403
    throw 'Server is not available'
404
  }
405

    
406
  if (response.ok) {
407
    const data = await response.json();
408
    return data;
409
  } else {
410
      switch (response.status) {
411
        case 400:
412
          throw new Error('Bad request. Check query parameters.')
413
        case 401:
414
          throw new Error('Not authenticated.')
415
        case 403:
416
          throw new Error('Not authorized.')
417
        case 500:
418
          throw new Error('Internal server error.')
419
        default:
420
          throw new Error(response.statusText)
421
      }    
422
    }
423
}
424

    
425
// ****************** LOAD DATA - UPCOMING REQUESTS - EMPLOYER ****************** //tady
426
export async function loadAdminRequests() {
427
  let response;
428

    
429
  try {
430
    response = await fetch(
431
      `${http}/users/requests/vacation?status=PENDING`, {
432
        headers: {
433
          Authorization: 1
434
        }
435
      },
436
    );
437
  } catch (e) {
438
    throw 'Server is not available'
439
    } 
440
  
441
  if (response.ok) {
442
    const data = await response.json();
443
      return data;
444
  } else {
445
      switch (response.status) {
446
        case 400:
447
          throw new Error('Bad request. Check query parameters.')
448
        case 401:
449
          throw new Error('Not authenticated.')
450
        case 403:
451
          throw new Error('Not authorized.')
452
        case 500:
453
          throw new Error('Internal server error.')
454
        default:
455
          throw new Error(response.statusText)
456
      }   
457
    }
458
}
459

    
460
// ************** SEND ACCEPTED DATA - UPCOMING REQUESTS - EMPLOYER **************
461

    
462
export async function sendAcceptedRequest(acceptedRequests) {
463
  let response;
464

    
465
  try {
466
    response = await fetch(`${http}/user/requests?type=VACATION`, {
467
      headers: {
468
        Authorization: 1,
469
        'Content-Type': 'application/json',
470
      },
471
      method: 'PUT',
472
      body: JSON.stringify(acceptedRequests),
473
    });
474
  } catch (e) {
475
    throw 'Server is not available'
476
    }
477

    
478
  if (!response.ok) {
479
    switch (response.status) {
480
      case 400:
481
        throw new Error('Bad request. Check query parameters and request body.')
482
      case 401:
483
        throw new Error('Not authenticated.')
484
      case 403:
485
        throw new Error('Not authorized.')
486
      case 404:
487
        throw new Error('Neither vacation nor authorization request with given ID exists.')
488
      case 500:
489
        throw new Error('Internal server error.')
490
      default:
491
        throw new Error(response.statusText)
492
    }   
493
  }
494
}
495

    
496
// ************** SEND REJECTED DATA - UPCOMING REQUESTS - EMPLOYER **************
497

    
498
export async function sendRejectedRequest(rejectedRequest) {
499
  let response;
500
  
501
  try {
502
    response = await fetch(`${http}/user/requests?type=VACATION`, {
503
      headers: {
504
        Authorization: 1,
505
        'Content-Type': 'application/json',
506
      },
507
      method: 'PUT',
508
      body: JSON.stringify(rejectedRequest),
509
    });
510
  } catch (e) {
511
    throw 'Server is not available'
512
    }
513
  
514
  if (!response.ok) {
515
    switch (response.status) {
516
      case 400:
517
        throw new Error('Bad request. Check query parameters and request body.')
518
      case 401:
519
        throw new Error('Not authenticated.')
520
      case 403:
521
        throw new Error('Not authorized.')
522
      case 404:
523
        throw new Error('Neither vacation nor authorization request with given ID exists.')
524
      case 500:
525
        throw new Error('Internal server error.')
526
      default:
527
        throw new Error(response.statusText)
528
    }    
529
  }
530
}
(13-13/17)