import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { MatSnackBar } from '@angular/material/snack-bar'; import { ResponseData } from 'src/app/interfaces/response-data'; import { map } from 'rxjs'; import { MCOMORInterface } from '../../../interfaces/mco/mcomor/mcomor-interface'; import { IAMService } from '../../iam/iam.service'; import { USERInterface } from 'src/app/interfaces/user-interface'; import { ENCService } from '../../enc/enc.service'; @Injectable({ providedIn: 'root', }) export class MCOMORService { private _url = 'https://qasirh.ittec.mx/v1/public/api/MCOMOR'; jwt = localStorage.getItem('jwt'); headers = new HttpHeaders({ Authorization: 'Bearer ' + this.jwt }); usuario_session: USERInterface = {} as USERInterface; constructor(private _http: HttpClient, private _snackBar: MatSnackBar, private _encService: ENCService) {} ngOnInit() { this.usuario_session = JSON.parse(localStorage.getItem('TIMUSERENC')!); } obtenerOrganigrama() { this.ngOnInit(); return this._http .get( `${this._url}/consultar/${ this._encService.desencriptar(this.usuario_session.IDUSUARIO) }/${this.formatoRFC(this.usuario_session.RFCEMPRESA)}`, { headers: this.headers } ) .pipe( map((data: any) => { return data; }, (error:any) => { if (!error.ok) { this.snackAlert('Ocurrió un error inesperado'); } if (error.error.msg != undefined) { this.snackAlert(error.error.msg); } if (error.status == 408) { this.snackAlert('Conexion lenta'); } }) ); } registrarOrganigrama(organigrama: any) { return this._http .post(`${this._url}/insertar`, organigrama, { headers: this.headers, }) .subscribe( (res) => { if (!res.error) { this.snackAlert('!Registro existoso!'); } else { this.snackAlert(res.msg); } }, (error) => { if (!error.ok) { this.snackAlert('Ocurrió un error inesperado'); } if (error.error.msg != undefined) { this.snackAlert(error.error.msg); } if (error.status == 408) { this.snackAlert('Conexion lenta'); } } ); } modificarOrganigrama(organigrama: any) { return this._http .post(`${this._url}/modificar`, organigrama, { headers: this.headers, }) .subscribe( (res) => { if (!res.error) { this.snackAlert('!Modificación existosa!'); } else { this.snackAlert(res.msg); } }, (error) => { if (!error.ok) { this.snackAlert('Ocurrió un error inesperado'); } if (error.error.msg != undefined) { this.snackAlert(error.error.msg); } if (error.status == 408) { this.snackAlert('Conexion lenta'); } } ); } private formatoRFC(rfc: string){ let rfc_decoded = window.atob(rfc); let rfc_replace = rfc_decoded.replace("|", "/"); return rfc_replace; } private snackAlert(mensaje: string) { this._snackBar.open(mensaje, 'Cerrar', { duration: 4000, }); } }