202 lines
5.2 KiB
TypeScript
202 lines
5.2 KiB
TypeScript
/**
|
|
* API Route: Wallet Withdrawal
|
|
*
|
|
* POST /api/wallets/:leagueId/withdraw
|
|
*
|
|
* Handles withdrawal requests from league wallets.
|
|
* Enforces the rule that withdrawals are only allowed after season is completed.
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
interface WithdrawRequest {
|
|
amount: number;
|
|
currency: string;
|
|
seasonId: string;
|
|
destinationAccount: string;
|
|
}
|
|
|
|
// Mock season status lookup
|
|
const MOCK_SEASONS: Record<string, { status: 'planned' | 'active' | 'completed'; name: string }> = {
|
|
'season-1': { status: 'completed', name: 'Season 1' },
|
|
'season-2': { status: 'active', name: 'Season 2' },
|
|
'season-3': { status: 'planned', name: 'Season 3' },
|
|
};
|
|
|
|
// Mock wallet balances
|
|
const MOCK_WALLETS: Record<string, { balance: number; currency: string }> = {
|
|
'league-1': { balance: 2500, currency: 'USD' },
|
|
'league-2': { balance: 1200, currency: 'USD' },
|
|
};
|
|
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ leagueId: string }> }
|
|
) {
|
|
try {
|
|
const { leagueId } = await params;
|
|
const body: WithdrawRequest = await request.json();
|
|
|
|
// Validate required fields
|
|
if (!body.amount || body.amount <= 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid withdrawal amount' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
if (!body.seasonId) {
|
|
return NextResponse.json(
|
|
{ error: 'Season ID is required' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
if (!body.destinationAccount) {
|
|
return NextResponse.json(
|
|
{ error: 'Destination account is required' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Get season status
|
|
const season = MOCK_SEASONS[body.seasonId];
|
|
if (!season) {
|
|
return NextResponse.json(
|
|
{ error: 'Season not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// CRITICAL: Enforce withdrawal restriction based on season status
|
|
// Withdrawals are ONLY allowed when the season is completed
|
|
if (season.status !== 'completed') {
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Withdrawal not allowed',
|
|
reason: `Withdrawals are only permitted after the season is completed. "${season.name}" is currently ${season.status}.`,
|
|
seasonStatus: season.status,
|
|
},
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
// Get wallet
|
|
const wallet = MOCK_WALLETS[leagueId];
|
|
if (!wallet) {
|
|
return NextResponse.json(
|
|
{ error: 'Wallet not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Check sufficient balance
|
|
if (wallet.balance < body.amount) {
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Insufficient balance',
|
|
available: wallet.balance,
|
|
requested: body.amount,
|
|
},
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Check currency match
|
|
if (wallet.currency !== body.currency) {
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Currency mismatch',
|
|
walletCurrency: wallet.currency,
|
|
requestedCurrency: body.currency,
|
|
},
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Process withdrawal (in-memory mock)
|
|
const newBalance = wallet.balance - body.amount;
|
|
MOCK_WALLETS[leagueId] = { ...wallet, balance: newBalance };
|
|
|
|
// Generate transaction ID
|
|
const transactionId = `txn-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
transactionId,
|
|
amount: body.amount,
|
|
currency: body.currency,
|
|
newBalance,
|
|
destinationAccount: body.destinationAccount,
|
|
processedAt: new Date().toISOString(),
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Wallet withdrawal error:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Internal server error' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GET /api/wallets/:leagueId/withdraw
|
|
*
|
|
* Check withdrawal eligibility for a league's wallet
|
|
*/
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ leagueId: string }> }
|
|
) {
|
|
try {
|
|
const { leagueId } = await params;
|
|
const { searchParams } = new URL(request.url);
|
|
const seasonId = searchParams.get('seasonId');
|
|
|
|
if (!seasonId) {
|
|
return NextResponse.json(
|
|
{ error: 'Season ID is required' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const season = MOCK_SEASONS[seasonId];
|
|
if (!season) {
|
|
return NextResponse.json(
|
|
{ error: 'Season not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
const wallet = MOCK_WALLETS[leagueId];
|
|
if (!wallet) {
|
|
return NextResponse.json(
|
|
{ error: 'Wallet not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
const canWithdraw = season.status === 'completed';
|
|
|
|
return NextResponse.json({
|
|
leagueId,
|
|
seasonId,
|
|
seasonName: season.name,
|
|
seasonStatus: season.status,
|
|
canWithdraw,
|
|
reason: canWithdraw
|
|
? 'Season is completed, withdrawals are allowed'
|
|
: `Withdrawals are only permitted after the season is completed. Season is currently ${season.status}.`,
|
|
balance: wallet.balance,
|
|
currency: wallet.currency,
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Withdrawal eligibility check error:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Internal server error' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
} |