games_app/backend/dist-test-faq/controllers/walletController.js

132 lines
5.3 KiB
JavaScript

"use strict";
/**
* Wallet controller — HTTP handlers for wallet types, balances, deposit addresses, and transaction history.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWalletTypes = getWalletTypes;
exports.getBalances = getBalances;
exports.getDepositAddress = getDepositAddress;
exports.getTransactions = getTransactions;
const walletService = __importStar(require("../services/walletService"));
/**
* Returns all supported wallet types configured on the platform.
* @param _req - Authenticated request (unused).
* @param res - JSON array of wallet types on success, 500 on failure.
*/
async function getWalletTypes(_req, res) {
try {
const types = await walletService.getWalletTypes();
res.json(types);
}
catch (e) {
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed' });
}
}
/**
* Returns the authenticated user's balances across all wallet types.
* @param req - Authenticated request; user ID is taken from the JWT.
* @param res - JSON array of balances on success, 500 on failure.
*/
async function getBalances(req, res) {
try {
const userId = req.user.userId;
const balances = await walletService.getUserBalances(userId);
res.json(balances);
}
catch (e) {
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed' });
}
}
/**
* Returns or creates a blockchain deposit address for the given wallet type.
* @param req - Authenticated request. Params: `walletType`.
* @param res - JSON `{ address, walletType }` on success, 503 if platform mnemonic is not configured, 400 otherwise.
*/
async function getDepositAddress(req, res) {
try {
const userId = req.user.userId;
const { walletType } = req.params;
if (!walletType) {
res.status(400).json({ error: 'walletType required' });
return;
}
const result = await walletService.getOrCreateDepositAddress(userId, walletType);
res.json({ address: result.address, walletType: result.walletType });
}
catch (e) {
const msg = e instanceof Error ? e.message : 'Failed';
if (msg.includes('PLATFORM_MNEMONIC') || msg.includes('mnemonic') || msg.includes('INVALID_ARGUMENT')) {
res.status(503).json({ error: 'Deposit addresses are not available. Server configuration required: set a valid BIP39 mnemonic in PLATFORM_MNEMONIC.' });
return;
}
if (/Deposits are disabled/i.test(msg)) {
res.status(403).json({ error: msg, code: 'DEPOSIT_NETWORK_DISABLED' });
return;
}
res.status(400).json({ error: msg });
}
}
/**
* Returns paginated transaction history for the authenticated user.
* @param req - Authenticated request. Query: optional `limit` (max 100, default 50), `offset` (default 0).
* @param res - JSON transaction list on success, 500 on failure.
*/
async function getTransactions(req, res) {
try {
const userId = req.user.userId;
const limit = Math.min(parseInt(req.query.limit) || 50, 100);
const offset = parseInt(req.query.offset) || 0;
const paginated = req.query.paginated === 'true' || req.query.paginated === '1';
const categoryRaw = req.query.category;
const category = categoryRaw === 'deposits' ||
categoryRaw === 'withdrawals' ||
categoryRaw === 'transfers' ||
categoryRaw === 'predictions'
? categoryRaw
: undefined;
if (paginated) {
const result = await walletService.listUserTransactionsPaginated(userId, limit, offset, category);
res.json(result);
return;
}
const list = await walletService.listUserTransactions(userId, limit, offset, category);
res.json(list);
}
catch (e) {
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed' });
}
}
//# sourceMappingURL=walletController.js.map