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