109 lines
2.9 KiB
TypeScript
109 lines
2.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
const BE_BASE_URL = process.env.BE_BASE_URL || "http://localhost:5000";
|
|
const COOKIE_NAME = "auth_token";
|
|
|
|
type FilterValue =
|
|
| string
|
|
| {
|
|
operator?: string;
|
|
value: string;
|
|
};
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const { cookies } = await import("next/headers");
|
|
const cookieStore = await cookies();
|
|
const token = cookieStore.get(COOKIE_NAME)?.value;
|
|
|
|
if (!token) {
|
|
return NextResponse.json(
|
|
{ message: "Missing Authorization header" },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const body = await request.json();
|
|
const { filters = {}, pagination = { page: 1, limit: 10 }, sort } = body;
|
|
|
|
// Force withdrawals filter while allowing other filters to stack
|
|
const mergedFilters: Record<string, FilterValue> = {
|
|
...filters,
|
|
Type: {
|
|
operator: "==",
|
|
value: "withdrawal",
|
|
},
|
|
};
|
|
|
|
const queryParts: string[] = [];
|
|
queryParts.push(`limit=${pagination.limit}`);
|
|
queryParts.push(`page=${pagination.page}`);
|
|
|
|
if (sort) {
|
|
queryParts.push(`sort=${sort.field}:${sort.order}`);
|
|
}
|
|
|
|
for (const [key, filterValue] of Object.entries(mergedFilters)) {
|
|
if (!filterValue) continue;
|
|
|
|
let operator: string;
|
|
let value: string;
|
|
|
|
if (typeof filterValue === "string") {
|
|
operator = "==";
|
|
value = filterValue;
|
|
} else {
|
|
operator = filterValue.operator || "==";
|
|
value = filterValue.value;
|
|
}
|
|
|
|
if (!value) continue;
|
|
|
|
const encodedValue = encodeURIComponent(value);
|
|
const needsEqualsPrefix = /^[A-Za-z]/.test(operator);
|
|
const operatorSegment = needsEqualsPrefix ? `=${operator}` : operator;
|
|
|
|
queryParts.push(`${key}${operatorSegment}/${encodedValue}`);
|
|
}
|
|
|
|
const queryString = queryParts.join("&");
|
|
const backendUrl = `${BE_BASE_URL}/api/v1/transactions${
|
|
queryString ? `?${queryString}` : ""
|
|
}`;
|
|
|
|
const response = await fetch(backendUrl, {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
cache: "no-store",
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response
|
|
.json()
|
|
.catch(() => ({ message: "Failed to fetch withdrawals" }));
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: errorData?.message || "Failed to fetch withdrawals",
|
|
},
|
|
{ status: response.status }
|
|
);
|
|
}
|
|
|
|
const data = await response.json();
|
|
return NextResponse.json(data, { status: response.status });
|
|
} catch (err: unknown) {
|
|
console.error(
|
|
"Proxy POST /api/dashboard/transactions/withdrawals error:",
|
|
err
|
|
);
|
|
const errorMessage = err instanceof Error ? err.message : "Unknown error";
|
|
return NextResponse.json(
|
|
{ message: "Internal server error", error: errorMessage },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|