Projekt

Obecné

Profil

Stáhnout (10.6 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

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

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

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

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

    
45
  /**
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
  }
52

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
292
  /**
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
  }
307
}
(4-4/5)