mcomor.service.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpHeaders } from '@angular/common/http';
  3. import { MatSnackBar } from '@angular/material/snack-bar';
  4. import { ResponseData } from 'src/app/interfaces/response-data';
  5. import { map } from 'rxjs';
  6. import { MCOMORInterface } from '../../../interfaces/mco/mcomor/mcomor-interface';
  7. import { IAMService } from '../../iam/iam.service';
  8. import { USERInterface } from 'src/app/interfaces/user-interface';
  9. import { ENCService } from '../../enc/enc.service';
  10. @Injectable({
  11. providedIn: 'root',
  12. })
  13. export class MCOMORService {
  14. private _url = 'https://qasirh.ittec.mx/v1/public/api/MCOMOR';
  15. jwt = localStorage.getItem('jwt');
  16. headers = new HttpHeaders({ Authorization: 'Bearer ' + this.jwt });
  17. usuario_session: USERInterface = {} as USERInterface;
  18. constructor(private _http: HttpClient, private _snackBar: MatSnackBar, private _encService: ENCService) {}
  19. ngOnInit() {
  20. this.usuario_session = JSON.parse(localStorage.getItem('TIMUSERENC')!);
  21. }
  22. obtenerOrganigrama() {
  23. this.ngOnInit();
  24. return this._http
  25. .get<ResponseData>(
  26. `${this._url}/consultar/${ this._encService.desencriptar(this.usuario_session.IDUSUARIO) }/${this.formatoRFC(this.usuario_session.RFCEMPRESA)}`,
  27. { headers: this.headers }
  28. )
  29. .pipe(
  30. map((data: any) => {
  31. return data;
  32. }, (error:any) => {
  33. if (!error.ok) {
  34. this.snackAlert('Ocurrió un error inesperado');
  35. }
  36. if (error.error.msg != undefined) {
  37. this.snackAlert(error.error.msg);
  38. }
  39. if (error.status == 408) {
  40. this.snackAlert('Conexion lenta');
  41. }
  42. })
  43. );
  44. }
  45. registrarOrganigrama(organigrama: any) {
  46. return this._http
  47. .post<ResponseData>(`${this._url}/insertar`, organigrama, {
  48. headers: this.headers,
  49. })
  50. .subscribe(
  51. (res) => {
  52. if (!res.error) {
  53. this.snackAlert('!Registro existoso!');
  54. } else {
  55. this.snackAlert(res.msg);
  56. }
  57. },
  58. (error) => {
  59. if (!error.ok) {
  60. this.snackAlert('Ocurrió un error inesperado');
  61. }
  62. if (error.error.msg != undefined) {
  63. this.snackAlert(error.error.msg);
  64. }
  65. if (error.status == 408) {
  66. this.snackAlert('Conexion lenta');
  67. }
  68. }
  69. );
  70. }
  71. modificarOrganigrama(organigrama: any) {
  72. return this._http
  73. .post<ResponseData>(`${this._url}/modificar`, organigrama, {
  74. headers: this.headers,
  75. })
  76. .subscribe(
  77. (res) => {
  78. if (!res.error) {
  79. this.snackAlert('!Modificación existosa!');
  80. } else {
  81. this.snackAlert(res.msg);
  82. }
  83. },
  84. (error) => {
  85. if (!error.ok) {
  86. this.snackAlert('Ocurrió un error inesperado');
  87. }
  88. if (error.error.msg != undefined) {
  89. this.snackAlert(error.error.msg);
  90. }
  91. if (error.status == 408) {
  92. this.snackAlert('Conexion lenta');
  93. }
  94. }
  95. );
  96. }
  97. private formatoRFC(rfc: string){
  98. let rfc_decoded = window.atob(rfc);
  99. let rfc_replace = rfc_decoded.replace("|", "/");
  100. return rfc_replace;
  101. }
  102. private snackAlert(mensaje: string) {
  103. this._snackBar.open(mensaje, 'Cerrar', {
  104. duration: 4000,
  105. });
  106. }
  107. }