|
| 1 | +import { AuditPlugin } from '@andes/mongoose-plugin-audit'; |
| 2 | +import * as mongoose from 'mongoose'; |
| 3 | +import { ProfesionalSubSchema } from '../../../core/tm/schemas/profesional'; |
| 4 | +import { PacienteSubSchema } from '../../../core-v2/mpi/paciente/paciente.schema'; |
| 5 | +const insumoSubSchema = new mongoose.Schema({ |
| 6 | + insumo: String, |
| 7 | + tipo: { |
| 8 | + type: String, |
| 9 | + enum: ['dispositivo', 'nutricion', 'magistral'] |
| 10 | + }, |
| 11 | + requiereEspecificacion: Boolean, |
| 12 | + cantidad: Number, |
| 13 | + especificacion: String |
| 14 | +}, { _id: false }); |
| 15 | + |
| 16 | +const cancelarSchema = new mongoose.Schema({ |
| 17 | + idDispensaApp: { |
| 18 | + type: String, |
| 19 | + required: false |
| 20 | + }, |
| 21 | + motivo: { |
| 22 | + type: String, |
| 23 | + required: false |
| 24 | + }, |
| 25 | + organizacion: { |
| 26 | + id: String, |
| 27 | + nombre: String |
| 28 | + } |
| 29 | +}); |
| 30 | +const sistemaSchema = { |
| 31 | + type: String, |
| 32 | + enum: ['sifaho', 'recetar'] |
| 33 | +}; |
| 34 | +const estadoDispensaSchema = new mongoose.Schema({ |
| 35 | + tipo: { |
| 36 | + type: String, |
| 37 | + enum: ['sin-dispensa', 'dispensada', 'dispensa-parcial'], |
| 38 | + required: true, |
| 39 | + default: 'sin-dispensa' |
| 40 | + }, |
| 41 | + idDispensaApp: { |
| 42 | + type: String, |
| 43 | + required: false |
| 44 | + }, |
| 45 | + fecha: Date, |
| 46 | + sistema: sistemaSchema, |
| 47 | + cancelada: { |
| 48 | + type: cancelarSchema, |
| 49 | + required: false |
| 50 | + } |
| 51 | +}); |
| 52 | +const estadosSchema = new mongoose.Schema({ |
| 53 | + tipo: { |
| 54 | + type: String, |
| 55 | + enum: ['pendiente', 'vigente', 'finalizada', 'vencida', 'suspendida', 'rechazada'], |
| 56 | + required: true, |
| 57 | + default: 'vigente' |
| 58 | + }, |
| 59 | + motivo: { |
| 60 | + type: String, |
| 61 | + required: false |
| 62 | + }, |
| 63 | + observacion: { |
| 64 | + type: String, |
| 65 | + required: false |
| 66 | + }, |
| 67 | + profesional: { |
| 68 | + type: ProfesionalSubSchema, |
| 69 | + required: false |
| 70 | + }, |
| 71 | + organizacionExterna: { |
| 72 | + id: { |
| 73 | + type: String, |
| 74 | + required: false |
| 75 | + }, |
| 76 | + nombre: { |
| 77 | + type: String, |
| 78 | + required: false |
| 79 | + } |
| 80 | + } |
| 81 | +}); |
| 82 | + |
| 83 | + |
| 84 | +export const recetaInsumoSchema = new mongoose.Schema({ |
| 85 | + organizacion: { |
| 86 | + id: mongoose.SchemaTypes.ObjectId, |
| 87 | + nombre: String |
| 88 | + }, |
| 89 | + profesional: { |
| 90 | + id: mongoose.SchemaTypes.ObjectId, |
| 91 | + nombre: String, |
| 92 | + apellido: String, |
| 93 | + documento: String, |
| 94 | + profesion: String, |
| 95 | + matricula: Number, |
| 96 | + especialidad: String, |
| 97 | + }, |
| 98 | + fechaRegistro: Date, |
| 99 | + fechaPrestacion: Date, |
| 100 | + idPrestacion: String, |
| 101 | + idRegistro: String, |
| 102 | + diagnostico: mongoose.SchemaTypes.Mixed, |
| 103 | + insumo: { type: insumoSubSchema, required: true }, |
| 104 | + dispensa: [ |
| 105 | + { |
| 106 | + idDispensaApp: String, |
| 107 | + fecha: Date, |
| 108 | + insumos: [{ |
| 109 | + cantidad: Number, |
| 110 | + descripcion: String, |
| 111 | + insumo: mongoose.SchemaTypes.Mixed, |
| 112 | + cantidadEnvases: Number, |
| 113 | + observacion: { |
| 114 | + type: String, |
| 115 | + required: false |
| 116 | + } |
| 117 | + }], |
| 118 | + organizacion: { |
| 119 | + id: String, |
| 120 | + nombre: String |
| 121 | + }, |
| 122 | + } |
| 123 | + ], |
| 124 | + estados: [estadosSchema], |
| 125 | + estadoActual: estadosSchema, |
| 126 | + estadosDispensa: [estadoDispensaSchema], |
| 127 | + estadoDispensaActual: estadoDispensaSchema, |
| 128 | + paciente: PacienteSubSchema, |
| 129 | + renovacion: String, |
| 130 | + appNotificada: [{ app: sistemaSchema, fecha: Date }], |
| 131 | + origenExterno: { |
| 132 | + id: String, // id receta creada por sistema que no es Andes |
| 133 | + app: sistemaSchema, |
| 134 | + fecha: Date |
| 135 | + } |
| 136 | +}); |
| 137 | + |
| 138 | +recetaInsumoSchema.pre('save', function (next) { |
| 139 | + const recetaInsumo: any = this; |
| 140 | + |
| 141 | + if (recetaInsumo.estados && recetaInsumo.estados.length > 0) { |
| 142 | + recetaInsumo.estadoActual = recetaInsumo.estados[recetaInsumo.estados.length - 1]; |
| 143 | + } |
| 144 | + if (recetaInsumo.estadosDispensa && recetaInsumo.estadosDispensa.length > 0) { |
| 145 | + recetaInsumo.estadoDispensaActual = recetaInsumo.estadosDispensa[recetaInsumo.estadosDispensa.length - 1]; |
| 146 | + } |
| 147 | + |
| 148 | + next(); |
| 149 | +}); |
| 150 | + |
| 151 | +recetaInsumoSchema.plugin(AuditPlugin); |
| 152 | + |
| 153 | +recetaInsumoSchema.index({ |
| 154 | + idPrestacion: 1, |
| 155 | +}); |
| 156 | + |
| 157 | +export const RecetaInsumo = mongoose.model('recetaInsumo', recetaInsumoSchema, 'recetaInsumo'); |
0 commit comments