Reputation: 1
We have an usecase, where we are building an app which books time slots for specific mentioned time. For that we have created a table with "From time and To time". Now, we are able to validate booking slot time against the already booked slots.
Struking point: For instance, if a person needs to book time slot from 9 am to 12 pm then we have booked the slot. If another person tries to book time slot for that same time or between the time, we are able to validate and generate error. Now if a person tries to book a slot from 8 am to 1 pm then we are unable to validate. Kindly show any ideas to achieve this requirement
Upvotes: 0
Views: 127
Reputation: 787
Considering that you are handling the models, dates and other stuff, something like this will help you solve the problem:
model.find({
$or: [{
$and: [{
"fromTime": { $gte: desiredFromTime },
},
{
"toTime": { $lte: desiredToTime },
}
]
}, {
$and: [{
"fromTime": { $lte: desiredFromTime },
},
{
"toTime": { $gte: desiredToTime },
}
]
}]
}).exec(function(err,result){
if(result)
console.log("You can't book")
else{
//book the slot
}
})
Upvotes: 3