Projekt

Obecné

Profil

Stáhnout (9.19 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, flatMap} from 'rxjs/operators';
7
import {Languages, RequestStatus, RequestTypes} from '../../enums/common.enum';
8
import {NotificationSettings, UserSettings} from '../../models/settings.model';
9
import {UserRequest} from '../../models/requests.model';
10
import {MatSnackBar} from '@angular/material';
11
import {DateFormatterService} from '../util/date-formatter.service';
12
import {TranslateService} from '@ngx-translate/core';
13
import {ProfileService} from "../util/profile.service";
14

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

    
21
  constructor(protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService, protected profileService: ProfileService, private dateFormater: DateFormatterService) {
22
    super(http, snackBar, translateService);
23
  }
24

    
25
  /**
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
    return this.profileService.getLoggedUser(true).pipe(flatMap((profile) => this.makeGetCalendarApiCall(profile.id.toString(), from, null, null, null)));
32
  }
33

    
34
  /**
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
    return this.profileService.getLoggedUser(true).pipe(flatMap((profile) => this.makeGetCalendarApiCall(profile.id.toString(), from, to, language, status)));
44
  }
45

    
46
  /**
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
  /**
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
  }
74

    
75
  /**
76
   * Put user settings with given id for the user
77
   * @param settings settings to be put
78
   */
79
  putUserSettings(settings: UserSettings) {
80
    return this.makePutUserSettingsApiCall(settings, null);
81
  }
82

    
83
  /**
84
   * Put user settings with given id for the user
85
   * @param settings settings to be put
86
   * @param language specified language
87
   */
88
  putUserSettingsWithLanguage(settings: UserSettings, language: Languages) {
89
    return this.makePutUserSettingsApiCall(settings, language);
90
  }
91

    
92
  putNotificationSettingsWithLanguage(settings: NotificationSettings, language: Languages) {
93
    return this.makePutNotificationSettingsApiCall(settings, language);
94
  }
95
  /**
96
   * Accept or deny user request
97
   * @param request request to accept or deny
98
   * @param type request type
99
   */
100
  putUserRequest(request: UserRequest, type: RequestTypes) {
101
    return this.makePutUserRequestApiCall(request, type, null);
102
  }
103

    
104
  /**
105
   * Accept or deny user request
106
   * @param request request to accept or deny
107
   * @param type reqeust type
108
   * @param language specify language
109
   */
110
  putUserRequestWithLanguage(request: UserRequest, type: RequestTypes, language: Languages) {
111
    return this.makePutUserRequestApiCall(request, type, language);
112
  }
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
    const fromString: string = this.dateFormater.formatDate(from);
143
    let toString: string;
144
    if (to != null) {
145
      toString = this.dateFormater.formatDate(to);
146
    }
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
  }
156

    
157
  /**
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
   * @param reqType request type
162
   * @param language specify language
163
   */
164
  private makePutUserRequestApiCall(request: UserRequest, reqType: RequestTypes, language: Languages) {
165
    const httpParams: HttpParams = this.createParams({type: reqType, lang: language});
166
    const options = {params: httpParams};
167

    
168
    return this.http.put<UserRequest>(this._userUrl + 'requests', request, options)
169
      .pipe(
170
        catchError(err => this.handleError(err))
171
      );
172
  }
173

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

    
184
    return this.http.put<UserSettings>(this._userUrl + 'settings', settings, options)
185
      .pipe(
186
        catchError(err => this.handleError(err))
187
      );
188
  }
189

    
190
  /**
191
   * Změna nastavení notifikace
192
   * 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
  /**
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
  }
222

    
223
  /**
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
    return this.http.delete(this.baseUrl + '/api/calendar/' + id + '/delete', options)
234
      .pipe(
235
        catchError(err => this.handleError(err))
236
      );
237
  }
238

    
239
  /**
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
  }
254
}
(4-4/5)