All files / src/utils infoForAllAppointments.ts

100% Statements 94/94
97.77% Branches 44/45
100% Functions 3/3
100% Lines 94/94

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              1x 39x 39x 39x 39x 39x 39x 39x         1x 9x 2x 2x     7x     7x 7x   9x 48x 48x 41x 48x 7x 7x 48x     9x 3x 3x 9x 3x 3x 3x     4x 4x                               1x 165x 165x 165x 165x 165x   165x 165x 165x     165x 9x 9x 9x   165x 61x 61x     95x 95x 178x 178x 178x 178x 178x 178x 178x 178x 95x 95x   153x 85x 85x     10x 10x 10x 140x 7x 7x     3x   3x 7x 7x 6x 6x 7x 7x 7x 3x     3x 3x 3x 6x 5x 5x 6x 1x 1x 1x 6x 6x 3x 3x 3x  
/**
 * Utility functions for handling infoForAllAppointments functionality
 */
 
/**
 * Cleans provider names by removing dashes, commas, numbers, and extra whitespace
 */
export const cleanProviderName = (name: string): string => {
  return name
    .replace(/[-–—]/g, " ") // Replace various types of dashes with spaces
    .replace(/[,;]/g, " ") // Replace commas and semicolons with spaces
    .replace(/\d+/g, " ") // Replace numbers with spaces
    .replace(/\s+/g, " ") // Replace multiple spaces with single space
    .trim(); // Remove leading/trailing whitespace
};
 
/**
 * Optimizes provider names by removing common prefixes when multiple providers share the same info
 */
export const optimizeProviderNames = (providerNames: string[]): string => {
  if (providerNames.length <= 1) {
    return cleanProviderName(providerNames[0] || "");
  }
 
  // Clean all provider names first
  const cleanedNames = providerNames.map(cleanProviderName);
 
  // Find the longest common prefix
  const first = cleanedNames[0];
  let commonPrefix = "";
 
  for (let i = 0; i < first.length; i++) {
    const char = first[i];
    if (cleanedNames.every((name) => name[i] === char)) {
      commonPrefix += char;
    } else {
      break;
    }
  }
 
  // If we found a meaningful common prefix (at least 3 characters and ends with a space)
  if (commonPrefix.length >= 3 && commonPrefix.endsWith(" ")) {
    const prefix = commonPrefix.trim();
    const suffixes = cleanedNames.map((name) =>
      name.substring(commonPrefix.length)
    );
    return `${prefix} ${suffixes.join(", ")}`;
  }
 
  // If no meaningful common prefix, return as is
  return cleanedNames.join(", ");
};
 
/**
 * Interface for provider info data
 */
export interface ProviderInfo {
  id?: string | number;
  name?: string;
  scope?: {
    infoForAllAppointments?: string | null;
  };
}
 
/**
 * Generates HTML for availability info based on selected providers
 */
export const generateAvailabilityInfoHtml = (
  selectedProviders: { [id: string]: boolean },
  selectableProviders: ProviderInfo[] | undefined,
  selectedProvider: ProviderInfo | undefined,
  sanitizeHtml: (html: string) => string
): string => {
  // Get all selected providers
  const selectedProviderIds = Object.keys(selectedProviders).filter(
    (id) => selectedProviders[id]
  );
 
  // If no providers are selected but we have a selectedProvider, use that
  if (selectedProviderIds.length === 0 && selectedProvider) {
    const info = selectedProvider.scope?.infoForAllAppointments || "";
    return info && info.trim() ? sanitizeHtml(info.trim()) : "";
  }
 
  if (selectedProviderIds.length === 0) {
    return "";
  }
 
  // Get all selected providers with their info
  const selectedProvidersWithInfo = selectedProviderIds
    .map((id) => {
      const provider = selectableProviders?.find(
        (p) => p.id?.toString() === id
      );
      return {
        id: provider?.id,
        name: provider?.name,
        info: provider?.scope?.infoForAllAppointments || "",
      };
    })
    .filter((provider) => provider.info.trim() !== "");
 
  if (selectedProvidersWithInfo.length === 0) {
    return "";
  }
 
  // If all providers have the same info, show it without location names
  const uniqueInfos = [
    ...new Set(selectedProvidersWithInfo.map((p) => p.info.trim())),
  ];
  if (uniqueInfos.length === 1) {
    return sanitizeHtml(uniqueInfos[0]);
  }
 
  // If providers have different info, group them by info and show with provider names
  const infoGroups: { [info: string]: string[] } = {};
 
  selectedProvidersWithInfo.forEach((provider) => {
    const info = provider.info.trim();
    if (!infoGroups[info]) {
      infoGroups[info] = [];
    }
    if (provider.name) {
      infoGroups[info].push(provider.name);
    }
  });
 
  // Generate HTML with provider names and info
  let html = "";
  let isFirst = true;
  Object.entries(infoGroups).forEach(([info, providerNames]) => {
    if (providerNames.length === 1) {
      const cleanedName = cleanProviderName(providerNames[0]);
      html += `<h3${isFirst ? ' class="first-provider"' : ""}>${sanitizeHtml(cleanedName)}</h3>`;
    } else {
      const optimizedNames = optimizeProviderNames(providerNames);
      html += `<h3${isFirst ? ' class="first-provider"' : ""}>${sanitizeHtml(optimizedNames)}</h3>`;
    }
    html += `<div>${sanitizeHtml(info)}</div>`;
    isFirst = false;
  });
  return sanitizeHtml(html);
};