35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { db } from '@/lib/db';
|
|
|
|
export async function GET(
|
|
req: NextRequest,
|
|
context: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await context.params;
|
|
|
|
const result = await db.query(
|
|
'SELECT network, token, balance, withdrawn, total_gross FROM merchant_balances WHERE merchant_id = $1 ORDER BY network, token',
|
|
[id]
|
|
);
|
|
|
|
// Also get the merchant fee
|
|
const merchantRes = await db.query('SELECT fee_percent FROM merchants WHERE id = $1', [id]);
|
|
const feePercent = parseFloat(merchantRes.rows[0]?.fee_percent || '1.0');
|
|
|
|
return NextResponse.json({
|
|
balances: result.rows.map(r => ({
|
|
network: r.network,
|
|
token: r.token,
|
|
balance: parseFloat(r.balance),
|
|
withdrawn: parseFloat(r.withdrawn),
|
|
totalGross: parseFloat(r.total_gross || r.balance),
|
|
available: parseFloat(r.balance) - parseFloat(r.withdrawn)
|
|
})),
|
|
feePercent
|
|
});
|
|
} catch (err: any) {
|
|
return NextResponse.json({ error: err.message }, { status: 500 });
|
|
}
|
|
}
|