Files

99 lines
2.9 KiB
JavaScript

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.validarNumeroEntero(body.idUsuario, 'id usuario');
const idCarrera = validar.validarNumeroEntero(body.idCarrera, 'id carrera');
const idStatus = validar.validarNumeroEntero(body.idStatus, 'id status');
const numeroCuenta = validar.validarNumeroCuenta(body.numeroCuenta);
const correo = validar.validarCorreo(body.correo);
const creditos = validar.validarNumero(body.creditos, 'créditos', true);
const telefono = validar.validarNumero(body.telefono, 'teléfono', true, 15);
const fechaInicio = validar.validarFecha(
body.fechaInicio,
'fecha de inicio',
false
);
const fechaFin = validar.validarFecha(body.fechaFin, 'fecha fin', false);
const fechaNacimiento = validar.validarFecha(
body.fechaNacimiento,
'fecha de nacimiento',
false
);
const direccion = validar.validarAlfanumerico(
body.direccion,
'dirección',
false,
200
);
const institucion = body.institucion
? validar.validarTexto(body.institucion, 'institucion', false, 200)
: '';
const dependencia = body.dependencia
? validar.validarAlfanumerico(body.dependencia, 'dependencia', false, 200)
: '';
const motivo = body.motivo
? validar.validarNumero(body.motivo, 'motivo', true, 1)
: '';
const path = `./server/uploads/${validar.validacionBasicaStr(
file,
'archivo',
true,
1000
)}`;
let carpeta = '';
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;