38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
||
import { db } from '@/lib/db';
|
||
import crypto from 'crypto';
|
||
|
||
export async function POST(req: NextRequest) {
|
||
try {
|
||
const { name } = await req.json();
|
||
|
||
if (!name || name.length < 3) {
|
||
return NextResponse.json({ error: 'Firma adı en az 3 karakter olmalıdır.' }, { status: 400 });
|
||
}
|
||
|
||
// Generate a 10-char ID
|
||
const short_id = crypto.randomBytes(5).toString('hex');
|
||
const api_key = crypto.randomBytes(24).toString('hex'); // Secure API Key
|
||
|
||
// Insert into database
|
||
const result = await db.query(
|
||
`INSERT INTO merchants (name, short_id, api_key)
|
||
VALUES ($1, $2, $3)
|
||
RETURNING *`,
|
||
[name, short_id, api_key]
|
||
);
|
||
|
||
const merchant = result.rows[0];
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
merchantId: merchant.id,
|
||
shortId: merchant.short_id,
|
||
apiKey: merchant.api_key
|
||
});
|
||
} catch (err: any) {
|
||
console.error('[Registration Error]:', err.message);
|
||
return NextResponse.json({ error: 'Sunucu hatası: ' + err.message }, { status: 500 });
|
||
}
|
||
}
|