Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion src/app/components/profesional/profesional.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Component, OnInit, Input } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { Router } from '@angular/router';
import { Auth } from '@andes/auth';
import { Plex } from '@andes/plex';
import { Observable } from 'rxjs';
@Component({
selector: 'profesionales',
Expand All @@ -30,6 +31,8 @@ export class ProfesionalComponent implements OnInit {
profesionalSelected: any = false;
fotoProfesional: any;
nuevoProfesional = false;
puedeDeshacerMatricula = false;
estadoDeshacerPorFormacion: { [formacionId: string]: any } = {};

public listado$: Observable<any[]>;

Expand Down Expand Up @@ -83,7 +86,8 @@ export class ProfesionalComponent implements OnInit {
private profesionalService: ProfesionalService,
public sanitizer: DomSanitizer,
private router: Router,
private auth: Auth,) { }
private auth: Auth,
private plex: Plex) { }

ngOnInit() {
if (this.auth.getPermissions('matriculaciones:profesionales:?').length < 1) {
Expand Down Expand Up @@ -115,13 +119,19 @@ export class ProfesionalComponent implements OnInit {

seleccionarProfesional(profesional) {
this.profesionalSelected = profesional;
this.puedeDeshacerMatricula = this.auth.getPermissions('matriculaciones:supervisor:aprobar').length > 0;
this.estadoDeshacerPorFormacion = {};
if (this.profesionalSelected.validadoRenaper) {
this.fotoProfesional = this.sanitizer.bypassSecurityTrustResourceUrl(this.profesionalSelected.foto);
} else {
this.profesionalService.getFoto({ id: this.profesionalSelected.id }).subscribe(resp => {
this.fotoProfesional = this.sanitizer.bypassSecurityTrustResourceUrl('data:image/jpeg;base64,' + resp);
});
}

if (this.puedeDeshacerMatricula) {
this.cargarEstadosDeshacer();
}
}

routeTo(action, id) {
Expand All @@ -131,4 +141,69 @@ export class ProfesionalComponent implements OnInit {
cerrar() {
this.profesionalSelected = false;
}

cargarEstadosDeshacer() {
const formaciones = this.profesionalSelected?.formacionGrado || [];
formaciones.forEach((formacion) => {
const formacionId = formacion?._id;
if (!formacionId || !formacion?.matriculacion?.length) {
return;
}
this.estadoDeshacerPorFormacion[formacionId] = { loading: true, canUndo: false, reason: null };
this.profesionalService.canUndoMatriculaGrado(this.profesionalSelected.id, formacionId).subscribe(
(estado) => {
this.estadoDeshacerPorFormacion[formacionId] = {
loading: false,
canUndo: !!estado?.canUndo,
reason: estado?.reason || null
};
},
() => {
this.estadoDeshacerPorFormacion[formacionId] = {
loading: false,
canUndo: false,
reason: 'no se pudo verificar el estado'
};
}
);
});
}

estadoDeshacer(formacion: any) {
const formacionId = formacion?._id;
return formacionId ? this.estadoDeshacerPorFormacion[formacionId] : null;
}

deshacerMatricula(formacion: any) {
const estado = this.estadoDeshacer(formacion);
if (!estado?.canUndo || estado?.loading) {
return;
}
const ultimaMatricula = formacion.matriculacion[formacion.matriculacion.length - 1];
const mensaje = `${formacion.profesion?.nombre || 'Profesion'} - matrícula ${ultimaMatricula?.matriculaNumero}`;
this.plex.confirm(`¿Desea deshacer el número de matrícula?<br>${mensaje}`, 'Atención').then(confirmacion => {
if (!confirmacion) {
return;
}
this.profesionalService.undoMatriculaGrado(this.profesionalSelected.id, formacion._id).subscribe(
() => {
this.plex.info('success', 'Número de matrícula deshecho correctamente.');
this.profesionalService.get({ id: this.profesionalSelected.id }).subscribe(prof => {
if (prof?.length) {
this.seleccionarProfesional(prof[0]);
}
});
},
(error) => {
const mensajeError = error?.error?.message || 'No fue posible deshacer la matrícula.';
this.plex.info('warning', mensajeError);
this.profesionalService.get({ id: this.profesionalSelected.id }).subscribe(prof => {
if (prof?.length) {
this.seleccionarProfesional(prof[0]);
}
});
}
);
});
}
}
8 changes: 8 additions & 0 deletions src/app/components/profesional/profesional.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@
<plex-label titulo="Matrícula"
subtitulo="{{formacionGrado.matriculacion[formacionGrado.matriculacion.length - 1].matriculaNumero}}"
size="md"></plex-label>
<plex-button *ngIf="puedeDeshacerMatricula"
type="warning"
icon="undo"
label="Deshacer número"
[disabled]="estadoDeshacer(formacionGrado)?.loading || !estadoDeshacer(formacionGrado)?.canUndo"
[tooltip]="estadoDeshacer(formacionGrado)?.reason || 'Deshacer número de matrícula'"
(click)="deshacerMatricula(formacionGrado)">
</plex-button>
</plex-item>
</plex-list>
</ng-container>
Expand Down
8 changes: 8 additions & 0 deletions src/app/services/profesional.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,12 @@ export class ProfesionalService {
actualizarProfesional(body, options?: Options): Observable<any> {
return this.server.put(this.profesionalUrl + '/actualizar', body, options);
}

canUndoMatriculaGrado(profesionalId: string, formacionId: string): Observable<any> {
return this.server.get(`${this.profesionalUrl}/${profesionalId}/formacionGrado/${formacionId}/deshacer-matricula`);
}

undoMatriculaGrado(profesionalId: string, formacionId: string): Observable<any> {
return this.server.post(`${this.profesionalUrl}/${profesionalId}/formacionGrado/${formacionId}/deshacer-matricula`, {});
}
}
Loading