All files / src/utils slotCalculations.ts

100% Statements 61/61
97.36% Branches 37/38
100% Functions 14/14
100% Lines 58/58

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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206                                  4x 283x 283x 180x 45x     283x                 4x     58x 58x 42x       10x 10x 8x       58x           4x     54x 54x               4x     160x 104x 104x 262x 16x     104x           4x           65x 65x 65x                       4x           77x   76x 76x   76x           4x       9x               4x           42x   42x           42x 2x     40x       42x                   4x             5x   5x   5x 3x     2x       2x   2x   2x           4x       5x 4x 4x 17x 3x     4x    
import { OfficeImpl } from "@/types/OfficeImpl";
import { MAX_SLOTS } from "@/utils/Constants";
 
/** Subservice type used in slot calculations */
export interface SlotSubservice {
  id?: string;
  count: number;
  providers: OfficeImpl[];
}
 
/**
 * Gets the maximum slots required for a service across all providers.
 * We use MAX (not min) because we need to ensure the booking works at
 * providers with higher slot requirements. If a service takes 3 slots
 * at most providers but 1 slot at one provider, we count 3 slots to be
 * conservative with the slot limit.
 */
export const getMaxSlotOfProvider = (providers: OfficeImpl[]): number => {
  let maxSlot = 1; // Default to 1 slot minimum
  providers.forEach((provider) => {
    if (provider.slots && provider.slots > maxSlot) {
      maxSlot = provider.slots;
    }
  });
  return maxSlot;
};
 
/**
 * Gets the minimum slotsPerAppointment across all providers.
 * We use MIN (not max) because we need to ensure the booking works at
 * ALL providers that offer this service. If Provider A allows 10 slots
 * and Provider B allows 3 slots, we must limit to 3 to work everywhere.
 */
export const getMinSlotsPerAppointmentOfProvider = (
  providers: OfficeImpl[]
): number => {
  let minSlot = 0;
  providers.forEach((provider) => {
    if (
      provider.slotsPerAppointment &&
      parseInt(provider.slotsPerAppointment) > 0
    ) {
      const providerSlots = parseInt(provider.slotsPerAppointment);
      if (minSlot === 0 || providerSlots < minSlot) {
        minSlot = providerSlots;
      }
    }
  });
  return minSlot;
};
 
/**
 * Calculates the effective minSlotsPerAppointment, applying MAX_SLOTS as fallback.
 */
export const getEffectiveMinSlotsPerAppointment = (
  providers: OfficeImpl[]
): number => {
  const minSlotsOfProvider = getMinSlotsPerAppointmentOfProvider(providers);
  return minSlotsOfProvider > 0
    ? Math.min(minSlotsOfProvider, MAX_SLOTS)
    : MAX_SLOTS;
};
 
/**
 * Calculates the total slots used by a list of subservices.
 */
export const calculateSubserviceSlots = (
  subServices: SlotSubservice[] | undefined
): number => {
  if (!subServices) return 0;
  let slots = 0;
  subServices.forEach((subservice) => {
    if (subservice.count > 0) {
      slots += getMaxSlotOfProvider(subservice.providers) * subservice.count;
    }
  });
  return slots;
};
 
/**
 * Calculates the total slots for an appointment (main service + all subservices).
 */
export const calculateTotalSlots = (
  mainServiceProviders: OfficeImpl[],
  mainServiceCount: number,
  subServices: SlotSubservice[] | undefined
): number => {
  const mainSlots =
    getMaxSlotOfProvider(mainServiceProviders) * mainServiceCount;
  const subSlots = calculateSubserviceSlots(subServices);
  return mainSlots + subSlots;
};
 
/**
 * Calculates the maximum allowed count for a service based on slot constraints.
 *
 * @param serviceSlots - Slots required per unit of this service
 * @param maxQuantity - The service's own quantity limit
 * @param minSlotsPerAppointment - Total slots allowed for the appointment
 * @param otherSlotsUsed - Slots already used by other services
 * @returns The maximum count allowed (at least 0)
 */
export const calculateMaxCountBySlots = (
  serviceSlots: number,
  maxQuantity: number,
  minSlotsPerAppointment: number,
  otherSlotsUsed: number
): number => {
  if (serviceSlots <= 0) return maxQuantity;
 
  const availableSlots = minSlotsPerAppointment - otherSlotsUsed;
  const maxCountBySlots = Math.floor(availableSlots / serviceSlots);
 
  return Math.max(0, Math.min(maxQuantity, maxCountBySlots));
};
 
/**
 * Checks if total slots exceed the limit.
 */
export const exceedsSlotLimit = (
  totalSlots: number,
  minSlotsPerAppointment: number
): boolean => {
  return minSlotsPerAppointment > 0 && totalSlots > minSlotsPerAppointment;
};
 
/**
 * Adjusts the main service count to fit within slot limits.
 * When at least one unit fits, count stays at least 1 (same as before the zero-count
 * path). Returns 0 only when quantity 1 already exceeds slotsPerAppointment.
 */
export const adjustMainServiceCount = (
  requestedCount: number,
  mainServiceProviders: OfficeImpl[],
  subServiceSlots: number,
  minSlotsPerAppointment: number
): { adjustedCount: number; totalSlots: number } => {
  const mainServiceSlots = getMaxSlotOfProvider(mainServiceProviders);
  const maxMainCount =
    mainServiceSlots > 0
      ? Math.floor(
          (minSlotsPerAppointment - subServiceSlots) / mainServiceSlots
        )
      : requestedCount;
 
  if (minSlotsPerAppointment > 0 && maxMainCount < 1) {
    return { adjustedCount: 0, totalSlots: subServiceSlots };
  }
 
  const adjustedCount = Math.max(
    1,
    Math.min(requestedCount || 1, maxMainCount)
  );
  return {
    adjustedCount,
    totalSlots: mainServiceSlots * adjustedCount + subServiceSlots,
  };
};
 
/**
 * Adjusts a subservice count to fit within slot limits.
 * Returns the adjusted count and updated total slots.
 */
export const adjustSubserviceCount = (
  requestedCount: number,
  subserviceProviders: OfficeImpl[],
  mainServiceSlots: number,
  otherSubserviceSlots: number,
  minSlotsPerAppointment: number
): { adjustedCount: number; totalSlots: number } => {
  const subServiceSlots = getMaxSlotOfProvider(subserviceProviders);
  const totalSlots =
    mainServiceSlots + otherSubserviceSlots + subServiceSlots * requestedCount;
 
  if (!exceedsSlotLimit(totalSlots, minSlotsPerAppointment)) {
    return { adjustedCount: requestedCount, totalSlots };
  }
 
  const maxSubCount = Math.floor(
    (minSlotsPerAppointment - mainServiceSlots - otherSubserviceSlots) /
      subServiceSlots
  );
  const adjustedCount = Math.max(0, Math.min(requestedCount, maxSubCount));
  const adjustedTotalSlots =
    mainServiceSlots + otherSubserviceSlots + subServiceSlots * adjustedCount;
 
  return { adjustedCount, totalSlots: adjustedTotalSlots };
};
 
/**
 * Calculates slots used by other subservices (excluding a specific one).
 */
export const calculateOtherSubserviceSlots = (
  subServices: SlotSubservice[] | undefined,
  excludeId: string
): number => {
  if (!subServices) return 0;
  let slots = 0;
  subServices.forEach((s) => {
    if (s.id !== excludeId && s.count > 0) {
      slots += getMaxSlotOfProvider(s.providers) * s.count;
    }
  });
  return slots;
};