Let's say I have the following appointments:
Appointment 1 = 1:00 Appointment 2 = 2:00 Appointment 3 = 3:00 Appointment 4 = 4:00
I need to reschedule all of these one hour later:
Appointment 1 = 2:00 Appointment 2 = 3:00 Appointment 3 = 4:00 Appointment 4 = 5:00
The problem is that I want to do this in one atomic operation, but appointment times must be unique. If I handle these in order, then I will get an error. I can't schedule Appointment 1 at 2:00, because 2:00 is not an open time. It's occupied by Appointment 2.
Times are stored in a table very simply:
AppointmentId: number, AppointmentTime: datetime
There is a unique constraint on AppointmentTime.
I would rather not have to unschedule all the appointments first. That's probably what I'll end up doing, but I'm trying to avoid that.
The simple solution is to do these in reverse order. But this is a simple case that is neatly ordered. Chances are, the data would not be sequential. Is there a pattern or algorithm that deals with this kind of thing? Finding the optimal sequence for performing an operation on a list of objects?
There is also the case where I want to schedule my appointments to existing times:
Appointment 1 = 4:00 Appointment 2 = 2:00 Appointment 3 = 3:00 Appointment 4 = 1:00
There is no way to calculate an order that will work here. It's circular. That's why I'll probably end up unscheduling everything. But I'm still interested in trying to determine the optimal path.
I'm doing this on Node using MongoDB.
Anyone have any suggestions?