Projekt

Obecné

Profil

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