How to apply custom availability filtering

This tutorial describes how you can apply further availability filtering through the use of callbacks in the Booking Widget.

d
Written by derrick mak
Updated over a week ago

Prerequisites

Please read the introduction to our booking widget 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; 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:

availability_constraints: [
    {"allow_period": {"start": "2018-01-01T00:00:00+01:00", "end": "2018-03-01T00:00:00+01:00"} },
    {"allow_day_and_time": {"day": "Monday", "start": 9, "end": 16} },
    {"allow_day_and_time": {"day": "Wednesday", "start": 12, "end": 18} }
]

The above filtering is not enough, as it will still produce Mondays in February and Wednesdays in January which conflicts with our business rules defined earlier.
We need to filter those out ourselves. The way we do this is by using the callback findTimeSuccessful and apply the missing filtering:

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?