import { Injectable } from '@angular/core' ; import { Headers , Http , RequestOptions } from "@angular/http" ; import { environment } from "../../../environments/environment" ; import { UtilsService } from "../utils.service" ; import "rxjs/add/operator/map" ; import { Observable } from "rxjs/Observable" ; import "rxjs/add/operator/catch" ; import { User } from "../../models/user.model" ; import { toPromise } from "rxjs/operator/toPromise" ; import { ContextService } from "../context.service" ; import * as _ from "lodash" ; import { utils } from "protractor" ; @ Injectable ( ) export class UsersService { constructor ( private http , private context , private utils ) { } authorizeUser ( userId , password ) { let body = { id : userId , password : password } ; if ( ! environment . production ) { body = { data : { authorization : 'authorization' } } ; } let headers = new Headers ( ) ; return this . http . post ( environment . login , body ) . toPromise ( ) . then ( ( response ) => { let data = response . json ( ) . data . authorization ; console . log ( 'Authorize' , data ) ; return data ; } ) . catch ( this . utils . catchError ) ; } createUser ( name , password , email , manager = '' , metadata = '' ) { let request = environment . create_user ( name , password , email , manager , metadata ) ; let headers = new Headers ( ) ; return this . http . post ( request . url , request . body ) . toPromise ( ) . then ( ( response ) => { return response . json ( ) . data ; } ) . catch ( this . utils . catchError ) ; } getUsers ( ) { return this . http . get ( environment . users , { headers : this . context . httpHeaders ( ) } ) . toPromise ( ) . then ( ( response ) => { console . log ( 'Users: ' , response . json ( ) . data ) ; return response . json ( ) . data ; } ) . catch ( this . utils . catchError ) ; } getUserRequests ( ) { let url = environment . user_proposals ( this . context . getUser ( ) . id ) ; return this . http . get ( url , { headers : this . context . httpHeaders ( ) } ) . toPromise ( ) . then ( ( response ) => { console . log ( 'User Proposals Open' , response . json ( ) . data ) ; return response . json ( ) . data ; } ) . catch ( this . utils . catchError ) ; } getUser ( userId ) { let request = environment . get_user ( userId ) ; return this . http . get ( request . url , this . context . httpOptions ( ) ) . toPromise ( ) . then ( ( response ) => { console . log ( 'User: ' , response . json ( ) . data ) ; return response . json ( ) . data ; } ) . catch ( this . utils . catchError ) ; } }