Projekt

Obecné

Profil

Stáhnout (10.7 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpParams} from '@angular/common/http';
3

    
4
import {Calendar, CalendarEdit, PostCalendar} from '../../models/calendar.model';
5
import {BasicService} from './basic.service';
6
import {catchError} from 'rxjs/operators';
7
import {Languages, RequestStatus, RequestTypes} from '../../enums/common.enum';
8
import {NotificationSettings, UserSettings} from '../../models/settings.model';
9
import {UserProfile} from '../../models/user.model';
10
import {UserRequest} from '../../models/requests.model';
11
import {MatSnackBar} from '@angular/material';
12
import {DateFormatterService} from '../util/date-formatter.service';
13
import {TranslateService} from '@ngx-translate/core';
14

    
15
@Injectable({
16
  providedIn: 'root'
17
})
18
export class UserService extends BasicService { // dost podobny k usersService, mozna zmenit v rest api
19
  private _userUrl = this.baseUrl + '/api/user/';
20

    
21
  constructor(protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService, private dateFormater: DateFormatterService) {
22
    super(http, snackBar, translateService);
23
  }
24

    
25
  /**
26
   * Returns user profile if the user making this call
27
   * is logged as admin
28
   * UserProfile.notification might be returned as string instead of date
29
   * @param id user profile id
30
   */
31
  getUserProfile(id: number) {
32
    return this.makeGetProfileApiCall(id.toString(), null);
33
  }
34

    
35
  /**
36
   * Overloaded version of getUserProfile to filter profiles
37
   * by language
38
   * UserProfile.notification might be returned as string instead of date
39
   * @param id user profile id
40
   * @param language language to filtery by
41
   */
42
  getUserProfileWithLanguage(id: number, language: Languages) {
43
    return this.makeGetProfileApiCall(id.toString(), language);
44
  }
45

    
46
  /**
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
  }
53

    
54
  /**
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
  }
62

    
63
  /**
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
  }
71

    
72
  /**
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
  /**
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
  /**
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
  }
112

    
113
  /**
114
   * Put user settings with given id for the user
115
   * @param settings settings to be put
116
   */
117
  putUserSettings(settings: UserSettings) {
118
    return this.makePutUserSettingsApiCall(settings, null);
119
  }
120

    
121
  /**
122
   * Put user settings with given id for the user
123
   * @param settings settings to be put
124
   * @param language specified language
125
   */
126
  putUserSettingsWithLanguage(settings: UserSettings, language: Languages) {
127
    return this.makePutUserSettingsApiCall(settings, language);
128
  }
129

    
130
  putNotificationSettingsWithLanguage(settings: NotificationSettings, language: Languages) {
131
    return this.makePutNotificationSettingsApiCall(settings, language);
132
  }
133
  /**
134
   * Accept or deny user request
135
   * @param request request to accept or deny
136
   * @param type request type
137
   */
138
  putUserRequest(request: UserRequest, type: RequestTypes) {
139
    return this.makePutUserRequestApiCall(request, type, null);
140
  }
141

    
142
  /**
143
   * Accept or deny user request
144
   * @param request request to accept or deny
145
   * @param type reqeust type
146
   * @param language specify language
147
   */
148
  putUserRequestWithLanguage(request: UserRequest, type: RequestTypes, language: Languages) {
149
    return this.makePutUserRequestApiCall(request, type, language);
150
  }
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
  }
185

    
186
  /**
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
    const fromString: string = this.dateFormater.formatDate(from);
197
    let toString: string;
198
    if (to != null) {
199
      toString = this.dateFormater.formatDate(to);
200
    }
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
  }
210

    
211
  /**
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
   * @param reqType request type
216
   * @param language specify language
217
   */
218
  private makePutUserRequestApiCall(request: UserRequest, reqType: RequestTypes, language: Languages) {
219
    const httpParams: HttpParams = this.createParams({type: reqType, lang: language});
220
    const options = {params: httpParams};
221

    
222
    return this.http.put<UserRequest>(this._userUrl + 'requests', request, options)
223
      .pipe(
224
        catchError(err => this.handleError(err))
225
      );
226
  }
227

    
228
  /**
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
  private makePutUserSettingsApiCall(settings: UserSettings, language: Languages) {
235
    const httpParams: HttpParams = this.createParams({lang: language});
236
    const options = {params: httpParams};
237

    
238
    return this.http.put<UserSettings>(this._userUrl + 'settings', settings, options)
239
      .pipe(
240
        catchError(err => this.handleError(err))
241
      );
242
  }
243

    
244
  /**
245
   * Změna nastavení notifikace
246
   * 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
  /**
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
  }
276

    
277
  /**
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
    return this.http.delete(this.baseUrl + '/api/calendar/' + id + '/delete', options)
288
      .pipe(
289
        catchError(err => this.handleError(err))
290
      );
291
  }
292

    
293
  /**
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
  }
308
}
(4-4/5)