Files
citas_recorridos_api/server/controller/PrimerSemestre/asistencia.js
T
2021-07-26 18:36:47 -05:00

33 lines
1.1 KiB
JavaScript

const moment = require('moment');
const helperPath = '../../helper';
const { validarNumeroCuenta } = require(`${helperPath}/validar`);
const dbPath = '../../db/tablas';
const Horario = require(`${dbPath}/Horario`);
const PrimerSemestre = require(`${dbPath}/PrimerSemestre`);
const hoy = moment();
const asistencia = async (body) => {
const numeroCuenta = validarNumeroCuenta(body.numeroCuenta);
return PrimerSemestre.findOne({
where: { numeroCuenta },
include: [{ model: Horario }],
})
.then((res) => {
if (!res) throw new Error('No existe este alumno.');
if (hoy.dayOfYear() != moment(res.Horario.horario).dayOfYear())
throw new Error('Este alumno no tiene una cita para el día de hoy.');
if (res.asistio)
throw new Error('Ya fue registrada la asistencia de este alumno.');
return PrimerSemestre.update(
{ asistio: true },
{ where: { numeroCuenta } }
);
})
.then((res) => ({
message: 'Se registro correctamente la asistencia de este alumno.',
}));
};
module.exports = asistencia;