Assist_Design/apps/portal/src/hooks/useInvoices.ts
tema 1640fae457 Add new payment methods and health check endpoints in Auth and Invoices services
- Introduced `validateSignup` endpoint in AuthController for customer number validation during signup.
- Added `healthCheck` method in AuthService to verify service integrations and database connectivity.
- Implemented `getPaymentMethods`, `getPaymentGateways`, and `refreshPaymentMethods` endpoints in InvoicesController for managing user payment options.
- Enhanced InvoicesService with methods to invalidate payment methods cache and improved error handling.
- Updated currency handling across various services and components to reflect JPY as the default currency.
- Added new dependencies in package.json for ESLint configuration.
2025-08-30 15:10:24 +09:00

142 lines
4.2 KiB
TypeScript

import { useQuery } from "@tanstack/react-query";
import type {
Invoice,
InvoiceList,
InvoiceSsoLink,
Subscription,
PaymentMethodList,
PaymentGatewayList,
InvoicePaymentLink,
} from "@customer-portal/shared";
import { useAuthStore } from "@/lib/auth/store";
import { authenticatedApi } from "@/lib/api";
interface UseInvoicesOptions {
page?: number;
limit?: number;
status?: string;
}
export function useInvoices(options: UseInvoicesOptions = {}) {
const { page = 1, limit = 10, status } = options;
const { token, isAuthenticated } = useAuthStore();
return useQuery<InvoiceList>({
queryKey: ["invoices", page, limit, status],
queryFn: async () => {
if (!token) {
throw new Error("Authentication required");
}
const params = new URLSearchParams({
page: page.toString(),
limit: limit.toString(),
...(status && { status }),
});
return authenticatedApi.get<InvoiceList>(`/invoices?${params}`);
},
staleTime: 60 * 1000, // 1 minute
gcTime: 5 * 60 * 1000, // 5 minutes
enabled: isAuthenticated && !!token, // Only run when authenticated
});
}
export function useInvoice(invoiceId: number) {
const { token, isAuthenticated } = useAuthStore();
return useQuery<Invoice>({
queryKey: ["invoice", invoiceId],
queryFn: async () => {
if (!token) {
throw new Error("Authentication required");
}
return authenticatedApi.get<Invoice>(`/invoices/${invoiceId}`);
},
staleTime: 60 * 1000, // 1 minute
gcTime: 5 * 60 * 1000, // 5 minutes
enabled: isAuthenticated && !!token, // Only run when authenticated
});
}
export function useInvoiceSubscriptions(invoiceId: number) {
const { token, isAuthenticated } = useAuthStore();
return useQuery<Subscription[]>({
queryKey: ["invoice-subscriptions", invoiceId],
queryFn: async () => {
if (!token) {
throw new Error("Authentication required");
}
return authenticatedApi.get<Subscription[]>(`/invoices/${invoiceId}/subscriptions`);
},
staleTime: 60 * 1000, // 1 minute
gcTime: 5 * 60 * 1000, // 5 minutes
enabled: isAuthenticated && !!token && !!invoiceId, // Only run when authenticated and invoiceId is valid
});
}
export async function createInvoiceSsoLink(
invoiceId: number,
target: "view" | "download" | "pay" = "view"
): Promise<InvoiceSsoLink> {
const params = new URLSearchParams();
if (target !== "view") params.append("target", target);
return authenticatedApi.post<InvoiceSsoLink>(
`/invoices/${invoiceId}/sso-link${params.toString() ? `?${params.toString()}` : ""}`
);
}
export function usePaymentMethods() {
const { token, isAuthenticated } = useAuthStore();
return useQuery<PaymentMethodList>({
queryKey: ["paymentMethods"],
queryFn: async () => {
if (!token) {
throw new Error("Authentication required");
}
return authenticatedApi.get<PaymentMethodList>(`/invoices/payment-methods`);
},
staleTime: 1 * 60 * 1000, // Reduced to 1 minute for better refresh
gcTime: 5 * 60 * 1000, // Reduced to 5 minutes
enabled: isAuthenticated && !!token,
retry: 3, // Retry failed requests
retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000), // Exponential backoff
});
}
export function usePaymentGateways() {
const { token, isAuthenticated } = useAuthStore();
return useQuery<PaymentGatewayList>({
queryKey: ["paymentGateways"],
queryFn: async () => {
if (!token) {
throw new Error("Authentication required");
}
return authenticatedApi.get<PaymentGatewayList>(`/invoices/payment-gateways`);
},
staleTime: 60 * 60 * 1000, // 1 hour
gcTime: 2 * 60 * 60 * 1000, // 2 hours
enabled: isAuthenticated && !!token,
});
}
export async function createInvoicePaymentLink(
invoiceId: number,
paymentMethodId?: number,
gatewayName?: string
): Promise<InvoicePaymentLink> {
const params = new URLSearchParams();
if (paymentMethodId) params.append("paymentMethodId", paymentMethodId.toString());
if (gatewayName) params.append("gatewayName", gatewayName);
return authenticatedApi.post<InvoicePaymentLink>(
`/invoices/${invoiceId}/payment-link${params.toString() ? `?${params.toString()}` : ""}`
);
}