325 lines
12 KiB
JavaScript
325 lines
12 KiB
JavaScript
"use strict";
|
|
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.getState = getState;
|
|
exports.getCandles = getCandles;
|
|
exports.placeBet = placeBet;
|
|
exports.getBetOutcome = getBetOutcome;
|
|
exports.getMyBets = getMyBets;
|
|
exports.getAutoSettings = getAutoSettings;
|
|
exports.updateAutoSettings = updateAutoSettings;
|
|
exports.getAdminConfig = getAdminConfig;
|
|
exports.updateAdminConfig = updateAdminConfig;
|
|
exports.listAdminIntervals = listAdminIntervals;
|
|
exports.createAdminInterval = createAdminInterval;
|
|
exports.updateAdminInterval = updateAdminInterval;
|
|
exports.deleteAdminInterval = deleteAdminInterval;
|
|
exports.placeMultiBet = placeMultiBet;
|
|
exports.listAdminMultiLevels = listAdminMultiLevels;
|
|
exports.updateAdminMultiLevels = updateAdminMultiLevels;
|
|
const candlePredictService = __importStar(require("../services/candlePredictService"));
|
|
const stakeAmountValidation_1 = require("../utils/stakeAmountValidation");
|
|
const placeBetResponse_1 = require("../utils/placeBetResponse");
|
|
function parseIntervalMinutes(value) {
|
|
if (value == null || value === '')
|
|
return undefined;
|
|
const n = Number(value);
|
|
return Number.isFinite(n) ? n : undefined;
|
|
}
|
|
async function getState(req, res) {
|
|
try {
|
|
const intervalMinutes = parseIntervalMinutes(req.query.intervalMinutes);
|
|
const includeInternal = req.user?.role === 'admin';
|
|
const state = await candlePredictService.getGameState(req.user?.userId, intervalMinutes, {
|
|
includeInternal,
|
|
});
|
|
res.json(state);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to load game state';
|
|
res.status(500).json({ error: msg });
|
|
}
|
|
}
|
|
async function getCandles(req, res) {
|
|
try {
|
|
const limit = parseInt(req.query.limit || '0', 10);
|
|
const intervalMinutes = parseIntervalMinutes(req.query.intervalMinutes);
|
|
const beforeRaw = req.query.beforeOpenMs;
|
|
const beforeOpenMs = beforeRaw != null && beforeRaw !== '' && Number.isFinite(Number(beforeRaw))
|
|
? Number(beforeRaw)
|
|
: undefined;
|
|
const page = await candlePredictService.getCandlesPage({
|
|
limit: limit > 0 ? limit : undefined,
|
|
intervalMinutes,
|
|
beforeOpenMs,
|
|
includeInternal: req.user?.role === 'admin',
|
|
});
|
|
res.json(page);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to load candles';
|
|
res.status(500).json({ error: msg });
|
|
}
|
|
}
|
|
async function placeBet(req, res) {
|
|
try {
|
|
if (!req.user?.userId) {
|
|
res.status(401).json({ error: 'Unauthorized' });
|
|
return;
|
|
}
|
|
const { direction, stakeAmount, intervalMinutes, stakeAccountKind } = req.body;
|
|
if (stakeAmount === undefined || stakeAmount === null) {
|
|
res.status(400).json({ error: 'stakeAmount is required' });
|
|
return;
|
|
}
|
|
const parsedStake = (0, stakeAmountValidation_1.parseStakeAmountFromBody)(stakeAmount);
|
|
const result = await candlePredictService.placeBet(req.user.userId, direction ?? '', parsedStake, intervalMinutes, stakeAccountKind, {
|
|
idempotencyKey: (0, placeBetResponse_1.readIdempotencyKey)(req, req.body),
|
|
source: 'http',
|
|
});
|
|
(0, placeBetResponse_1.sendPlaceBetResult)(res, result);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to place prediction';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|
|
async function getBetOutcome(req, res) {
|
|
try {
|
|
if (!req.user?.userId) {
|
|
res.status(401).json({ error: 'Unauthorized' });
|
|
return;
|
|
}
|
|
const idempotencyKey = String(req.params.idempotencyKey ?? '').trim();
|
|
if (!idempotencyKey) {
|
|
res.status(400).json({ error: 'idempotencyKey is required' });
|
|
return;
|
|
}
|
|
const record = await candlePredictService.getBetIdempotency(idempotencyKey);
|
|
if (!record) {
|
|
res.status(404).json({ error: 'Bet outcome not found' });
|
|
return;
|
|
}
|
|
if (record.status === 'accepted') {
|
|
const betUserId = String(record.bet.user_id ?? record.bet.userId ?? '').trim();
|
|
if (betUserId && betUserId !== req.user.userId) {
|
|
res.status(403).json({ error: 'Forbidden' });
|
|
return;
|
|
}
|
|
res.json({ status: 'accepted', bet: record.bet });
|
|
return;
|
|
}
|
|
if (record.status === 'rejected') {
|
|
res.json({ status: 'rejected', error: record.error });
|
|
return;
|
|
}
|
|
res.json({ status: 'processing' });
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to load bet outcome';
|
|
res.status(500).json({ error: msg });
|
|
}
|
|
}
|
|
async function getMyBets(req, res) {
|
|
try {
|
|
if (!req.user?.userId) {
|
|
res.status(401).json({ error: 'Unauthorized' });
|
|
return;
|
|
}
|
|
const page = Math.max(1, parseInt(req.query.page || '1', 10));
|
|
const pageSize = Math.min(50, Math.max(1, parseInt(req.query.pageSize || '10', 10)));
|
|
const intervalMinutes = parseIntervalMinutes(req.query.intervalMinutes);
|
|
const predictions = await candlePredictService.getRecentPredictions(req.user.userId, page, pageSize, intervalMinutes);
|
|
res.json(predictions);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to load predictions';
|
|
res.status(500).json({ error: msg });
|
|
}
|
|
}
|
|
async function getAutoSettings(req, res) {
|
|
try {
|
|
if (!req.user?.userId) {
|
|
res.status(401).json({ error: 'Unauthorized' });
|
|
return;
|
|
}
|
|
const settings = await candlePredictService.getAutoSettings(req.user.userId);
|
|
res.json(settings);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to load auto settings';
|
|
res.status(500).json({ error: msg });
|
|
}
|
|
}
|
|
async function updateAutoSettings(req, res) {
|
|
try {
|
|
if (!req.user?.userId) {
|
|
res.status(401).json({ error: 'Unauthorized' });
|
|
return;
|
|
}
|
|
const settings = await candlePredictService.saveAutoSettings(req.user.userId, req.body);
|
|
res.json(settings);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to update auto settings';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|
|
async function getAdminConfig(req, res) {
|
|
try {
|
|
const config = await candlePredictService.getPublicConfig();
|
|
res.json(config);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to load config';
|
|
res.status(500).json({ error: msg });
|
|
}
|
|
}
|
|
async function updateAdminConfig(req, res) {
|
|
try {
|
|
const config = await candlePredictService.updateAdminConfig(req.body);
|
|
res.json(config);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to update config';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|
|
async function listAdminIntervals(req, res) {
|
|
try {
|
|
const intervals = await candlePredictService.listAdminIntervals();
|
|
res.json(intervals);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to load intervals';
|
|
res.status(500).json({ error: msg });
|
|
}
|
|
}
|
|
async function createAdminInterval(req, res) {
|
|
try {
|
|
const interval = await candlePredictService.createAdminInterval(req.body);
|
|
res.status(201).json(interval);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to create interval';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|
|
async function updateAdminInterval(req, res) {
|
|
try {
|
|
const id = Number(req.params.id);
|
|
if (!Number.isFinite(id)) {
|
|
res.status(400).json({ error: 'Invalid interval id' });
|
|
return;
|
|
}
|
|
const interval = await candlePredictService.updateAdminInterval(id, req.body);
|
|
res.json(interval);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to update interval';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|
|
async function deleteAdminInterval(req, res) {
|
|
try {
|
|
const id = Number(req.params.id);
|
|
if (!Number.isFinite(id)) {
|
|
res.status(400).json({ error: 'Invalid interval id' });
|
|
return;
|
|
}
|
|
const result = await candlePredictService.deleteAdminInterval(id);
|
|
res.json(result);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to delete interval';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|
|
async function placeMultiBet(req, res) {
|
|
try {
|
|
if (!req.user?.userId) {
|
|
res.status(401).json({ error: 'Unauthorized' });
|
|
return;
|
|
}
|
|
const { level, sequence, stakeAmount, intervalMinutes, stakeAccountKind } = req.body;
|
|
if (stakeAmount === undefined || stakeAmount === null) {
|
|
res.status(400).json({ error: 'stakeAmount is required' });
|
|
return;
|
|
}
|
|
if (level == null || !Number.isFinite(Number(level))) {
|
|
res.status(400).json({ error: 'level is required' });
|
|
return;
|
|
}
|
|
if (!Array.isArray(sequence) || sequence.length === 0) {
|
|
res.status(400).json({ error: 'sequence is required' });
|
|
return;
|
|
}
|
|
const parsedStake = (0, stakeAmountValidation_1.parseStakeAmountFromBody)(stakeAmount);
|
|
const directions = sequence.filter((d) => d === 'short' || d === 'long');
|
|
const bet = await candlePredictService.placeMultiCandleBet(req.user.userId, {
|
|
level: Number(level),
|
|
sequence: directions,
|
|
stakeDisplay: parsedStake,
|
|
intervalMinutes,
|
|
stakeAccountKind,
|
|
});
|
|
res.status(201).json(bet);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to place multi-candle prediction';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|
|
async function listAdminMultiLevels(req, res) {
|
|
try {
|
|
const levels = await candlePredictService.listAdminMultiLevels();
|
|
res.json(levels);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to load multi levels';
|
|
res.status(500).json({ error: msg });
|
|
}
|
|
}
|
|
async function updateAdminMultiLevels(req, res) {
|
|
try {
|
|
const levels = await candlePredictService.updateAdminMultiLevels(req.body);
|
|
res.json(levels);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to update multi levels';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|
|
//# sourceMappingURL=candlePredictController.js.map
|