Created
October 29, 2019 14:12
-
-
Save alanfoandrade/d55a72a689c839a1d5a1e64bdd8069a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { subDays, endOfDay, startOfDay } from 'date-fns'; | |
import { Op } from 'sequelize'; | |
import * as Yup from 'yup'; | |
import Enrollment from '../models/Enrollment'; | |
import Student from '../models/Student'; | |
import Checkin from '../schemas/Checkin'; | |
// CHECKINS ARMAZENADOS NO MONGO | |
class CheckinController { | |
async store(req, res) { | |
const schema = Yup.object().shape({ | |
studentId: Yup.number(), | |
}); | |
if (!(await schema.isValid(req.params))) | |
return res.status(400).json({ error: 'Erro de validação' }); | |
const student_id = req.params.studentId; | |
const isStudent = await Student.findByPk(student_id); | |
if (!isStudent) | |
return res.status(401).json({ message: 'Aluno não encontrado' }); | |
const isEnrolled = await Enrollment.findOne({ | |
where: { | |
student_id, | |
canceled_at: null, | |
start_date: { | |
[Op.lte]: startOfDay(new Date()), | |
}, | |
end_date: { | |
[Op.gte]: endOfDay(new Date()), | |
}, | |
}, | |
}); | |
if (!isEnrolled) | |
return res.status(401).json({ message: 'Aluno não matriculado' }); | |
const daysCheckin = 7; // Numero de dias anteriores a contar os checkin | |
const checkinLimit = 5; // Limite de checkins | |
const lastCheckins = await Checkin.countDocuments({ | |
student_id, | |
createdAt: { $gt: endOfDay(subDays(new Date(), daysCheckin)) }, | |
}); | |
if (lastCheckins >= checkinLimit) | |
return res.status(401).json({ message: 'Limite de checkins atingido' }); | |
const { createdAt } = await Checkin.create({ student_id }); | |
const studentName = isStudent.name; | |
const studentEmail = isStudent.email; | |
return res.json({ | |
student_id, | |
studentName, | |
studentEmail, | |
createdAt, | |
}); | |
} | |
async index(req, res) { | |
const schema = Yup.object().shape({ | |
studentId: Yup.number(), | |
}); | |
if (!(await schema.isValid(req.params))) | |
return res.status(400).json({ error: 'Erro de validação' }); | |
const student_id = req.params.studentId; | |
const checkins = await Checkin.find({ student_id }); | |
if (checkins.length === 0) | |
return res.status(400).json({ error: 'Nenhum checkin encontrado' }); | |
return res.json(checkins); | |
} | |
} | |
export default new CheckinController(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment