All files / packages/utils/src/date timeSlots.ts

100% Statements 25/25
100% Branches 19/19
100% Functions 6/6
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 662x       2x                     6x         6x   6x 4x     6x 108x                   5x       6x   6x 33x   33x 31x   30x       3x         3x   1x          
import { DAILY_TIME_SLOTS } from "@repo/constants/time";
 
import { TimeRange, TimeSlot } from "@repo/types";
 
import { timeToMinutes } from "./comparison";
 
/**
 * Filter the time slots based on the selected date and the start time.
 * If the start time is provided, filter out the time slots that are earlier than the start time.
 * If the start time is not provided, return all the time slots.
 * @param selectedDate - The selected date.
 * @param startDate - The start date of the range. Defaults to the selected date.
 * @param startTime - The start time of the range. Defaults to the current time.
 * @returns An array of time slots that are filtered based on the selected date and the start time.
 */
export const filterTimeSlots = (
  selectedDate: Date,
  startDate?: Date,
  startTime?: string,
): TimeSlot[] => {
  let minMinutes = 0;
 
  if (startDate && startTime && selectedDate.toDateString() === startDate.toDateString()) {
    minMinutes = timeToMinutes(startTime);
  }
 
  return DAILY_TIME_SLOTS.filter((slot) => {
    return timeToMinutes(slot.value) > minMinutes;
  });
};
 
interface FilterTimeSlotOptions {
  min?: number;
  max?: number;
  excludeRanges?: TimeRange[];
}
 
export const getBookedTimeSlots = (
  slots: TimeSlot[],
  options: FilterTimeSlotOptions = {},
): TimeSlot[] => {
  const { min, max, excludeRanges = [] } = options;
 
  return slots.filter((slot) => {
    const minutes = timeToMinutes(slot.value);
 
    if (min !== undefined && minutes <= min) return true;
    if (max !== undefined && minutes > max) return true;
 
    return excludeRanges.some((range) => minutes >= range.start && minutes < range.end);
  });
};
 
export const getBlockedRoomSlots = (
  date: Date | null | undefined,
  bookedRanges: TimeRange[],
  minMinutes?: number,
): TimeSlot[] => {
  if (!date) return [];
 
  return getBookedTimeSlots(DAILY_TIME_SLOTS, {
    min: minMinutes,
    excludeRanges: bookedRanges,
  });
};