Projekt

Obecné

Profil

Stáhnout (12.2 KB) Statistiky
| Větev: | Tag: | Revize:
1
const http = 'http://devcz.yoso.fi:8090/ymanager';
2

    
3
// ******************** GET DATA APP getCurrentProfile ********************
4

    
5
export const getCurrentProfile = async () => {
6

    
7
    try {
8
    const response = await fetch(
9
      `${http}/users/current/profile`, {
10
        headers: {
11
          Authorization: 1
12
        }
13
      }
14
    );
15

    
16
    if (response.ok) {
17
        const data = await response.json();
18
        return {
19
        name: data.firstName + ' ' + data.lastName,
20
        role: data.role,
21
        id: data.id,
22
        holiday: data.vacationCount,
23
        sickday: data.sickDayCount
24
    }
25
    } else {
26
        if(response.status === 400) {
27
          throw 'error 400 GET DATA APP (getCurrentProfile)'
28
        }
29
        else if (response.status === 500) {
30
          throw 'error 500 GET DATA APP (getCurrentProfile)'
31
        }
32
        else {
33
          throw 'error GET DATA APP (getCurrentProfile)'
34
        }
35
    }
36

    
37
} catch (e) {
38
  throw 'error catch GET DATA APP (getCurrentProfile)'
39
  }
40
}
41

    
42
// ******************** LOAD DATA to CALENDAR - EMPLOYEE ********************
43
export const getUserCalendar = async (userName, fromDate ) => {
44
  try {
45
  const response = await fetch(
46
    `${http}/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
47
      headers: {
48
        'Accept': 'application/json',
49
        Authorization: 6
50
      },
51
      method: 'GET',
52
    }
53
  );
54

    
55
  if (response.ok) {
56
  const data = await response.json();
57
  
58
  return data.filter(day => {
59
    return day.status !== 'PENDING'
60
  }).map(day => {
61

    
62
    const newDate = day.date.split("/").join("-");
63

    
64
    return ({
65
    title: userName.name,
66
    start: newDate,
67
    backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
68
    })
69
  })
70
} else {
71
    if(response.status === 400) {
72
      throw 'error 400 LOADING DATA (CALENDAR, EMPLOYEE)'
73
    }
74
    else if (response.status === 500) {
75
      throw 'error 500 LOADING DATA (CALENDAR, EMPLOYEE)'
76
    }
77
    else {
78
      throw 'error LOADING DATA (CALENDAR, EMPLOYEE)'
79
    }
80
}
81
} catch (e) {
82
  throw 'error catch LOADING DATA (CALENDAR, EMPLOYEE)'
83
}
84
}
85

    
86
// ******************** LOAD DATA to CALENDAR - EMPLOYER ********************
87
export const getAdminCalendar = async () => {
88
    try {
89
    const response = await fetch(
90
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=ACCEPTED', {
91
        headers: {
92
          'Accept': 'application/json',
93
          Authorization: 1
94
        },
95
        method: 'GET',
96
      }
97
    );
98

    
99
    if (response.ok) {
100
    const data = await response.json();
101
    
102
    return data.map(day => {
103

    
104
      const newDate = day.date.split("/").join("-");
105

    
106
      return ( {
107
      title: day.firstName + ' ' + day.lastName,
108
      start: newDate
109
      })
110
    })
111
  } else {
112
      if(response.status === 400) {
113
        throw 'error 400 LOADING DATA (CALENDAR, EMPLOYER)'
114
      }
115
      else if (response.status === 500) {
116
        throw 'error 500 LOADING DATA (CALENDAR, EMPLOYER))'
117
      }
118
      else {
119
        throw 'error LOADING DATA (CALENDAR, EMPLOYER)'
120
      }
121
    }
122
  } catch (e) {
123
    throw 'error catch LOADING DATA (CALENDAR, EMPLOYER)'
124
  }
125
}
126

    
127
// ******************** ADD EVENT to CALENDAR - EMPLOYEE ********************
128
export async function addEventApi(dataAddEventEmployee) {
129
try {
130
  // send accepted request to server
131
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
132
      headers: {
133
        Authorization: 6,
134
        'Content-Type': 'application/json',
135
      },
136
      method: 'POST',
137
  // object which is sent to server
138
  body: JSON.stringify(dataAddEventEmployee),
139
    });
140
    if (response.ok) {
141
      
142
      const response = await fetch(
143
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
144
        headers: {
145
          Authorization: 1
146
        },
147
      });
148
      const data = await response.json();
149
      
150
    return data.map(request => {
151
      const a = request.date;
152
      const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
153

    
154
      return (
155
        {
156
          title: request.firstName + ' ' + request.lastName,
157
          id: request.id,
158
          type: request.type,
159
          start: b,
160
          end: null,
161
          status: request.status
162
        })
163
    })
164

    
165
  } else {
166
    
167
    if(response.status === 400) {
168
      throw 'error 400 ADD EVENT - EMPLOYEE'
169
    }
170
    else if (response.status === 500) {
171
      throw 'error 500 ADD EVENT - EMPLOYEE'
172
    }
173
    else {
174
      throw 'error ADD EVENT - EMPLOYEE'
175
    }
176
  }
177
} catch (e) {
178
    throw 'error catch ADD EVENT - EMPLOYEE'
179
  }
180
}
181
      
182
// ******************** ADD EVENT to CALENDAR - EMPLOYER ********************
183
export async function addEventApiAdmin(dataAddEventAdmin) {
184
  try {
185
    // send accepted request to server
186
        const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
187
          headers: {
188
            Authorization: 1,
189
            'Content-Type': 'application/json',
190
          },
191
          method: 'POST',
192
    // object which is sent to server
193
          body: JSON.stringify(dataAddEventAdmin),
194
        });
195
        if (response.ok) {
196
          return;
197
    } else {
198
      if(response.status === 400) {
199
        throw('error 400 ADD EVENT ADMIN - EMPLOYER')
200
     }
201
        else if (response.status === 500) {
202
           throw ('error 500 ADD EVENT ADMIN - EMPLOYER')
203
        }
204
        else {
205
           throw('error ADD EVENT ADMIN - EMPLOYER')
206
        }
207
    }
208
  } catch (e) {
209
      throw('error catch ADD EVENT ADMIN - EMPLOYER')
210
    }
211
}
212

    
213
// ******************** GET DATA to OVERVIEW - EMPLOYER ********************
214
export const getUsersOverview = async () => {
215
  try {
216
  const response = await fetch (
217
     'http://devcz.yoso.fi:8090/ymanager/users', {
218
      headers: {
219
        Authorization: 1          }
220
    }
221
  );
222
  
223
if (response.ok) {
224

    
225
  const data = await response.json();
226
  return data.map(user => {
227

    
228
     return (
229
        {
230
           name: user.firstName + ' ' + user.lastName,
231
           id: user.id,
232
           sickday: user.sickDayCount,
233
           holiday: user.vacationCount,
234
           role: user.role
235
        })
236
  })
237
}  else {
238
      if(response.status === 400) {
239
        throw 'error 400 GET DATA (OVERVIEW, EMPLOYER)'
240
      }
241
      else if (response.status === 500) {
242
        throw 'error 500 GET DATA (OVERVIEW, EMPLOYER)'
243
      }
244
      else {
245
        throw 'error GET DATA (OVERVIEW, EMPLOYER)'
246
        }
247
      }
248
  } catch (e) {
249
      throw 'error catch GET DATA (OVERVIEW, EMPLOYER)'
250
  }
251
}
252

    
253
// ******************** SAVE DATA to OVERVIEW - EMPLOYER ********************
254
export async function saveDataOverview(dataOverviewObject) {
255
  try {
256
    // send accepted request to server
257
        const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/settings', {
258
          headers: {
259
            Authorization: 1,
260
            'Content-Type': 'application/json',
261
          },
262
          method: 'PUT',
263
 
264
    // object which is sent to server
265
          body: JSON.stringify(dataOverviewObject),        
266
        });
267
        console.log(response.status)
268
       if (response.status === 400) {
269
        throw 'error 400 SAVE DATA (OVERVIEW, EMPLOYER)'
270
          }
271
      else if (response.status === 500) {
272
        throw 'error 500 SAVE DATA (OVERVIEW, EMPLOYER)'
273
      }
274
      else if (!response.ok) {
275
        throw 'error SAVE DATA (OVERVIEW, EMPLOYER)'
276
      }
277

    
278
  } catch (e) {
279
    throw 'error catch SAVE DATA (OVERVIEW, EMPLOYER'
280
  }
281
}
282

    
283
// ******************** LOAD DATA to SETTING - EMPLOYER ********************
284
export const getSettingData = async () =>  {
285
  try {
286
    const response = await fetch(
287
      'http://devcz.yoso.fi:8090/ymanager/settings', {
288
        headers: {
289
          Authorization: 1
290
        }
291
      });
292

    
293
      if (response.ok) {
294
      const data = await response.json();
295
      return {
296
        sickday: data.sickDayCount,
297
      }
298
    } else {
299
        if(response.status === 400) {
300
          throw 'error 400 LOADING DATA (SETTING, EMPLOYER)'
301
        }
302
        else if (response.status === 500) {
303
           throw 'error 500 LOADING DATA (SETTING, EMPLOYER)'
304
        }
305
        else {
306
           throw 'error LOADING DATA (SETTING, EMPLOYER)'
307
        }
308
      }
309
  } catch (e) {
310
    throw 'error catch LOADING DATA (SETTING, EMPLOYER)'
311
    }
312
}
313

    
314
// ******************** SAVE DATA to SETTING - EMPLOYER ********************
315
export async function saveDataSetting(dataSettingObject) {
316
  try {
317
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/settings', {
318
      headers: {
319
        'Authorization': 6,
320
        'Content-Type': 'application/json'
321
      },
322
      method: 'POST',
323
      body: JSON.stringify(dataSettingObject),
324
    });
325

    
326
    switch (response.status) {
327
      case 200:
328
        throw '...'
329
      case 500:
330
        throw ''
331
      default:
332
        throw response.statusText
333

    
334
    }
335

    
336
    if(response.status === 400) {
337
      throw 'error 400 SAVE DATA (OVERVIEW, EMPLOYER)'
338
    }
339
    else if (response.status === 500) {
340
      throw 'error 500 SAVE DATA (OVERVIEW, EMPLOYER)'
341
    }
342
    else if (!response.ok) {
343
      throw 'error SAVE DATA (OVERVIEW, EMPLOYER)'
344
      }
345
  } catch (e) {
346
      throw 'error catch SAVE DATA (OVERVIEW, EMPLOYER)'
347
    }
348
}
349

    
350
// ****************** LOAD DATA to YOUR REQUESTS - EMPLOYEE ******************
351

    
352
export async function loadYourRequests() {
353
  try {
354
    const response = await fetch(
355
      'http://devcz.yoso.fi:8090/ymanager/user/6/calendar?from=2020/06/24&status=PENDING', {
356
        headers: {
357
          Authorization: 6
358
        },
359
      }
360
    );
361

    
362
  if (response.ok) {
363
    const data = await response.json();
364
    return data;
365
  } else {
366
    if(response.status === 400) {
367
      alert('error 400 GET DATA (YOUR REQUEST)')
368
   }
369
      else if (response.status === 500) {
370
         alert ('error 500 GET DATA (YOUR REQUEST)')
371
      }
372
      else {
373
         alert('error GET DATA (YOUR REQUEST)')
374
      }
375
  }
376
} catch (e) {
377
  console.log(e)
378
  alert('error catch GET DATA (YOUR REQUEST)')
379
  }
380
}
381

    
382
// ****************** LOAD DATA - UPCOMING REQUESTS - EMPLOYER ******************
383
export async function loadAdminRequests() {
384
  try {
385
    const response = await fetch(
386
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
387
        headers: {
388
          Authorization: 1
389
        }
390
      },
391
    );
392

    
393
     if (response.ok) {
394
    const data = await response.json();
395
      return data;
396
  } else {
397
    if(response.status === 400) {
398
      alert('error 400 GET DATA (UPCOMING REQUESTS)')
399
   }
400
      else if (response.status === 500) {
401
         alert ('error 500 GET DATA (UPCOMING REQUESTS)')
402
      }
403
      else {
404
         alert('error GET DATA (UPCOMING REQUESTS)')
405
      }
406
    }
407
} catch (e) {
408
  alert('error catch GET DATA (UPCOMING REQUESTS)')
409
  } 
410
}
411

    
412
// ************** SEND ACCEPTED DATA - UPCOMING REQUESTS - EMPLOYER **************
413
export async function sendAcceptedRequest(acceptedRequests) {
414
  try {
415
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', {
416
      headers: {
417
        Authorization: 1,
418
        'Content-Type': 'application/json',
419
      },
420
      method: 'PUT',
421
      body: JSON.stringify(acceptedRequests),
422
    });
423

    
424
    if (response.ok) {
425
    return;
426

    
427
    } else {
428
      if(response.status === 400) {
429
        alert('error 400 SEND ACCEPTED DATA (UPCOMING REQUESTS)')
430
     }
431
        else if (response.status === 500) {
432
           alert ('error 500 SEND ACCEPTED DATA (UPCOMING REQUESTS)')
433
        }
434
        else {
435
           alert('error SEND ACCEPTED DATA (UPCOMING REQUESTS)')
436
        }
437
    }
438
  } catch (e) {
439
    alert('error catch SEND ACCEPTED DATA (UPCOMING REQUESTS)')
440
    }
441
}
442

    
443
// ************** SEND REJECTED DATA - UPCOMING REQUESTS - EMPLOYER **************
444

    
445
export async function sendRejectedRequest(rejectedRequest) {
446
  try {
447
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', {
448
      headers: {
449
        Authorization: 1,
450
        'Content-Type': 'application/json',
451
      },
452
      method: 'PUT',
453
      body: JSON.stringify(rejectedRequest),
454
    });
455

    
456
  if (response.ok) {    
457
    return;
458

    
459
  } else {
460
    if(response.status === 400) {
461
      alert('error 400 SEND REJECTED DATA (UPCOMING REQUESTS)')
462
   }
463
      else if (response.status === 500) {
464
         alert ('error 500 SEND REJECTED DATA (UPCOMING REQUESTS)')
465
      }
466
      else {
467
         alert('error SEND REJECTED DATA (UPCOMING REQUESTS)')
468
      }
469
  }
470
} catch (e) {
471
  alert('error catch SEND REJECTED DATA (UPCOMING REQUESTS)')
472
  }
473
}
474

    
475

    
(13-13/17)