const helperPath = '../../helper'; const drive = require(`${helperPath}/drive`); const validar = require(`${helperPath}/validar`); const dbPath = '../../db/tablas'; const Carrera = require(`${dbPath}/Carrera`); const CasoEspecial = require(`${dbPath}/CasoEspecial`); const Usuario = require(`${dbPath}/Usuario`); const nuevo = async (body, file) => { const idUsuario = validar.validarId(body.idUsuario); const idCarrera = validar.validarId(body.idCarrera); const idStatus = validar.validarId(body.idStatus); const numeroCuenta = validar.validarNumeroCuenta(body.numeroCuenta); const creditos = validar.validarNumero(body.creditos); const correo = validar.validarCorreo(body.correo); const telefono = validar.validarNumero(body.telefono, 15); const fechaInicio = validar.validarFecha(body.fechaInicio); const fechaFin = validar.validarFecha(body.fechaFin); const fechaNacimiento = validar.validarFecha(body.fechaNacimiento); const direccion = validar.validarAlfanumerico( body.direccion, 'La dirección', 200 ); const institucion = body.institucion ? validar.validarTexto(body.institucion, 'El texto institucion.', 200) : ''; const dependencia = body.dependencia ? validar.validarTexto(body.dependencia, 'El texto dependencia.', 200) : ''; const motivo = body.motivo ? validar.validarNumero(body.motivo, 'El texto motivo.', 1) : ''; let carpeta = ''; const path = `./server/uploads/${validar.validar(file, 'El archivo', 1000)}`; return Usuario.findOne({ where: { idUsuario }, }) .then((res) => { if (!res) throw new Error('Este alumno no existe en la db.'); if (res.idTipoUsuario !== 3) throw new Error('Este usuario no es de tipo Alumno.'); return Carrera.findOne({ where: { idCarrera } }); }) .then((res) => { if (!res) throw new Error('Esta carrera no existe en la db.'); return CasoEspecial.findOne({ where: { idUsuario }, }); }) .then(async (res) => { if (res) throw new Error('Este alumno ya tienen un Caso Especial.'); return drive.folder(numeroCuenta); }) .then((res) => { carpeta = res; return drive.uploadFile(path, `archivos.zip`, 'application/zip', carpeta); }) .then((res) => CasoEspecial.create({ idUsuario, idCarrera, idStatus, creditos, correo, telefono, institucion, dependencia, motivo, direccion, fechaInicio, fechaFin, fechaNacimiento, carpeta, archivoZip: res, }) ) .then((res) => ({ message: `Se ha registro el Caso Especial correctamente.`, })); }; module.exports = nuevo;