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

    
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
    console.log(settings);
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 uživatele podle id
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._userUrl + '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)