Files

104 lines
3.1 KiB
JavaScript
Raw Permalink 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) => {
2022-01-02 15:07:49 -06:00
const idUsuario = validar.validarNumeroEntero(body.idUsuario, 'id usuario');
const idPrograma = validar.validarNumeroEntero(
body.idPrograma,
'id programa'
);
const idCarrera = validar.validarNumeroEntero(body.idCarrera, 'id carrera');
2021-01-14 03:13:37 -06:00
const numeroCuenta = validar.validarNumeroCuenta(body.numeroCuenta);
2022-01-02 18:25:31 -06:00
const creditos = validar.validarNumero(body.creditos, 'créditos', true);
2021-01-14 03:13:37 -06:00
const correo = validar.validarCorreo(body.correo);
2022-01-02 17:56:42 -06:00
const fechaInicio = validar.validarFecha(
body.fechaInicio,
'fecha de inicio',
false
);
const fechaFin = validar.validarFecha(body.fechaFin, 'fecha fin', false);
2022-01-02 18:36:46 -06:00
const path = `./server/uploads/${validar.validacionBasicaStr(
file,
'archivo',
true,
1000
)}`;
2021-01-14 03:13:37 -06:00
const programaInterno = body.programaInterno
2021-04-07 15:01:38 -05:00
? validar.validarAlfanumerico(
2020-11-27 12:36:53 -06:00
body.programaInterno,
2022-01-02 17:45:29 -06:00
'programa interno',
true,
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
2022-01-02 15:27:30 -06:00
? validar.validarTexto(body.profesor, 'profesor', true, 50)
: '';
2020-11-16 12:47:46 -06:00
2022-11-22 19:46:02 -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.');
2022-10-14 12:16:24 -05:00
return 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(),
2022-10-14 12:16:24 -05:00
carpeta: '',
cartaAceptacion: '',
profesor,
programaInterno,
2022-10-14 12:16:24 -05:00
});
})
.then((servicio) => {
let carpeta = '';
drive
2022-11-22 19:46:02 -06:00
.mkDir(numeroCuenta)
2022-10-14 12:16:24 -05:00
.then((res) => {
carpeta = res;
return drive.uploadFile(
path,
`Carta_Aceptacion.pdf`,
'application/pdf',
carpeta
);
})
.then((res) =>
Servicio.update(
{ carpeta, cartaAceptacion: res },
{ where: { idServicio: servicio.idServicio } }
)
2022-11-22 19:46:02 -06:00
);
2022-10-14 17:22:09 -05:00
return { message: `Se Pre-Registro correctamente a este alumno.` };
2022-10-14 12:16:24 -05:00
});
2020-11-16 09:58:55 -06:00
};
module.exports = nuevo;