Files
tbbank-frontend/src/features/currencyRates/api/currencyRatesApi.ts
2026-08-30 02:29:18 +05:00

98 lines
3.2 KiB
TypeScript

import apiClient from "@/lib/api/client";
// ─── Types ────────────────────────────────────────────────────────────────────
export interface CurrencyRateUser {
id: number;
name: string;
username: string;
phone: string;
email: string;
}
/** Raw backend entity (GET responses mirror this shape). */
export interface CurrencyRate {
id: number;
currencyFrom: string;
currencyTo: string;
value: number;
createdBy: CurrencyRateUser;
updatedBy: CurrencyRateUser;
createdAt: string;
updatedAt: string;
}
/** LIST RESPONSE: GET /currencies */
export interface CurrencyRateListResponse {
count: number;
currencies: CurrencyRate[];
}
/** LIST PARAMS */
export interface CurrencyRateListParams {
page?: number;
limit?: number;
search?: string;
currency_from?: string;
currency_to?: string;
}
/** CREATE / UPDATE PAYLOAD (camelCase, JSON) */
export interface CreateCurrencyRatePayload {
currencyFrom: string;
currencyTo: string;
value: number;
}
export type UpdateCurrencyRatePayload = CreateCurrencyRatePayload;
// ─── API Functions ─────────────────────────────────────────────────────────────
export const currencyRatesApi = {
getList: async (params: CurrencyRateListParams = {}, signal?: AbortSignal): Promise<CurrencyRateListResponse> => {
const { page, limit, search, currency_from, currency_to } = params;
const { data } = await apiClient.get<CurrencyRateListResponse>("/currencies", {
params: {
page,
limit,
search: search || undefined,
currency_from: currency_from || undefined,
currency_to: currency_to || undefined,
},
signal,
});
return data;
},
getById: async (id: number): Promise<CurrencyRate> => {
const { data } = await apiClient.get<CurrencyRate>(`/currencies/${id}`);
return data;
},
create: async (payload: CreateCurrencyRatePayload): Promise<CurrencyRate> => {
const { data } = await apiClient.post<CurrencyRate>("/currencies", payload);
return data;
},
update: async (id: number, payload: UpdateCurrencyRatePayload): Promise<CurrencyRate> => {
const { data } = await apiClient.put<CurrencyRate>(`/currencies/${id}`, payload);
return data;
},
delete: async (id: number): Promise<void> => {
await apiClient.delete(`/currencies/${id}`);
},
};
// ─── Constants ────────────────────────────────────────────────────────────────
export const CURRENCY_OPTIONS: { value: string; label: string }[] = [
{ value: "USD", label: "USD — US Dollar" },
{ value: "RUB", label: "RUB — Russian Ruble" },
{ value: "TMT", label: "TMT — Turkmen Manat" },
{ value: "EUR", label: "EUR — Euro" },
{ value: "GBP", label: "GBP — British Pound" },
{ value: "CNY", label: "CNY — Chinese Yuan" },
{ value: "TRY", label: "TRY — Turkish Lira" },
];