Files
clinicpro/assets/admin/pages/WaitlistPage.tsx
T
hamedandClaude Opus 5 fba1555f22 feat(cancellation): cancellation policy, no-show tracking and a waitlist
Cancelling worked but had no policy behind it: no window, no penalty, nothing
happened to the deposit, and the no_show status had no effect at all.

Two rules that are expensive to get wrong, and both are load-bearing:
- The clinic cancelling its own appointment is never charged. That check is the
  first line of the calculation, not somewhere in the middle, so a later
  refactor cannot reorder it into charging patients for the clinic's decision.
- A penalty never exceeds what was actually paid. Anything above that is a
  debt, and debt belongs to billing, not to cancellation. An unpaid appointment
  is charged nothing and the response says why.

The default is no penalty at all — a penalising default would have made every
patient with a near appointment liable the moment this deployed.

No-shows are rows, not a counter on the patient: a counter loses which
appointment and when, which makes the 12-month window impossible. Crossing the
threshold adds an existing TenantTag; it never blocks the patient, because
blocking is an eligibility policy (task 09) written on top of that same tag.

Waitlist notifies up to ten matching people and the first to book wins. An
exclusive queue reads fairer but means a freed slot sits locked for half an
hour while someone ignores their phone — so the SMS says so explicitly instead.

Insufficient wallet balance does not fail the cancellation: the slot is freed
either way. A slot should not be held hostage to money.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-31 12:06:48 +03:30

132 lines
4.6 KiB
TypeScript

import React, { useMemo } from 'react';
import { TrashIcon } from '@heroicons/react/24/outline';
import PageHeader from '../components/ui/PageHeader';
import DataTable, { type Column } from '../components/ui/DataTable';
import SearchableSelect from '../components/ui/SearchableSelect';
import { formatDate } from '../lib/utils';
import { useUrlState } from '../hooks/useUrlState';
import { usePermissions } from '../hooks/usePermissions';
import { useWaitlist } from '../hooks/useCancellation';
import type { WaitlistEntry } from '../types';
const STATUS: Record<WaitlistEntry['status'], { label: string; className: string }> = {
waiting: { label: 'در انتظار', className: 'badge' },
notified: { label: 'خبر داده شد', className: 'badge amber' },
converted: { label: 'رزرو شد', className: 'badge green' },
expired: { label: 'منقضی', className: 'badge red' },
};
/**
* لیست انتظار.
*
* ستون «تعداد اطلاع» عمداً دیده می‌شود: وقتی ظرفیتی آزاد می‌شود همه خبر می‌گیرند و
* اولین رزروکننده می‌برد، پس اپراتور باید بداند چه کسی چند بار خبر شده.
*/
export default function WaitlistPage() {
const [urlState, setUrlState] = useUrlState({ search: '', status: '' });
const { entries, loading, remove } = useWaitlist(urlState.status || undefined);
const { can } = usePermissions();
const canManage = can('appointment_settings', 'update');
const rows = useMemo(() => {
const q = urlState.search.trim();
return entries.filter((e) => q === '' || e.service_name.includes(q));
}, [entries, urlState.search]);
const columns: Column<WaitlistEntry>[] = [
{
key: 'service_name',
header: 'خدمت',
render: (e) => <span style={{ fontWeight: 600 }}>{e.service_name}</span>,
},
{
key: 'window',
header: 'بازهٔ دلخواه',
render: (e) => (
<span style={{ fontSize: 13 }}>
{formatDate(e.desired_from)} تا {formatDate(e.desired_to)}
</span>
),
},
{
key: 'preferred_day_parts',
header: 'زمان ترجیحی',
render: (e) => (
<span style={{ fontSize: 12, color: 'var(--text-2)' }}>
{e.preferred_day_parts.length === 0 ? 'بی‌تفاوت' : e.preferred_day_parts.join('، ')}
</span>
),
},
{
key: 'notify_count',
header: 'تعداد اطلاع',
render: (e) => (
<span style={{ fontSize: 13 }}>
{e.notify_count}
{e.notified_at !== null && (
<span style={{ fontSize: 11, color: 'var(--text-3)' }}> · {formatDate(e.notified_at)}</span>
)}
</span>
),
},
{
key: 'status',
header: 'وضعیت',
render: (e) => (
<span className={STATUS[e.status].className}>
<span className="bdot" />
{STATUS[e.status].label}
</span>
),
},
];
return (
<div className="fade-in">
<PageHeader
title="لیست انتظار"
description="وقتی ظرفیتی آزاد می‌شود همهٔ منتظرانِ آن بازه خبر می‌گیرند و اولین رزروکننده آن را می‌برد."
backTo="/admin/settings-menu"
/>
<div style={{ overflowX: 'auto' }}>
<DataTable
columns={columns}
data={rows}
loading={loading}
searchValue={urlState.search}
onSearchChange={(v) => setUrlState({ search: v })}
searchPlaceholder="جستجو در خدمات..."
emptyMessage="کسی در لیست انتظار نیست"
headerExtra={
<div style={{ minWidth: 180, marginRight: 'auto' }}>
<SearchableSelect
value={urlState.status}
onChange={(v) => setUrlState({ status: String(v ?? '') })}
options={[
{ value: '', label: 'همهٔ وضعیت‌ها' },
...Object.entries(STATUS).map(([value, meta]) => ({ value, label: meta.label })),
]}
placeholder="وضعیت"
/>
</div>
}
actions={(e) =>
canManage ? (
<button
type="button"
className="btn secondary sm"
disabled={remove.isPending}
onClick={() => remove.mutate(e.uuid)}
aria-label="حذف از لیست انتظار"
>
<TrashIcon style={{ width: 15 }} />
</button>
) : null
}
/>
</div>
</div>
);
}