Projekt

Obecné

Profil

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