usuario.service.ts 1.72 KB
Newer Older
1
import { Injectable } from '@angular/core';
2
import { Router } from '@angular/router';
3
4
5
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Usuario, Configuracion } from '../objects/usuario';
6
import { TranslateService } from '@ngx-translate/core';
7
import { AuthenticationService } from './authentication.service';
8
9
10
11
12
13
14
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/catch'

import { SERVER } from '../config';

@Injectable()
export class UsuarioService {
15
16
17
18
19
20
21
22
    translateService: any;

    constructor(
        private http: Http, private router: Router,
        private authService: AuthenticationService,
        public translate: TranslateService) {
            this.translateService = translate;
        }
23
24

    actualizarConfiguracion(cedula: string, config: Configuracion) {
25
        let headers = new Headers({ 'Content-Type': 'application/json', 'Authorization':'Bearer '+this.authService.getToken()  });
26
27
28
29
30
31
32
33
34
35
36
37
        let options = new RequestOptions({ headers: headers });
        return this.http.put(SERVER + '/servicios/usuario/' + cedula + "/configuracion", config, options)
            .map(this.extractData)
            .catch(this.handleError);
    }

    private extractData(res: Response) {
        let body = res.json();
        return body || [];
    }

    private handleError(error: any) {
38
        if(error.status == 401){
39
            this.router.navigate(['/'+this.translateService.get('i18n.code').value+'/login']);
40
        }
41
42
43
44
45
46
        let errMsg = (error.message) ? error.message :
            error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        console.error(errMsg); // log to console instead
        return Observable.throw(errMsg);
    }
}