All files / src/utils jumpinLink.ts

93.61% Statements 44/47
91.89% Branches 34/37
100% Functions 8/8
93.61% Lines 44/47

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                9x 2x   7x             7x     7x                   46x 46x 6x   40x                       15x 15x 15x 1x   14x     14x 2x   12x 2x   10x               2x                             11x 11x                                     3x     3x 5x 5x   3x 8x 8x     8x                       13x 2x   11x 11x 1x   10x 13x 13x 9x     10x 10x 4x   10x 10x    
/**
 * Host-page URLs for the citizenview shells.
 * `/` locally, `/buergeransicht/` on zms hosts.
 */
export function citizenviewRootHref(page: {
  origin: string;
  pathname: string;
}): string {
  if (page.pathname.startsWith("/buergeransicht")) {
    return `${page.origin}/buergeransicht/`;
  }
  return `${page.origin}/`;
}
 
export function citizenviewPageHref(
  page: { origin: string; pathname: string },
  file: string
): string {
  Iif (!file || file === "/" || file === "index.html") {
    return citizenviewRootHref(page);
  }
  return `${citizenviewRootHref(page)}${file.replace(/^\//, "")}`;
}
 
export type JumpinLinkInput = {
  serviceId: string;
  locationId?: string;
  exclusiveLocation?: boolean;
};
 
function numericId(value: string): string | null {
  const trimmed = value.trim();
  if (!/^\d+$/.test(trimmed)) {
    return null;
  }
  return trimmed;
}
 
export type JumpinLinkError =
  | "jumpinServiceIdMissing"
  | "jumpinServiceIdNumeric"
  | "jumpinLocationIdNumeric";
 
/** Why the current fields do not make a link. Empty fields are not an error. */
export function jumpinLinkError(
  input: JumpinLinkInput
): JumpinLinkError | null {
  const service = input.serviceId.trim();
  const location = (input.locationId ?? "").trim();
  if (!service && !location) {
    return null;
  }
  Iif (!service) {
    return "jumpinServiceIdMissing";
  }
  if (!numericId(service)) {
    return "jumpinServiceIdNumeric";
  }
  if (location && !numericId(location)) {
    return "jumpinLocationIdNumeric";
  }
  return null;
}
 
/**
 * Public Magnolia pages for an internal ZMS host.
 * The builder stays on zms*.muenchen.de; these are the links it hands out.
 * zms-test and zms-load have no public page.
 */
const EXTERNAL_JUMPIN_PAGES: Record<string, readonly string[]> = {
  "zms-dev.muenchen.de": [
    "https://stadt-i.muenchen.de/buergerservice/terminvereinbarung.html",
    "https://lhm-i.muenchen.swm.de/buergerservice/terminvereinbarung.html",
  ],
  "zms-demo.muenchen.de": [
    "https://stadt-k.muenchen.de/buergerservice/terminvereinbarung.html",
    "https://lhm-k.muenchen.swm.de/buergerservice/terminvereinbarung.html",
  ],
  "zms.muenchen.de": [
    "https://stadt.muenchen.de/buergerservice/terminvereinbarung.html",
  ],
};
 
export function externalJumpinPages(hostname: string): readonly string[] {
  const host = hostname.trim().toLowerCase().replace(/\.$/, "");
  return EXTERNAL_JUMPIN_PAGES[host] ?? [];
}
 
export type JumpinBuiltLink = {
  id: string;
  label: string;
  href: string;
};
 
/**
 * Internal booking link, then the public pages for this environment.
 * `internalLabel` is the visible name of the internal link.
 */
export function buildJumpinLinks(
  internalPageHref: string,
  hostname: string,
  input: JumpinLinkInput,
  internalLabel: string
): JumpinBuiltLink[] {
  const pages: { id: string; label: string; page: string }[] = [
    { id: "internal", label: internalLabel, page: internalPageHref },
  ];
  for (const page of externalJumpinPages(hostname)) {
    const label = new URL(page).hostname;
    pages.push({ id: label, label, page });
  }
  return pages.flatMap((entry) => {
    const href = buildJumpinHref(entry.page, input);
    Iif (!href) {
      return [];
    }
    return [{ id: entry.id, label: entry.label, href }];
  });
}
 
/**
 * Booking-page jump-in: `#/services/{id}` and optional `/locations/{id}`,
 * plus `exclusiveLocation=1` when the link should pin that Standort.
 */
export function buildJumpinHref(
  bookingPageHref: string,
  input: JumpinLinkInput
): string | null {
  if (jumpinLinkError(input)) {
    return null;
  }
  const serviceId = numericId(input.serviceId);
  if (!serviceId) {
    return null;
  }
  const locationId = numericId(input.locationId ?? "");
  let hash = `/services/${serviceId}`;
  if (locationId) {
    hash += `/locations/${locationId}`;
  }
 
  const url = new URL(bookingPageHref);
  if (input.exclusiveLocation) {
    url.searchParams.set("exclusiveLocation", "1");
  }
  url.hash = hash;
  return url.href;
}