26 lines
816 B
TypeScript
26 lines
816 B
TypeScript
|
|
import { isSameLocalCalendarDay } from '@/lib/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;
|
||
|
|
}
|