Files
servicio_social_api/server/controller/Servicio/nuevo.js
T

88 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-11-16 12:47:46 -06:00
const { Op } = require('sequelize');
2021-05-05 18:49:23 -05:00
const helperPath = '../../helper';
2021-05-12 21:05:53 -05:00
const drive = require(`${helperPath}/drive`);
2021-05-13 14:06:07 -05:00
const validar = require(`${helperPath}/validar`);
2021-05-05 18:49:23 -05:00
const dbPath = '../../db/tablas';
2021-05-12 21:05:53 -05:00
const Carrera = require(`${dbPath}/Carrera`);
const Programa = require(`${dbPath}/Programa`);
const Servicio = require(`${dbPath}/Servicio`);
const Usuario = require(`${dbPath}/Usuario`);
2020-11-16 09:58:55 -06:00
2020-11-22 18:27:54 -06:00
const nuevo = async (body, file) => {
2021-01-14 03:13:37 -06:00
const idUsuario = validar.validarId(body.idUsuario);
const idPrograma = validar.validarId(body.idPrograma);
const idCarrera = validar.validarId(body.idCarrera);
const numeroCuenta = validar.validarNumeroCuenta(body.numeroCuenta);
const creditos = validar.validarNumero(body.creditos);
const correo = validar.validarCorreo(body.correo);
const fechaInicio = validar.validarFecha(body.fechaInicio);
const fechaFin = validar.validarFecha(body.fechaFin);
const path = `./server/uploads/${validar.validar(file, 'El archivo', 1000)}`;
const programaInterno = body.programaInterno
2021-04-07 15:01:38 -05:00
? validar.validarAlfanumerico(
2020-11-27 12:36:53 -06:00
body.programaInterno,
2021-02-16 15:44:19 -06:00
'El texto programa interno',
2021-02-10 16:45:15 -06:00
250
2020-11-27 12:36:53 -06:00
)
: '';
2021-01-14 03:13:37 -06:00
const profesor = body.profesor
2020-11-27 12:36:53 -06:00
? validar.validarTexto(body.profesor, 'El texto de profesor.', 50)
: '';
2021-01-14 03:13:37 -06:00
let carpeta = '';
2020-11-16 12:47:46 -06:00
2021-05-12 21:05:53 -05:00
console.log(fechaInicio.format());
2020-11-16 12:47:46 -06:00
return Usuario.findOne({
where: { idUsuario },
2020-11-16 12:47:46 -06:00
})
.then((res) => {
if (!res) throw new Error('Este alumno no existe en la db.');
2020-11-23 11:03:30 -06:00
if (res.idTipoUsuario !== 3)
throw new Error('Este usuario no es de tipo Alumno.');
2020-11-16 12:47:46 -06:00
return Programa.findOne({ where: { idPrograma } });
})
.then((res) => {
if (!res) throw new Error('Este programa no existe en la db.');
return Carrera.findOne({ where: { idCarrera } });
})
.then((res) => {
if (!res) throw new Error('Esta carrera no existe en la db.');
return Servicio.findOne({
2021-02-16 15:44:19 -06:00
where: { idUsuario, idStatus: { [Op.ne]: 10 } },
2020-11-16 12:47:46 -06:00
});
})
2020-11-22 19:01:24 -06:00
.then(async (res) => {
2020-11-16 12:47:46 -06:00
if (res)
throw new Error('Este alumno ya tienen un Servicio Social activo.');
2020-11-23 11:03:30 -06:00
return drive.folder(numeroCuenta);
})
.then((res) => {
carpeta = res;
return drive.uploadFile(
2020-11-22 19:01:24 -06:00
path,
`Carta_Aceptacion.pdf`,
'application/pdf',
carpeta
);
2020-11-23 11:03:30 -06:00
})
2021-01-12 21:09:37 -06:00
.then((res) =>
Servicio.create({
2020-11-16 12:47:46 -06:00
idUsuario,
idPrograma,
idCarrera,
creditos,
correo,
2021-05-12 21:05:53 -05:00
fechaInicio: fechaInicio.format(),
fechaFin: fechaFin.format(),
2020-11-17 10:12:30 -06:00
carpeta,
cartaAceptacion: res,
profesor,
programaInterno,
2021-01-12 21:09:37 -06:00
})
)
2021-01-14 16:02:57 -06:00
.then((res) => ({
message: `Se Pre-Registro correctamente al alumno.`,
}));
2020-11-16 09:58:55 -06:00
};
module.exports = nuevo;