mcomau.component.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. import { Component, ViewChild, AfterViewInit } from '@angular/core';
  2. import { MatPaginator } from '@angular/material/paginator';
  3. import { MatTableDataSource } from '@angular/material/table';
  4. import { Router } from '@angular/router';
  5. import { MatDialog } from '@angular/material/dialog';
  6. import { MatSnackBar } from '@angular/material/snack-bar';
  7. import { MCOMAUService } from 'src/app/services/mco/mcomau/mcomau.service';
  8. import { AlertaComponent } from '../../resources/dialogos/alerta/alerta.component';
  9. import { CambiarContrasenaComponent } from '../../resources/dialogos/cambiar-contrasena/cambiar-contrasena.component';
  10. import { MCOMAUINFOComponent } from './mcomau-info/mcomau-info.component';
  11. import { ResponseData } from 'src/app/interfaces/response-data';
  12. import { MCOMAUInterface } from 'src/app/interfaces/mco/mcomau/mcomau-interface';
  13. import { IAMService } from 'src/app/services/iam/iam.service';
  14. import { MCOMUNService } from 'src/app/services/mco/mcomun/mcomun.service';
  15. import { MCOMPVService } from 'src/app/services/mco/mcompv/mcompv.service';
  16. import { USERInterface } from 'src/app/interfaces/user-interface';
  17. import { ENCService } from 'src/app/services/enc/enc.service';
  18. import { LoginGuard } from 'src/app/auth/login.guard';
  19. import { MCOMAPService } from 'src/app/services/mco/mcomap/mcomap.service';
  20. import { ValidationsService } from 'src/app/services/validations.service';
  21. import { lastValueFrom } from 'rxjs';
  22. @Component({
  23. selector: 'app-mcomau',
  24. templateUrl: './mcomau.component.html',
  25. styleUrls: ['./mcomau.component.css'],
  26. })
  27. export class MCOMAUComponent implements AfterViewInit {
  28. usuarios: any = [];
  29. loading: Boolean = true;
  30. data_empty = false;
  31. filtro_usuarios: number = 0;
  32. filtro: any = [
  33. {
  34. tipo: 1,
  35. filtro: 'Todos',
  36. },
  37. {
  38. tipo: 2,
  39. filtro: 'Activos',
  40. },
  41. {
  42. tipo: 3,
  43. filtro: 'Inactivos',
  44. },
  45. {
  46. tipo: 4,
  47. filtro: 'Eliminados',
  48. },
  49. ];
  50. usuario_session: USERInterface = JSON.parse(
  51. localStorage.getItem('TIMUSERENC')!
  52. );
  53. usuarios_todos: any;
  54. displayedColumns: string[] = [
  55. 'no_empleado',
  56. 'nombre',
  57. 'correo',
  58. 'rfc',
  59. 'unidadNegocio',
  60. 'status',
  61. 'accion',
  62. ];
  63. dataSource: MatTableDataSource<MCOMAUInterface> =
  64. new MatTableDataSource<MCOMAUInterface>(this.usuarios);
  65. unidades_negocio: any = [];
  66. usuarios_menos: any = [];
  67. politicas: any = [];
  68. perfiles: any = [];
  69. isFilter: boolean = false;
  70. boton_register_disabled: boolean = false;
  71. @ViewChild(MatPaginator) paginator!: MatPaginator;
  72. constructor(
  73. private router: Router,
  74. public dialog: MatDialog,
  75. private _mcomauService: MCOMAUService,
  76. private _encService: ENCService,
  77. private _iamService: IAMService,
  78. private _mcomunService: MCOMUNService,
  79. private _mcomapService: MCOMAPService,
  80. private _mcompvService: MCOMPVService,
  81. private _validationService: ValidationsService
  82. ) {}
  83. async ngAfterViewInit() {
  84. this.dataSource.paginator = this.paginator;
  85. this.dataSource.filterPredicate = function (data, filter: string): boolean {
  86. let nombre_completo = `${data.NOMBRE} ${data.APELLIDO_PATERNO} ${data.APELLIDO_MATERNO}`;
  87. return (
  88. nombre_completo.toString().toLowerCase().includes(filter) ||
  89. data.IDUSUARIO.toLowerCase().includes(filter) ||
  90. data.EMAIL.toLowerCase().includes(filter) ||
  91. data.RFCUSUARIO.toLowerCase().includes(filter)
  92. );
  93. };
  94. await this.obtener();
  95. }
  96. async obtener() {
  97. let usuario = this._mcomauService.obtener().toPromise();
  98. await usuario.then(
  99. (res: any) => {
  100. if (res.status == 'Token is Expired') {
  101. this._iamService.logout();
  102. this._validationService.openSnackBar('Sesión expirada. Vuelva a iniciar sesión');
  103. window.location.href = 'https://qasirh.ittec.mx/';
  104. } else if (res.status == 'IP change') {
  105. localStorage.removeItem('jwt');
  106. localStorage.removeItem('jwt');
  107. this._validationService.openSnackBar('La IP fue cambiada');
  108. window.location.href = 'https://qasirh.ittec.mx/';
  109. } else if (!res.status && res.response.length > 0) {
  110. this.dataSource.data = res.response;
  111. this.usuarios_todos = res.response;
  112. } else {
  113. this._validationService.openSnackBar(
  114. res.response.length > 0 ? res.msg : 'No hay datos para mostrar'
  115. );
  116. this.data_empty = true;
  117. }
  118. this.loading = false;
  119. },
  120. (error: any) => {
  121. if (!error.ok) {
  122. this._validationService.openSnackBar('Ocurrió un error inesperado');
  123. }
  124. if (error.error.msg != undefined) {
  125. this._validationService.openSnackBar(error.error.msg);
  126. }
  127. if (error.status == 408) {
  128. this._validationService.openSnackBar('Conexion lenta');
  129. }
  130. }
  131. );
  132. }
  133. eliminar(item: MCOMAUInterface) {
  134. let datos = {
  135. numero_empleado: item.IDUSUARIO,
  136. numero_empleado_modifica: this._encService.desencriptar(
  137. this.usuario_session.IDUSUARIO
  138. ),
  139. };
  140. this._mcomauService.eliminar(datos).subscribe(
  141. (res) => {
  142. if (res.status == 'Token is Expired') {
  143. this._iamService.logout();
  144. this._validationService.openSnackBar(
  145. 'Sesión expirada. Vuelva a iniciar sesión'
  146. );
  147. window.location.href = 'https://qasirh.ittec.mx/';
  148. } else if (res.status == 'IP change') {
  149. localStorage.removeItem('jwt');
  150. localStorage.removeItem('jwt');
  151. this._validationService.openSnackBar('La IP fue cambiada');
  152. window.location.href = 'https://qasirh.ittec.mx/';
  153. } else if (!res.error) {
  154. this.obtener();
  155. this._validationService.openSnackBar('¡Eliminación Exitosa!');
  156. } else {
  157. this._validationService.openSnackBar(res.msg);
  158. }
  159. },
  160. (error) => {
  161. if (!error.ok) {
  162. this._validationService.openSnackBar('Ocurrió un error inesperado');
  163. }
  164. if (error.error.msg != undefined) {
  165. this._validationService.openSnackBar(error.error.msg);
  166. }
  167. if (error.status == 408) {
  168. this._validationService.openSnackBar('Conexion lenta');
  169. }
  170. }
  171. );
  172. }
  173. cambiarContrasena(contrasena: String, item: any) {
  174. let datos = {
  175. numero_empleado: item.IDUSUARIO,
  176. numero_empleado_modifica: this._encService.desencriptar(this.usuario_session.IDUSUARIO),
  177. password: contrasena,
  178. password_confirmation: contrasena,
  179. };
  180. this._mcomauService.cambiarContrasena(datos).subscribe(
  181. (res) => {
  182. if (res.status == 'Token is Expired') {
  183. this._iamService.logout();
  184. this._validationService.openSnackBar('Sesión expirada. Vuelva a iniciar sesión');
  185. window.location.href = 'https://qasirh.ittec.mx/';
  186. } else if (res.status == 'IP change') {
  187. localStorage.removeItem('jwt');
  188. localStorage.removeItem('jwt');
  189. this._validationService.openSnackBar('La IP fue cambiada');
  190. window.location.href = 'https://qasirh.ittec.mx/';
  191. } else if (!res.error) {
  192. this._validationService.openSnackBar('¡Contraseña Cambiada!');
  193. } else {
  194. this._validationService.openSnackBar(res.msg);
  195. }
  196. },
  197. (error) => {
  198. if (!error.ok) {
  199. this._validationService.openSnackBar('Ocurrió un error inesperado');
  200. }
  201. if (error.error.msg != undefined) {
  202. this._validationService.openSnackBar(error.error.msg);
  203. }
  204. if (error.status == 408) {
  205. this._validationService.openSnackBar('Conexion lenta');
  206. }
  207. }
  208. );
  209. }
  210. bloquear(item: any) {
  211. let datos = {
  212. numero_empleado: item.IDUSUARIO,
  213. numero_empleado_modifica: this._encService.desencriptar(
  214. this.usuario_session.IDUSUARIO
  215. ),
  216. };
  217. this._mcomauService.bloquear(datos).subscribe(
  218. (res) => {
  219. if (res.status == 'Token is Expired') {
  220. this._iamService.logout();
  221. this._validationService.openSnackBar(
  222. 'Sesión expirada. Vuelva a iniciar sesión'
  223. );
  224. window.location.href = 'https://qasirh.ittec.mx/';
  225. } else if (res.status == 'IP change') {
  226. localStorage.removeItem('jwt');
  227. localStorage.removeItem('jwt');
  228. this._validationService.openSnackBar('La IP fue cambiada');
  229. window.location.href = 'https://qasirh.ittec.mx/';
  230. } else if (!res.error) {
  231. this.obtener();
  232. this._validationService.openSnackBar('¡Usuario Bloqueado!');
  233. } else {
  234. this._validationService.openSnackBar(res.msg);
  235. }
  236. },
  237. (error) => {
  238. if (!error.ok) {
  239. this._validationService.openSnackBar('Ocurrió un error inesperado');
  240. }
  241. if (error.error.msg != undefined) {
  242. this._validationService.openSnackBar(error.error.msg);
  243. }
  244. if (error.status == 408) {
  245. this._validationService.openSnackBar('Conexion lenta');
  246. }
  247. }
  248. );
  249. }
  250. desbloquear(item: any) {
  251. let datos = {
  252. user_id_empleado: item.IDUSUARIO,
  253. user_id_empleado_modifica: this._encService.desencriptar(
  254. this.usuario_session.IDUSUARIO
  255. ),
  256. };
  257. this._mcomauService.desbloquear(datos).subscribe(
  258. (res) => {
  259. if (res.status == 'Token is Expired') {
  260. this._iamService.logout();
  261. this._validationService.openSnackBar(
  262. 'Sesión expirada. Vuelva a iniciar sesión'
  263. );
  264. window.location.href = 'https://qasirh.ittec.mx/';
  265. } else if (res.status == 'IP change') {
  266. localStorage.removeItem('jwt');
  267. localStorage.removeItem('jwt');
  268. this._validationService.openSnackBar('La IP fue cambiada');
  269. window.location.href = 'https://qasirh.ittec.mx/';
  270. } else if (!res.error) {
  271. this.obtener();
  272. this._validationService.openSnackBar('¡Usuario Desbloqueado!');
  273. } else {
  274. this._validationService.openSnackBar(res.msg);
  275. }
  276. },
  277. (error) => {
  278. if (!error.ok) {
  279. this._validationService.openSnackBar('Ocurrió un error inesperado');
  280. }
  281. if (error.error.msg != undefined) {
  282. this._validationService.openSnackBar(error.error.msg);
  283. }
  284. if (error.status == 408) {
  285. this._validationService.openSnackBar('Conexion lenta');
  286. }
  287. }
  288. );
  289. }
  290. applyFilter(filterValue: any) {
  291. this.dataSource.filter = filterValue.target.value.trim().toLowerCase();
  292. }
  293. filterUser() {
  294. let usuarios_aux: MCOMAUInterface[] = [];
  295. switch (this.filtro_usuarios) {
  296. case 1:
  297. usuarios_aux = this.usuarios_todos;
  298. break;
  299. case 2:
  300. this.usuarios_todos.forEach((element: MCOMAUInterface) => {
  301. if (element.ESTATUS === 'Activo') {
  302. usuarios_aux.push(element);
  303. }
  304. });
  305. break;
  306. case 3:
  307. this.usuarios_todos.forEach((element: MCOMAUInterface) => {
  308. if (element.ESTATUS === 'Inactivo') {
  309. usuarios_aux.push(element);
  310. }
  311. });
  312. break;
  313. case 4:
  314. this.usuarios_todos.forEach((element: MCOMAUInterface) => {
  315. if (element.ESTATUS === 'Eliminado') {
  316. usuarios_aux.push(element);
  317. }
  318. });
  319. break;
  320. default:
  321. this._validationService.openSnackBar('Ocurrió un error inesperado');
  322. break;
  323. }
  324. usuarios_aux.length > 0
  325. ? (this.data_empty = false)
  326. : (this.data_empty = true);
  327. this.dataSource.data = usuarios_aux;
  328. this.isFilter = true;
  329. }
  330. openDialog(item: MCOMAUInterface, action: String) {
  331. let dataAction = {};
  332. switch (action) {
  333. case 'Eliminar':
  334. dataAction = {
  335. titulo: 'Eliminar usuario',
  336. descripcion: `¿Esta seguro de eliminar a ${item.NOMBRE}?`,
  337. };
  338. break;
  339. case 'Bloquear':
  340. dataAction = {
  341. titulo: 'Bloquear acceso',
  342. descripcion: `¿Esta seguro bloquear el acceso a ${item.NOMBRE}?`,
  343. };
  344. break;
  345. case 'Desbloquear':
  346. dataAction = {
  347. titulo: 'Desbloquear acceso',
  348. descripcion: `¿Esta seguro desbloquear el acceso a ${item.NOMBRE}?`,
  349. };
  350. break;
  351. default:
  352. this._validationService.openSnackBar('Ocurrió un error inesperado');
  353. break;
  354. }
  355. const dialogRef = this.dialog.open(AlertaComponent, {
  356. data: dataAction,
  357. });
  358. dialogRef.afterClosed().subscribe((result) => {
  359. if (result) {
  360. switch (action) {
  361. case 'Eliminar':
  362. this.eliminar(item);
  363. break;
  364. case 'Bloquear':
  365. this.bloquear(item);
  366. break;
  367. case 'Desbloquear':
  368. this.desbloquear(item);
  369. break;
  370. default:
  371. this._validationService.openSnackBar('Ocurrió un error inesperado');
  372. break;
  373. }
  374. }
  375. });
  376. }
  377. openDialogPassword(item: any) {
  378. const dialogRef = this.dialog.open(CambiarContrasenaComponent, {
  379. data: item,
  380. disableClose: true
  381. });
  382. dialogRef.afterClosed().subscribe((result) => {
  383. if (result != '') {
  384. this.cambiarContrasena(result, item);
  385. }
  386. });
  387. }
  388. openDialogInfo(item: MCOMAUInterface) {
  389. this.dialog.open(MCOMAUINFOComponent, {
  390. data: item,
  391. });
  392. }
  393. async actionForm(action: String, item: any = null) {
  394. this.boton_register_disabled = true;
  395. this.loading = true;
  396. let data = item != null ? JSON.stringify(item) : null;
  397. await lastValueFrom(this._mcomunService.obtener()).then(
  398. (res: any) => {
  399. if (res.status == 'Token is Expired') {
  400. this._iamService.logout();
  401. this._validationService.openSnackBar(
  402. 'Sesión expirada. Vuelva a iniciar sesión'
  403. );
  404. window.location.href = 'https://qasirh.ittec.mx/';
  405. } else if (res.status == 'IP change') {
  406. localStorage.removeItem('jwt');
  407. localStorage.removeItem('jwt');
  408. this._validationService.openSnackBar('La IP fue cambiada');
  409. window.location.href = 'https://qasirh.ittec.mx/';
  410. } else if (!res.error) {
  411. this.unidades_negocio = res.response;
  412. } else {
  413. this._validationService.openSnackBar(res.msg);
  414. }
  415. },
  416. (error) => {
  417. if (!error.ok) {
  418. this._validationService.openSnackBar('Ocurrió un error inesperado');
  419. }
  420. if (error.error.msg != undefined) {
  421. this._validationService.openSnackBar(error.error.msg);
  422. }
  423. if (error.status == 408) {
  424. this._validationService.openSnackBar('Conexion lenta');
  425. }
  426. }
  427. );
  428. await lastValueFrom(this._mcompvService.obtener()).then(
  429. (res: any) => {
  430. if (res.status == 'Token is Expired') {
  431. this._iamService.logout();
  432. this._validationService.openSnackBar(
  433. 'Sesión expirada. Vuelva a iniciar sesión'
  434. );
  435. window.location.href = 'https://qasirh.ittec.mx/';
  436. } else if (res.status == 'IP change') {
  437. localStorage.removeItem('jwt');
  438. localStorage.removeItem('jwt');
  439. this._validationService.openSnackBar('La IP fue cambiada');
  440. window.location.href = 'https://qasirh.ittec.mx/';
  441. } else if (!res.error) {
  442. this.politicas = res.response;
  443. } else {
  444. this._validationService.openSnackBar(res.msg);
  445. }
  446. },
  447. (error) => {
  448. if (!error.ok) {
  449. this._validationService.openSnackBar('Ocurrió un error inesperado');
  450. }
  451. if (error.error.msg != undefined) {
  452. this._validationService.openSnackBar(error.error.msg);
  453. }
  454. if (error.status == 408) {
  455. this._validationService.openSnackBar('Conexion lenta');
  456. }
  457. }
  458. );
  459. await lastValueFrom(this._mcomauService.obtenerTodos()).then(
  460. (res: any) => {
  461. if (res.status == 'Token is Expired') {
  462. this._iamService.logout();
  463. this._validationService.openSnackBar(
  464. 'Sesión expirada. Vuelva a iniciar sesión'
  465. );
  466. window.location.href = 'https://qasirh.ittec.mx/';
  467. } else if (res.status == 'IP change') {
  468. localStorage.removeItem('jwt');
  469. localStorage.removeItem('jwt');
  470. this._validationService.openSnackBar('La IP fue cambiada');
  471. window.location.href = 'https://qasirh.ittec.mx/';
  472. } else if (!res.error) {
  473. this.usuarios_menos = res.response;
  474. } else {
  475. this._validationService.openSnackBar(res.msg);
  476. }
  477. },
  478. (error) => {
  479. if (!error.ok) {
  480. this._validationService.openSnackBar('Ocurrió un error inesperado');
  481. }
  482. if (error.error.msg != undefined) {
  483. this._validationService.openSnackBar(error.error.msg);
  484. }
  485. if (error.status == 408) {
  486. this._validationService.openSnackBar('Conexion lenta');
  487. }
  488. }
  489. );
  490. await lastValueFrom(this._mcomapService.obtener()).then(
  491. (res: any) => {
  492. if (res.status == 'Token is Expired') {
  493. this._iamService.logout();
  494. this._validationService.openSnackBar(
  495. 'Sesión expirada. Vuelva a iniciar sesión'
  496. );
  497. window.location.href = 'https://qasirh.ittec.mx/';
  498. } else if (res.status == 'IP change') {
  499. localStorage.removeItem('jwt');
  500. localStorage.removeItem('jwt');
  501. this._validationService.openSnackBar('La IP fue cambiada');
  502. window.location.href = 'https://qasirh.ittec.mx/';
  503. } else if (!res.status) {
  504. this.perfiles = res.response;
  505. } else {
  506. this._validationService.openSnackBar(
  507. res.response.length > 0 ? res.msg : 'No hay datos para mostrar'
  508. );
  509. this.data_empty = true;
  510. }
  511. },
  512. (error: any) => {
  513. if (!error.ok) {
  514. this._validationService.openSnackBar('Ocurrió un error inesperado');
  515. }
  516. if (error.error.msg != undefined) {
  517. this._validationService.openSnackBar(error.error.msg);
  518. }
  519. if (error.status == 408) {
  520. this._validationService.openSnackBar('Conexion lenta');
  521. }
  522. }
  523. );
  524. let unidades = JSON.stringify(this.unidades_negocio);
  525. let politicas = JSON.stringify(this.politicas);
  526. let usuarios_menos = JSON.stringify(this.usuarios_menos);
  527. let perfiles = JSON.stringify(this.perfiles);
  528. this.router.navigate([
  529. '/admin/mcomau-form',
  530. {
  531. action: action,
  532. data: data,
  533. unidades: unidades,
  534. politicas: politicas,
  535. usuarios_menos: usuarios_menos,
  536. perfiles: perfiles,
  537. },
  538. ]);
  539. this.boton_register_disabled = false;
  540. this.loading = false;
  541. }
  542. }