Projekt

Obecné

Profil

Stáhnout (9.86 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
import {UserSettings} from '../../models/settings.model';
9
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 4bcef705 Hung Hoang
  /**
130
   * Accept or deny user request
131
   * @param request request to accept or deny
132 6916e74c Hung Hoang
   * @param type request type
133 4bcef705 Hung Hoang
   */
134 ecf648bb Hung Hoang
  putUserRequest(request: UserRequest, type: RequestTypes) {
135
    return this.makePutUserRequestApiCall(request, type, null);
136 4bcef705 Hung Hoang
  }
137
138
  /**
139
   * Accept or deny user request
140
   * @param request request to accept or deny
141 6916e74c Hung Hoang
   * @param type reqeust type
142 4bcef705 Hung Hoang
   * @param language specify language
143
   */
144 ecf648bb Hung Hoang
  putUserRequestWithLanguage(request: UserRequest, type: RequestTypes, language: Languages) {
145
    return this.makePutUserRequestApiCall(request, type, language);
146 4bcef705 Hung Hoang
  }
147
148
  /**
149
   * Edit calendar
150
   * @param calendarEdit calendar day to be edited
151
   * @param language specify language
152
   */
153
  putCalendarEdit(calendarEdit: CalendarEdit, language: Languages) {
154
    return this.makePutCalendarEditApiCall(calendarEdit, null);
155
  }
156
157
  /**
158
   * Delete calendar vacation day with given id
159
   * @param id calendar day id to be deleted
160
   * @param language specify language
161
   */
162
  deleteCalendar(id: number, language: Languages) {
163
    return this.makeDeleteCalendarApiCall(id, language);
164
  }
165
166
  /**
167
   * Získání profilu aktuálně přihlášeného uživatele nebo uživatele podle zadaného id.
168
   * GET /user/<{id} || me>/profile?[lang=<CZ,EN>]
169
   * @param id id of profile to get (number or 'me')
170
   * @param language filter by language
171
   */
172
  private makeGetProfileApiCall(id: string, language: string) {
173
    const httpParams: HttpParams = this.createParams({lang: language});
174
    const options = {params: httpParams};
175
176
    return this.http.get<UserProfile>(this._userUrl + id + '/profile', options)
177
      .pipe(
178
        catchError(err => this.handleError(err))
179
      );
180 41741550 Hung Hoang
  }
181
182 4bcef705 Hung Hoang
  /**
183
   * 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í.
184
   * GET /user/<{id} || me>/calendar?[lang=<CZ,EN>]&from=yyyy/mm/dd, [to=yyyy/mm/dd], [status=<ACCEPTED, PENDING, REJECTED>]
185
   * @param id id of calendar to get (number or 'me')
186
   * @param from mandatory param
187
   * @param to upper limit of days
188
   * @param language filter by language
189
   * @param status filter by status
190
   */
191
  private makeGetCalendarApiCall(id: string, from: Date, to: Date, language: Languages, status: RequestStatus) {
192 696f3358 Václav Jirák
    const fromString: string = this.dateFormater.formatDate(from);
193 4bcef705 Hung Hoang
    let toString: string;
194
    if (to != null) {
195 696f3358 Václav Jirák
      toString = this.dateFormater.formatDate(to);
196 4bcef705 Hung Hoang
    }
197
198
    const httpParams: HttpParams = this.createParams({lang: language, from: fromString, to: toString, status});
199
    const options = {params: httpParams};
200
201
    return this.http.get<Calendar[]>(this._userUrl + id + '/calendar', options)
202
      .pipe(
203
        catchError(err => this.handleError(err))
204
      );
205 41741550 Hung Hoang
  }
206
207 4bcef705 Hung Hoang
  /**
208
   * Povolení nebo zamítnutí žádosti nebo “smazání“ uživatele (změna statusu na REJECTED)
209
   * PUT /user/requests?[lang=<CZ,EN>]&type=<VACATION, AUTHORIZATION>
210
   * @param request request to accept or reject
211 6916e74c Hung Hoang
   * @param reqType request type
212 4bcef705 Hung Hoang
   * @param language specify language
213
   */
214 ecf648bb Hung Hoang
  private makePutUserRequestApiCall(request: UserRequest, reqType: RequestTypes, language: Languages) {
215
    const httpParams: HttpParams = this.createParams({type: reqType, lang: language});
216 4bcef705 Hung Hoang
    const options = {params: httpParams};
217
218 ecf648bb Hung Hoang
    return this.http.put<UserRequest>(this._userUrl + 'requests', request, options)
219 41741550 Hung Hoang
      .pipe(
220
        catchError(err => this.handleError(err))
221
      );
222
  }
223
224 4bcef705 Hung Hoang
225
  /**
226
   * Změna nastavení uživatele podle id
227
   * PUT /user/settings?[lang=<CZ,EN>]
228
   * @param settings setting to be set for given user
229
   * @param language specified language
230
   */
231 f8b40fd5 Hung Hoang
  private makePutUserSettingsApiCall(settings: UserSettings, language: Languages) {
232 4bcef705 Hung Hoang
    const httpParams: HttpParams = this.createParams({lang: language});
233
    const options = {params: httpParams};
234
235 f8b40fd5 Hung Hoang
    return this.http.put<UserSettings>(this._userUrl + 'settings', settings, options)
236 4bcef705 Hung Hoang
      .pipe(
237
        catchError(err => this.handleError(err))
238
      );
239 41741550 Hung Hoang
  }
240
241 4bcef705 Hung Hoang
  /**
242
   * Vytvoření nové dovolené nebo sick day
243
   * POST /user/calendar/create?[lang=<CZ,EN>]
244
   * @param calendar calendar to be posted
245
   * @param language specified language
246
   */
247
  private makePostCalendarApiCall(calendar: PostCalendar, language: Languages) {
248
    const httpParams: HttpParams = this.createParams({lang: language});
249
    const options = {params: httpParams};
250
251
    return this.http.post<PostCalendar>(this._userUrl + 'calendar/create', calendar, options)
252
      .pipe(
253
        catchError(err => this.handleError(err))
254
      );
255
256 41741550 Hung Hoang
  }
257
258 4bcef705 Hung Hoang
  /**
259
   * Smazání dovolené nebo sickday podle jejího id
260
   * DELETE /calendar/delete?[lang=<CZ,EN>]
261
   * @param id id of calendar to delete
262
   * @param language specify language
263
   */
264
  private makeDeleteCalendarApiCall(id: number, language: Languages) {
265
    const httpParams: HttpParams = this.createParams({lang: language});
266
    const options = {params: httpParams};
267
268 6916e74c Hung Hoang
    return this.http.delete(this._userUrl + 'calendar/' + id + '/delete', options)
269 41741550 Hung Hoang
      .pipe(
270
        catchError(err => this.handleError(err))
271
      );
272
  }
273
274 4bcef705 Hung Hoang
  /**
275
   * Editace dovolené nebo sickday podle jejího id
276
   * PUT /calendar/edit?[lang=<CZ,EN>]
277
   * @param calendarEdit calendar to edit
278
   * @param language specify language
279
   */
280
  private makePutCalendarEditApiCall(calendarEdit: CalendarEdit, language: Languages) {
281
    const httpParams: HttpParams = this.createParams({lang: language});
282
    const options = {params: httpParams};
283
284
    return this.http.put<CalendarEdit>(this._userUrl + 'calendar/edit', calendarEdit, options)
285
      .pipe(
286
        catchError(err => this.handleError(err))
287
      );
288 41741550 Hung Hoang
  }
289
}