93 lines
2.8 KiB
JavaScript
93 lines
2.8 KiB
JavaScript
const helperPath = '../../helper';
|
|
const drive = require(`${helperPath}/drive`);
|
|
const validar = require(`${helperPath}/validar`);
|
|
const Servicio = require('../../db/tablas/Servicio');
|
|
|
|
const update = async (body, files) => {
|
|
const idServicio = validar.validarNumeroEntero(
|
|
body.idServicio,
|
|
'id servicio'
|
|
);
|
|
const dataUpdate = {};
|
|
|
|
return Servicio.findOne({ where: { idServicio } })
|
|
.then(async (res) => {
|
|
if (!res) throw new Error('Este Servicio Social no existe en la db.');
|
|
try {
|
|
if (files['cartaAceptacion'][0].filename)
|
|
dataUpdate.cartaAceptacion = await drive.uploadFile(
|
|
`./server/uploads/${files['cartaAceptacion'][0].filename}`,
|
|
`Carta_Aceptacion.pdf`,
|
|
'application/pdf',
|
|
res.carpeta
|
|
);
|
|
} catch (err) {}
|
|
try {
|
|
if (files['cartaTermino'][0].filename)
|
|
dataUpdate.cartaTermino = await drive.uploadFile(
|
|
`./server/uploads/${files['cartaTermino'][0].filename}`,
|
|
`Carta_Termino.pdf`,
|
|
'application/pdf',
|
|
res.carpeta
|
|
);
|
|
} catch (err) {}
|
|
try {
|
|
if (files['informeGlobal'][0].filename)
|
|
dataUpdate.informeGlobal = await drive.uploadFile(
|
|
`./server/uploads/${files['informeGlobal'][0].filename}`,
|
|
`Informe_Global.pdf`,
|
|
'application/pdf',
|
|
res.carpeta
|
|
);
|
|
} catch (err) {}
|
|
if (body.correo) dataUpdate.correo = validar.validarCorreo(body.correo);
|
|
if (body.fechaInicio)
|
|
dataUpdate.fechaInicio = validar.validarFecha(
|
|
body.fechaInicio,
|
|
'fecha de inicio',
|
|
false
|
|
);
|
|
if (body.fechaFin)
|
|
dataUpdate.fechaFin = validar.validarFecha(
|
|
body.fechaFin,
|
|
'fecha fin',
|
|
false
|
|
);
|
|
if (body.fechaNacimiento)
|
|
dataUpdate.fechaNacimiento = validar.validarFecha(
|
|
body.fechaNacimiento,
|
|
'fecha de nacimiento',
|
|
false
|
|
);
|
|
if (body.direccion)
|
|
dataUpdate.direccion = validar.validarAlfanumerico(
|
|
body.direccion,
|
|
'dirección',
|
|
false,
|
|
200
|
|
);
|
|
if (body.telefono)
|
|
dataUpdate.telefono = validar.validarNumero(
|
|
body.telefono,
|
|
'teléfono',
|
|
true,
|
|
15
|
|
);
|
|
if (validar.validarObjetoVacio(dataUpdate))
|
|
throw new Error(
|
|
'No se envio nada para actualizar este Servicio Social'
|
|
);
|
|
return Servicio.update(dataUpdate, { where: { idServicio } });
|
|
})
|
|
.then(async (res) => {
|
|
if (dataUpdate.cartaTermino || dataUpdate.informeGlobal)
|
|
return validar.validarPreTermino(idServicio);
|
|
return false;
|
|
})
|
|
.then((res) => ({
|
|
message: 'Se guardo correctamente los cambios de este servicio.',
|
|
}));
|
|
};
|
|
|
|
module.exports = update;
|