Code Monkey home page Code Monkey logo

node-sscheduler's Introduction

sscheduler

Flexible scheduler to find free time slots in the schedule of a resource (which could be a person, a meeting room, a car, etc...)

sscheduler can also intersect the availability of multiple resources in order to find the time slots at which all the resources are available.

Installation

npm install @digvan/sscheduler

Basic usage

As an example, let's say that we want to book a 1 hour appointment with a doctor in the month of february considering that:

  • We can only book on weekdays from 9AM to 5PM

  • We can't book between noon and 1PM (lunch time !)

  • The doctor is on vacation the week of the 20th

  • There are already two one-hour appointments booked on February 1st at 1PM and 2PM

import {Scheduler} from '@digvan/sscheduler';

const scheduler = new Scheduler();
const schedule = {
    from: '2019-01-10',
    to: '2019-01-17',
    duration: 60,
    interval: 60,
    timezone: "America/Los_Angeles",
    schedule: {
        weekdays: {
            from: '08:00', to: '17:00',
            unavailability: [
                { from: '12:00', to: '13:00' }
            ]
        },
        unavailability: [
            // two different types of unavailability structure
            { from: '2019-01-20 00:00', to: '2019-01-27 00:00' },
            { date: '2019-01-15', from: '14:00', to: '15:00' }
        ],
        allocated: [
          { from: '2019-02-01 13:00' , duration: 60 },
          { from: '2019-02-01 14:00' , duration: 60 }
        ]
    }
};
console.log(scheduler.getAvailabilityWithTimezone(schedule, "America/Los_Angeles"));

The returned value is a structure that looks like the following:

{
  '2017-02-01': [
    { time: '09:00', available: true },
    { time: '10:00', available: true }
    // ...
  ]
  // ...
}

Options

The possible options for the getAvailability function are:

from (required) The start date for which we want to get availability times
to (required) The end date for which we want to get availability times (exclusive)
interval (required) The interval (in minutes) of the returned availability times. For example, a value of 15 would returns availability times such as *10:00*, *10:15*, *10:30*, *10:45*, etc..
duration (required) The duration (in minutes) for which we need the resource.
schedule (required) The schedule of the resource for each day of the week.

Example:

{
  monday: {
    from: '09:00',
    to: '17:00',
    unavailability: [
      { from: '12:00', to: '13:00' }
    ]
  },
  custom_schedule: [
    { "date": "2017-01-23", "from": "12:00", "to": "17:00" },
  ]
}

Schedule intersection

Using the same example as before, let's say that we also need to book a room for our appointment.

So, we need to intersect the doctor's availability times with the room's availability times, considering that:

  • We can only book the room on weekdays from 8AM to 8PM

  • The room is out of service from February 6th to February 16th

import {Scheduler} from '@digvan/sscheduler';

const scheduler = new Scheduler();
const schedule1 = {
    from: '2019-01-10',
    to: '2019-01-17',
    duration: 60,
    interval: 60,
    timezone: "America/Los_Angeles",
    schedule: {
        weekdays: {
            from: '08:00', to: '17:00',
            unavailability: [
                { from: '12:00', to: '13:00' }
            ]
        },
        unavailability: [
            // two different types of unavailability structure
            { from: '2019-01-20 00:00', to: '2019-01-27 00:00' },
            { date: '2019-01-15', from: '14:00', to: '15:00' }
        ],
        allocated: [
          { from: '2019-02-01 13:00' , duration: 60 },
          { from: '2019-02-01 14:00' , duration: 60 }
        ]
    }
};
const schedule2 = {
    from: '2019-01-10',
    to: '2019-01-17',
    duration: 60,
    interval: 60,
    timezone: "CET",
    schedule: {
        weekdays: {
            from: '07:00', to: '18:00',
            unavailability: [
                { from: '13:00', to: '14:00' }
            ]
        },
        unavailability: [
            // two different types of unavailability structure
            { from: '2019-01-15 17:00', to: '2019-01-15 18:00' },
            { date: '2019-01-16', from: '17:00', to: '18:00' }
        ],
        allocated: [
          { from: '2019-02-01 13:00' , duration: 60 },
          { from: '2019-02-01 14:00' , duration: 60 }
        ]
    }
};
const availability = scheduler.getAvailabilityWithTimezone(schedule2, "America/Los_Angeles");

Build

npm install
gulp

Test

npm run compile
npm run test

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

node-sscheduler's People

Contributors

abdizriel avatar asacco-ssense avatar digvan avatar ninjapixel avatar rundef avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.