Projekt

Obecné

Profil

Stáhnout (7.27 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpParams} from '@angular/common/http';
3
import {BasicService} from './basic.service';
4
import {catchError} from 'rxjs/operators';
5

    
6
import {AuthorizationRequest, VacationRequest} from '../../models/requests.model';
7
import {Languages, ProfileStatus} from '../../enums/common.enum';
8
import {Observable} from 'rxjs';
9
import {UserBasicInformation, UserProfile} from '../../models/user.model';
10
import {MatSnackBar} from '@angular/material';
11
import {TranslateService} from '@ngx-translate/core';
12
import { Config } from '../util/config.service';
13

    
14
@Injectable({
15
  providedIn: 'root'
16
})
17
export class UsersService extends BasicService {
18
  private _usersUrl = this.baseUrl + '/api/users';
19

    
20
  constructor(protected config: Config, protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService) {
21
    super(config, http, snackBar, translateService);
22
  }
23

    
24
  /**
25
   * Returns profile of currently logged user
26
   * UserProfile.notification might be returned as string instead of date
27
   */
28
  getLoggedUserProfile() {
29
    return this.makeGetProfileApiCall('current', null);
30
  }
31

    
32
  /**
33
   * Returns profile of currently logged user filtered by language
34
   * UserProfile.notification might be returned as string instead of date
35
   * @param language filter profile by language
36
   */
37
  getLoggedUserProfileWithLanguage(language: Languages) {
38
    return this.makeGetProfileApiCall('current', language);
39
  }
40

    
41
  /**
42
   * Returns user profile if the user making this call
43
   * is logged as admin
44
   * UserProfile.notification might be returned as string instead of date
45
   * @param id user profile id
46
   */
47
  getUserProfile(id: number) {
48
    return this.makeGetProfileApiCall(id.toString(), null);
49
  }
50

    
51
  /**
52
   * Overloaded version of getUserProfile to filter profiles
53
   * by language
54
   * UserProfile.notification might be returned as string instead of date
55
   * @param id user profile id
56
   * @param language language to filtery by
57
   */
58
  getUserProfileWithLanguage(id: number, language: Languages) {
59
    return this.makeGetProfileApiCall(id.toString(), language);
60
  }
61

    
62
  /**
63
   * Returns all authorized users
64
   */
65
  getAuthorizedUsers() {
66
    return this.makeGetUsersApiCall(null, ProfileStatus.AUTHORIZED);
67
  }
68

    
69
  /**
70
   * Returns all authorized users with specified language
71
   * overloaded version of getAuthorizedUsers
72
   * @param language filter users based on language
73
   */
74
  getAuthorizedUsersWithLanguage(language: Languages) {
75
    return this.makeGetUsersApiCall(language, ProfileStatus.AUTHORIZED);
76
  }
77

    
78
  /**
79
   * Returns all pending users
80
   */
81
  getPendingUsers() {
82
    return this.makeGetUsersApiCall(null, ProfileStatus.PENDING);
83
  }
84

    
85
  /**
86
   * Returns all pending users with specified language
87
   * overloaded version of getPendingUsers
88
   * @param language filter users based on language
89
   */
90
  getPendingUsersWithLanguage(language: Languages) {
91
    return this.makeGetUsersApiCall(language, ProfileStatus.PENDING);
92
  }
93

    
94
  /**
95
   * Return all rejected users
96
   */
97
  getRejectedUsers() {
98
    return this.makeGetUsersApiCall(null, ProfileStatus.REJECTED);
99
  }
100

    
101
  /**
102
   * Returns all rejected users with specified language
103
   * overloaded version of getRejectedUsers
104
   * @param language filter users based on language
105
   */
106
  getRejectedUsersWithLanguage(language: Languages) {
107
    return this.makeGetUsersApiCall(language, ProfileStatus.REJECTED);
108
  }
109

    
110
 /**
111
  * Default api call which returns all users in the database
112
  * regardless of language and status
113
  */
114
  getUsers() {
115
    return this.makeGetUsersApiCall();
116
  }
117

    
118
  /**
119
   * Returns all vacation requests specified by status, if status
120
   * is not specified, returns all vacation requests
121
   * @param status optional vacation request status
122
   */
123
  getVacationRequests(status?: string) {
124
    return this.makeGetVacationRequestsApiCall( null, status);
125
  }
126

    
127
  /**
128
   * Returns vacations filtered by language
129
   * @param language filter by passed language
130
   * @param status optionalvacation request status
131
   */
132
  getVacationRequestsWithLanguage(language: Languages, status?: string) {
133
    return this.makeGetVacationRequestsApiCall(language, status);
134
  }
135

    
136

    
137
  /**
138
   * Returns all authorization requests
139
   * @param status optional authorization request status
140
   */
141
  getAuthorizationRequests(status?: string) {
142
    return this.makeGetAuthorizationRequestsApiCall(null, status);
143
  }
144

    
145

    
146
  /**
147
   * Returns authorization requests filtered by language
148
   * @param language filter by passed language
149
   * @param status optional authorization request status
150
   */
151
  getAuthorizationRequestsWithLanguage(language: Languages, status?: string) {
152
    return this.makeGetAuthorizationRequestsApiCall(language, status);
153
  }
154

    
155
  /**
156
   * Získání žádostí o autorizaci všech uživatelů s možností filtrace pomocí úrovně schválení.
157
   * GET /users/requests/authorization?[lang=<CZ,EN>]&[status=<ACCEPTED, PENDING, REJECTED>
158
   * @param language filter by language
159
   * @param status optional authorization request status
160
   */
161
  private makeGetAuthorizationRequestsApiCall(language?: string, statusReq?: string) {
162
    const httpParams: HttpParams = this.createParams({lang: language, status: statusReq});
163
    const options = {params: httpParams};
164

    
165
    return this.http.get<AuthorizationRequest[]>(this._usersUrl + '/requests/authorization', options)
166
      .pipe(
167
        catchError(err => this.handleError(err))
168
      );
169
  }
170

    
171
  /**
172
   * Získání žádostí o dovolené a sick days všech uživatelů s možností filtrace pomocí úrovně schválení.
173
   * GET /users/requests/vacation? [lang=<CZ,EN>]&[status=<ACCEPTED, PENDING, REJECTED>]
174
   * @param language filter by language
175
   * @param status vacation request status
176
   */
177
  private makeGetVacationRequestsApiCall(language?: string, statusReq?: string) {
178
    const httpParams: HttpParams = this.createParams({lang: language, status: statusReq});
179
    const options = {params: httpParams};
180

    
181
    return this.http.get<VacationRequest[]>(this._usersUrl + '/requests/vacation', options)
182
      .pipe(
183
        catchError(err => this.handleError(err))
184
      );
185
  }
186

    
187
  /**
188
   * Získání všech uživatelů s možností filtrace pomocí statusu
189
   * GET /users?[lang=<CZ,EN>]&[status=<ACCEPTED, PENDING, REJECTED>]
190
   * @param status filter by status
191
   * @param language filter by language
192
   */
193
  private makeGetUsersApiCall(language?: string, status?: string): Observable<UserBasicInformation[]> {
194
    const httpParams: HttpParams = this.createParams({lang: language, status});
195
    const options = {params: httpParams};
196

    
197
    return this.http.get<UserBasicInformation[]>(this._usersUrl, options)
198
      .pipe(
199
        catchError(err => this.handleError(err))
200
      );
201
  }
202

    
203
  /**
204
   * Získání profilu aktuálně přihlášeného uživatele nebo uživatele podle zadaného id.
205
   * GET /user/<{id} || current>/profile?[lang=<CZ,EN>]
206
   * @param id id of profile to get (number or 'current')
207
   * @param language filter by language
208
   */
209
  private makeGetProfileApiCall(id: string, language: string) {
210
    const httpParams: HttpParams = this.createParams({lang: language});
211
    const options = {params: httpParams};
212

    
213
    return this.http.get<UserProfile>(this._usersUrl + "/" + id + '/profile', options)
214
      .pipe(
215
        catchError(err => this.handleError(err))
216
      );
217
  }
218

    
219

    
220
}
(5-5/5)