Files
clinicpro/tests/Payment/AppointmentPaidConfirmFilesSessionTest.php
T
hamedandClaude Opus 4.8 e6422014d1 fix(appointments): file the case file on every confirmation path
Confirming an appointment was supposed to create the patient's record and its
session, and PatientService already knew how. Only two of the five paths that
confirm an appointment ever called it, and the one that mattered most did not:
a booking paid for online was confirmed inside the payment callback, which
never ran the side-effects. Every Nobat724 booking therefore went unfiled — 7
confirmed appointments in dev had no session at all.

The side-effects now run through AppointmentConfirmationService, which every
path calls: the payment callback, both PATCH endpoints, and panel/admin
bookings. Creating the record can no longer roll back a confirmation or a
payment; a failure is logged and can be repaired with the new
app:appointment:backfill-sessions command.

Two related defects fixed along the way:

- A doctor working at a clinic got two records for one appointment, one under
  the doctor and one under the clinic, so a single visit's revenue was counted
  twice. The booking context now decides, and it decides once.
- That context was inferred from address_id, falling back to "the doctor's only
  clinic" — a guess that files an appointment under the wrong practice now that
  schedules are per-context. It is stored as appointments.clinic_id instead.

Panel and admin bookings were left pending forever: nothing confirmed them and
no payment was expected. They are created confirmed.

Repeat confirmations no longer duplicate the session; an archived one still
counts as filed, so archiving a mistaken visit does not resurrect it.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-18 16:55:39 +03:30

107 lines
3.8 KiB
PHP

<?php
namespace App\Tests\Payment;
use App\Appointment\Entity\Appointment;
use App\Clinic\Entity\Clinic;
use App\Config\Entity\SiteConfig;
use App\Doctor\Entity\Doctor;
use App\Patient\Entity\PatientRecord;
use App\Patient\Entity\PatientSession;
use App\Payment\Entity\Payment;
use App\Tests\ApiTestCase;
/**
* Paying for an online booking must file the case file, same as any other way
* of confirming.
*
* This is the path every Nobat724 booking takes, and it was the one path that
* never created a record: the payment callback confirmed the appointment
* without running the confirmation side-effects.
*/
class AppointmentPaidConfirmFilesSessionTest extends ApiTestCase
{
private function enableTestMode(): void
{
$cfg = $this->em->getRepository(SiteConfig::class)->findOneBy(['configKey' => 'payment_test_mode']);
if ($cfg === null) {
$this->em->persist(new SiteConfig('payment_test_mode', '1'));
} else {
$cfg->setValue('1');
}
$this->em->flush();
}
/** @return array{0: Payment, 1: Appointment} */
private function pendingPaidBooking(?callable $withClinic = null): array
{
$doctor = new Doctor($this->createUser(['ROLE_DOCTOR']), 'دکتر پرداخت');
$this->em->persist($doctor);
$this->em->flush();
$appointment = new Appointment($doctor, $this->createUser(['ROLE_USER']), 1_790_200_000, 1_790_201_800);
if ($withClinic !== null) {
$appointment->setClinic($withClinic($doctor));
}
$this->em->persist($appointment);
$payment = new Payment($appointment->getUser(), 50_000, 'mock', Payment::TYPE_APPOINTMENT);
$payment->setAppointment($appointment);
$this->em->persist($payment);
$this->em->flush();
return [$payment, $appointment];
}
private function fireCallback(Payment $payment): void
{
$this->client->request('POST', '/api/v1/payment/callback/mock?' . http_build_query([
'order_id' => $payment->getOrderId(),
'mock' => '1',
'ResCode' => '0',
'mock_amount' => '50000',
]));
}
public function testPaidPersonalBookingIsConfirmedAndFiled(): void
{
$this->enableTestMode();
[$payment, $appointment] = $this->pendingPaidBooking();
$this->fireCallback($payment);
$this->em->clear();
$reloaded = $this->em->getRepository(Appointment::class)->find($appointment->getId());
self::assertSame(Appointment::STATUS_CONFIRMED, $reloaded->getStatus());
$sessions = $this->em->getRepository(PatientSession::class)->findBy(['appointment' => $reloaded]);
self::assertCount(1, $sessions, 'پرداخت آنلاین هم باید پرونده بسازد');
$record = $sessions[0]->getRecord();
self::assertSame('doctor', $record->getEntityType());
}
public function testPaidClinicBookingIsFiledUnderTheClinic(): void
{
$this->enableTestMode();
[$payment, $appointment] = $this->pendingPaidBooking(function (Doctor $doctor): Clinic {
$clinic = new Clinic($this->createUser(['ROLE_CLINIC']));
$clinic->setName('کلینیک پرداخت');
$clinic->getDoctors()->add($doctor);
$this->em->persist($clinic);
$this->em->flush();
return $clinic;
});
$this->fireCallback($payment);
$this->em->clear();
$reloaded = $this->em->getRepository(Appointment::class)->find($appointment->getId());
$records = $this->em->getRepository(PatientRecord::class)->findBy(['user' => $reloaded->getUser()]);
self::assertCount(1, $records, 'یک نوبت، یک پرونده — نه یکی برای پزشک و یکی برای کلینیک');
self::assertSame('clinic', $records[0]->getEntityType());
}
}