import { Injectable } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { Router } from '@angular/router' ; import { Observable , BehaviorSubject } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { ApiService } from './api.service' ; import { GlobalService } from './global.service' ; import { AlertService } from '../alerts/alert.service' ; import { User } from '@eda/models/model.index' ; import Swal from 'sweetalert2' ; @ Injectable ( ) export class UserService extends ApiService { private route = '/admin/user' ; public user : User ; public isAdmin : boolean ; public token : string ; constructor ( protected http , private router , private globalService , private alertService ) { super ( http ) ; this . loadStorage ( ) ; } getUsers ( ) { return this . get ( this . route ) ; } getUser ( id ) { return this . get ( `${ this . route }/${ id }` ) ; } getIsAdminUser ( id ) { return this . get ( `${ this . route }/is-admin/${ id }` ) ; } searchUser ( param ) { return this . globalService . search ( param ) . pipe ( map ( ( res ) => ( res . users ) ) ) ; } /** Create new user */ createUser ( user ) { return this . post ( this . route , user ) ; } /** Update User credentials and save in localstorage */ updateUser ( user ) { return this . put ( `${ this . route }/me/${ user . _id }` , user ) . pipe ( map ( ( res ) => { this . savingStorage ( res . user . _id , this . token , res . user ) ; Swal . fire ( 'Usuario actualizado' , user . name , 'success' ) ; return true ; } ) ) ; } manageUpdateUsers ( user ) { return this . put ( `${ this . route }/management/${ user . _id }` , user ) ; } /** Delete User */ deleteUser ( id ) { return this . delete ( `${ this . route }/${ id }` ) . pipe ( map ( ( ) => { Swal . fire ( $localize `:@@DeletedUser:Usuario borrado` , $localize `:@@UserDeletedOk:El usuario a sido eliminado correctamente` , 'success' ) ; return true ; } ) ) ; } /** Save User and Token in localstorage */ savingStorage ( id , token , user ) { sessionStorage . setItem ( 'id' , id ) ; sessionStorage . setItem ( 'token' , token ) ; sessionStorage . setItem ( 'user' , JSON . stringify ( user ) ) ; this . user = user ; this . token = token ; this . getIsAdminUser ( this . user . _id ) . subscribe ( ( value ) => this . isAdmin = value . isAdmin , ( err ) => this . alertService . addError ( err ) ) ; } /** Login user into app */ login ( user , remember ) { if ( remember ) { sessionStorage . setItem ( 'email' , user . email ) ; } else { sessionStorage . removeItem ( 'email' ) ; } return this . post ( `${ this . route }/login` , user , true ) . pipe ( map ( ( res ) => { this . savingStorage ( res . id , res . token , res . user ) ; return true ; } , ( err ) => this . alertService . addError ( err ) ) ) ; } /** Renovar Token */ refreshToken ( ) { return this . get ( `${ this . route }/refresh-token` ) . pipe ( map ( ( res ) => { this . token = res . token ; sessionStorage . setItem ( 'token' , this . token ) ; return true ; } ) ) ; } /** Login Google Accounts into app */ loginGoogle ( token ) { return this . post ( `${ this . route }/login/google/` , { token } , true ) . pipe ( map ( ( res ) => { this . savingStorage ( res . id , res . token , res . user ) ; return true ; } ) ) ; } /** Load items localstorage */ loadStorage ( ) { if ( sessionStorage . getItem ( 'token' ) ) { this . token = sessionStorage . getItem ( 'token' ) ; this . user = JSON . parse ( sessionStorage . getItem ( 'user' ) ) ; this . getIsAdminUser ( this . user . _id ) . subscribe ( ( value ) => this . isAdmin = value . isAdmin , ( err ) => this . alertService . addError ( err ) ) ; } else { this . token = '' ; this . user = null ; } } /** Verify if user is logged */ isLogged ( ) { return this . token . length > 5 || ! ! sessionStorage . getItem ( 'token' ) ; } /** Logout user and clean localstorage */ logout ( ) { this . user = null ; this . token = '' ; sessionStorage . removeItem ( 'user' ) ; sessionStorage . removeItem ( 'token' ) ; this . router . navigate ( [ '/login' ] ) ; } /** Change the user profile image */ changeImage ( file , id , from ? ) { const params = { file , id , from } ; this . globalService . uploadFile ( params ) . then ( ( res ) => { if ( from === 'user' ) { this . user . img = res . user . img ; Swal . fire ( $localize `:@@picUpdated:Imagen Actualizada` , this . user . name , 'success' ) ; this . savingStorage ( id , this . token , this . user ) ; } else if ( from === 'group' ) { } } ) . catch ( ( err ) => { this . alertService . addError ( err ) ; } ) ; } getUserObject ( ) { return JSON . parse ( sessionStorage . getItem ( 'user' ) ) ; } getToken ( ) { return sessionStorage . getItem ( 'token' ) ; } }