All files / src/utils resolveOfficeById.ts

96.66% Statements 29/30
68.08% Branches 32/47
100% Functions 6/6
96.66% Lines 29/30

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        3x                 51x                                                                   41x     41x   41x 22x   41x 21x     20x 18x   41x 12x     8x   5x     41x 3x 3x 3x 3x   3x   3x 3x   3x     5x             5x                           5x 4x   1x 5x                                              
import { AppointmentDTO } from "@/api/models/AppointmentDTO";
import { Office } from "@/api/models/Office";
import { OfficeImpl } from "@/types/OfficeImpl";
 
const EMPTY_ADDRESS = {
  street: "",
  house_number: "",
  postal_code: "",
  city: "",
  hint: false,
};
 
export function toOfficeImpl(office: Office): OfficeImpl {
  return new OfficeImpl(
    String(office.id),
    office.name,
    office.address ?? EMPTY_ADDRESS,
    office.showAlternativeLocations,
    office.displayNameAlternatives ?? [],
    office.organization,
    office.organizationUnit,
    office.slotTimeInMinutes,
    office.disabledByServices,
    office.allowDisabledServicesMix,
    office.scope,
    office.slotsPerAppointment,
    office.slots,
    office.priority || 1,
    office.parentId,
    office.sharedBookingOfficeIds
  );
}
 
/**
 * Resolve the office that owns an appointment.
 * Shared-booking peers are collapsed to one Ort in the catalog, so the real
 * officeId (e.g. Ausbildung 10503) may be missing from `offices` while a
 * display peer (10489) still lists it in sharedBookingOfficeIds.
 */
export function resolveOfficeById(
  officeId: string | number | undefined | null,
  options: {
    offices?: Office[];
    providers?: Office[];
    appointment?: AppointmentDTO | null;
  } = {}
): OfficeImpl | undefined {
  Iif (officeId == null || officeId === "") {
    return undefined;
  }
  const id = String(officeId);
 
  const fromProviders = options.providers?.find(
    (office) => String(office.id) === id
  );
  if (fromProviders) {
    return toOfficeImpl(fromProviders);
  }
 
  const fromOffices = options.offices?.find(
    (office) => String(office.id) === id
  );
  if (fromOffices) {
    return toOfficeImpl(fromOffices);
  }
 
  const peer = (options.offices ?? []).find(
    (office) =>
      Array.isArray(office.sharedBookingOfficeIds) &&
      office.sharedBookingOfficeIds.map(Number).includes(Number(id))
  );
  if (peer) {
    const cloned = toOfficeImpl(peer);
    cloned.id = id;
    Eif (options.appointment?.scope) {
      cloned.scope = options.appointment.scope;
    }
    const provider = options.appointment?.scope?.provider as
      { displayName?: string; name?: string } | null | undefined;
    Eif (provider?.displayName || provider?.name) {
      cloned.name = provider.displayName || provider.name || cloned.name;
    }
    return cloned;
  }
 
  return officeFromAppointmentScope(id, options.appointment);
}
 
function officeFromAppointmentScope(
  id: string,
  appointment?: AppointmentDTO | null
): OfficeImpl | undefined {
  const provider = appointment?.scope?.provider as
    | {
        id?: string | number;
        displayName?: string;
        name?: string;
        contact?: {
          street?: string;
          streetNumber?: string;
          postalCode?: string;
          city?: string;
        };
      }
    | null
    | undefined;
  if (!provider || String(provider.id) !== id) {
    return undefined;
  }
  const contact = provider.contact ?? {};
  return new OfficeImpl(
    id,
    provider.displayName || provider.name || "",
    {
      street: contact.street ?? "",
      house_number: contact.streetNumber ?? "",
      postal_code: contact.postalCode ?? "",
      city: contact.city ?? "",
      hint: false,
    },
    false,
    [],
    "",
    undefined,
    0,
    undefined,
    undefined,
    appointment?.scope,
    undefined,
    undefined,
    1
  );
}