O filtro não busca as turmas com data_inico entre um range de datas ex: http://localhost:3000/turmas?data_inicial=2020-06-01&data_final=2023-07-28
Dado no banco: [ { "id": 1, "data_inicio": "2020-07-01", "createdAt": "2023-06-28T21:16:47.000Z", "updatedAt": "2023-06-28T21:16:47.000Z", "deletedAt": null, "nivel_id": 1, "docente_id": 1 }, { "id": 2, "data_inicio": "2021-07-01", "createdAt": "2023-06-28T21:16:47.000Z", "updatedAt": "2023-06-28T21:16:47.000Z", "deletedAt": null, "nivel_id": 2, "docente_id": 3 }, { "id": 3, "data_inicio": "2022-07-01", "createdAt": "2023-06-28T21:16:47.000Z", "updatedAt": "2023-06-28T21:16:47.000Z", "deletedAt": null, "nivel_id": 3, "docente_id": 2 } ]
Minha função para buscar todas as turmas:
static async buscaTodasAsTurmas(req, res) {
const { data_inicial, data_final } = req.query;
const where = {};
data_inicial || data_final ? (where.data_inicio = {}) : null;
data_inicial ? (where.data_inicio[Op.gte] = data_inicial) : null;
data_final ? (where.data_inicio[Op.lte] = data_final) : null;
try {
const turmas = await database.Turmas.findAll({ where });
return res.status(200).json(turmas);
} catch (error) {
return res.status(500).json(error.message);
}
}