Projekt

Obecné

Profil

Stáhnout (9.19 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 9223bc22 Jakub Danek
import {catchError, flatMap} from 'rxjs/operators';
7 fd5ab42e Hung Hoang
import {Languages, RequestStatus, RequestTypes} from '../../enums/common.enum';
8 9cc55d8d Václav Jirák
import {NotificationSettings, UserSettings} from '../../models/settings.model';
9 fd5ab42e Hung Hoang
import {UserRequest} from '../../models/requests.model';
10 1d169f6d Hung Hoang
import {MatSnackBar} from '@angular/material';
11 696f3358 Václav Jirák
import {DateFormatterService} from '../util/date-formatter.service';
12 5d32cbea Václav Jirák
import {TranslateService} from '@ngx-translate/core';
13 9223bc22 Jakub Danek
import {ProfileService} from "../util/profile.service";
14 41741550 Hung Hoang
15
@Injectable({
16
  providedIn: 'root'
17
})
18
export class UserService extends BasicService { // dost podobny k usersService, mozna zmenit v rest api
19 9223bc22 Jakub Danek
  private _userUrl = this.baseUrl + '/api/user/';
20 41741550 Hung Hoang
21 9223bc22 Jakub Danek
  constructor(protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService, protected profileService: ProfileService, private dateFormater: DateFormatterService) {
22 5d32cbea Václav Jirák
    super(http, snackBar, translateService);
23 41741550 Hung Hoang
  }
24
25 4bcef705 Hung Hoang
  /**
26
   * Returns vacation and sick days from the given date
27
   * for logged user
28
   * @param from returns days from this date forward
29
   */
30
  getLoggedUserCalendar(from: Date) {
31 9223bc22 Jakub Danek
    return this.profileService.getLoggedUser(true).pipe(flatMap((profile) => this.makeGetCalendarApiCall(profile.id.toString(), from, null, null, null)));
32 41741550 Hung Hoang
  }
33
34 4bcef705 Hung Hoang
  /**
35
   * Returns vacation and sick days from the given date
36
   * for logged user
37
   * @param from returns days from this date forward
38
   * @param to limit returned days, returns <from, to>
39
   * @param language filter by language
40
   * @param status filter by status
41
   */
42
  getLoggedUserCalendarWithOptions(from: Date, to: Date, language: Languages, status: RequestStatus) {
43 9223bc22 Jakub Danek
    return this.profileService.getLoggedUser(true).pipe(flatMap((profile) => this.makeGetCalendarApiCall(profile.id.toString(), from, to, language, status)));
44 4bcef705 Hung Hoang
  }
45
46 366930a6 Václav Jirák
  /**
47
   * Returns vacation and sick days in interval between given dates
48
   * @param id user's id
49
   * @param from days from this date forward
50
   * @param to limit returned days, returns <from, to>
51
   * @param language error's language
52
   * @param status filter by status
53
   */
54
  getUserCalendarWithOptions(id: string, from: Date, to: Date, language: Languages, status: RequestStatus) {
55
    return this.makeGetCalendarApiCall(id, from, to, language, status);
56
  }
57
58 4bcef705 Hung Hoang
  /**
59
   * Post user calendar using POST
60
   * @param calendar to be posted
61
   */
62
  postCalendar(calendar: PostCalendar) {
63
    return this.makePostCalendarApiCall(calendar, null);
64
  }
65
66
  /**
67
   * Post user calendar using POST with specified language
68
   * @param calendar to be posted
69
   * @param language specified language
70
   */
71
  postCalendarWithLanguage(calendar: PostCalendar, language: Languages) {
72
    return this.makePostCalendarApiCall(calendar, language);
73 41741550 Hung Hoang
  }
74
75 4bcef705 Hung Hoang
  /**
76
   * Put user settings with given id for the user
77
   * @param settings settings to be put
78
   */
79 f8b40fd5 Hung Hoang
  putUserSettings(settings: UserSettings) {
80 4bcef705 Hung Hoang
    return this.makePutUserSettingsApiCall(settings, null);
81 41741550 Hung Hoang
  }
82
83 4bcef705 Hung Hoang
  /**
84
   * Put user settings with given id for the user
85
   * @param settings settings to be put
86
   * @param language specified language
87
   */
88 f8b40fd5 Hung Hoang
  putUserSettingsWithLanguage(settings: UserSettings, language: Languages) {
89 4bcef705 Hung Hoang
    return this.makePutUserSettingsApiCall(settings, language);
90 41741550 Hung Hoang
  }
91
92 9cc55d8d Václav Jirák
  putNotificationSettingsWithLanguage(settings: NotificationSettings, language: Languages) {
93
    return this.makePutNotificationSettingsApiCall(settings, language);
94
  }
95 4bcef705 Hung Hoang
  /**
96
   * Accept or deny user request
97
   * @param request request to accept or deny
98 6916e74c Hung Hoang
   * @param type request type
99 4bcef705 Hung Hoang
   */
100 ecf648bb Hung Hoang
  putUserRequest(request: UserRequest, type: RequestTypes) {
101
    return this.makePutUserRequestApiCall(request, type, null);
102 4bcef705 Hung Hoang
  }
103
104
  /**
105
   * Accept or deny user request
106
   * @param request request to accept or deny
107 6916e74c Hung Hoang
   * @param type reqeust type
108 4bcef705 Hung Hoang
   * @param language specify language
109
   */
110 ecf648bb Hung Hoang
  putUserRequestWithLanguage(request: UserRequest, type: RequestTypes, language: Languages) {
111
    return this.makePutUserRequestApiCall(request, type, language);
112 4bcef705 Hung Hoang
  }
113
114
  /**
115
   * Edit calendar
116
   * @param calendarEdit calendar day to be edited
117
   * @param language specify language
118
   */
119
  putCalendarEdit(calendarEdit: CalendarEdit, language: Languages) {
120
    return this.makePutCalendarEditApiCall(calendarEdit, null);
121
  }
122
123
  /**
124
   * Delete calendar vacation day with given id
125
   * @param id calendar day id to be deleted
126
   * @param language specify language
127
   */
128
  deleteCalendar(id: number, language: Languages) {
129
    return this.makeDeleteCalendarApiCall(id, language);
130
  }
131
132
  /**
133
   * 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í.
134
   * GET /user/<{id} || me>/calendar?[lang=<CZ,EN>]&from=yyyy/mm/dd, [to=yyyy/mm/dd], [status=<ACCEPTED, PENDING, REJECTED>]
135
   * @param id id of calendar to get (number or 'me')
136
   * @param from mandatory param
137
   * @param to upper limit of days
138
   * @param language filter by language
139
   * @param status filter by status
140
   */
141
  private makeGetCalendarApiCall(id: string, from: Date, to: Date, language: Languages, status: RequestStatus) {
142 696f3358 Václav Jirák
    const fromString: string = this.dateFormater.formatDate(from);
143 4bcef705 Hung Hoang
    let toString: string;
144
    if (to != null) {
145 696f3358 Václav Jirák
      toString = this.dateFormater.formatDate(to);
146 4bcef705 Hung Hoang
    }
147
148
    const httpParams: HttpParams = this.createParams({lang: language, from: fromString, to: toString, status});
149
    const options = {params: httpParams};
150
151
    return this.http.get<Calendar[]>(this._userUrl + id + '/calendar', options)
152
      .pipe(
153
        catchError(err => this.handleError(err))
154
      );
155 41741550 Hung Hoang
  }
156
157 4bcef705 Hung Hoang
  /**
158
   * Povolení nebo zamítnutí žádosti nebo “smazání“ uživatele (změna statusu na REJECTED)
159
   * PUT /user/requests?[lang=<CZ,EN>]&type=<VACATION, AUTHORIZATION>
160
   * @param request request to accept or reject
161 6916e74c Hung Hoang
   * @param reqType request type
162 4bcef705 Hung Hoang
   * @param language specify language
163
   */
164 ecf648bb Hung Hoang
  private makePutUserRequestApiCall(request: UserRequest, reqType: RequestTypes, language: Languages) {
165
    const httpParams: HttpParams = this.createParams({type: reqType, lang: language});
166 4bcef705 Hung Hoang
    const options = {params: httpParams};
167
168 ecf648bb Hung Hoang
    return this.http.put<UserRequest>(this._userUrl + 'requests', request, options)
169 41741550 Hung Hoang
      .pipe(
170
        catchError(err => this.handleError(err))
171
      );
172
  }
173
174 4bcef705 Hung Hoang
  /**
175
   * Změna nastavení uživatele podle id
176
   * PUT /user/settings?[lang=<CZ,EN>]
177
   * @param settings setting to be set for given user
178
   * @param language specified language
179
   */
180 f8b40fd5 Hung Hoang
  private makePutUserSettingsApiCall(settings: UserSettings, language: Languages) {
181 4bcef705 Hung Hoang
    const httpParams: HttpParams = this.createParams({lang: language});
182
    const options = {params: httpParams};
183
184 f8b40fd5 Hung Hoang
    return this.http.put<UserSettings>(this._userUrl + 'settings', settings, options)
185 4bcef705 Hung Hoang
      .pipe(
186
        catchError(err => this.handleError(err))
187
      );
188 41741550 Hung Hoang
  }
189
190 9cc55d8d Václav Jirák
  /**
191 6496dcfb Václav Jirák
   * Změna nastavení notifikace
192 9cc55d8d Václav Jirák
   * PUT /user/settings?[lang=<CZ,EN>]
193
   * @param settings notification setting to be set for given user
194
   * @param language specified language
195
   */
196
  private makePutNotificationSettingsApiCall(settings: NotificationSettings, language: Languages) {
197
    const httpParams: HttpParams = this.createParams({lang: language});
198
    const options = {params: httpParams};
199
200
    return this.http.put<NotificationSettings>(this._userUrl + 'settings', settings, options)
201
      .pipe(
202
        catchError(err => this.handleError(err))
203
      );
204
  }
205
206 4bcef705 Hung Hoang
  /**
207
   * Vytvoření nové dovolené nebo sick day
208
   * POST /user/calendar/create?[lang=<CZ,EN>]
209
   * @param calendar calendar to be posted
210
   * @param language specified language
211
   */
212
  private makePostCalendarApiCall(calendar: PostCalendar, language: Languages) {
213
    const httpParams: HttpParams = this.createParams({lang: language});
214
    const options = {params: httpParams};
215
216
    return this.http.post<PostCalendar>(this._userUrl + 'calendar/create', calendar, options)
217
      .pipe(
218
        catchError(err => this.handleError(err))
219
      );
220
221 41741550 Hung Hoang
  }
222
223 4bcef705 Hung Hoang
  /**
224
   * Smazání dovolené nebo sickday podle jejího id
225
   * DELETE /calendar/delete?[lang=<CZ,EN>]
226
   * @param id id of calendar to delete
227
   * @param language specify language
228
   */
229
  private makeDeleteCalendarApiCall(id: number, language: Languages) {
230
    const httpParams: HttpParams = this.createParams({lang: language});
231
    const options = {params: httpParams};
232
233 b9bbb1d8 Hung Hoang
    return this.http.delete(this.baseUrl + '/api/calendar/' + id + '/delete', options)
234 41741550 Hung Hoang
      .pipe(
235
        catchError(err => this.handleError(err))
236
      );
237
  }
238
239 4bcef705 Hung Hoang
  /**
240
   * Editace dovolené nebo sickday podle jejího id
241
   * PUT /calendar/edit?[lang=<CZ,EN>]
242
   * @param calendarEdit calendar to edit
243
   * @param language specify language
244
   */
245
  private makePutCalendarEditApiCall(calendarEdit: CalendarEdit, language: Languages) {
246
    const httpParams: HttpParams = this.createParams({lang: language});
247
    const options = {params: httpParams};
248
249
    return this.http.put<CalendarEdit>(this._userUrl + 'calendar/edit', calendarEdit, options)
250
      .pipe(
251
        catchError(err => this.handleError(err))
252
      );
253 41741550 Hung Hoang
  }
254
}