All files / src/utils jumpinCatalog.ts

100% Statements 46/46
82.75% Branches 24/29
100% Functions 19/19
100% Lines 44/44

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                                                                    22x       10x 10x               20x 20x 10x   10x       21x       15x     1x       21x                   7x 7x 2x   5x 9x   5x 2x   3x         8x                   5x 10x   5x 15x     5x 5x 25x 25x 25x 25x 25x 10x   15x     5x     15x   9x   5x     15x   14x     5x 7x   5x 14x     5x    
/**
 * Offices and services offered by the jump-in builder.
 * The lists come from this environment's catalog. A chosen service keeps only
 * its offices, and a chosen office keeps only its services.
 */
 
export type JumpinService = { id: string | number; name: string };
 
export type JumpinOffice = {
  id: string | number;
  name: string;
  disabledByServices?: Array<string | number>;
  allowDisabledServicesMix?: boolean | number[];
};
 
export type JumpinRelation = {
  serviceId: string | number;
  officeId: string | number;
};
 
export type JumpinChoice = {
  id: string;
  name: string;
  label: string;
};
 
export type JumpinCatalog = {
  services: JumpinService[];
  offices: JumpinOffice[];
  relations: JumpinRelation[];
};
 
/** Same "(Id: …) name" text the workstation Standort search uses. */
export function jumpinChoiceLabel(id: string, name: string): string {
  return `(Id: ${id}) ${name}`;
}
 
function allowsDisabledService(office: JumpinOffice): boolean {
  const mix = office.allowDisabledServicesMix;
  return mix === true || (Array.isArray(mix) && mix.length > 0);
}
 
/** A relation the booking page would accept for a jump-in link. */
export function isBookableJumpinPair(
  office: JumpinOffice,
  serviceId: string
): boolean {
  const disabled = (office.disabledByServices ?? []).map((id) => String(id));
  if (!disabled.includes(serviceId)) {
    return true;
  }
  return allowsDisabledService(office);
}
 
function choice(id: string, name: string): JumpinChoice {
  return { id, name, label: jumpinChoiceLabel(id, name) };
}
 
function byName(a: JumpinChoice, b: JumpinChoice): number {
  return a.name.localeCompare(b.name, "de");
}
 
const idInLabel = /^\(Id: ([^)]+)\)/;
 
/** The id shown at the start of a workstation-style "(Id: …) name" label. */
export function jumpinIdFromLabel(label: string): string | null {
  return label.trim().match(idInLabel)?.[1] ?? null;
}
 
/**
 * When the query is only digits, the choice whose id equals that query comes
 * first. Every other hit keeps its relative order.
 */
export function promoteExactIdResults<
  T extends { item: { label?: string }; rank: number },
>(results: T[], query: string): T[] {
  const id = query.trim();
  if (!/^\d+$/.test(id)) {
    return results;
  }
  const index = results.findIndex(
    (result) => jumpinIdFromLabel(result.item.label ?? "") === id
  );
  if (index <= 0) {
    return results;
  }
  const ordered = [
    results[index],
    ...results.slice(0, index),
    ...results.slice(index + 1),
  ];
  return ordered.map((result, position) => ({
    ...result,
    rank: position + 1,
  }));
}
 
export function jumpinChoices(
  catalog: JumpinCatalog,
  selected: { serviceId?: string; officeId?: string } = {}
): { services: JumpinChoice[]; offices: JumpinChoice[] } {
  const servicesById = new Map(
    catalog.services.map((service) => [String(service.id), service])
  );
  const officesById = new Map(
    catalog.offices.map((office) => [String(office.id), office])
  );
 
  const pairs: { serviceId: string; officeId: string }[] = [];
  for (const relation of catalog.relations) {
    const serviceId = String(relation.serviceId);
    const officeId = String(relation.officeId);
    const service = servicesById.get(serviceId);
    const office = officesById.get(officeId);
    if (!service || !office || !isBookableJumpinPair(office, serviceId)) {
      continue;
    }
    pairs.push({ serviceId, officeId });
  }
 
  const serviceIds = new Set(
    pairs
      .filter(
        (pair) => !selected.officeId || pair.officeId === selected.officeId
      )
      .map((pair) => pair.serviceId)
  );
  const officeIds = new Set(
    pairs
      .filter(
        (pair) => !selected.serviceId || pair.serviceId === selected.serviceId
      )
      .map((pair) => pair.officeId)
  );
 
  const services = [...serviceIds]
    .map((id) => choice(id, servicesById.get(id)?.name ?? id))
    .sort(byName);
  const offices = [...officeIds]
    .map((id) => choice(id, officesById.get(id)?.name ?? id))
    .sort(byName);
 
  return { services, offices };
}