32 lines
983 B
TypeScript
32 lines
983 B
TypeScript
import { authFetch } from "../../../utils/authFetch";
|
|
|
|
interface VatRate {
|
|
countryCode: string;
|
|
standardRate: number;
|
|
}
|
|
|
|
const normalizeVatRate = (rate: number | null | undefined): number | null => {
|
|
if (rate == null || Number.isNaN(rate)) return null;
|
|
return rate > 1 ? rate / 100 : rate;
|
|
};
|
|
|
|
/**
|
|
* Fetches the standard VAT rate for a given ISO country code.
|
|
* Returns null if not found or on any error.
|
|
*/
|
|
export async function getStandardVatRate(countryCode: string): Promise<number | null> {
|
|
try {
|
|
const res = await authFetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/api/tax/vat-rates`, { method: "GET" });
|
|
if (!res.ok) return null;
|
|
|
|
const data = await res.json().catch(() => null);
|
|
if (!data || !Array.isArray(data)) return null;
|
|
|
|
const upper = countryCode.toUpperCase();
|
|
const match = (data as VatRate[]).find(r => r.countryCode.toUpperCase() === upper);
|
|
return normalizeVatRate(match?.standardRate);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|