fix(tenant): scope the patient wallet ledger to the environment reading it

ownsRecord guards the patient record, not the rows underneath it, so
GET /api/v1/patient/{uuid}/wallet/transactions — and the recent_transactions
in the balance summary — returned the patient's entire history. Clinic A
could read what the patient paid at clinic B, down to the name of the staff
member who entered it.

The wallet stays the person's: the balance is still the sum of that user's
credits minus debits across every environment. Scoping it would show a
patient part of their own money and would make the running balance_after
meaningless. So this is attribution per row, not ownership per wallet.

The columns are deliberately named recorded_entity_type / recorded_entity_id
rather than entity_type / entity_id. TenantFilter keys on the latter and
would then scope the balance query too — the exact bug this avoids. The
naming is load-bearing, and both the entity and the architecture doc say so.

Rows that cannot be attributed — entered before this split, or outside any
environment such as a representation's commission — stay NULL and remain
visible everywhere; hiding them would make an existing patient's history
look deleted. The migration reports how many there are (0 in dev, all
attributable from payments and session references).

Consequence, documented in both docs/api/patient.md and the wallet tab: the
listed rows no longer sum to the displayed balance.

Removing the fix turns 3 of the 6 new tests red.

Tests: 902 backend (+6), 570 frontend. PHPStan unchanged at 17.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-28 15:21:55 +03:30
co-authored by Claude Opus 5
parent c9d4348c46
commit 6ab1eb6483
10 changed files with 410 additions and 10 deletions
+14 -3
View File
@@ -105,11 +105,13 @@ class PatientController extends BaseController
$patient = $record->getUser();
// موجودی سراسری است (پول مالِ بیمار است، نه محیط) ولی سطرهای دفتر به همین
// محیط محدودند؛ وگرنه اینجا هم پیدا بود بیمار در محیط دیگر چه پرداخت کرده.
return $this->success([
'balance_rials' => $this->settlementRepo->getWalletBalance($patient),
'recent_transactions' => array_map(
fn(\App\Settlement\Entity\WalletTransaction $t) => $t->toArray(),
$this->walletRepo->findByUser($patient, 10)
$this->walletRepo->findByUserForEnvironment($patient, $entityType, (int) $entityId, 10)
),
]);
}
@@ -128,12 +130,19 @@ class PatientController extends BaseController
$limit = min(100, max(1, (int) $request->query->get('limit', 50)));
$patient = $record->getUser();
// دفتر به محیط جاری محدود می‌شود: موجودی مالِ شخص است ولی تاریخچهٔ او در
// کلینیک دیگر به این محیط ربطی ندارد.
$txns = array_map(
fn(\App\Settlement\Entity\WalletTransaction $t) => $t->toArray(),
$this->walletRepo->findByUser($patient, $limit, ($page - 1) * $limit)
$this->walletRepo->findByUserForEnvironment($patient, $entityType, (int) $entityId, $limit, ($page - 1) * $limit)
);
return $this->paginated($txns, $this->walletRepo->countByUser($patient), $page, $limit);
return $this->paginated(
$txns,
$this->walletRepo->countByUserForEnvironment($patient, $entityType, (int) $entityId),
$page,
$limit,
);
}
/**
@@ -164,6 +173,7 @@ class PatientController extends BaseController
trim((string) ($data['description'] ?? '')) ?: null,
$this->normalizeMethod($data['payment_method'] ?? null),
trim((string) ($data['reference'] ?? '')) ?: null,
$entityType, (int) $entityId,
);
return $this->success([
@@ -202,6 +212,7 @@ class PatientController extends BaseController
trim((string) ($data['description'] ?? '')) ?: null,
$this->normalizeMethod($data['payment_method'] ?? null),
trim((string) ($data['reference'] ?? '')) ?: null,
$entityType, (int) $entityId,
);
return $this->success([