Files
dyolink/frontend/src/components/shared/treatmentSelection.ts

26 lines
836 B
TypeScript

import { isSameLocalCalendarDay } from '@/components/appointments/appointmentTime';
import type { TreatmentAppointment } from '@/types/treatment';
/**
* For the selected calendar day: if it is today, pick the appointment whose time range contains now;
* otherwise pick the first appointment of that day. Returns null when there are no appointments.
*/
export function pickAutoAppointment(
appointments: TreatmentAppointment[],
selectedDay: Date,
): string | null {
if (appointments.length === 0) return null;
const now = new Date();
if (isSameLocalCalendarDay(selectedDay, now)) {
const t = now.getTime();
for (const a of appointments) {
const s = new Date(a.startAt).getTime();
const e = new Date(a.endAt).getTime();
if (t >= s && t <= e) return a.id;
}
}
return appointments[0].id;
}