api completa

This commit is contained in:
2022-05-13 22:10:54 -05:00
parent f9fa0ac0ca
commit 2015de3729
31 changed files with 366 additions and 89 deletions
+33 -12
View File
@@ -1,26 +1,44 @@
const Invitado = require('../../db/tablas/Invitado');
const Profesor = require('../../db/tablas/Profesor');
const Premiacion = require('../../db/tablas/Premiacion');
const validar = require('../../helper/validar');
const { correo } = require('../../helper/correos');
const gmail = require('../../helper/gmail');
const ProfesorPremiacion = require('../../db/tablas/ProfesorPremiacion');
const infoInvitados = async (body) => {
const idProfesor = validar.validarNumeroEntero(body.idProfesor, 'idProfesor');
const idPremiacion = validar.validarNumeroEntero(
body.idPremiacion,
'idPremiacion'
);
const invitados = body.invitados;
const numeroInvitados = invitados.length;
const premiacion = await Premiacion.findOne({ where: idPremiacion });
if (!premiacion)
throw new Error('No existe ninguna premiación con este id con este id.');
invitados.forEach((invitado) => {
if (invitado.edad < 18)
throw new Error('Los invitados deben de ser mayores de edad.');
if (invitado.edad < 12)
throw new Error(
'Alguno de sus invitados no cuenta con la edad suficiente.'
);
});
if (numeroInvitados > 3)
throw new Error('El número de invitados no puede ser mayor que 3.');
const prof = await Profesor.findOne({ where: idProfesor });
if (prof.numeroInvitados)
const profPrem = await ProfesorPremiacion.findOne({
where: { idProfesor, idPremiacion },
});
if (!profPrem)
throw new Error(
'No existe ningún profesor con este id en esta premiación.'
);
if (profPrem.numeroInvitados)
throw new Error('A este profesor ya se le han guardado sus invitados.');
prof.update({ numeroInvitados: numeroInvitados });
profPrem.update({ numeroInvitados: numeroInvitados });
const respuesta = [];
for (let i = 0; i < numeroInvitados; i++) {
@@ -40,23 +58,26 @@ const infoInvitados = async (body) => {
nombreCompleto: nombre,
edad: edad,
idProfesor: idProfesor,
idPremiacion: idPremiacion,
});
if (!res)
throw new Error('No se ha podido ingresar los datos del invitado');
respuesta.push(res);
}
const dataProf = await Profesor.findOne({ where: idProfesor });
const Mail = correo(
dataProf.nombre,
dataProf.numeroTrabajador,
dataProf.numeroInvitados
profPrem.idProfesor,
profPrem.idPremiacion,
premiacion.premiacion,
premiacion.fecha,
premiacion.hora
);
const dataProf = await Profesor.findOne({ where: idProfesor });
gmail(
Mail.subject,
`yosoymarco3@gmail.com`,
// `${dataProf.numeroTrabajador}@pcpuma.acatlan.unam.mx`,
// `jeremy@acatlan.unam.mx`,
`${dataProf.numeroTrabajador}@pcpuma.acatlan.unam.mx`,
Mail.message
);
return {
+17 -3
View File
@@ -1,6 +1,20 @@
const Invitado = require('../../db/tablas/Invitado');
const Profesor = require('../../db/tablas/Profesor')
const Profesor = require('../../db/tablas/Profesor');
const { validarNumeroEntero } = require('../../helper/validar');
const get = () => Invitado.findAll({include: [{ model: Profesor }],});
const get = async (query) => {
const idPremiacion = validarNumeroEntero(
query.idPremiacion,
'idPremiacion',
true
);
module.exports = get;
const invitados = await Invitado.findAll({
where: { idPremiacion },
include: [{ model: Profesor }],
});
if (!invitados) throw new Error('No se ha podido traer a los invitados');
return invitados;
};
module.exports = get;
@@ -0,0 +1,21 @@
const Premiacion = require('../../db/tablas/Premiacion');
const { validarNumeroEntero, validarFecha } = require('../../helper/validar');
const activarDesactivar = async (body) => {
if (!body.activa)
throw new Error('Se necesita saber si se quiere activar o desactivar.');
const idPremiacion = validarNumeroEntero(
body.idPremiacion,
'idPremiacion',
true
);
const activa = body.activa;
const premiacion = await Premiacion.findOne({ where: idPremiacion });
if (!premiacion) throw new Error('No existe esta premiación.');
return premiacion.update({ activa });
};
module.exports = activarDesactivar;
+21
View File
@@ -0,0 +1,21 @@
const Premiacion = require('../../db/tablas/Premiacion');
const { validarNumeroEntero, validarFecha } = require('../../helper/validar');
const editar = async (body) => {
let fecha;
let hora;
if (body.fecha) fecha = validarFecha(body.fecha);
if (body.hora) hora = body.hora;
const idPremiacion = validarNumeroEntero(
body.idPremiacion,
'idPremiacion',
true
);
const premiacion = await Premiacion.findOne({ where: idPremiacion });
if (!premiacion) throw new Error('No existe esta premiación.');
return premiacion.update({ fecha, hora});
};
module.exports = editar;
+18
View File
@@ -0,0 +1,18 @@
const Premiacion = require('../../db/tablas/Premiacion');
const validar = require('../../helper/validar');
const nueva = async (body) => {
const premiacion = validar.validacionBasicaStr(
body.premiacion,
'premiacion',
true
);
const fecha = validar.validarFecha(body.fecha, 'fecha', true);
if (!body.hora) throw new Error('Falta la hora.');
const hora = body.hora;
const res = await Premiacion.create({ premiacion, fecha, hora, activa: true });
return res;
};
module.exports = nueva;
@@ -0,0 +1,5 @@
const Premiacion = require('../../db/tablas/Premiacion');
const get = () => Premiacion.findAll();
module.exports = get;
+2 -1
View File
@@ -10,7 +10,8 @@ const adscripciones = async () => {
result.push(item.adscripcion);
}
});
return result
return result;
});
};
module.exports = adscripciones;
+14 -5
View File
@@ -1,8 +1,11 @@
const csv = require('csvtojson');
const { eliminarArchivo } = require('../../helper/helper');
const Profesor = require('../../db/tablas/Profesor');
const { validarNumeroEntero } = require('../../helper/validar');
const ProfesorPremiacion = require('../../db/tablas/profesorPremiacion');
const cargaMasiva = async (file) => {
const cargaMasiva = async (file, body) => {
const idPremiacion = validarNumeroEntero(body.idPremiacion);
const path = `server/uploads/${file}`;
let res = [];
@@ -15,15 +18,21 @@ const cargaMasiva = async (file) => {
nombre: profesores[i].nombre,
correo: `${profesores[i].numeroTrabajador}@pcpuma.acatlan.unam.mx`,
adscripcion: profesores[i].adscripcion,
})
res.push(resp)
});
res.push(resp);
await ProfesorPremiacion.create({
idPremiacion,
idProfesor: resp.idProfesor,
numeroInvitados: null,
numeroInvitadosDentro: null,
});
}
await eliminarArchivo(path);
});
return {
message: 'Se subió correctamente el archivo csv.',
res
res,
};
};
module.exports = cargaMasiva;
module.exports = cargaMasiva;
+22 -6
View File
@@ -1,21 +1,37 @@
const Profesor = require('../../db/tablas/Profesor');
const ProfesorPremiacion = require('../../db/tablas/ProfesorPremiacion');
const validar = require('../../helper/validar');
const entradaInvitado = async (body) => {
const idProfesor = validar.validarNumeroEntero(body.idProfesor, 'idProfesor');
const idPremiacion = validar.validarNumeroEntero(
body.idPremiacion,
'idPremiacion'
);
const profesor = await Profesor.findOne({ where: idProfesor });
if (!profesor) throw new Error('Este profesor no existe');
if (!profesor.noInvitados) throw new Error('Este profesor no registró a ninún invitado.')
let noInvDentro = profesor.dataValues.numeroInvitadosDentro;
let noInv = profesor.dataValues.numeroInvitados;
if (!profesor) throw new Error('Este profesor no existe.');
const profPrem = await ProfesorPremiacion.findOne({
where: { idPremiacion, idProfesor },
});
if (!profPrem)
throw new Error(
'Este profesor no está registrado en esta entrega de medallas.'
);
if (!profPrem.numeroInvitados)
throw new Error('Este profesor no registró a ninún invitado.');
let noInvDentro = profPrem.dataValues.numeroInvitadosDentro;
let noInv = profPrem.dataValues.numeroInvitados;
if (noInvDentro && noInvDentro >= noInv)
throw new Error('Este profesor ya ingreso a todos sus invitados.');
const res = await profesor.update({ numeroInvitadosDentro: noInvDentro + 1 });
const res = await profPrem.update({ numeroInvitadosDentro: noInvDentro + 1 });
if (!res)
throw new Error(
'Ha ocurrido un error al actualizar los datos del profesor.'
'Ha ocurrido un error al registrar los invitados del profesor.'
);
return res;
};
+9 -7
View File
@@ -8,15 +8,17 @@ const login = async (body) => {
'numero de trabajador',
true
);
const adscripcion = validar.validacionBasicaStr(
body.adscripcion,
'adscripcion',
false,
150
);
// const adscripcion = validar.validacionBasicaStr(
// body.adscripcion,
// 'adscripcion',
// false,
// 150
// );
return Profesor.findOne({
where: { numeroTrabajador, adscripcion },
where: { numeroTrabajador
// , adscripcion
},
}).then((res) => {
if (!res) throw new Error('No existe este profesor en la base de datos.');
return {