Prerequisites

Please read the introduction to our booking.js and its also a very good idea to have read the how to make code customizations article. You also need some basic javascript skills, in order to comprehend the applied filtering code, specifically arrays, loops and dates. 

Motivation

The filtering provided by Timekit might not be enough to accomplish some special business rules. This tutorial will demonstrate how you can further filter the availability response from Timekit to fit your exact needs.

The business rules

In this example the business-hours are defined like this: Mondays between 9 and 16 but only in January and Wednesdays between 12 and 18 but only in February.
Such business rules are not satisfiable purely by means of the availability filters provided by Timekit, so we need to to some additional filtering or post-processing if you will of the availability result.

The solution

First of all we define the common search space (January and February) with a between_timestamp filter, we could also define this by the start and future parameters instead of filters. Secondly we define the specific day-and-time filters as OR filters. Like so:

filters: {
  and:[
    {between_timestamps: {
      start: "2018-01-01T00:00:00+01:00",
      end: "2018-03-01T00:00:00+01:00"
    }}
  ],
  or:[
    {specific_day_and_time:{day: "Monday",start: 9, end: 16}},
    {specific_day_and_time:{day: "Wednesday",start: 12, end: 18}},
  ]
}

Please also remember to set the future parameter accordingly, the default future is 4 weeks which will not span 2 months. 

This filtering will however produce Mondays in February and Wednesdays in January which conflicts with our business rules, so we need to filter those out ourselves.
The way we do this is by using the callback findTimeSuccessful and apply the missing filtering. Like so:

callbacks: {
  findTimeSuccessful: function(response) {
    var filteredDates = new Array();
    var numDates = response.data.length;
    for (var i = 0; i < numDates; i++) {
      var start = new Date(response.data[i].start);
      if(start.getDay() == 1 && start.getMonth() == 1){
        //Wednesdays in January: skip
        continue;
      }
      if(start.getDay() == 3 && start.getMonth() == 0){
        //Mondays in February: skip
        continue;
      }
      //If the date wasn't skipped, it must be valid.
      filteredDates.push(response.data[i]);
    }
    response.data = filteredDates;
    return response;
  }
}

The bookable time-slots in the widget will now be in accordance with the business rules. 

Conclusion

Callbacks offer you the ability to expand Timekits out-of-the-box functionality to suit almost any use-case you can imagine, because the possibilities are only limited by what you can envision and define through code.

Did this answer your question?