Created
October 28, 2019 19:00
-
-
Save alanfoandrade/74eec8e8536e96dc181e5db3cc213c75 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 { startOfDay, endOfDay, parseISO } from 'date-fns'; | |
import { Op } from 'sequelize'; | |
import Appointment from '../models/Appointment'; | |
import User from '../models/User'; | |
import File from '../models/File'; | |
class ScheduleController { | |
async index(req, res) { | |
const isProvider = await User.findOne({ | |
where: { id: req.userId, provider: true }, | |
}); | |
if (!isProvider) | |
return res.status(401).json({ error: 'Não é um prestador de serviços' }); | |
const { date, page = 1 } = req.query; | |
const parsedDate = parseISO(date); | |
if (!date) { | |
const appointments = await Appointment.findAll({ | |
where: { | |
provider_id: req.userId, | |
canceled_at: null, | |
}, | |
order: ['date'], | |
attributes: ['id', 'date'], | |
limit: 20, | |
offset: (page - 1) * 20, | |
include: [ | |
{ | |
model: User, | |
as: 'user', | |
attributes: ['id', 'name'], | |
include: [ | |
{ | |
model: File, | |
as: 'avatar', | |
attributes: ['id', 'path', 'url'], | |
}, | |
], | |
}, | |
], | |
}); | |
return res.json(appointments); | |
} | |
/** | |
* Lista os agendamentos referentes ao Provider logado, não cancelados e | |
* com horário entre o inicio e o final do dia especificado em req.query, | |
* ordenados por data | |
*/ | |
const appointments = await Appointment.findAll({ | |
where: { | |
provider_id: req.userId, | |
canceled_at: null, | |
date: { | |
[Op.between]: [startOfDay(parsedDate), endOfDay(parsedDate)], | |
}, | |
}, | |
order: ['date'], | |
attributes: ['id', 'date'], | |
limit: 20, | |
offset: (page - 1) * 20, | |
include: [ | |
{ | |
model: User, | |
as: 'user', | |
attributes: ['id', 'name'], | |
include: [ | |
{ | |
model: File, | |
as: 'avatar', | |
attributes: ['id', 'path', 'url'], | |
}, | |
], | |
}, | |
], | |
}); | |
return res.json(appointments); | |
} | |
} | |
export default new ScheduleController(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment