first commit
This commit is contained in:
109
frontend/app/utils/api/fxRate/index.tsx
Normal file
109
frontend/app/utils/api/fxRate/index.tsx
Normal file
@@ -0,0 +1,109 @@
|
||||
import { baseApiRequest } from "../apiRequests/baseApiRequest";
|
||||
import { FxRateResponse } from "./type";
|
||||
|
||||
// interface FxRateApiResponse {
|
||||
// id: string;
|
||||
// year: string;
|
||||
// currency: string;
|
||||
// rate: string;
|
||||
// }
|
||||
|
||||
// interface PaginatedResponse<T> {
|
||||
// content: T[];
|
||||
// }
|
||||
|
||||
const PROJECT_MANAGEMENT_BASE_URL = `${process.env.NEXT_PUBLIC_PROJECT_MANAGEMENT_API_URL}/api/v1`;
|
||||
|
||||
export const getAllFXRate = async (): Promise<FxRateResponse> => {
|
||||
return await baseApiRequest(
|
||||
`${PROJECT_MANAGEMENT_BASE_URL}/fx-rate`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "include",
|
||||
},
|
||||
{
|
||||
isAccessTokenRequird: true,
|
||||
revalidatePaths: ["/fx-rate"],
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
export const getFxRateByYear = async (
|
||||
year: string,
|
||||
): Promise<FxRateResponse> => {
|
||||
return await baseApiRequest(
|
||||
`${PROJECT_MANAGEMENT_BASE_URL}/fx-rate/year=${year}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
credentials: "include",
|
||||
},
|
||||
{
|
||||
isAccessTokenRequird: true,
|
||||
revalidatePaths: [`/fx-rate/year=${year}`],
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
// export type FxRateMap = Map<string, Map<string, number>>;
|
||||
|
||||
// export async function createFxRateMap(): Promise<FxRateMap> {
|
||||
// const fxRateMap: FxRateMap = new Map();
|
||||
|
||||
// try {
|
||||
// console.log("Fetching FX Rates for map...");
|
||||
|
||||
// const response = await baseApiRequest(
|
||||
// "${process.env.SALES_CRM_API_URL}/fxrate",
|
||||
// {
|
||||
// method: "GET",
|
||||
// credentials: "include",
|
||||
// },
|
||||
// );
|
||||
|
||||
// const paginatedData = response as PaginatedResponse<FxRateApiResponse>;
|
||||
|
||||
// if (!paginatedData || !Array.isArray(paginatedData.content)) {
|
||||
// console.error(
|
||||
// "Invalid response structure received from FX Rate API:",
|
||||
// paginatedData,
|
||||
// );
|
||||
// throw new Error("Failed to fetch FX Rates: Invalid data format.");
|
||||
// }
|
||||
|
||||
// const rates: FxRateApiResponse[] = paginatedData.content;
|
||||
// console.log(`Processing ${rates.length} FX rates...`);
|
||||
|
||||
// rates.forEach((rateData) => {
|
||||
// const year = rateData.year;
|
||||
// const currency = rateData.currency.toUpperCase();
|
||||
// const rateValue = parseFloat(rateData.rate);
|
||||
|
||||
// if (!year || !currency || isNaN(rateValue)) {
|
||||
// console.warn("Skipping invalid rate data:", rateData);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// if (!fxRateMap.has(year)) {
|
||||
// fxRateMap.set(year, new Map<string, number>());
|
||||
// }
|
||||
// const yearMap = fxRateMap.get(year)!;
|
||||
// yearMap.set(currency, rateValue);
|
||||
// });
|
||||
|
||||
// console.log("FX Rate Map created successfully:", fxRateMap);
|
||||
// return fxRateMap;
|
||||
// } catch (error) {
|
||||
// console.error("Error creating FX Rate Map:", error);
|
||||
// let errorMessage = "An unknown error occurred";
|
||||
// if (error instanceof Error) {
|
||||
// errorMessage = error.message;
|
||||
// } else if (typeof error === "string") {
|
||||
// errorMessage = error;
|
||||
// }
|
||||
// throw new Error(`Failed to create FX Rate Map: ${errorMessage}`);
|
||||
// }
|
||||
// }
|
||||
65
frontend/app/utils/api/fxRate/type.ts
Normal file
65
frontend/app/utils/api/fxRate/type.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
export interface FxRateDetail {
|
||||
id: string;
|
||||
currency: string;
|
||||
fxRate: number;
|
||||
activeStatus: boolean;
|
||||
year: string;
|
||||
}
|
||||
|
||||
export interface FxRateYearGroup {
|
||||
[year: string]: {
|
||||
[key: string]: FxRateDetail;
|
||||
};
|
||||
}
|
||||
|
||||
export interface FxContentItem {
|
||||
fxRateDetails: FxRateYearGroup;
|
||||
}
|
||||
|
||||
export interface SortInfo {
|
||||
empty: boolean;
|
||||
sorted: boolean;
|
||||
unsorted: boolean;
|
||||
}
|
||||
|
||||
export interface PageableInfo {
|
||||
pageNumber: number;
|
||||
pageSize: number;
|
||||
sort: SortInfo;
|
||||
offset: number;
|
||||
paged: boolean;
|
||||
unpaged: boolean;
|
||||
}
|
||||
|
||||
export interface FxRateEntry {
|
||||
id: string;
|
||||
currency: string;
|
||||
fxRate: number;
|
||||
activeStatus: boolean;
|
||||
year: string;
|
||||
}
|
||||
|
||||
export interface FxRateDetails {
|
||||
[key: string]: {
|
||||
[key: string]: FxRateEntry;
|
||||
};
|
||||
}
|
||||
|
||||
export interface FxRateResponse {
|
||||
content: FxContentItem[];
|
||||
pageable: PageableInfo;
|
||||
last: boolean;
|
||||
totalPages: number;
|
||||
totalElements: number;
|
||||
sort: SortInfo;
|
||||
first: boolean;
|
||||
number: number;
|
||||
size: number;
|
||||
numberOfElements: number;
|
||||
empty: boolean;
|
||||
}
|
||||
|
||||
export interface CurrencyOption {
|
||||
value: string;
|
||||
label: string;
|
||||
}
|
||||
Reference in New Issue
Block a user