All files / src/utils calculateEstimatedDuration.ts

100% Statements 31/31
100% Branches 22/22
100% Functions 1/1
100% Lines 31/31

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                    1x 74x 74x 74x 74x   62x     74x 74x 74x 29x 28x 27x 74x 21x 21x 21x     65x 52x 73x 73x 73x 42x 6x 6x 73x 6x 6x 73x 52x   62x 62x  
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 {
  if (!service || !provider) return 0;
 
  let total = 0;
 
  // Main service
  const mainProvider = service.providers?.find((p) => p.id == provider.id);
  if (
    mainProvider &&
    service.count &&
    mainProvider.slots &&
    mainProvider.slotTimeInMinutes
  ) {
    total +=
      service.count * mainProvider.slots * mainProvider.slotTimeInMinutes;
  }
 
  // Subservices
  if (service.subServices) {
    for (const sub of service.subServices) {
      const subProvider = sub.providers?.find((p) => p.id == provider.id);
      if (
        subProvider &&
        sub.count &&
        subProvider.slots &&
        subProvider.slotTimeInMinutes
      ) {
        total += sub.count * subProvider.slots * subProvider.slotTimeInMinutes;
      }
    }
  }
 
  return total;
}