games_app/backend/src/controllers/treasuryController.ts
2026-07-02 09:21:05 +02:00

140 lines
4.8 KiB
TypeScript

import { Response } from 'express';
import { AuthRequest } from '../middleware/auth';
import * as treasuryService from '../services/treasuryService';
import * as fundApprovalService from '../services/fundApprovalService';
export async function getOverview(_req: AuthRequest, res: Response): Promise<void> {
try {
const overview = await treasuryService.getTreasuryOverview();
res.json(overview);
} catch (e) {
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed to load treasury' });
}
}
export async function listColdWallets(_req: AuthRequest, res: Response): Promise<void> {
try {
res.json({ items: await treasuryService.listColdWallets() });
} catch (e) {
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed to list cold wallets' });
}
}
export async function setColdWallet(req: AuthRequest, res: Response): Promise<void> {
try {
const { walletTypeId, address, label } = req.body as {
walletTypeId?: number;
address?: string;
label?: string;
};
if (typeof walletTypeId !== 'number' || !address) {
res.status(400).json({ error: 'walletTypeId and address are required' });
return;
}
const row = await treasuryService.setColdWallet({ walletTypeId, address, label });
res.json(row);
} catch (e) {
res.status(400).json({ error: e instanceof Error ? e.message : 'Failed to set cold wallet' });
}
}
export async function listSweeps(req: AuthRequest, res: Response): Promise<void> {
try {
const status = (req.query.status as string) || undefined;
const limit = parseInt((req.query.limit as string) || '50', 10);
const offset = parseInt((req.query.offset as string) || '0', 10);
const result = await treasuryService.listSweeps({ status, limit, offset });
res.json(result);
} catch (e) {
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed to list sweeps' });
}
}
export async function runSweeps(req: AuthRequest, res: Response): Promise<void> {
try {
const result = await fundApprovalService.createFundRequest({
operationType: 'sweep_manual',
payload: {},
requestedBy: req.user!.userId,
});
res.status(202).json({
requestId: result.id,
status: result.status,
message: 'Sweep run submitted — pending super-admin approval',
});
} catch (e) {
res.status(400).json({ error: e instanceof Error ? e.message : 'Sweep request failed' });
}
}
export async function requestFunding(req: AuthRequest, res: Response): Promise<void> {
try {
const { walletTypeId, amount, notes } = req.body as {
walletTypeId?: number;
amount?: string | number;
notes?: string;
};
if (typeof walletTypeId !== 'number' || amount == null) {
res.status(400).json({ error: 'walletTypeId and amount are required' });
return;
}
const result = await fundApprovalService.createFundRequest({
operationType: 'treasury_cold_to_hot',
payload: {
walletTypeId,
amountRaw: BigInt(amount).toString(),
notes,
},
requestedBy: req.user!.userId,
});
res.status(202).json({
requestId: result.id,
status: result.status,
message: 'Funding request submitted — pending super-admin approval',
});
} catch (e) {
res.status(400).json({ error: e instanceof Error ? e.message : 'Funding request failed' });
}
}
export async function confirmFunding(req: AuthRequest, res: Response): Promise<void> {
try {
const fundingId = req.params.fundingId;
const { blockchainTxId } = req.body as { blockchainTxId?: string };
if (!blockchainTxId) {
res.status(400).json({ error: 'blockchainTxId is required' });
return;
}
const result = await treasuryService.confirmColdToHotFunding(
req.user!.userId,
fundingId,
blockchainTxId
);
res.json(result);
} catch (e) {
const msg = e instanceof Error ? e.message : 'Confirm failed';
res.status(msg.includes('not found') ? 404 : 400).json({ error: msg });
}
}
export async function listFunding(req: AuthRequest, res: Response): Promise<void> {
try {
const status = (req.query.status as string) || undefined;
const limit = parseInt((req.query.limit as string) || '50', 10);
const offset = parseInt((req.query.offset as string) || '0', 10);
const result = await treasuryService.listFunding({ status, limit, offset });
res.json(result);
} catch (e) {
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed to list funding' });
}
}
export async function cancelFunding(req: AuthRequest, res: Response): Promise<void> {
try {
await treasuryService.cancelFunding(req.params.fundingId);
res.json({ message: 'Cancelled' });
} catch (e) {
res.status(400).json({ error: e instanceof Error ? e.message : 'Cancel failed' });
}
}