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>
140 lines
5.3 KiB
TypeScript
140 lines
5.3 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { api } from '../lib/api';
|
|
import type { ApiResponse } from '../lib/api';
|
|
|
|
/**
|
|
* روشهای پرداختِ **محیط فعال** (حساب بانکی + کارتخوان) برای صفحهٔ «مدیریت پرداخت».
|
|
* منبع: /api/v1/my/payment-methods/... — این رکوردها بعداً از فاکتور مراجعهکننده
|
|
* برای ثبت روش پرداختِ یک سرویس ارجاع داده میشوند.
|
|
*
|
|
* از فاز ۶، کارتها به محیط تعلق دارند نه به کاربر: با تعویض محیط، فهرست عوض
|
|
* میشود. کارتهای بازماندهای که هنوز محیطی ندارند با entity_type = null در همین
|
|
* فهرست میآیند و باید پیش از استفاده به محیطی منتسب شوند.
|
|
*/
|
|
|
|
/** null یعنی «محیطش هنوز تعیین نشده». */
|
|
export type PaymentMethodEnvironment = 'doctor' | 'clinic' | null;
|
|
|
|
export interface BankAccount {
|
|
uuid: string;
|
|
bank_name: string;
|
|
card_number: string | null;
|
|
account_number: string;
|
|
shaba_number: string | null;
|
|
is_active: boolean;
|
|
created_at: number;
|
|
entity_type: PaymentMethodEnvironment;
|
|
}
|
|
|
|
export interface Pos {
|
|
uuid: string;
|
|
bank_name: string;
|
|
serial_number: string | null;
|
|
terminal_number: string;
|
|
account_number: string | null;
|
|
is_active: boolean;
|
|
created_at: number;
|
|
entity_type: PaymentMethodEnvironment;
|
|
}
|
|
|
|
export interface BankAccountInput {
|
|
bank_name: string;
|
|
account_number: string;
|
|
card_number?: string;
|
|
shaba_number?: string;
|
|
}
|
|
|
|
export interface PosInput {
|
|
bank_name: string;
|
|
terminal_number: string;
|
|
serial_number?: string;
|
|
account_number?: string;
|
|
}
|
|
|
|
const BANK_KEY = ['payment-methods', 'bank-accounts'] as const;
|
|
const POS_KEY = ['payment-methods', 'pos'] as const;
|
|
|
|
// ── Bank accounts ────────────────────────────────────────────────────────────
|
|
|
|
export function useBankAccounts() {
|
|
return useQuery<ApiResponse<BankAccount[]>>({
|
|
queryKey: BANK_KEY,
|
|
queryFn: () => api.get('/api/v1/my/payment-methods/bank-accounts'),
|
|
});
|
|
}
|
|
|
|
export function useCreateBankAccount() {
|
|
const qc = useQueryClient();
|
|
return useMutation<ApiResponse<BankAccount>, unknown, BankAccountInput>({
|
|
mutationFn: (body) => api.post('/api/v1/my/payment-methods/bank-accounts', body),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: BANK_KEY }),
|
|
});
|
|
}
|
|
|
|
export function useUpdateBankAccount() {
|
|
const qc = useQueryClient();
|
|
return useMutation<ApiResponse<BankAccount>, unknown, { uuid: string; body: BankAccountInput }>({
|
|
mutationFn: ({ uuid, body }) => api.put(`/api/v1/my/payment-methods/bank-accounts/${uuid}`, body),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: BANK_KEY }),
|
|
});
|
|
}
|
|
|
|
export function useToggleBankAccountStatus() {
|
|
const qc = useQueryClient();
|
|
return useMutation<ApiResponse<BankAccount>, unknown, string>({
|
|
mutationFn: (uuid) => api.patch(`/api/v1/my/payment-methods/bank-accounts/${uuid}/status`, {}),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: BANK_KEY }),
|
|
});
|
|
}
|
|
|
|
/** کارتِ بیمحیط را به محیط فعال میچسباند؛ پیش از آن قابل ویرایش نیست. */
|
|
export function useAssignBankAccountEnvironment() {
|
|
const qc = useQueryClient();
|
|
return useMutation<ApiResponse<BankAccount>, unknown, string>({
|
|
mutationFn: (uuid) => api.patch(`/api/v1/my/payment-methods/bank-accounts/${uuid}/environment`, {}),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: BANK_KEY }),
|
|
});
|
|
}
|
|
|
|
// ── POS devices ──────────────────────────────────────────────────────────────
|
|
|
|
export function usePosDevices() {
|
|
return useQuery<ApiResponse<Pos[]>>({
|
|
queryKey: POS_KEY,
|
|
queryFn: () => api.get('/api/v1/my/payment-methods/pos'),
|
|
});
|
|
}
|
|
|
|
export function useCreatePos() {
|
|
const qc = useQueryClient();
|
|
return useMutation<ApiResponse<Pos>, unknown, PosInput>({
|
|
mutationFn: (body) => api.post('/api/v1/my/payment-methods/pos', body),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: POS_KEY }),
|
|
});
|
|
}
|
|
|
|
export function useUpdatePos() {
|
|
const qc = useQueryClient();
|
|
return useMutation<ApiResponse<Pos>, unknown, { uuid: string; body: PosInput }>({
|
|
mutationFn: ({ uuid, body }) => api.put(`/api/v1/my/payment-methods/pos/${uuid}`, body),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: POS_KEY }),
|
|
});
|
|
}
|
|
|
|
export function useTogglePosStatus() {
|
|
const qc = useQueryClient();
|
|
return useMutation<ApiResponse<Pos>, unknown, string>({
|
|
mutationFn: (uuid) => api.patch(`/api/v1/my/payment-methods/pos/${uuid}/status`, {}),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: POS_KEY }),
|
|
});
|
|
}
|
|
|
|
/** قرینهٔ useAssignBankAccountEnvironment برای کارتخوان. */
|
|
export function useAssignPosEnvironment() {
|
|
const qc = useQueryClient();
|
|
return useMutation<ApiResponse<Pos>, unknown, string>({
|
|
mutationFn: (uuid) => api.patch(`/api/v1/my/payment-methods/pos/${uuid}/environment`, {}),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: POS_KEY }),
|
|
});
|
|
}
|