Files
clinicpro/assets/admin/pages/MyFinancialPage.environment.test.tsx
hamedandClaude Opus 5 c9d4348c46 feat(tenant): mark the financial tables with their owning environment
Phase 6 of the tenant series. GlobalTables::DEFERRED is now empty and the
coverage test asserts it stays that way.

payments carries the (entity_type, entity_id) pair and belongs to the
receiving side, never the payer: an appointment payment takes the
appointment's environment, a subscription takes the environment its buyer
owns, and an SMS wallet top-up takes the wallet's. The patient never chose
an environment, so TenantFilter stays off for them and they still see their
own payment.

Three corrections to the analysis the phase was planned on, each backed by
the code or the data rather than the plan:

- A third payment type exists. Payment::TYPE_SMS_WALLET is created in
  SmsWalletController and already carries its environment in the metadata;
  without assigning it the write would fail at flush.
- clinic_subscriptions has no user_id, and its trial rows carry no payment,
  so it cannot drive the subscription backfill. The environment is derived
  the way handleSubscriptionActivation derives it — and that method now
  reads the pair off the payment instead of re-deriving it, so a payment and
  the subscription it buys can no longer land on different environments.
- WalletTransaction is not a child of Payment. payment_id is nullable and
  none of the four creation sites set it; the wallet is a person's, with a
  running balance per user. It and Settlement, which withdraws from that same
  wallet, are global with a recorded reason instead.

bank_accounts and pos_devices move from the registering user to the
environment. Their pair is deliberately nullable: nothing in the existing
data says which of a multi-environment owner's cards belongs where, and
guessing would point real money at the wrong account. Ambiguous rows stay
unassigned and the migration reports how many. The cost is that such a row
is invisible in every environment, so the owner reaches it through a
user-scoped lookup that runs outside the filter, and assigns it with
PATCH .../{uuid}/environment. The admin panel marks those rows and offers
the assignment.

Tests: 896 backend (+11), 570 frontend (+4). PHPStan unchanged at its 17
pre-existing errors.

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

82 lines
3.2 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { screen, fireEvent, waitFor } from '@testing-library/react';
import { renderWithProviders } from '../test/utils';
vi.mock('../lib/api', () => ({
api: { get: vi.fn(), post: vi.fn(), patch: vi.fn(), put: vi.fn(), delete: vi.fn() },
ApiError: class extends Error {},
}));
import { api } from '../lib/api';
import { useAuthStore } from '../stores/authStore';
import MyFinancialPage from './MyFinancialPage';
const get = api.get as ReturnType<typeof vi.fn>;
const patch = api.patch as ReturnType<typeof vi.fn>;
const assigned = {
uuid: 'bank-assigned',
bank_name: 'ملی',
card_number: '6037',
account_number: '111',
shaba_number: null,
is_active: true,
created_at: 0,
entity_type: 'clinic' as const,
};
const unassigned = { ...assigned, uuid: 'bank-orphan', bank_name: 'ملت', entity_type: null };
beforeEach(() => {
get.mockReset();
patch.mockReset();
useAuthStore.setState({
primaryRole: 'clinic',
context: { type: 'clinic', db_uuid: 'c1', name: 'کلینیک مرکزی', role: 'clinic' },
} as any);
get.mockImplementation((url: string) =>
Promise.resolve({ success: true, data: url.includes('bank-accounts') ? [assigned, unassigned] : [] }),
);
patch.mockResolvedValue({ success: true, data: { ...unassigned, entity_type: 'clinic' } });
});
/**
* کارت‌ها از فاز ۶ به محیط تعلق دارند نه به کاربر. کاربرِ چندمحیطی باید ببیند
* کارت‌های جلوی چشمش مالِ کدام محیط‌اند، وگرنه با تعویض محیط فکر می‌کند گمشان کرده.
*/
describe('MyFinancialPage — محیط روش‌های پرداخت', () => {
it('نام محیط فعال را در سرتیتر می‌آورد', async () => {
renderWithProviders(<MyFinancialPage />);
expect(await screen.findByText(/روش‌های پرداخت «کلینیک مرکزی»/)).toBeInTheDocument();
});
it('کارت محیط‌دار را با نام محیط و کارت بی‌محیط را با نشانهٔ «تعیین‌نشده» نشان می‌دهد', async () => {
renderWithProviders(<MyFinancialPage />);
expect(await screen.findByText('محیط تعیین‌نشده')).toBeInTheDocument();
// نام محیط هم در سرتیتر می‌آید هم در ستون محیطِ کارتِ محیط‌دار
expect(screen.getAllByText(/کلینیک مرکزی/).length).toBeGreaterThan(1);
});
it('کارت بی‌محیط به‌جای «ویرایش» دکمهٔ انتساب می‌گیرد و آن را به محیط فعال می‌چسباند', async () => {
renderWithProviders(<MyFinancialPage />);
const assign = await screen.findByRole('button', { name: /انتساب به کلینیک مرکزی/ });
fireEvent.click(assign);
await waitFor(() =>
expect(patch).toHaveBeenCalledWith(
'/api/v1/my/payment-methods/bank-accounts/bank-orphan/environment',
{},
),
);
});
it('کارت محیط‌دار همچنان دکمهٔ ویرایش دارد', async () => {
renderWithProviders(<MyFinancialPage />);
expect(await screen.findAllByRole('button', { name: /ویرایش/ })).toHaveLength(1);
});
});