All files / src/utils useReservationTimer.ts

69.23% Statements 18/26
50% Branches 6/12
62.5% Functions 5/8
75% Lines 18/24

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                        73x   73x   73x 73x 73x 73x     73x 73x         73x     73x   73x 73x     73x 73x       73x       73x         73x         73x                    
import type { SelectedAppointmentProvider } from "@/types/ProvideInjectTypes";
import type { Ref } from "vue";
 
import { computed, inject } from "vue";
 
import {
  isInTimeoutWarningWindow,
  remainingMsUntil,
  useNowTicker,
} from "@/utils/useTimeoutWarning";
 
export function useReservationTimer() {
  const { appointment } = inject<SelectedAppointmentProvider>("appointment")!;
 
  const reservationStartMs = inject<Ref<number | null>>("reservationStartMs")!;
 
  const reservationDurationMinutes = computed<number | undefined>(() => {
    const raw: unknown = (appointment.value as any)?.scope?.reservationDuration;
    const n = raw as number | undefined;
    return Number.isFinite(n) ? n : undefined;
  });
 
  const deadlineMs = computed<number | null>(() => {
    Iif (
      reservationStartMs.value == null ||
      reservationDurationMinutes.value == null
    )
      return null;
    return reservationStartMs.value + reservationDurationMinutes.value * 60_000;
  });
 
  const nowMs = useNowTicker();
 
  const remainingMs = computed<number | null>(() =>
    remainingMsUntil(deadlineMs.value, nowMs.value)
  );
 
  const isReservationExpired = computed<boolean>(
    () => remainingMs.value !== null && remainingMs.value <= 0
  );
 
  /** ZMSKVR-501: last minute of the reservation on Kontakt / Übersicht. */
  const showReservationTimeoutWarning = computed(() =>
    isInTimeoutWarningWindow(remainingMs.value)
  );
 
  const remainingSeconds = computed(() => {
    if (remainingMs.value == null) return 0;
    return Math.max(0, Math.ceil(remainingMs.value / 1000));
  });
 
  const timeLeftString = computed<string>(() => {
    if (remainingMs.value == null) return "";
    return `${remainingSeconds.value} Sekunden`;
  });
 
  return {
    isReservationExpired,
    remainingMs,
    remainingSeconds,
    deadlineMs,
    nowMs,
    timeLeftString,
    showReservationTimeoutWarning,
  };
}