Skip to content
Open
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
36 changes: 26 additions & 10 deletions modules/recetas/recetasController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,23 +548,39 @@ export async function rechazar(idReceta, sistema, req) {
}
}

export async function getProfesionActualizada(profesional) {
export async function getProfesionActualizada(profesional, esRecetaInsumo = false) {
let profesionGrado = '';
let matriculaGrado = 0;
let especialidades = '';

if (profesional.formacionGrado) {
// Los codigos de los roles permitidos son los de las profesiones: Médico, Odontólogo y Obstetra respectivamente.
const filterFormaciones = (e) => { return e.matriculacion?.length && [1, 2, 23].includes(e.profesion.codigo); };
if (!profesional) {
return { profesionGrado, matriculaGrado, especialidades };
}

let prof = profesional;
if (typeof profesional === 'string' || Types.ObjectId.isValid(profesional)) {
prof = await Profesional.findById(profesional);
if (!prof) {
return { profesionGrado, matriculaGrado, especialidades };
}
}

if (prof.formacionGrado) {
// Los codigos de los roles permitidos son los de las profesiones: Médico, Odontólogo y Obstetra respectivamente. Para insumos se permiten todas.
const filterFormaciones = (e) => {
const tieneMatricula = Boolean(e.matriculacion?.length);
const esCodigoPermitido = esRecetaInsumo || [1, 2, 23].includes(e.profesion?.codigo);
return tieneMatricula && esCodigoPermitido;
};

const formacionEncontrada = profesional.formacionGrado.find(filterFormaciones);
const formacionEncontrada = prof.formacionGrado.find(filterFormaciones);

profesionGrado = formacionEncontrada ? formacionEncontrada.profesion.nombre : '';
matriculaGrado = formacionEncontrada ? formacionEncontrada.matriculacion[formacionEncontrada.matriculacion.length - 1].matriculaNumero : '';
profesionGrado = formacionEncontrada ? formacionEncontrada.profesion?.nombre || '' : '';
matriculaGrado = formacionEncontrada && formacionEncontrada.matriculacion?.length ? formacionEncontrada.matriculacion[formacionEncontrada.matriculacion.length - 1].matriculaNumero : '';
}
if (profesional.formacionPosgrado) {
const especialidadesTxt = profesional.formacionPosgrado.length ? profesional.formacionPosgrado.map(postgrado => postgrado.matriculacion ? `${postgrado.especialidad?.nombre}
(Mat. ${postgrado.matriculacion[postgrado.matriculacion.length - 1].matriculaNumero})` : '') : [];
if (prof.formacionPosgrado) {
const especialidadesTxt = prof.formacionPosgrado.length ? prof.formacionPosgrado.map(postgrado => (postgrado.matriculacion && postgrado.matriculacion.length) ? `${postgrado.especialidad?.nombre}
(Mat. ${postgrado.matriculacion[postgrado.matriculacion.length - 1].matriculaNumero})` : '').filter(Boolean) : [];
especialidades = especialidadesTxt?.join(', ') || especialidades;
}

Expand Down
19 changes: 10 additions & 9 deletions modules/recetas/recetasInsumos/receta-insumo.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { crearRecetaInsumo } from '../../recetas/recetasInsumos/recetaInsumosCon
import { getProfesionActualizada } from '../../recetas/recetasController';
import * as moment from 'moment';
import { RecetaInsumo } from './receta-insumo.schema';
import { createLog } from './../recetaLogs';
import { createLog } from './recetaInsumoLogs';
import { Profesional } from '../../../core/tm/schemas/profesional';

EventCore.on('prestacion:recetaInsumo:create', async ({ prestacion, registro }) => {
Expand All @@ -12,17 +12,18 @@ EventCore.on('prestacion:recetaInsumo:create', async ({ prestacion, registro })
try {
const idRegistro = registro._id;
const documentoProfesional = prestacion.estadoActual.createdBy?.documento ? prestacion.estadoActual.createdBy?.documento : prestacion.solicitud.profesional.documento;
const profPrestacion = await Profesional.findOne({ documento: documentoProfesional });
if (!profPrestacion) {
createLog.error('create', { dataReceta, prestacion, profesional: null }, null, { prestacion, registro });
const profPrestacion: any = await Profesional.findOne({ documento: documentoProfesional });
const prof = Array.isArray(profPrestacion) ? profPrestacion[0] : profPrestacion;
if (!prof || !prof._id) {
createLog.error('create', { dataReceta, prestacion, profesional: null }, `No se encontró el profesional con documento ${documentoProfesional}`, { prestacion, registro });
return;
}
const { profesionGrado, matriculaGrado, especialidades } = await getProfesionActualizada(profPrestacion.id);
const { profesionGrado, matriculaGrado, especialidades } = await getProfesionActualizada(prof, true);
profesional = {
id: profPrestacion.id,
nombre: profPrestacion.nombre,
apellido: profPrestacion.apellido,
documento: profPrestacion.documento,
id: prof.id || prof._id,
nombre: prof.nombre,
apellido: prof.apellido,
documento: prof.documento,
profesion: profesionGrado,
especialidad: especialidades,
matricula: matriculaGrado
Expand Down
42 changes: 42 additions & 0 deletions modules/recetas/recetasInsumos/recetaInsumoLogs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Connections } from '../../../connections';
import { Logger } from '@andes/log';

export const updateLog = new Logger({
connection: Connections.logs,
module: 'recetas-insumo',
type: 'update',
application: 'andes',
bucketBy: 'h',
bucketSize: 100,
expiredAt: '6 M'
});

export const informarLog = new Logger({
connection: Connections.logs,
module: 'recetas-insumo',
type: 'informar-receta-insumo',
application: 'andes',
bucketBy: 'h',
bucketSize: 100,
expiredAt: '3 M'
});

export const createLog = new Logger({
connection: Connections.logs,
module: 'recetas-insumo',
type: 'crear-receta-insumo',
application: 'andes',
bucketBy: 'h',
bucketSize: 100,
expiredAt: '3 M'
});

export const jobsLog = new Logger({
connection: Connections.logs,
module: 'recetas-insumo',
type: 'jobs',
application: 'andes',
bucketBy: 'h',
bucketSize: 100,
expiredAt: '6 M'
});
4 changes: 2 additions & 2 deletions modules/recetas/recetasInsumos/recetaInsumosController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as moment from 'moment';
import { Types } from 'mongoose';
import { Auth } from '../../../auth/auth.class';
import { RecetaInsumo } from './receta-insumo.schema';
import { createLog, informarLog, updateLog } from '../recetaLogs';
import { createLog, informarLog, updateLog } from './recetaInsumoLogs';
import { ParamsIncorrect, RecetaNotFound, RecetaNotEdit } from '../recetas.error';
import { Paciente } from '../../../core-v2/mpi/paciente/paciente.schema';
import { Profesional } from '../../../core/tm/schemas/profesional';
Expand Down Expand Up @@ -163,7 +163,7 @@ export async function create(req) {
if (!profAndes) {
throw new ParamsIncorrect('Profesional no encontrado');
}
const { profesionGrado, matriculaGrado, especialidades } = await getProfesionActualizada(profRecetar.id);
const { profesionGrado, matriculaGrado, especialidades } = await getProfesionActualizada(profAndes, true);
dataRecetaInsumo.profesional = {
_id: profAndes._id,
id: profAndes._id,
Expand Down
Loading