scheduling-sdk
Version:
Brought to you by Recal - A TypeScript SDK for scheduling functionality
72 lines • 2.8 kB
TypeScript
import type { WeeklyAvailability } from '../../types/availability.types';
import type { BusyTime } from '../../types/scheduling.types';
/**
* Converts a weekly availability pattern into busy times for a specific week.
*
* This function inverts the availability concept: it takes available time periods
* and generates busy times for all the unavailable periods. For example, if you're
* available Monday 9-17, it creates busy times for Monday 0-9 and 17-24, plus
* all day Tuesday through Sunday.
*
* @param availability - The weekly availability pattern to convert
* @param weekStart - The Monday date for the week to process (MUST be a Monday)
*
* @returns Array of busy times representing unavailable periods for that week
*
* @throws {Error} If weekStart is not a Monday (getDay() !== 1)
* @throws {Error} If availability contains invalid time formats or ranges
*
* @example
* ```typescript
* const availability = {
* schedules: [
* { days: ['monday', 'wednesday', 'friday'], start: '09:00', end: '17:00' },
* { days: ['tuesday', 'thursday'], start: '10:00', end: '16:00' }
* ]
* }
*
* const mondayDate = new Date('2024-01-01T00:00:00Z') // Must be Monday
* const busyTimes = weeklyAvailabilityToBusyTimes(availability, mondayDate)
*
* // Returns busy times for:
* // - Monday: 00:00-09:00, 17:00-23:59
* // - Tuesday: 00:00-10:00, 16:00-23:59
* // - Wednesday: 00:00-09:00, 17:00-23:59
* // - Thursday: 00:00-10:00, 16:00-23:59
* // - Friday: 00:00-09:00, 17:00-23:59
* // - Saturday: 00:00-23:59 (all day)
* // - Sunday: 00:00-23:59 (all day)
* ```
*
* @example
* ```typescript
* // Creating breaks with multiple schedules for the same day
* const scheduleWithLunch = {
* schedules: [
* { days: ['monday'], start: '09:00', end: '12:00' }, // Morning
* { days: ['monday'], start: '13:00', end: '17:00' } // Afternoon
* ]
* }
*
* const busyTimes = weeklyAvailabilityToBusyTimes(scheduleWithLunch, mondayDate)
* // Creates automatic lunch break: Monday 12:00-13:00 becomes busy time
* ```
*
* @example
* ```typescript
* // Helper function to get Monday of any week
* function getMondayOfWeek(date: Date): Date {
* const day = date.getDay()
* const diff = day === 0 ? -6 : 1 - day
* const monday = new Date(date)
* monday.setDate(date.getDate() + diff)
* return new Date(monday.getFullYear(), monday.getMonth(), monday.getDate())
* }
*
* const anyDate = new Date('2024-01-15T14:30:00Z') // Tuesday
* const mondayOfThatWeek = getMondayOfWeek(anyDate)
* const busyTimes = weeklyAvailabilityToBusyTimes(availability, mondayOfThatWeek)
* ```
*/
export declare function weeklyAvailabilityToBusyTimes(availability: WeeklyAvailability, weekStart: Date): BusyTime[];
//# sourceMappingURL=converter.d.ts.map