106 lines
3.1 KiB
TypeScript
106 lines
3.1 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 * FROM merchants WHERE id = $1 LIMIT 1', [id]);
|
||
const data = result.rows[0];
|
||
|
||
if (!data) {
|
||
return NextResponse.json({ error: 'Merchant not found' }, { status: 404 });
|
||
}
|
||
|
||
return NextResponse.json(data);
|
||
} catch (err: any) {
|
||
return NextResponse.json(
|
||
{ error: `Internal Server Error: ${err.message}` },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|
||
|
||
export async function PATCH(
|
||
req: NextRequest,
|
||
context: { params: Promise<{ id: string }> }
|
||
) {
|
||
try {
|
||
const { id } = await context.params;
|
||
const body = await req.json();
|
||
|
||
if (!body.name) {
|
||
return NextResponse.json(
|
||
{ error: 'Firma adı zorunludur.' },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// 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) {
|
||
return NextResponse.json({ error: 'Update failed or merchant not found' }, { status: 500 });
|
||
}
|
||
|
||
return NextResponse.json(data);
|
||
} catch (err: any) {
|
||
console.error('[Merchant PATCH Error]', err);
|
||
return NextResponse.json(
|
||
{ error: `Internal Server Error: ${err.message}` },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|
||
|
||
export async function DELETE(
|
||
req: NextRequest,
|
||
context: { params: Promise<{ id: string }> }
|
||
) {
|
||
try {
|
||
const { id } = await context.params;
|
||
await db.query('DELETE FROM merchants WHERE id = $1', [id]);
|
||
|
||
return NextResponse.json({ success: true });
|
||
} catch (err: any) {
|
||
return NextResponse.json(
|
||
{ error: `Internal Server Error: ${err.message}` },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|