142 lines
3.7 KiB
TypeScript
142 lines
3.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
// Alpha: In-memory wallet storage
|
|
const wallets: Map<string, {
|
|
id: string;
|
|
leagueId: string;
|
|
balance: number;
|
|
totalRevenue: number;
|
|
totalPlatformFees: number;
|
|
totalWithdrawn: number;
|
|
createdAt: Date;
|
|
}> = new Map();
|
|
|
|
const transactions: Map<string, {
|
|
id: string;
|
|
walletId: string;
|
|
type: 'deposit' | 'withdrawal' | 'platform_fee';
|
|
amount: number;
|
|
description: string;
|
|
referenceId?: string;
|
|
referenceType?: 'sponsorship' | 'membership_fee' | 'prize';
|
|
createdAt: Date;
|
|
}> = new Map();
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const { searchParams } = new URL(request.url);
|
|
const leagueId = searchParams.get('leagueId');
|
|
|
|
if (!leagueId) {
|
|
return NextResponse.json(
|
|
{ error: 'leagueId is required' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
let wallet = Array.from(wallets.values()).find(w => w.leagueId === leagueId);
|
|
|
|
if (!wallet) {
|
|
// Create wallet if doesn't exist
|
|
const id = `wallet-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
wallet = {
|
|
id,
|
|
leagueId,
|
|
balance: 0,
|
|
totalRevenue: 0,
|
|
totalPlatformFees: 0,
|
|
totalWithdrawn: 0,
|
|
createdAt: new Date(),
|
|
};
|
|
wallets.set(id, wallet);
|
|
}
|
|
|
|
const walletTransactions = Array.from(transactions.values())
|
|
.filter(t => t.walletId === wallet!.id)
|
|
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
|
|
return NextResponse.json({ wallet, transactions: walletTransactions });
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
const { leagueId, type, amount, description, referenceId, referenceType } = body;
|
|
|
|
if (!leagueId || !type || !amount || !description) {
|
|
return NextResponse.json(
|
|
{ error: 'Missing required fields: leagueId, type, amount, description' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
if (!['deposit', 'withdrawal'].includes(type)) {
|
|
return NextResponse.json(
|
|
{ error: 'Type must be "deposit" or "withdrawal"' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Get or create wallet
|
|
let wallet = Array.from(wallets.values()).find(w => w.leagueId === leagueId);
|
|
|
|
if (!wallet) {
|
|
const id = `wallet-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
wallet = {
|
|
id,
|
|
leagueId,
|
|
balance: 0,
|
|
totalRevenue: 0,
|
|
totalPlatformFees: 0,
|
|
totalWithdrawn: 0,
|
|
createdAt: new Date(),
|
|
};
|
|
wallets.set(id, wallet);
|
|
}
|
|
|
|
if (type === 'withdrawal') {
|
|
if (amount > wallet.balance) {
|
|
return NextResponse.json(
|
|
{ error: 'Insufficient balance' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Alpha: In production, check if season is over before allowing withdrawal
|
|
// For now we allow it for demo purposes
|
|
}
|
|
|
|
// Create transaction
|
|
const transactionId = `txn-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
const transaction = {
|
|
id: transactionId,
|
|
walletId: wallet.id,
|
|
type,
|
|
amount,
|
|
description,
|
|
referenceId: referenceId || undefined,
|
|
referenceType: referenceType || undefined,
|
|
createdAt: new Date(),
|
|
};
|
|
|
|
transactions.set(transactionId, transaction);
|
|
|
|
// Update wallet balance
|
|
if (type === 'deposit') {
|
|
wallet.balance += amount;
|
|
wallet.totalRevenue += amount;
|
|
} else {
|
|
wallet.balance -= amount;
|
|
wallet.totalWithdrawn += amount;
|
|
}
|
|
|
|
wallets.set(wallet.id, wallet);
|
|
|
|
return NextResponse.json({ wallet, transaction }, { status: 201 });
|
|
} catch (err) {
|
|
console.error('Wallet transaction failed:', err);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to process wallet transaction' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
} |