All files / src/utils rebookingContact.ts

95.12% Statements 78/82
90.47% Branches 76/84
100% Functions 20/20
95.12% Lines 78/82

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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231                  3x           2x 2x       15x       52x 5x   47x     52x       50x 50x     50x       109x       53x       115x                           33x 165x 33x 33x                                 52x 52x 33x   19x 19x 4x   15x                   7x 14x 14x               17x 17x 8x   17x 8x   17x       9x   17x 5x   17x 6x   17x 5x                       17x 5x   12x     12x 4x 2x     10x 3x       10x 4x 2x     8x     3x 3x     148x 148x         148x     148x 148x 17x 17x         17x 17x   131x 131x 131x 131x                       29x 44x 44x 44x 1x   43x         29x     29x         29x 15x      
import type { AppointmentDTO } from "@/api/models/AppointmentDTO";
import type { Scope } from "@/api/models/Scope";
import type { CustomerData } from "@/types/CustomerData";
import type { ComputedRef, Ref } from "vue";
 
import { nextTick, onUpdated, watch } from "vue";
 
/** Same default as zmscitizenapi when ZMS_CITIZENAPI_PLACEHOLDER_EMAIL is unset. */
export const PLACEHOLDER_RESERVE_EMAIL =
  "noreply-terminvereinbarung@muenchen.de";
 
/** Address from the appointment response, which the API read from the env var. */
let placeholderFromApi: string | undefined;
 
export function setPlaceholderReserveEmail(email?: string | null): void {
  const trimmed = (email ?? "").trim();
  placeholderFromApi = trimmed || undefined;
}
 
export function resetPlaceholderReserveEmail(): void {
  placeholderFromApi = undefined;
}
 
export function getPlaceholderReserveEmail(): string {
  if (placeholderFromApi) {
    return placeholderFromApi;
  }
  const fromEnv = String(
    import.meta.env.ZMS_CITIZENAPI_PLACEHOLDER_EMAIL ?? ""
  ).trim();
  return fromEnv || PLACEHOLDER_RESERVE_EMAIL;
}
 
export function isPlaceholderEmail(email?: string | null): boolean {
  const placeholder = getPlaceholderReserveEmail().toLowerCase();
  Iif (!placeholder) {
    return false;
  }
  return (email ?? "").trim().toLowerCase() === placeholder;
}
 
export function isFilledContactValue(value?: string | null): boolean {
  return (value ?? "").trim() !== "";
}
 
export function isFilledEmail(email?: string | null): boolean {
  return isFilledContactValue(email) && !isPlaceholderEmail(email);
}
 
export function isReservedProcessStatus(status?: string | null): boolean {
  return (status ?? "").trim().toLowerCase() === "reserved";
}
 
export function getContactFieldLocks(
  isRebooking: boolean | undefined,
  sourceAppointment?: AppointmentDTO | null
): {
  firstName: boolean;
  lastName: boolean;
  mailAddress: boolean;
  telephoneNumber: boolean;
  customTextfield: boolean;
  customTextfield2: boolean;
} {
  const lockIfStoredOnSource = (value?: string | null) =>
    Boolean(isRebooking) && isFilledContactValue(value);
  const storedName = splitFamilyName(sourceAppointment?.familyName);
  return {
    // Single-word familyName ("Max") locks firstName only. The API accepts
    // completing that stored value with further name parts.
    firstName: lockIfStoredOnSource(storedName.firstName),
    lastName: lockIfStoredOnSource(storedName.lastName),
    mailAddress:
      Boolean(isRebooking) && isFilledEmail(sourceAppointment?.email),
    telephoneNumber: lockIfStoredOnSource(sourceAppointment?.telephone),
    customTextfield: lockIfStoredOnSource(sourceAppointment?.customTextfield),
    customTextfield2: lockIfStoredOnSource(sourceAppointment?.customTextfield2),
  };
}
 
export function splitFamilyName(familyName?: string | null): {
  firstName: string;
  lastName: string;
} {
  const trimmed = (familyName ?? "").trim();
  if (!trimmed) {
    return { firstName: "", lastName: "" };
  }
  const space = trimmed.indexOf(" ");
  if (space === -1) {
    return { firstName: trimmed, lastName: "" };
  }
  return {
    firstName: trimmed.slice(0, space),
    lastName: trimmed.slice(space + 1).trim(),
  };
}
 
export function joinFamilyName(
  firstName?: string | null,
  lastName?: string | null
): string {
  return [firstName, lastName]
    .map((part) => (part ?? "").trim())
    .filter((part) => part !== "")
    .join(" ");
}
 
export function applyAppointmentContactToCustomerData(
  customerData: CustomerData,
  appointment: AppointmentDTO
): void {
  const { firstName, lastName } = splitFamilyName(appointment.familyName);
  if (firstName && !customerData.firstName) {
    customerData.firstName = firstName;
  }
  if (lastName && !customerData.lastName) {
    customerData.lastName = lastName;
  }
  if (
    isFilledEmail(appointment.email) &&
    !isFilledEmail(customerData.mailAddress)
  ) {
    customerData.mailAddress = appointment.email;
  }
  if (appointment.telephone && !customerData.telephoneNumber) {
    customerData.telephoneNumber = appointment.telephone;
  }
  if (appointment.customTextfield && !customerData.customTextfield) {
    customerData.customTextfield = appointment.customTextfield;
  }
  if (appointment.customTextfield2 && !customerData.customTextfield2) {
    customerData.customTextfield2 = appointment.customTextfield2;
  }
}
 
/**
 * True when the target scope still needs contact data the citizen has not provided.
 * Email is always required in the Bürgerfrontend contact form.
 */
export function hasMissingRequiredContact(
  appointment: AppointmentDTO,
  scope?: Scope | null
): boolean {
  if (!isFilledEmail(appointment.email)) {
    return true;
  }
  Iif (!isFilledContactValue(appointment.familyName)) {
    return true;
  }
  if (scope?.telephoneActivated && scope?.telephoneRequired) {
    if (!isFilledContactValue(appointment.telephone)) {
      return true;
    }
  }
  if (scope?.customTextfieldActivated && scope?.customTextfieldRequired) {
    Iif (!isFilledContactValue(appointment.customTextfield)) {
      return true;
    }
  }
  if (scope?.customTextfield2Activated && scope?.customTextfield2Required) {
    if (!isFilledContactValue(appointment.customTextfield2)) {
      return true;
    }
  }
  return false;
}
 
const LOCKED_CONTROL_BG = "var(--color-neutral-100, #f2f2f2)";
const LOCKED_CONTROL_FG = "var(--color-neutral-600, #6d6d6d)";
 
function applyNativeDisabledOnFieldset(fieldset: HTMLFieldSetElement): void {
  const locked = fieldset.disabled;
  const control = fieldset.querySelector<
    HTMLInputElement | HTMLTextAreaElement
  >(
    "input:not([type=hidden]):not([type=checkbox]):not([type=radio]), textarea"
  );
  Iif (!control) {
    return;
  }
  control.disabled = locked;
  if (locked) {
    control.setAttribute("disabled", "");
    control.style.setProperty(
      "background-color",
      LOCKED_CONTROL_BG,
      "important"
    );
    control.style.setProperty("color", LOCKED_CONTROL_FG, "important");
    control.style.setProperty("cursor", "not-allowed", "important");
  } else {
    control.removeAttribute("disabled");
    control.style.removeProperty("background-color");
    control.style.removeProperty("color");
    control.style.removeProperty("cursor");
  }
}
 
/**
 * muc-input / muc-text-area have no disabled prop (fallthrough lands on a
 * wrapper div). A fieldset plus native disable actually locks the control.
 */
export function useNativeContactLocks(
  form: Ref<HTMLFormElement | null>,
  locks: Array<Ref<boolean> | ComputedRef<boolean>>
): void {
  const applyContactLocksToNativeControls = async () => {
    await nextTick();
    const formEl = form.value;
    if (!formEl) {
      return;
    }
    formEl
      .querySelectorAll<HTMLFieldSetElement>("fieldset[data-contact-lock]")
      .forEach(applyNativeDisabledOnFieldset);
  };
 
  watch(
    locks,
    () => {
      void applyContactLocksToNativeControls();
    },
    { immediate: true, flush: "post" }
  );
 
  onUpdated(() => {
    void applyContactLocksToNativeControls();
  });
}