All files / src/utils calculateEstimatedDuration.ts

100% Statements 33/33
100% Branches 23/23
100% Functions 1/1
100% Lines 33/33

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                    1x 73x 73x 73x 73x   67x 73x   73x 73x 29x 28x 27x 73x 46x 46x   21x   29x 18x 44x 8x   8x 8x 6x 6x 8x 2x 2x   6x 6x 44x 16x   19x 19x  
import { OfficeImpl } from "@/types/OfficeImpl";
import { ServiceImpl } from "@/types/ServiceImpl";
import { SubService } from "@/types/SubService";
 
/**
 * Calculates the total estimated duration for the selected service and subservices for a given provider.
 * @param service The main service (with subServices and counts)
 * @param provider The selected provider/office
 * @returns The total estimated duration in minutes
 */
export function calculateEstimatedDuration(
  service: ServiceImpl | undefined,
  provider: OfficeImpl | undefined
): number | null {
  if (!service || !provider) return null;
 
  let total = 0;
  const mainProvider = service.providers?.find((p) => p.id == provider.id);
 
  if (
    !mainProvider ||
    !service.count ||
    !mainProvider.slots ||
    !mainProvider.slotTimeInMinutes
  ) {
    return null;
  }
 
  total += service.count * mainProvider.slots * mainProvider.slotTimeInMinutes;
 
  if (service.subServices) {
    for (const sub of service.subServices) {
      if (sub.count > 0) {
        const subProvider = sub.providers?.find((p) => p.id == provider.id);
 
        if (
          !subProvider ||
          !subProvider.slots ||
          !subProvider.slotTimeInMinutes
        ) {
          return null;
        }
 
        total += sub.count * subProvider.slots * subProvider.slotTimeInMinutes;
      }
    }
  }
 
  return total;
}