All files / apps/timeOff/src/utils timeOffSummary.ts

100% Statements 59/59
90.19% Branches 46/51
100% Functions 9/9
100% Lines 59/59

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170              2x 2x 2x 2x   2x 136x 136x 136x     2x           68x     2x 23x         2x 40x 40x 40x 40x     2x         40x 40x 2x     38x 38x 2x     36x     2x             30x 2x     28x         3x 1x     2x     25x 2x     23x   23x 23x   23x 23x   23x 37x 3x 3x     34x           34x           34x 30x     34x           34x           34x 26x     34x     23x     2x 13x     2x                         18x                       2x 4x    
import { DATE_PICKER_TIME_SLOT } from '@repo/ui/components/DatePicker';
import { DatePickerTimeSlot } from '@repo/ui/components/DatePicker';
 
import { formatDateWithWeekday, formatDaysOff } from '@repo/utils/date';
 
import { NullableDate } from '../types/timeOff';
 
const MORNING_START = { hour: 8, minute: 0 };
const MORNING_END = { hour: 12, minute: 0 };
const AFTERNOON_START = { hour: 13, minute: 30 };
const AFTERNOON_END = { hour: 17, minute: 30 };
 
const createTimeByLocal = (date: Date, hour: number, minute: number): Date => {
  const localDate = new Date(date);
  localDate.setHours(hour, minute, 0, 0);
  return localDate;
};
 
const isOverlap = (
  rangeStart: Date,
  rangeEnd: Date,
  slotStart: Date,
  slotEnd: Date,
) => {
  return rangeStart < slotEnd && rangeEnd > slotStart;
};
 
const isMidnight = (date: Date): boolean =>
  date.getHours() === 0 &&
  date.getMinutes() === 0 &&
  date.getSeconds() === 0 &&
  date.getMilliseconds() === 0;
 
const formatDateToYMD = (date: Date) => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
};
 
const isDisabledBusinessDate = (
  date: Date,
  holidays: string[],
  makeUpWorkdays: string[],
) => {
  const dateKey = formatDateToYMD(date);
  if (holidays.includes(dateKey)) {
    return true;
  }
 
  const isWeekend = date.getDay() === 0 || date.getDay() === 6;
  if (isWeekend && !makeUpWorkdays.includes(dateKey)) {
    return true;
  }
 
  return false;
};
 
export const calculateQuickSelectDaysOff = (
  startDate: NullableDate = null,
  endDate: NullableDate = null,
  timeSlot: DatePickerTimeSlot = DATE_PICKER_TIME_SLOT.ALL_DAY,
  holidays: string[] = [],
  makeUpWorkdays: string[] = [],
): number => {
  if (!startDate || !endDate) {
    return 0;
  }
 
  if (
    startDate.toDateString() === endDate.toDateString() &&
    isMidnight(startDate) &&
    isMidnight(endDate)
  ) {
    if (isDisabledBusinessDate(startDate, holidays, makeUpWorkdays)) {
      return 0;
    }
 
    return timeSlot === DATE_PICKER_TIME_SLOT.ALL_DAY ? 1 : 0.5;
  }
 
  if (endDate <= startDate) {
    return 0;
  }
 
  let total = 0;
 
  const current = new Date(startDate);
  current.setHours(0, 0, 0, 0);
 
  const last = new Date(endDate);
  last.setHours(0, 0, 0, 0);
 
  while (current <= last) {
    if (isDisabledBusinessDate(current, holidays, makeUpWorkdays)) {
      current.setDate(current.getDate() + 1);
      continue;
    }
 
    const morningStart = createTimeByLocal(
      current,
      MORNING_START.hour,
      MORNING_START.minute,
    );
 
    const morningEnd = createTimeByLocal(
      current,
      MORNING_END.hour,
      MORNING_END.minute,
    );
 
    if (isOverlap(startDate, endDate, morningStart, morningEnd)) {
      total += 0.5;
    }
 
    const afternoonStart = createTimeByLocal(
      current,
      AFTERNOON_START.hour,
      AFTERNOON_START.minute,
    );
 
    const afternoonEnd = createTimeByLocal(
      current,
      AFTERNOON_END.hour,
      AFTERNOON_END.minute,
    );
 
    if (isOverlap(startDate, endDate, afternoonStart, afternoonEnd)) {
      total += 0.5;
    }
 
    current.setDate(current.getDate() + 1);
  }
 
  return total;
};
 
export const buildSummaryDateText = (startDate?: NullableDate) => {
  return startDate ? formatDateWithWeekday(startDate) : 'No date selected';
};
 
export const buildSummaryValueText = ({
  startDate = null,
  endDate = null,
  timeSlot = DATE_PICKER_TIME_SLOT.ALL_DAY,
  holidays = [],
  makeUpWorkdays = [],
}: {
  startDate?: NullableDate;
  endDate?: NullableDate;
  timeSlot?: DatePickerTimeSlot;
  holidays?: string[];
  makeUpWorkdays?: string[];
}) => {
  return formatDaysOff(
    calculateQuickSelectDaysOff(
      startDate,
      endDate,
      timeSlot,
      holidays,
      makeUpWorkdays,
    ),
    startDate || undefined,
  );
};
 
export const buildSafeReasonText = (reason?: string) => {
  return reason?.trim() || 'No reason provided';
};