Projekt

Obecné

Profil

Stáhnout (9.25 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 {UserSettings} from '../models/settings.model';
9
import {UserProfile} from '../models/user.model';
10
import {UserRequest} from '../models/requests.model';
11

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

    
18
  constructor(protected http: HttpClient) {
19
    super(http);
20
  }
21

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

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

    
43
  /**
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
  }
50

    
51
  /**
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
  }
59

    
60
  /**
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
  }
68

    
69
  /**
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
  /**
82
   * Post user calendar using POST
83
   * @param calendar to be posted
84
   */
85
  postCalendar(calendar: PostCalendar) {
86
    return this.makePostCalendarApiCall(calendar, null);
87
  }
88

    
89
  /**
90
   * Post user calendar using POST with specified language
91
   * @param calendar to be posted
92
   * @param language specified language
93
   */
94
  postCalendarWithLanguage(calendar: PostCalendar, language: Languages) {
95
    return this.makePostCalendarApiCall(calendar, language);
96
  }
97

    
98
  /**
99
   * Put user settings with given id for the user
100
   * @param settings settings to be put
101
   */
102
  putUserSettings(settings: UserSettings) {
103
    return this.makePutUserSettingsApiCall(settings, null);
104
  }
105

    
106
  /**
107
   * Put user settings with given id for the user
108
   * @param settings settings to be put
109
   * @param language specified language
110
   */
111
  putUserSettingsWithLanguage(settings: UserSettings, language: Languages) {
112
    return this.makePutUserSettingsApiCall(settings, language);
113
  }
114

    
115
  /**
116
   * Accept or deny user request
117
   * @param request request to accept or deny
118
   * @param type request type
119
   */
120
  putUserRequest(request: UserRequest, type: RequestTypes) {
121
    return this.makePutUserRequestApiCall(request, type, null);
122
  }
123

    
124
  /**
125
   * Accept or deny user request
126
   * @param request request to accept or deny
127
   * @param type reqeust type
128
   * @param language specify language
129
   */
130
  putUserRequestWithLanguage(request: UserRequest, type: RequestTypes, language: Languages) {
131
    return this.makePutUserRequestApiCall(request, type, language);
132
  }
133

    
134
  /**
135
   * Edit calendar
136
   * @param calendarEdit calendar day to be edited
137
   * @param language specify language
138
   */
139
  putCalendarEdit(calendarEdit: CalendarEdit, language: Languages) {
140
    return this.makePutCalendarEditApiCall(calendarEdit, null);
141
  }
142

    
143
  /**
144
   * Delete calendar vacation day with given id
145
   * @param id calendar day id to be deleted
146
   * @param language specify language
147
   */
148
  deleteCalendar(id: number, language: Languages) {
149
    return this.makeDeleteCalendarApiCall(id, language);
150
  }
151

    
152
  /**
153
   * Získání profilu aktuálně přihlášeného uživatele nebo uživatele podle zadaného id.
154
   * GET /user/<{id} || me>/profile?[lang=<CZ,EN>]
155
   * @param id id of profile to get (number or 'me')
156
   * @param language filter by language
157
   */
158
  private makeGetProfileApiCall(id: string, language: string) {
159
    const httpParams: HttpParams = this.createParams({lang: language});
160
    const options = {params: httpParams};
161

    
162
    return this.http.get<UserProfile>(this._userUrl + id + '/profile', options)
163
      .pipe(
164
        catchError(err => this.handleError(err))
165
      );
166
  }
167

    
168
  /**
169
   * 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í.
170
   * GET /user/<{id} || me>/calendar?[lang=<CZ,EN>]&from=yyyy/mm/dd, [to=yyyy/mm/dd], [status=<ACCEPTED, PENDING, REJECTED>]
171
   * @param id id of calendar to get (number or 'me')
172
   * @param from mandatory param
173
   * @param to upper limit of days
174
   * @param language filter by language
175
   * @param status filter by status
176
   */
177
  private makeGetCalendarApiCall(id: string, from: Date, to: Date, language: Languages, status: RequestStatus) {
178
    const fromString: string = from.getFullYear() + '/' + from.getMonth() + '/' + from.getDate();
179
    let toString: string;
180
    if (to != null) {
181
      toString = to.getFullYear() + '/' + to.getMonth() + '/' + to.getDate();
182
    }
183

    
184
    const httpParams: HttpParams = this.createParams({lang: language, from: fromString, to: toString, status});
185
    const options = {params: httpParams};
186

    
187
    return this.http.get<Calendar[]>(this._userUrl + id + '/calendar', options)
188
      .pipe(
189
        catchError(err => this.handleError(err))
190
      );
191
  }
192

    
193
  /**
194
   * Povolení nebo zamítnutí žádosti nebo “smazání“ uživatele (změna statusu na REJECTED)
195
   * PUT /user/requests?[lang=<CZ,EN>]&type=<VACATION, AUTHORIZATION>
196
   * @param request request to accept or reject
197
   * @param reqType request type
198
   * @param language specify language
199
   */
200
  private makePutUserRequestApiCall(request: UserRequest, reqType: RequestTypes, language: Languages) {
201
    const httpParams: HttpParams = this.createParams({type: reqType, lang: language});
202
    const options = {params: httpParams};
203

    
204
    return this.http.put<UserRequest>(this._userUrl + 'requests', request, options)
205
      .pipe(
206
        catchError(err => this.handleError(err))
207
      );
208
  }
209

    
210

    
211
  /**
212
   * Změna nastavení uživatele podle id
213
   * PUT /user/settings?[lang=<CZ,EN>]
214
   * @param settings setting to be set for given user
215
   * @param language specified language
216
   */
217
  private makePutUserSettingsApiCall(settings: UserSettings, language: Languages) {
218
    const httpParams: HttpParams = this.createParams({lang: language});
219
    const options = {params: httpParams};
220

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

    
227
  /**
228
   * Vytvoření nové dovolené nebo sick day
229
   * POST /user/calendar/create?[lang=<CZ,EN>]
230
   * @param calendar calendar to be posted
231
   * @param language specified language
232
   */
233
  private makePostCalendarApiCall(calendar: PostCalendar, language: Languages) {
234
    const httpParams: HttpParams = this.createParams({lang: language});
235
    const options = {params: httpParams};
236

    
237
    return this.http.post<PostCalendar>(this._userUrl + 'calendar/create', calendar, options)
238
      .pipe(
239
        catchError(err => this.handleError(err))
240
      );
241

    
242
  }
243

    
244
  /**
245
   * Smazání dovolené nebo sickday podle jejího id
246
   * DELETE /calendar/delete?[lang=<CZ,EN>]
247
   * @param id id of calendar to delete
248
   * @param language specify language
249
   */
250
  private makeDeleteCalendarApiCall(id: number, language: Languages) {
251
    const httpParams: HttpParams = this.createParams({lang: language});
252
    const options = {params: httpParams};
253

    
254
    return this.http.delete(this._userUrl + 'calendar/' + id + '/delete', options)
255
      .pipe(
256
        catchError(err => this.handleError(err))
257
      );
258
  }
259

    
260
  /**
261
   * Editace dovolené nebo sickday podle jejího id
262
   * PUT /calendar/edit?[lang=<CZ,EN>]
263
   * @param calendarEdit calendar to edit
264
   * @param language specify language
265
   */
266
  private makePutCalendarEditApiCall(calendarEdit: CalendarEdit, language: Languages) {
267
    const httpParams: HttpParams = this.createParams({lang: language});
268
    const options = {params: httpParams};
269

    
270
    return this.http.put<CalendarEdit>(this._userUrl + 'calendar/edit', calendarEdit, options)
271
      .pipe(
272
        catchError(err => this.handleError(err))
273
      );
274
  }
275
}
(7-7/9)