Projekt

Obecné

Profil

Stáhnout (9.25 KB) Statistiky
| Větev: | Tag: | Revize:
1 4bcef705 Hung Hoang
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpParams} from '@angular/common/http';
3 41741550 Hung Hoang
4 4bcef705 Hung Hoang
import {Calendar, CalendarEdit, PostCalendar} from '../models/calendar.model';
5
import {BasicService} from './basic.service';
6
import {catchError} from 'rxjs/operators';
7 ecf648bb Hung Hoang
import {Languages, RequestStatus, RequestTypes} from '../enums/common.enum';
8 f8b40fd5 Hung Hoang
import {UserSettings} from '../models/settings.model';
9 3fcf8b67 Hung Hoang
import {UserProfile} from '../models/user.model';
10
import {UserRequest} from '../models/requests.model';
11 41741550 Hung Hoang
12
@Injectable({
13
  providedIn: 'root'
14
})
15
export class UserService extends BasicService { // dost podobny k usersService, mozna zmenit v rest api
16 4bcef705 Hung Hoang
  private _userUrl = this.baseUrl + '/api/user/';
17 41741550 Hung Hoang
18 4bcef705 Hung Hoang
  constructor(protected http: HttpClient) {
19
    super(http);
20 41741550 Hung Hoang
  }
21
22 4bcef705 Hung Hoang
  /**
23 f8b40fd5 Hung Hoang
   * Returns user profile if the user making this call
24 4bcef705 Hung Hoang
   * is logged as admin
25
   * UserProfile.notification might be returned as string instead of date
26
   * @param id employee id
27
   */
28 f8b40fd5 Hung Hoang
  getUserProfile(id: number) {
29
    return this.makeGetProfileApiCall(id.toString(), null);
30 41741550 Hung Hoang
  }
31
32 4bcef705 Hung Hoang
  /**
33 f8b40fd5 Hung Hoang
   * Overloaded version of getUserProfile to filter profiles
34 4bcef705 Hung Hoang
   * by language
35
   * UserProfile.notification might be returned as string instead of date
36 f8b40fd5 Hung Hoang
   * @param id user profile id
37 4bcef705 Hung Hoang
   * @param language language to filtery by
38
   */
39 f8b40fd5 Hung Hoang
  getUserProfileWithLanguage(id: number, language: Languages) {
40 4bcef705 Hung Hoang
    return this.makeGetProfileApiCall(id.toString(), language);
41 41741550 Hung Hoang
  }
42
43 4bcef705 Hung Hoang
  /**
44
   * Returns profile of currently logged user
45
   * UserProfile.notification might be returned as string instead of date
46
   */
47
  getLoggedUserProfile() {
48
    return this.makeGetProfileApiCall('me', null);
49 41741550 Hung Hoang
  }
50
51 4bcef705 Hung Hoang
  /**
52
   * Returns profile of currently logged user filtered by language
53
   * UserProfile.notification might be returned as string instead of date
54
   * @param language filter profile by language
55
   */
56
  getLoggedUserProfileWithLanguage(language: Languages) {
57
    return this.makeGetProfileApiCall('me', language);
58 41741550 Hung Hoang
  }
59
60 4bcef705 Hung Hoang
  /**
61
   * Returns vacation and sick days from the given date
62
   * for logged user
63
   * @param from returns days from this date forward
64
   */
65
  getLoggedUserCalendar(from: Date) {
66
    return this.makeGetCalendarApiCall('me', from, null, null, null);
67 41741550 Hung Hoang
  }
68
69 4bcef705 Hung Hoang
  /**
70
   * Returns vacation and sick days from the given date
71
   * for logged user
72
   * @param from returns days from this date forward
73
   * @param to limit returned days, returns <from, to>
74
   * @param language filter by language
75
   * @param status filter by status
76
   */
77
  getLoggedUserCalendarWithOptions(from: Date, to: Date, language: Languages, status: RequestStatus) {
78
    return this.makeGetCalendarApiCall('me', from, to, language, status);
79
  }
80
81
  /**
82
   * Post user calendar using POST
83
   * @param calendar to be posted
84
   */
85
  postCalendar(calendar: PostCalendar) {
86
    return this.makePostCalendarApiCall(calendar, null);
87
  }
88
89
  /**
90
   * Post user calendar using POST with specified language
91
   * @param calendar to be posted
92
   * @param language specified language
93
   */
94
  postCalendarWithLanguage(calendar: PostCalendar, language: Languages) {
95
    return this.makePostCalendarApiCall(calendar, language);
96 41741550 Hung Hoang
  }
97
98 4bcef705 Hung Hoang
  /**
99
   * Put user settings with given id for the user
100
   * @param settings settings to be put
101
   */
102 f8b40fd5 Hung Hoang
  putUserSettings(settings: UserSettings) {
103 4bcef705 Hung Hoang
    return this.makePutUserSettingsApiCall(settings, null);
104 41741550 Hung Hoang
  }
105
106 4bcef705 Hung Hoang
  /**
107
   * Put user settings with given id for the user
108
   * @param settings settings to be put
109
   * @param language specified language
110
   */
111 f8b40fd5 Hung Hoang
  putUserSettingsWithLanguage(settings: UserSettings, language: Languages) {
112 4bcef705 Hung Hoang
    return this.makePutUserSettingsApiCall(settings, language);
113 41741550 Hung Hoang
  }
114
115 4bcef705 Hung Hoang
  /**
116
   * Accept or deny user request
117
   * @param request request to accept or deny
118 6916e74c Hung Hoang
   * @param type request type
119 4bcef705 Hung Hoang
   */
120 ecf648bb Hung Hoang
  putUserRequest(request: UserRequest, type: RequestTypes) {
121
    return this.makePutUserRequestApiCall(request, type, null);
122 4bcef705 Hung Hoang
  }
123
124
  /**
125
   * Accept or deny user request
126
   * @param request request to accept or deny
127 6916e74c Hung Hoang
   * @param type reqeust type
128 4bcef705 Hung Hoang
   * @param language specify language
129
   */
130 ecf648bb Hung Hoang
  putUserRequestWithLanguage(request: UserRequest, type: RequestTypes, language: Languages) {
131
    return this.makePutUserRequestApiCall(request, type, language);
132 4bcef705 Hung Hoang
  }
133
134
  /**
135
   * Edit calendar
136
   * @param calendarEdit calendar day to be edited
137
   * @param language specify language
138
   */
139
  putCalendarEdit(calendarEdit: CalendarEdit, language: Languages) {
140
    return this.makePutCalendarEditApiCall(calendarEdit, null);
141
  }
142
143
  /**
144
   * Delete calendar vacation day with given id
145
   * @param id calendar day id to be deleted
146
   * @param language specify language
147
   */
148
  deleteCalendar(id: number, language: Languages) {
149
    return this.makeDeleteCalendarApiCall(id, language);
150
  }
151
152
  /**
153
   * Získání profilu aktuálně přihlášeného uživatele nebo uživatele podle zadaného id.
154
   * GET /user/<{id} || me>/profile?[lang=<CZ,EN>]
155
   * @param id id of profile to get (number or 'me')
156
   * @param language filter by language
157
   */
158
  private makeGetProfileApiCall(id: string, language: string) {
159
    const httpParams: HttpParams = this.createParams({lang: language});
160
    const options = {params: httpParams};
161
162
    return this.http.get<UserProfile>(this._userUrl + id + '/profile', options)
163
      .pipe(
164
        catchError(err => this.handleError(err))
165
      );
166 41741550 Hung Hoang
  }
167
168 4bcef705 Hung Hoang
  /**
169
   * Získání dovolené a sick days v zadaném období. Pokud není zadán parameter “to” vrátí všechny dovolené a sick days od “from”. Navíc umožňuje filtrovat pomocí statusu schválení.
170
   * GET /user/<{id} || me>/calendar?[lang=<CZ,EN>]&from=yyyy/mm/dd, [to=yyyy/mm/dd], [status=<ACCEPTED, PENDING, REJECTED>]
171
   * @param id id of calendar to get (number or 'me')
172
   * @param from mandatory param
173
   * @param to upper limit of days
174
   * @param language filter by language
175
   * @param status filter by status
176
   */
177
  private makeGetCalendarApiCall(id: string, from: Date, to: Date, language: Languages, status: RequestStatus) {
178
    const fromString: string = from.getFullYear() + '/' + from.getMonth() + '/' + from.getDate();
179
    let toString: string;
180
    if (to != null) {
181
      toString = to.getFullYear() + '/' + to.getMonth() + '/' + to.getDate();
182
    }
183
184
    const httpParams: HttpParams = this.createParams({lang: language, from: fromString, to: toString, status});
185
    const options = {params: httpParams};
186
187
    return this.http.get<Calendar[]>(this._userUrl + id + '/calendar', options)
188
      .pipe(
189
        catchError(err => this.handleError(err))
190
      );
191 41741550 Hung Hoang
  }
192
193 4bcef705 Hung Hoang
  /**
194
   * Povolení nebo zamítnutí žádosti nebo “smazání“ uživatele (změna statusu na REJECTED)
195
   * PUT /user/requests?[lang=<CZ,EN>]&type=<VACATION, AUTHORIZATION>
196
   * @param request request to accept or reject
197 6916e74c Hung Hoang
   * @param reqType request type
198 4bcef705 Hung Hoang
   * @param language specify language
199
   */
200 ecf648bb Hung Hoang
  private makePutUserRequestApiCall(request: UserRequest, reqType: RequestTypes, language: Languages) {
201
    const httpParams: HttpParams = this.createParams({type: reqType, lang: language});
202 4bcef705 Hung Hoang
    const options = {params: httpParams};
203
204 ecf648bb Hung Hoang
    return this.http.put<UserRequest>(this._userUrl + 'requests', request, options)
205 41741550 Hung Hoang
      .pipe(
206
        catchError(err => this.handleError(err))
207
      );
208
  }
209
210 4bcef705 Hung Hoang
211
  /**
212
   * Změna nastavení uživatele podle id
213
   * PUT /user/settings?[lang=<CZ,EN>]
214
   * @param settings setting to be set for given user
215
   * @param language specified language
216
   */
217 f8b40fd5 Hung Hoang
  private makePutUserSettingsApiCall(settings: UserSettings, language: Languages) {
218 4bcef705 Hung Hoang
    const httpParams: HttpParams = this.createParams({lang: language});
219
    const options = {params: httpParams};
220
221 f8b40fd5 Hung Hoang
    return this.http.put<UserSettings>(this._userUrl + 'settings', settings, options)
222 4bcef705 Hung Hoang
      .pipe(
223
        catchError(err => this.handleError(err))
224
      );
225 41741550 Hung Hoang
  }
226
227 4bcef705 Hung Hoang
  /**
228
   * Vytvoření nové dovolené nebo sick day
229
   * POST /user/calendar/create?[lang=<CZ,EN>]
230
   * @param calendar calendar to be posted
231
   * @param language specified language
232
   */
233
  private makePostCalendarApiCall(calendar: PostCalendar, language: Languages) {
234
    const httpParams: HttpParams = this.createParams({lang: language});
235
    const options = {params: httpParams};
236
237
    return this.http.post<PostCalendar>(this._userUrl + 'calendar/create', calendar, options)
238
      .pipe(
239
        catchError(err => this.handleError(err))
240
      );
241
242 41741550 Hung Hoang
  }
243
244 4bcef705 Hung Hoang
  /**
245
   * Smazání dovolené nebo sickday podle jejího id
246
   * DELETE /calendar/delete?[lang=<CZ,EN>]
247
   * @param id id of calendar to delete
248
   * @param language specify language
249
   */
250
  private makeDeleteCalendarApiCall(id: number, language: Languages) {
251
    const httpParams: HttpParams = this.createParams({lang: language});
252
    const options = {params: httpParams};
253
254 6916e74c Hung Hoang
    return this.http.delete(this._userUrl + 'calendar/' + id + '/delete', options)
255 41741550 Hung Hoang
      .pipe(
256
        catchError(err => this.handleError(err))
257
      );
258
  }
259
260 4bcef705 Hung Hoang
  /**
261
   * Editace dovolené nebo sickday podle jejího id
262
   * PUT /calendar/edit?[lang=<CZ,EN>]
263
   * @param calendarEdit calendar to edit
264
   * @param language specify language
265
   */
266
  private makePutCalendarEditApiCall(calendarEdit: CalendarEdit, language: Languages) {
267
    const httpParams: HttpParams = this.createParams({lang: language});
268
    const options = {params: httpParams};
269
270
    return this.http.put<CalendarEdit>(this._userUrl + 'calendar/edit', calendarEdit, options)
271
      .pipe(
272
        catchError(err => this.handleError(err))
273
      );
274 41741550 Hung Hoang
  }
275
}