Projekt

Obecné

Profil

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