0

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

1
  • 2
    Try put some code that shows you tried some. Commented Oct 24, 2018 at 11:58

1 Answer 1

3

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
        }
    })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.