-1
I am developing a scheduling system but came across the following difficulty:
In the schedule table (Appointment
) has a field quantidade
and would like each time a record was created to add +1 in this field, and when that field reached the value 6
return the error 'Agendamento já preenchido'
.
However, the function create
Sequelize creates a new record with a new id
. I would like to edit the record already created, I tried with conditional (if registration exists , do it...), but it did not work.
Any idea how to do this? Follow the code below:
import { startOfHour, parseISO, isBefore } from 'date-fns';
import Appointment from '../models/Appointment';
class AppointmentController {
async store(req, res) {
const { date } = req.body;
const hourStart = startOfHour(parseISO(date));
// Checando horas vencidas
if (isBefore(hourStart, new Date())) {
return res.status(400).json({ error: 'Data vencida' });
}
// Checando disponibilidade do agendamento
const checkAvailability = await Appointment.findOne({
where: {
date: hourStart,
canceled_at: null,
quantidade: 6,
},
});
if (checkAvailability) {
return res.status(400).json({ error: 'Agendamento já preenchido' });
}
// Criando agendamento
const appointment = await Appointment.create({
user_id: req.userId,
date,
qualidade: +1,
});
return res.json(appointment);
}
}
export default new AppointmentController();
You say you want to add +1 instead of creating a new record. This means you will always have the
date
availinghourStart
and thecanceled_at
asnull
? I’m in doubt if you’re having one XY problem there– Rafael Tavares
If the scheduling is at the same time I want to add to the +1 quantity field if the time is different then it will be a new record
– Douglas Sena