mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-08-01 01:30:24 +00:00
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 7m19s
44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { getServerSession } from "next-auth";
|
|
import { authOptions } from "@/app/auth";
|
|
import type { ApiError, ApiResponse, WalletTransaction } from "@/lib/backend-types";
|
|
|
|
type GenericGetResponseProps = {
|
|
offset?: number;
|
|
limit?: number;
|
|
page?: number;
|
|
[key: string]: string | number | undefined;
|
|
};
|
|
export async function getWaleltTransactions(
|
|
params: GenericGetResponseProps,
|
|
allTransactions = false,
|
|
) {
|
|
// Build query string from all defined params
|
|
const query = Object.entries(params)
|
|
.filter(([_, value]) => value !== undefined && value !== "")
|
|
.map(
|
|
([key, value]) =>
|
|
`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`,
|
|
)
|
|
.join("&");
|
|
const session = await getServerSession(authOptions);
|
|
const response = await fetch(
|
|
`${process.env.SARLINK_API_BASE_URL}/api/billing/wallet-transactions/?${query}&all_transactions=${allTransactions}`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Token ${session?.apiToken}`,
|
|
},
|
|
},
|
|
);
|
|
if (!response.ok) {
|
|
const errorData = (await response.json()) as ApiError;
|
|
const errorMessage =
|
|
errorData.message || errorData.detail || "An error occurred.";
|
|
const error = new Error(errorMessage);
|
|
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
|
throw error;
|
|
}
|
|
const data = (await response.json()) as ApiResponse<WalletTransaction>;
|
|
return data;
|
|
} |