feat: add Solana USDT/USDC support and refine admin payouts UI

This commit is contained in:
mstfyldz
2026-03-13 05:17:04 +03:00
parent 5f0df83686
commit 641498957c
16 changed files with 1335 additions and 120 deletions

View File

@@ -0,0 +1,34 @@
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 });
}
}

View File

@@ -29,19 +29,48 @@ export async function PATCH(
) {
try {
const { id } = await context.params;
const { name, webhook_url, payment_provider, provider_config, fee_percent } = await req.json();
const body = await req.json();
if (!name) {
if (!body.name) {
return NextResponse.json(
{ error: 'Firma adı zorunludur.' },
{ status: 400 }
);
}
const result = await db.query(
'UPDATE merchants SET name = $1, webhook_url = $2, payment_provider = $3, provider_config = $4, fee_percent = $5 WHERE id = $6 RETURNING *',
[name, webhook_url, payment_provider, provider_config, fee_percent || 1.0, id]
);
// Build dynamic update
const fields: string[] = [];
const values: any[] = [];
let idx = 1;
const addField = (col: string, val: any) => {
if (val !== undefined) {
fields.push(`${col} = $${idx++}`);
values.push(val);
}
};
addField('name', body.name);
addField('webhook_url', body.webhook_url);
addField('fee_percent', body.fee_percent || 1.0);
addField('payout_address', body.payout_address);
if (body.payout_addresses !== undefined) {
fields.push(`payout_addresses = $${idx++}`);
values.push(JSON.stringify(body.payout_addresses));
}
if (body.payment_provider !== undefined) {
addField('payment_provider', body.payment_provider);
}
if (body.provider_config !== undefined) {
fields.push(`provider_config = $${idx++}`);
values.push(JSON.stringify(body.provider_config));
}
values.push(id);
const query = `UPDATE merchants SET ${fields.join(', ')} WHERE id = $${idx} RETURNING *`;
const result = await db.query(query, values);
const data = result.rows[0];
if (!data) {
@@ -50,6 +79,7 @@ export async function PATCH(
return NextResponse.json(data);
} catch (err: any) {
console.error('[Merchant PATCH Error]', err);
return NextResponse.json(
{ error: `Internal Server Error: ${err.message}` },
{ status: 500 }

View File

@@ -48,8 +48,37 @@ export async function POST(req: NextRequest) {
export async function GET() {
try {
const result = await db.query('SELECT * FROM merchants ORDER BY created_at DESC');
return NextResponse.json(result.rows);
const merchantsResult = await db.query('SELECT * FROM merchants ORDER BY created_at DESC');
const merchants = merchantsResult.rows;
// Fetch breakdown per merchant
const breakdownResult = await db.query(`
SELECT
merchant_id,
COALESCE(paid_network, 'SİSTEM') as network,
COALESCE(paid_token, 'TRY') as token,
SUM(COALESCE(paid_amount_crypto, amount)) as amount
FROM transactions
WHERE status = 'succeeded'
GROUP BY merchant_id, paid_network, paid_token
`);
const breakdowns = breakdownResult.rows.reduce((acc: any, row: any) => {
if (!acc[row.merchant_id]) acc[row.merchant_id] = [];
acc[row.merchant_id].push({
network: row.network,
token: row.token,
amount: row.amount
});
return acc;
}, {});
const merchantsWithBreakdown = merchants.map(m => ({
...m,
balance_breakdown: breakdowns[m.id] || []
}));
return NextResponse.json(merchantsWithBreakdown);
} catch (err: any) {
return NextResponse.json(
{ error: `Internal Server Error: ${err.message}` },