Files
clinicpro/assets/admin/components/CancelAppointmentDialog.tsx
T
hamedandClaude Opus 5 4049daf071 feat: close the last four domain events, and the panel paths they describe
Every one of the fourteen named events now has an emit point. The four that
were missing all sat on paths owned by earlier tasks:

- AppointmentCompleted fires from both status-change routes, after the row is
  saved. A rejected transition or a version conflict leaves no event; otherwise
  the completed count runs ahead of the appointments themselves.
- AppointmentRescheduled is a third event, not a replacement. A rebook is a
  confirm plus a cancel, and a consumer that only hears the cancel messages a
  patient who still has an appointment.
- ResourceBlocked / ResourceReleased are a pair. Capacity coming back has to be
  as audible as capacity going away, or the resource reads as permanently taken.

Publishing is now on the scheduler rather than an unregistered command: the
logic moved out of PublishDomainEventsCommand into OutboxPublisher so the
recurring message and the manual command share it, and the existing
worker-scheduler container consumes it. The scheduler message carries no data
on purpose — what to publish is read from the table, so an event recorded
between two ticks is not skipped. DomainEventMessage routes to async, since a
slow consumer was otherwise slowing the drain itself and its failure marked a
row failed that had in fact been delivered.

Panel work that these paths made reachable:

- Cancelling from the appointment page now goes through the policy-aware
  endpoint and shows the penalty preview before the confirm, so the operator
  does not discover the patient's penalty after the fact. The cancellation
  service writes the timeline entry itself and accepts a reason, which that
  path previously dropped on the floor.
- Rescheduling reuses the booking page under ?rebook=<uuid> — the search and
  hold steps are identical and only the final step differs. The doctor picker
  is hidden there: a reschedule is not an invitation to change doctors.
- A new GET /appointment/{uuid}/segments exposes the recorded plan. An empty
  list is not an error, it means the appointment is slot-based, and that is
  exactly what gates the resource-mode reschedule button.

AppointmentInvoiceCard no longer crashes the whole detail page when an older
invoice has no discount breakdown.

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

124 lines
4.2 KiB
TypeScript

import React, { useState } from 'react';
import ConfirmDialog from './ui/ConfirmDialog';
import { useCancellationPreview, useCancelAppointment } from '../hooks/useCancellation';
import { formatRial } from '../lib/utils';
interface Props {
open: boolean;
appointmentUuid: string;
/** چه کسی لغو می‌کند — جریمه برای لغو کلینیک همیشه صفر است. */
by?: 'user' | 'doctor';
onClose: () => void;
onCancelled?: () => void;
}
/**
* لغو نوبت با نمایش پیامد مالی **قبل از** تأیید.
*
* پیش‌نمایش از همان محاسبه‌ای می‌آید که خودِ لغو انجام می‌دهد، پس عددی که اپراتور
* می‌بیند همان است که کسر می‌شود. دکمهٔ لغوِ بی‌پیش‌نمایش یعنی اپراتور جریمهٔ بیمار را
* بعد از وقوعش کشف می‌کند.
*/
export default function CancelAppointmentDialog({
open,
appointmentUuid,
by = 'doctor',
onClose,
onCancelled,
}: Props) {
const [reason, setReason] = useState('');
const { preview, loading } = useCancellationPreview(open ? appointmentUuid : undefined, by);
const cancel = useCancelAppointment();
const close = () => {
setReason('');
onClose();
};
return (
<ConfirmDialog
open={open}
title="لغو نوبت"
message="آیا از لغو این نوبت اطمینان دارید؟"
confirmLabel="لغو نوبت"
danger
loading={cancel.isPending}
onConfirm={() =>
cancel.mutate(
{ uuid: appointmentUuid, by, reason },
{
onSuccess: () => {
close();
onCancelled?.();
},
},
)
}
onCancel={close}
>
<div
style={{
marginTop: 14,
padding: 12,
borderRadius: 'var(--r-sm)',
background: 'var(--surface-2)',
fontSize: 13,
lineHeight: 1.9,
}}
>
{loading ? (
<span style={{ color: 'var(--text-3)' }}>در حال محاسبهٔ پیامد لغو…</span>
) : !preview ? (
<span style={{ color: 'var(--text-3)' }}>
پیامد مالی لغو در دسترس نیست؛ لغو انجام می‌شود ولی مبلغ را دستی بررسی کنید.
</span>
) : (
<>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<span style={{ color: 'var(--text-2)' }}>جریمهٔ لغو</span>
<strong style={{ color: preview.penalty_rials > 0 ? 'var(--danger)' : 'var(--success)' }}>
{preview.penalty_rials > 0 ? formatRial(preview.penalty_rials) : 'بدون جریمه'}
</strong>
</div>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<span style={{ color: 'var(--text-2)' }}>مبلغ پرداخت‌شده</span>
<span>{formatRial(preview.paid_rials)}</span>
</div>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<span style={{ color: 'var(--text-2)' }}>بازگشت اعتبار پکیج</span>
<span>{preview.credit_refundable ? 'بله' : 'خیر'}</span>
</div>
{preview.within_free_window && (
<div style={{ color: 'var(--success)' }}>در بازهٔ لغو رایگان است.</div>
)}
{preview.notes.map((note, i) => (
<div key={i} style={{ color: 'var(--text-3)', fontSize: 12 }}>
{note}
</div>
))}
</>
)}
</div>
<div style={{ marginTop: 14 }}>
<label className="cp-label mb-2" htmlFor="cancel-reason">
دلیل لغو (اختیاری)
</label>
<textarea
id="cancel-reason"
className="cp-input"
rows={2}
value={reason}
onChange={(e) => setReason(e.target.value)}
placeholder="دلیل لغو نوبت را وارد کنید..."
style={{ width: '100%', resize: 'vertical' }}
/>
</div>
</ConfirmDialog>
);
}