Created
October 26, 2019 16:12
-
-
Save alanfoandrade/1fa0ad4257a66da8ca8c1ca4a2dc5dae 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 * as Yup from 'yup'; | |
import { parseISO, endOfDay, addMonths } from 'date-fns'; | |
import Registration from '../models/Registration'; | |
import Student from '../models/Student'; | |
import Plan from '../models/Plan'; | |
class RegistrationController { | |
async store(req, res) { | |
// validação para os campos | |
const schema = Yup.object().shape({ | |
start_date: Yup.date().required(), | |
student_id: Yup.number().required(), | |
plan_id: Yup.number().required(), | |
}); | |
// if validation is no valid | |
if (!(await schema.isValid(req.body))) { | |
return res.status(400).json({ error: 'Validations fails' }); | |
} | |
// check if the plan exists | |
const { student_id, plan_id, start_date } = req.body; | |
const isPlan = await Plan.findByPk(plan_id); | |
if (!isPlan) { | |
return res.status(401).json({ message: 'Plan not found' }); | |
} | |
const startDate = endOfDay(parseISO(start_date)); | |
const end_date = addMonths(startDate, isPlan.duration); | |
const RegPrice = isPlan.price * isPlan.duration; | |
// getting a single registration | |
const registration = await Registration.create( | |
{ | |
...req.body, | |
start_date: startDate, | |
end_date, | |
price: RegPrice, | |
}); | |
return res.json(registration); | |
} | |
} | |
export default new RegistrationController(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment