- Implemented a new API endpoint `/api/v1/my/billing/payments/summary` to provide a financial summary of payments with filters for national code, status, and date range. - Updated the InvoiceRepository to aggregate totals for paid and unsettled invoices. - Created a new hook `usePaymentsSummary` to fetch summary data in the frontend. - Redesigned the MyPaymentsPage to align with the ClaimsPage structure, incorporating a design system, summary statistics, and improved filtering options. - Added tests for the new payments summary endpoint to ensure correct functionality and filtering behavior.
93 lines
3.3 KiB
TypeScript
93 lines
3.3 KiB
TypeScript
import { useQuery } from '@tanstack/react-query';
|
|
import { api } from '../lib/api';
|
|
import type { ApiResponse, PaginatedResponse } from '../lib/api';
|
|
|
|
/** Two-state invoice status shown on both the list (node 1) and detail (node 2). */
|
|
export type PaymentRowStatus = 'paid' | 'unsettled';
|
|
|
|
/** One recorded invoice on the flat payments list (node 1). */
|
|
export interface PaymentRow {
|
|
invoice_uuid: string;
|
|
patient_uuid: string;
|
|
patient_name: string | null;
|
|
national_code: string | null;
|
|
issued_at: number;
|
|
amount_rials: number;
|
|
status: PaymentRowStatus;
|
|
}
|
|
|
|
export interface PaymentFilters {
|
|
page: number;
|
|
national_code?: string;
|
|
status?: string; // paid | unsettled
|
|
from?: number; // unix seconds
|
|
to?: number; // unix seconds
|
|
}
|
|
|
|
/** A single invoice status on the detail table (node 2) — two-state. */
|
|
export type InvoiceRowStatus = 'paid' | 'unsettled';
|
|
|
|
export interface PatientInvoiceRow {
|
|
uuid: string;
|
|
number: number;
|
|
issued_at: number;
|
|
total_rials: number;
|
|
status: InvoiceRowStatus;
|
|
service_title: string | null;
|
|
items: Array<{ uuid: string; title: string; quantity: number; total_rials: number; patient_rials: number }>;
|
|
}
|
|
|
|
export interface PatientInvoicesPayload {
|
|
patient: { uuid: string; name: string | null; national_code: string | null };
|
|
data: PatientInvoiceRow[];
|
|
meta: { totalRecords: number; totalPages: number; currentPage: number };
|
|
}
|
|
|
|
const LIMIT = 20;
|
|
|
|
/** Node 1 — flat, paginated list of the current tenant's recorded invoices. */
|
|
export function usePayments(filters: PaymentFilters) {
|
|
const qs = new URLSearchParams({ page: String(filters.page), limit: String(LIMIT) });
|
|
if (filters.national_code) qs.set('national_code', filters.national_code);
|
|
if (filters.status) qs.set('status', filters.status);
|
|
if (filters.from) qs.set('from', String(filters.from));
|
|
if (filters.to) qs.set('to', String(filters.to));
|
|
|
|
return useQuery<PaginatedResponse<PaymentRow>>({
|
|
queryKey: ['payments', filters],
|
|
queryFn: () => api.get(`/api/v1/my/billing/payments?${qs.toString()}`),
|
|
});
|
|
}
|
|
|
|
export interface PaymentsSummary {
|
|
total_rials: number;
|
|
paid_rials: number;
|
|
unsettled_rials: number;
|
|
invoices_count: number;
|
|
}
|
|
|
|
/** Summary over the same filters as the list, so the stat cards match the table. */
|
|
export function usePaymentsSummary(filters: Omit<PaymentFilters, 'page'>) {
|
|
const qs = new URLSearchParams();
|
|
if (filters.national_code) qs.set('national_code', filters.national_code);
|
|
if (filters.status) qs.set('status', filters.status);
|
|
if (filters.from) qs.set('from', String(filters.from));
|
|
if (filters.to) qs.set('to', String(filters.to));
|
|
|
|
return useQuery<ApiResponse<PaymentsSummary>>({
|
|
queryKey: ['payments-summary', filters],
|
|
queryFn: () => api.get(`/api/v1/my/billing/payments/summary?${qs.toString()}`),
|
|
});
|
|
}
|
|
|
|
/** Node 2 — a single patient's recorded invoices (header + paginated list). */
|
|
export function usePatientInvoices(patientUuid: string | undefined, page: number) {
|
|
return useQuery<ApiResponse<PatientInvoicesPayload>>({
|
|
queryKey: ['patient-invoices', patientUuid, page],
|
|
queryFn: () => api.get(`/api/v1/my/billing/patients/${patientUuid}/invoices?page=${page}&limit=${LIMIT}`),
|
|
enabled: !!patientUuid,
|
|
});
|
|
}
|
|
|
|
export const MY_PAYMENTS_LIMIT = LIMIT;
|