user.service.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. import { environment } from '@environments/environment';
  4. import { User } from '@app/models';
  5. import { Observable } from 'rxjs/internal/Observable';
  6. import { throwError } from 'rxjs/internal/observable/throwError';
  7. import { map, catchError } from 'rxjs/operators';
  8. @Injectable({ providedIn: 'root' })
  9. export class UserService {
  10. constructor(private http: HttpClient) { }
  11. getAllUsers() {
  12. return this.http.get<User[]>(`${environment.productionApiUrl}/users`);
  13. }
  14. createUser(user: { first_name :string, last_name :string,
  15. email :string, role :number, organizations?: any }): Observable<boolean> {
  16. return this.http.post<any>(`${environment.productionApiUrl}/users`, user)
  17. .pipe(
  18. map(response => {
  19. return response;
  20. }),
  21. catchError(this.errorHandl)
  22. )
  23. }
  24. validateUserToken(user: {token :string}): Observable<boolean> {
  25. return this.http.post<any>(`${environment.productionApiUrl}/user/tokenvalidation`, user)
  26. .pipe(
  27. map(response => {
  28. return response;
  29. }),
  30. catchError(this.errorHandl)
  31. )
  32. }
  33. activateUser(user: { first_name :string, last_name :string,
  34. email :string, password: string, confirm_password :string }): Observable<boolean> {
  35. return this.http.post<any>(`${environment.productionApiUrl}/user/activate`, user)
  36. .pipe(
  37. map(response => {
  38. return response;
  39. }),
  40. catchError(this.errorHandl)
  41. )
  42. }
  43. errorHandl(error) {
  44. let errorMessage = '';
  45. if(error.error) {
  46. // Get client-side error
  47. errorMessage = error.error;
  48. } else {
  49. // Get server-side error
  50. errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
  51. }
  52. return throwError(errorMessage);
  53. }
  54. }