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
1 change: 0 additions & 1 deletion modules/cda/controller/CDAPatient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ export function findByMetadata(conds) {
export async function CDAExists(id, fecha, orgId) {
const existe = await findByMetadata({
'metadata.extras.id': id,
'metadata.fecha': fecha,
'metadata.extras.organizacion': Types.ObjectId(orgId),
});
return existe;
Expand Down
27 changes: 22 additions & 5 deletions modules/cda/routes/cda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,23 @@ router.post('/', async (req: any, res, next) => {
cdaData.paciente.sexo = cdaData.paciente.sexo === 'M' ? 'masculino' : 'femenino';

const yaExiste = await cdaCtr.CDAExists(cdaData.id, cdaData.fecha, orgId);
const prestacion = await cdaCtr.matchCodeByLoinc(cdaData.loinc);

if (yaExiste) {
return next({ error: 'prestacion_existente' });
const esLaboratorio = prestacion?.snomed?.semanticTag === 'procedimiento';
const fechaCda = moment(yaExiste.metadata.fecha);
const haceUnMes = moment().subtract(1, 'month');

if (esLaboratorio && fechaCda.isAfter(haceUnMes)) {
await cdaCtr.deleteCda(yaExiste._id, null);
} else {
return next({ error: 'prestacion_existente' });
}
}

const organizacion = await Organizacion.findById(orgId);
const dataProfesional = req.body.profesional;

const prestacion = await cdaCtr.matchCodeByLoinc(cdaData.loinc);
if (!prestacion) {
// Es obligatorio que posea prestación
return next({ error: 'prestacion_invalida' });
Expand Down Expand Up @@ -344,15 +353,23 @@ async function createCDA(req, res, next) {
orgId = req.body.organizacion;
}
const yaExiste = await cdaCtr.CDAExists(idPrestacion, fecha, orgId);
const prestacion = await cdaCtr.matchCode(req.body.tipoPrestacion);

if (yaExiste) {
return res.json({ cda: yaExiste._id, paciente: yaExiste.paciente?._id });
const esLaboratorio = prestacion?.snomed?.semanticTag === 'procedimiento';
const fechaCda = moment(yaExiste.metadata.fecha);
const haceUnMes = moment().subtract(1, 'month');

if (esLaboratorio && fechaCda.isAfter(haceUnMes)) {
await cdaCtr.deleteCda(yaExiste._id, null);
} else {
return res.json({ cda: yaExiste._id, paciente: yaExiste.paciente?._id });
}
}

const dataPaciente = req.body.paciente;
const dataProfesional = req.body.profesional;

// Devuelve un Loinc asociado al código SNOMED
const prestacion = await cdaCtr.matchCode(req.body.tipoPrestacion);
if (!prestacion) {
// Es obligatorio que posea prestación
return next(`prestacion_invalida ${req.body.tipoPrestacion}`);
Expand Down
Loading