Projekt

Obecné

Profil

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