The engine from tasks 06 and 07 could find slots and hold them, but nothing in the panel could actually book one. - Search, hold, confirm stay three separate steps because they are three separate states: between seeing a slot and taking it the seat is still open, and between taking and confirming there is a deadline - HoldCountdown reads the server's expires_at rather than starting its own timer at render: browser clock skew and network latency both cost seconds, and those seconds are exactly where a hold is lost. It turns urgent under a minute and tells the parent the moment it lapses - Per-role resource swap offers only the resources the engine returned for that same slot. Listing every resource in the branch would let an operator pick one that was never free and collect a 409 - An empty result is not an error: the reason code renders as a sentence saying what to change - Confirm requires a doctor and stays disabled until one is chosen — the endpoint rejects it anyway, and finding that out after the hold clock has been running is the wrong time Reached from the appointments page as a separate action rather than folded into the existing form: its search comes from the intersection of resource calendars, not from one doctor's slots, and merging the two would confuse both. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
114 lines
3.9 KiB
TypeScript
114 lines
3.9 KiB
TypeScript
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
import { toast } from 'sonner';
|
|
import { api, ApiError, type ApiResponse } from '../lib/api';
|
|
|
|
/**
|
|
* جستجوی وقت، رزرو موقت و ثبت نهایی در حالت منبعمحور.
|
|
*
|
|
* سه عمل جدا هستند و باید جدا بمانند: بین «دیدن وقت» و «گرفتنش» صندلی هنوز آزاد است،
|
|
* و بین «گرفتن» و «ثبت» یک مهلت وجود دارد که اگر نگذرد، ظرفیت برای همیشه قفل میماند.
|
|
*/
|
|
export interface ResourceRef {
|
|
uuid: string;
|
|
name: string;
|
|
}
|
|
|
|
export type SlotAssignment = Record<string, ResourceRef[]>;
|
|
|
|
export interface AvailableSlot {
|
|
start: number;
|
|
end: number;
|
|
assignment: SlotAssignment;
|
|
}
|
|
|
|
export interface PlanSegment {
|
|
sequence: number;
|
|
name: string;
|
|
offset_minutes: number;
|
|
duration_minutes: number;
|
|
patient_present: boolean;
|
|
requirements: { role: string; role_name: string; count: number }[];
|
|
}
|
|
|
|
export interface AvailabilityResult {
|
|
plan: { total_minutes: number; segments: PlanSegment[] };
|
|
slots: AvailableSlot[];
|
|
/** خالی بودن فهرست خطا نیست؛ این میگوید چرا خالی است. */
|
|
reason: string | null;
|
|
}
|
|
|
|
export interface HoldResult {
|
|
hold_uuid: string;
|
|
starts_at: number;
|
|
ends_at: number;
|
|
expires_at: number;
|
|
confirmed: boolean;
|
|
assignment: SlotAssignment;
|
|
}
|
|
|
|
export const REASON_LABELS: Record<string, string> = {
|
|
no_capacity_in_range: 'در این بازه هیچ ظرفیتی نیست — بازه را بزرگتر کنید یا شعبهٔ دیگری را امتحان کنید.',
|
|
no_working_hours: 'شعبه در این بازه ساعت کاری ندارد.',
|
|
no_eligible_resource: 'هیچ منبعی شرایط بخشهای این خدمت را ندارد.',
|
|
};
|
|
|
|
function fail(e: unknown, fallback: string) {
|
|
toast.error(e instanceof ApiError ? e.message : fallback);
|
|
}
|
|
|
|
export function useAvailabilitySearch(
|
|
params: { serviceUuid: string; branchUuid: string; from: number; to: number; stepMinutes?: number },
|
|
enabled: boolean,
|
|
) {
|
|
const query = useQuery({
|
|
queryKey: ['resource-availability', params],
|
|
queryFn: () =>
|
|
api.post<ApiResponse<AvailabilityResult>>('/api/v1/appointment-availability', {
|
|
service_uuid: params.serviceUuid,
|
|
branch_uuid: params.branchUuid,
|
|
from: params.from,
|
|
to: params.to,
|
|
...(params.stepMinutes ? { step_minutes: params.stepMinutes } : {}),
|
|
}),
|
|
enabled: enabled && !!params.serviceUuid && !!params.branchUuid,
|
|
retry: false,
|
|
});
|
|
|
|
return {
|
|
result: query.data?.data,
|
|
loading: query.isFetching,
|
|
error: query.error,
|
|
refetch: query.refetch,
|
|
};
|
|
}
|
|
|
|
export function useHold() {
|
|
const create = useMutation({
|
|
mutationFn: (body: {
|
|
service_uuid: string;
|
|
branch_uuid: string;
|
|
start: number;
|
|
assignment: Record<string, string[]>;
|
|
item_uuids?: string[];
|
|
patient_gender?: string;
|
|
}) => api.post<ApiResponse<HoldResult>>('/api/v1/appointment-hold', body),
|
|
// ۴۰۹ یعنی همین لحظه کس دیگری گرفت — پیام سرور دقیقاً همین را میگوید.
|
|
onError: (e) => fail(e, 'گرفتن این زمان ناموفق بود'),
|
|
});
|
|
|
|
const release = useMutation({
|
|
mutationFn: (uuid: string) => api.delete<ApiResponse<null>>(`/api/v1/appointment-hold/${uuid}`),
|
|
onSuccess: () => toast.success('رزرو موقت آزاد شد'),
|
|
onError: (e) => fail(e, 'آزادسازی ناموفق بود'),
|
|
});
|
|
|
|
const confirm = useMutation({
|
|
mutationFn: (body: { hold_uuid: string; doctor_uuid: string; patient_uuid?: string }) =>
|
|
api.post<ApiResponse<{ uuid: string }>>('/api/v1/appointment-confirm', body),
|
|
onSuccess: () => toast.success('نوبت ثبت شد'),
|
|
onError: (e) => fail(e, 'ثبت نهایی ناموفق بود'),
|
|
});
|
|
|
|
return { create, release, confirm };
|
|
}
|