-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlaboratorios.controller.ts
More file actions
118 lines (107 loc) · 4.41 KB
/
laboratorios.controller.ts
File metadata and controls
118 lines (107 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { PacienteCtr } from '../../core-v2/mpi';
import { services } from './../../services';
import { IDENTIFICACION } from '../../shared/constantes';
import * as moment from 'moment';
function agrupar(elementos) {
const setAreas = new Set(elementos.map(d => d.area));
const areasStr = Array.from(setAreas);
const areas = [];
const toItem = (e) => {
const resultado = e.Resultado || e.resultado;
return {
nombre: e.item,
esTitulo: e.esTitulo === 'True' && !resultado ? true : false,
resultado,
unidadMedida: e.UnidadMedida || e.unidadMedida,
metodo: e.Metodo,
valorReferencia: e.valorReferencia,
firma: e.esTitulo === 'True' ? '' : e.userValida,
};
};
areasStr.forEach(area => {
const detallesArea = elementos.filter(d => d.area === area);
const setGrupos = new Set(detallesArea.map(d => d.grupo));
const grupos = Array.from(setGrupos);
const item = {
area,
grupos: grupos.map(g => {
const detallesAreaGrupo = detallesArea.filter(da => da.grupo === g);
const res: any = {};
res.grupo = g;
if (detallesAreaGrupo.length === 1 && detallesAreaGrupo[0].grupo === g) {
res.items = [toItem(detallesAreaGrupo[0])];
res.visible = true;
} else {
res.items = detallesAreaGrupo.map(toItem);
res.visible = true;
}
return res;
})
};
areas.push(item);
});
return areas;
}
export async function search(data) {
let params;
const service = 'get-LABAPI';
try {
if (data.idProtocolo) {
params = {
parametros: `nombre=LABAPI_GetResultadoProtocolo¶metros=${data.idProtocolo}`
};
} else {
params = {
parametros: `nombre=LABAPI_GetProtocolos¶metros=${data.estado}|${data.dni}|${data.fechaNac}|${data.apellido}|${data.fechaDesde}|${data.fechaHasta}`
};
}
const response = await services.get(service).exec(params);
if (!response || (Array.isArray(response) && response.length === 0)) {
throw new Error('El servicio no devolvió datos');
}
const salida = data.idProtocolo ? agrupar(response[0].Data) : response;
return salida;
} catch (e) {
const errorMessage = e.message || 'Error desconocido';
throw new Error('Error al obtener laboratorio: ' + errorMessage);
}
}
export async function searchByDocumento(pacienteId, fechaDesde?, fechaHasta?) {
let dataSearch;
fechaDesde = fechaDesde ? moment(fechaDesde).format('YYYYMMDD') : moment('01/01/2020', 'DD-MM-YYYY').format('YYYYMMDD');
fechaHasta = fechaHasta ? moment(fechaHasta).format('YYYYMMDD') : moment().format('YYYYMMDD');
try {
const paciente = await PacienteCtr.findById(pacienteId);
if (paciente) {
let estado;
let documento = paciente.documento;
const documentosExtranjeros = IDENTIFICACION.enum.filter(item => item !== null);
if (documento) { // dni argentino
estado = 'validado';
} else if (documentosExtranjeros.includes(paciente.tipoIdentificacion)) { // dni extranjero o pasaporte
estado = 'EX';
documento = paciente.numeroIdentificacion;
} else {
if (paciente.edad <= 5 && paciente.relaciones?.length) { // recien nacido (aún sin dni)
estado = 'RN';
const tutorProgenitor = paciente.relaciones.find(rel => rel.relacion.nombre === 'progenitor/a') || paciente.relaciones.find(rel => rel.relacion.nombre === 'tutor');
documento = tutorProgenitor?.documento || tutorProgenitor?.numeroIdentificacion || null;
}
}
if (!estado || !documento) {
return [];
}
dataSearch = {
estado,
dni: documento,
fechaNac: moment(paciente.fechaNacimiento).utc().format('YYYYMMDD'),
apellido: paciente.apellido,
fechaDesde,
fechaHasta
};
return await this.search(dataSearch);
}
} catch (err) {
return { err, dataSearch };
}
}