notification-dialog.component.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. import { Component, Inject, OnInit } from '@angular/core';
  2. import {
  3. NotificationAction,
  4. NotificationDetailsResponse,
  5. NotificationsListItem,
  6. } from '../../../interfaces/notification.interface';
  7. import {
  8. MAT_DIALOG_DATA,
  9. MatDialog,
  10. MatDialogRef,
  11. } from '@angular/material/dialog';
  12. import { EncService } from '../../../services/enc.service';
  13. import { NotificationsService } from '../../../services/notifications.service';
  14. import { ResourcesService } from '../../../services/resources.service';
  15. import { CorrectiveMaintenanceService } from '../../../services/corrective-maintenance.service';
  16. import { PreventiveMaintenanceService } from '../../../services/preventive-maintenance.service';
  17. import { GdelService } from '../../../services/gdel.service';
  18. import { ProcessManagementService } from '../../../services/process-management/process-management.service';
  19. import { lastValueFrom } from 'rxjs';
  20. import { CorrectiveOrderDetailsComponent } from '../../corrective-maintenance/operations-management/corrective-order-details/corrective-order-details.component';
  21. import { PreventiveOrderDetailsComponent } from '../../preventive-maintenance/preventive-order-details/preventive-order-details.component';
  22. import { AgreeOrderComponent } from './agree-order/agree-order.component';
  23. import { SignatureViewComponent } from './signature-view/signature-view.component';
  24. import { StaffSelectionComponent } from '../../corrective-maintenance/operations-management/staff-selection/staff-selection.component';
  25. import { CommentsViewComponent } from './comments-view/comments-view.component';
  26. import { ValidateApplicationsModalComponent } from './validate-applications-modal/validate-applications-modal.component';
  27. import { ResponseDataValidateApplications } from '../../../interfaces/process-managementv/workflow-management.interface';
  28. @Component({
  29. selector: 'app-notification-dialog',
  30. templateUrl: './notification-dialog.component.html',
  31. styleUrl: './notification-dialog.component.css',
  32. standalone: false,
  33. })
  34. export class NotificationDialogComponent implements OnInit {
  35. idNotification: string;
  36. isLoading: boolean;
  37. hasError: boolean;
  38. errorStr: string;
  39. isProcessingArchives: boolean;
  40. notification: NotificationsListItem | null;
  41. constructor(
  42. @Inject(MAT_DIALOG_DATA) private _data: any,
  43. private _encService: EncService,
  44. private _notificationsService: NotificationsService,
  45. private _dialog: MatDialog,
  46. private _resourcesService: ResourcesService,
  47. private _correctiveMaintenanceService: CorrectiveMaintenanceService,
  48. private _preventiveMaintenanceService: PreventiveMaintenanceService,
  49. private _processManagementService: ProcessManagementService,
  50. private _dialogRef: MatDialogRef<NotificationDialogComponent>,
  51. private _gdelService: GdelService
  52. ) {
  53. this.idNotification = '';
  54. this.isLoading = true;
  55. this.hasError = false;
  56. this.errorStr = '';
  57. this.isProcessingArchives = false;
  58. this.notification = null;
  59. }
  60. ngOnInit(): void {
  61. this.init();
  62. }
  63. async init() {
  64. this.idNotification = await this._encService.decrypt(
  65. this._data.idNotification
  66. );
  67. this.getNotification();
  68. }
  69. async getNotification() {
  70. try {
  71. let idUser = localStorage.getItem('idusuario')!;
  72. let notification: NotificationDetailsResponse = await lastValueFrom(
  73. this._notificationsService.getNotification(
  74. this._data.idNotification,
  75. 'S',
  76. idUser,
  77. 1
  78. )
  79. );
  80. this.hasError = notification.error;
  81. this.errorStr = notification.msg;
  82. if (!this.hasError) {
  83. let notificationActionsArr: NotificationAction[] = JSON.parse(
  84. notification.response.ACCIONES
  85. ).filter(
  86. (action: any) => action.BOTON && action.FUNCION && action.PARAMETROS
  87. );
  88. notification.response.ACCIONES_ARR = notificationActionsArr;
  89. if (notification.response.ID_ORDEN !== null) {
  90. notification.response.ID_ORDEN = await this._encService.decrypt(
  91. notification.response.ID_ORDEN
  92. );
  93. }
  94. this.notification = notification.response;
  95. }
  96. this.isLoading = false;
  97. } catch (error: any) {
  98. if (error.error == undefined) {
  99. this.errorStr = 'Ocurrió un error inesperado.';
  100. } else if (error.error.msg == undefined) {
  101. this.errorStr = 'Ocurrió un error inesperado.';
  102. } else {
  103. this.errorStr = error.error.msg;
  104. }
  105. this.isLoading = false;
  106. this.hasError = true;
  107. }
  108. }
  109. executeNotificationAction(func: string, params: string) {
  110. let paramsArr = JSON.parse(params);
  111. switch (func) {
  112. case 'openCorrectiveWorkOrderDetails':
  113. this.openCorrectiveWorkOrderDetails(paramsArr[0]);
  114. break;
  115. case 'validateCorrectiveWorkOrder':
  116. this.validateCorrectiveWorkOrder(paramsArr[0]);
  117. break;
  118. case 'openModule':
  119. this._dialogRef.close(`go|${paramsArr[0]}`);
  120. break;
  121. case 'attendCorrectiveWorkOrder':
  122. this.attendCorrectiveWorkOrder(paramsArr[0]);
  123. break;
  124. case 'reviewSignatureInCorrectiveWorkOrder':
  125. this.reviewSignatureInCorrectiveWorkOrder(paramsArr[0], paramsArr[1]);
  126. break;
  127. case 'reviewCommentsInCorrectiveWorkOrder':
  128. this.reviewCommentsInCorrectiveWorkOrder(paramsArr[0], paramsArr[1]);
  129. break;
  130. case 'revalidateCorrectiveWorkOrder':
  131. this.revalidateCorrectiveWorkOrder(paramsArr[0]);
  132. break;
  133. case 'getValidateApplications':
  134. this.getValidateApplications(paramsArr[0], paramsArr[1]);
  135. break;
  136. case 'asignStateToApplication':
  137. this.asignStateToApplication(
  138. paramsArr[0],
  139. paramsArr[1],
  140. paramsArr[2],
  141. paramsArr[3]
  142. );
  143. break;
  144. case 'openPreventiveWorkOrderDetails':
  145. this.openPreventiveWorkOrderDetails(paramsArr[0]);
  146. break;
  147. case 'validatePreventiveWorkOrder':
  148. this.validatePreventiveWorkOrder(paramsArr[0]);
  149. break;
  150. case 'revalidatePreventiveVisit':
  151. this.revalidatePreventiveVisit(paramsArr[0]);
  152. break;
  153. case 'attendPreventiveWorkOrder':
  154. this.attendPreventiveWorkOrder(paramsArr[0]);
  155. break;
  156. case 'reviewCommentsInPreventiveVisit':
  157. this.reviewCommentsInPreventiveWorkOrder(paramsArr[0], paramsArr[1]);
  158. break;
  159. case 'reviewSignatureInPreventiveVisit':
  160. this.reviewSignatureInPreventiveVisit(paramsArr[0], paramsArr[1]);
  161. break;
  162. default:
  163. this._resourcesService.openSnackBar(
  164. 'La función seleccionada no existe.'
  165. );
  166. break;
  167. }
  168. }
  169. //FUNCIONES POSIBLES PARA LAS NOTIFICACIONES
  170. async getValidateApplications(user: string, line: number) {
  171. if (!user || !line) {
  172. this._resourcesService.openSnackBar('Parámetros inválidos.');
  173. return;
  174. }
  175. try {
  176. const response: ResponseDataValidateApplications = await lastValueFrom(
  177. this._processManagementService.getValidateApplications(
  178. await this._encService.encrypt(user),
  179. line
  180. )
  181. );
  182. console.log(response);
  183. if (!response.error) {
  184. this._dialogRef.close();
  185. this._dialog.open(ValidateApplicationsModalComponent, {
  186. width: '1200px',
  187. maxWidth: '90vw',
  188. data: { applications: response.response },
  189. });
  190. } else {
  191. this._resourcesService.openSnackBar(response.msg);
  192. }
  193. } catch (error: any) {
  194. this._resourcesService.openSnackBar(
  195. error?.error?.msg || 'Ocurrió un error inesperado.'
  196. );
  197. }
  198. }
  199. async asignStateToApplication(
  200. user: string,
  201. line: number,
  202. idapplication: number,
  203. state: string
  204. ) {
  205. if (!user || !line || !idapplication || !state) {
  206. this._resourcesService.openSnackBar('Parámetros inválidos.');
  207. return;
  208. }
  209. try {
  210. await lastValueFrom(
  211. this._processManagementService.assignStateToApplication({
  212. USUARIO: await this._encService.encrypt(user),
  213. NUMERO_LINEA: line,
  214. ID_APPLICATION: idapplication,
  215. STATE: state,
  216. })
  217. );
  218. this._resourcesService.openSnackBar(
  219. 'Estado asignado correctamente a la solicitud.'
  220. );
  221. } catch (error: any) {
  222. this._resourcesService.openSnackBar(
  223. error?.error?.msg || 'Ocurrió un error inesperado.'
  224. );
  225. }
  226. }
  227. async processLoadArchives(params: any) {
  228. this.isProcessingArchives = true;
  229. try {
  230. let idUser = localStorage.getItem('idusuario')!;
  231. let body = {
  232. id_user: idUser,
  233. linea: '1',
  234. temp_files: params.temp_files,
  235. individual_temp_files: params.individual_temp_files,
  236. };
  237. await lastValueFrom(this._gdelService.processZipFile(body));
  238. this._resourcesService.openSnackBar('Archivos procesados correctamente.');
  239. this._dialogRef.close();
  240. } catch (error: any) {
  241. if (error.error?.msg) {
  242. this._resourcesService.openSnackBar(error.error.msg);
  243. } else {
  244. this._resourcesService.openSnackBar('Ocurrió un error inesperado.');
  245. }
  246. } finally {
  247. this.isProcessingArchives = false;
  248. }
  249. }
  250. openCorrectiveWorkOrderDetails(idOrder: string) {
  251. this._dialog.open(CorrectiveOrderDetailsComponent, {
  252. width: '480px',
  253. data: {
  254. idOrder: idOrder,
  255. },
  256. });
  257. }
  258. openPreventiveWorkOrderDetails(idOrder: string) {
  259. this._dialog.open(PreventiveOrderDetailsComponent, {
  260. width: '480px',
  261. data: {
  262. idOrder: idOrder,
  263. },
  264. });
  265. }
  266. private validateCorrectiveWorkOrder(idOrder: string) {
  267. let dialogRef = this._dialog.open(StaffSelectionComponent, {
  268. disableClose: true,
  269. width: '540px',
  270. maxWidth: '540px',
  271. data: {
  272. idOrder: idOrder,
  273. orderType: 'Correctivo',
  274. },
  275. });
  276. dialogRef.afterClosed().subscribe((res) => {
  277. if (res != null && res != undefined && res != '') {
  278. this.approveWorkOrder(idOrder, res);
  279. }
  280. });
  281. }
  282. private revalidateCorrectiveWorkOrder(idOrder: string) {
  283. let dialogRef = this._dialog.open(StaffSelectionComponent, {
  284. disableClose: true,
  285. width: '540px',
  286. maxWidth: '540px',
  287. data: {
  288. idOrder: idOrder,
  289. orderType: 'Correctivo',
  290. },
  291. });
  292. dialogRef.afterClosed().subscribe((res) => {
  293. if (res != null && res != undefined && res != '') {
  294. this.updateApprovedWorkOrder(idOrder, res);
  295. }
  296. });
  297. }
  298. private async approveWorkOrder(idOrder: string, config: string) {
  299. try {
  300. let idUser = localStorage.getItem('idusuario')!;
  301. let formData = new FormData();
  302. formData.append('id_user', idUser);
  303. formData.append('linea', '1');
  304. formData.append('id_order', idOrder);
  305. formData.append('config', config);
  306. await lastValueFrom(
  307. this._correctiveMaintenanceService.approveWorkOrder(formData)
  308. );
  309. this._resourcesService.openSnackBar(
  310. 'La orden de trabajo se aprobó correctamente.'
  311. );
  312. this._dialogRef.close();
  313. } catch (error: any) {
  314. if (error.error == undefined) {
  315. this._resourcesService.openSnackBar(
  316. 'Ocurrió un error inesperado (14).'
  317. );
  318. } else if (error.error.msg == undefined) {
  319. this._resourcesService.openSnackBar(
  320. 'Ocurrió un error inesperado (15).'
  321. );
  322. } else {
  323. this._resourcesService.openSnackBar(error.error.msg);
  324. }
  325. }
  326. }
  327. private async updateApprovedWorkOrder(idOrder: string, config: string) {
  328. try {
  329. let idUser = localStorage.getItem('idusuario')!;
  330. let formData = new FormData();
  331. formData.append('id_user', idUser);
  332. formData.append('linea', '1');
  333. formData.append('id_order', idOrder);
  334. formData.append('config', config);
  335. await lastValueFrom(
  336. this._correctiveMaintenanceService.updateApprovedWorkOrder(formData)
  337. );
  338. this._resourcesService.openSnackBar(
  339. 'La orden de trabajo se actualizó correctamente.'
  340. );
  341. this._dialogRef.close();
  342. } catch (error: any) {
  343. if (error.error == undefined) {
  344. this._resourcesService.openSnackBar(
  345. 'Ocurrió un error inesperado (14).'
  346. );
  347. } else if (error.error.msg == undefined) {
  348. this._resourcesService.openSnackBar(
  349. 'Ocurrió un error inesperado (15).'
  350. );
  351. } else {
  352. this._resourcesService.openSnackBar(error.error.msg);
  353. }
  354. }
  355. }
  356. attendCorrectiveWorkOrder(idOrder: string) {
  357. let dialogRef = this._dialog.open(AgreeOrderComponent, {
  358. disableClose: true,
  359. width: '560px',
  360. maxWidth: '560px',
  361. data: {
  362. idOrder: idOrder,
  363. },
  364. });
  365. dialogRef.afterClosed().subscribe((res) => {
  366. if (res != null && res != undefined && res != '') {
  367. this.saveAttendanceCorrectiveOrder(idOrder, res);
  368. }
  369. });
  370. }
  371. private async saveAttendanceCorrectiveOrder(
  372. idOrder: string,
  373. dataEnc: string
  374. ) {
  375. try {
  376. let idUser = localStorage.getItem('idusuario')!;
  377. let dataDec = await this._encService.decrypt(dataEnc);
  378. let dataArr = JSON.parse(dataDec);
  379. let formData = new FormData();
  380. formData.append('id_user', idUser);
  381. formData.append('linea', '1');
  382. formData.append('id_order', idOrder);
  383. formData.append('attendance', dataArr[0]);
  384. formData.append('data', dataArr[1]);
  385. await lastValueFrom(
  386. this._correctiveMaintenanceService.setWorkOrderAttendance(formData)
  387. );
  388. this._resourcesService.openSnackBar(
  389. 'La atención a la orden de trabajo se registró correctamente.'
  390. );
  391. this._dialogRef.close();
  392. } catch (error: any) {
  393. if (error.error == undefined) {
  394. this._resourcesService.openSnackBar('Ocurrió un error inesperado.');
  395. } else if (error.error.msg == undefined) {
  396. this._resourcesService.openSnackBar('Ocurrió un error inesperado.');
  397. } else {
  398. this._resourcesService.openSnackBar(error.error.msg);
  399. }
  400. }
  401. }
  402. private reviewSignatureInCorrectiveWorkOrder(
  403. idOrder: string,
  404. idUserAction: string
  405. ) {
  406. this._dialog.open(SignatureViewComponent, {
  407. width: '420px',
  408. maxWidth: '420px',
  409. data: {
  410. idOrder: idOrder,
  411. idUserAction: idUserAction,
  412. orderType: 'Correctivo',
  413. },
  414. });
  415. }
  416. private reviewCommentsInCorrectiveWorkOrder(
  417. idOrder: string,
  418. idUserAction: string
  419. ) {
  420. this._dialog.open(CommentsViewComponent, {
  421. width: '420px',
  422. maxWidth: '420px',
  423. data: {
  424. idOrder: idOrder,
  425. idUserAction: idUserAction,
  426. orderType: 'Correctivo',
  427. },
  428. });
  429. }
  430. // PREVENTIVE MAINTENANCE FUNCTIONS
  431. private reviewCommentsInPreventiveWorkOrder(
  432. idOrder: string,
  433. idUserAction: string
  434. ) {
  435. this._dialog.open(CommentsViewComponent, {
  436. width: '420px',
  437. maxWidth: '420px',
  438. data: {
  439. idOrder: idOrder,
  440. idUserAction: idUserAction,
  441. orderType: 'Preventivo',
  442. },
  443. });
  444. }
  445. private reviewSignatureInPreventiveVisit(
  446. idOrder: string,
  447. idUserAction: string
  448. ) {
  449. this._dialog.open(SignatureViewComponent, {
  450. width: '420px',
  451. maxWidth: '420px',
  452. data: {
  453. idOrder: idOrder,
  454. idUserAction: idUserAction,
  455. orderType: 'Preventivo',
  456. },
  457. });
  458. }
  459. attendPreventiveWorkOrder(idOrder: string) {
  460. let dialogRef = this._dialog.open(AgreeOrderComponent, {
  461. disableClose: true,
  462. width: '560px',
  463. maxWidth: '560px',
  464. data: {
  465. idOrder: idOrder,
  466. },
  467. });
  468. dialogRef.afterClosed().subscribe((res) => {
  469. if (res != null && res != undefined && res != '') {
  470. this.saveAttendancePreventiveOrder(idOrder, res);
  471. }
  472. });
  473. }
  474. private async saveAttendancePreventiveOrder(
  475. idOrder: string,
  476. dataEnc: string
  477. ) {
  478. try {
  479. let idUser = localStorage.getItem('idusuario')!;
  480. let dataDec = await this._encService.decrypt(dataEnc);
  481. let dataArr = JSON.parse(dataDec);
  482. let formData = new FormData();
  483. formData.append('id_user', idUser);
  484. formData.append('linea', '1');
  485. formData.append('id_order', idOrder);
  486. formData.append('attendance', dataArr[0]);
  487. formData.append('data', dataArr[1]);
  488. await lastValueFrom(
  489. this._preventiveMaintenanceService.setVisitAttendance(formData)
  490. );
  491. this._resourcesService.openSnackBar(
  492. 'La atención a la visita preventiva se registró correctamente.'
  493. );
  494. this._dialogRef.close();
  495. } catch (error: any) {
  496. if (error.error == undefined) {
  497. this._resourcesService.openSnackBar('Ocurrió un error inesperado.');
  498. } else if (error.error.msg == undefined) {
  499. this._resourcesService.openSnackBar('Ocurrió un error inesperado.');
  500. } else {
  501. this._resourcesService.openSnackBar(error.error.msg);
  502. }
  503. }
  504. }
  505. private validatePreventiveWorkOrder(idOrder: string) {
  506. let dialogRef = this._dialog.open(StaffSelectionComponent, {
  507. disableClose: true,
  508. width: '540px',
  509. maxWidth: '540px',
  510. data: {
  511. idOrder: idOrder,
  512. orderType: 'Preventivo',
  513. },
  514. });
  515. dialogRef.afterClosed().subscribe((res) => {
  516. if (res != null && res != undefined && res != '') {
  517. this.approvePreventiveWorkOrder(idOrder, res);
  518. }
  519. });
  520. }
  521. private revalidatePreventiveVisit(idOrder: string) {
  522. let dialogRef = this._dialog.open(StaffSelectionComponent, {
  523. disableClose: true,
  524. width: '540px',
  525. maxWidth: '540px',
  526. data: {
  527. idOrder: idOrder,
  528. orderType: 'Preventivo',
  529. },
  530. });
  531. dialogRef.afterClosed().subscribe((res) => {
  532. if (res != null && res != undefined && res != '') {
  533. this.updateApprovedPreventiveWorkOrder(idOrder, res);
  534. }
  535. });
  536. }
  537. private async approvePreventiveWorkOrder(idOrder: string, config: string) {
  538. try {
  539. let idUser = localStorage.getItem('idusuario')!;
  540. let formData = new FormData();
  541. formData.append('id_user', idUser);
  542. formData.append('linea', '1');
  543. formData.append('id_order', idOrder);
  544. formData.append('config', config);
  545. await lastValueFrom(
  546. this._preventiveMaintenanceService.assignOperariosToPreventiveVisit(
  547. formData
  548. )
  549. );
  550. this._resourcesService.openSnackBar(
  551. 'La orden de trabajo preventivo se aprobó correctamente.'
  552. );
  553. this._dialogRef.close();
  554. } catch (error: any) {
  555. if (error.error == undefined) {
  556. this._resourcesService.openSnackBar(
  557. 'Ocurrió un error inesperado (14).'
  558. );
  559. } else if (error.error.msg == undefined) {
  560. this._resourcesService.openSnackBar(
  561. 'Ocurrió un error inesperado (15).'
  562. );
  563. } else {
  564. this._resourcesService.openSnackBar(error.error.msg);
  565. }
  566. }
  567. }
  568. private async updateApprovedPreventiveWorkOrder(
  569. idOrder: string,
  570. config: string
  571. ) {
  572. try {
  573. let idUser = localStorage.getItem('idusuario')!;
  574. let formData = new FormData();
  575. formData.append('id_user', idUser);
  576. formData.append('linea', '1');
  577. formData.append('id_order', idOrder);
  578. formData.append('config', config);
  579. await lastValueFrom(
  580. this._preventiveMaintenanceService.assignOperariosToPreventiveVisit(
  581. formData
  582. )
  583. );
  584. this._resourcesService.openSnackBar(
  585. 'La orden de trabajo preventivo se actualizó correctamente.'
  586. );
  587. this._dialogRef.close();
  588. } catch (error: any) {
  589. if (error.error == undefined) {
  590. this._resourcesService.openSnackBar(
  591. 'Ocurrió un error inesperado (14).'
  592. );
  593. } else if (error.error.msg == undefined) {
  594. this._resourcesService.openSnackBar(
  595. 'Ocurrió un error inesperado (15).'
  596. );
  597. } else {
  598. this._resourcesService.openSnackBar(error.error.msg);
  599. }
  600. }
  601. }
  602. }