games_app/backend/src/controllers/transferController.ts
2026-07-25 22:48:54 +02:00

32 lines
1.1 KiB
TypeScript

/**
* Transfer controller — HTTP handler for internal peer-to-peer wallet transfers between users.
*/
import { Response } from 'express';
import * as transferService from '../services/transferService';
import { AuthRequest } from '../middleware/auth';
/**
* Submits an internal transfer from the authenticated user to another user identified by email.
* May require a TOTP code when 2FA is enabled for the user.
* @param req - Authenticated request. Body: `toEmail`, `walletType`, `amount`, optional `totpCode`.
* @param res - JSON `{ transactionId, status }` on success, 400 on validation or transfer failure.
*/
export async function transfer(req: AuthRequest, res: Response): Promise<void> {
const userId = req.user!.userId;
const { toEmail, walletType, amount, totpCode } = req.body;
if (!toEmail || !walletType || amount == null) {
res.status(400).json({ error: 'toEmail, walletType and amount required' });
return;
}
const amountRaw = BigInt(amount);
const { transactionId, status } = await transferService.requestTransfer(
userId,
toEmail,
walletType,
amountRaw,
totpCode
);
res.json({ transactionId, status });
}