314 lines
12 KiB
JavaScript
314 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.getMyReferralInfo = getMyReferralInfo;
|
|
exports.getMyReferralDashboard = getMyReferralDashboard;
|
|
exports.getMyReferralCommissions = getMyReferralCommissions;
|
|
exports.getBonusBalances = getBonusBalances;
|
|
exports.transferBonusToMain = transferBonusToMain;
|
|
exports.getAdminReferralConfig = getAdminReferralConfig;
|
|
exports.updateAdminReferralSettings = updateAdminReferralSettings;
|
|
exports.upsertReferralLevel = upsertReferralLevel;
|
|
exports.deleteReferralLevel = deleteReferralLevel;
|
|
exports.getReferralStats = getReferralStats;
|
|
exports.fundBonusPool = fundBonusPool;
|
|
exports.getReferralReviewQueue = getReferralReviewQueue;
|
|
exports.approveReferralCommission = approveReferralCommission;
|
|
exports.rejectReferralCommission = rejectReferralCommission;
|
|
exports.getReferralFraudDashboard = getReferralFraudDashboard;
|
|
exports.clawbackReferralCommission = clawbackReferralCommission;
|
|
exports.exportReferralClawbacksCsv = exportReferralClawbacksCsv;
|
|
const referralService = __importStar(require("../services/referralService"));
|
|
const bonusService = __importStar(require("../services/bonusService"));
|
|
const referralRepository = __importStar(require("../repositories/referralRepository"));
|
|
const bonusPoolRepository = __importStar(require("../repositories/bonusPoolRepository"));
|
|
const referralExportService_1 = require("../services/referralExportService");
|
|
async function getMyReferralInfo(req, res) {
|
|
try {
|
|
const info = await referralService.getUserReferralInfo(req.user.userId);
|
|
res.json(info);
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed' });
|
|
}
|
|
}
|
|
async function getMyReferralDashboard(req, res) {
|
|
try {
|
|
const maxDepth = req.query.maxDepth ? Number(req.query.maxDepth) : 5;
|
|
const dashboard = await referralService.getUserReferralDashboard(req.user.userId, Number.isFinite(maxDepth) ? maxDepth : 5);
|
|
res.json(dashboard);
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed' });
|
|
}
|
|
}
|
|
async function getMyReferralCommissions(req, res) {
|
|
try {
|
|
const limit = req.query.limit ? Number(req.query.limit) : 25;
|
|
const offset = req.query.offset ? Number(req.query.offset) : 0;
|
|
const result = await referralService.getUserReferralCommissions(req.user.userId, {
|
|
limit: Number.isFinite(limit) ? limit : 25,
|
|
offset: Number.isFinite(offset) ? offset : 0,
|
|
});
|
|
res.json(result);
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed' });
|
|
}
|
|
}
|
|
async function getBonusBalances(req, res) {
|
|
try {
|
|
const balances = await bonusService.getUserBonusBalances(req.user.userId);
|
|
const bonusWithdrawable = await bonusService.isBonusWithdrawable();
|
|
res.json({ balances, bonusWithdrawable });
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed' });
|
|
}
|
|
}
|
|
async function transferBonusToMain(req, res) {
|
|
try {
|
|
const { walletTypeId, amountRaw } = req.body;
|
|
if (!walletTypeId || !amountRaw) {
|
|
res.status(400).json({ error: 'walletTypeId and amountRaw are required' });
|
|
return;
|
|
}
|
|
await bonusService.transferBonusToMain({
|
|
userId: req.user.userId,
|
|
walletTypeId,
|
|
amountRaw,
|
|
});
|
|
res.json({ message: 'Bonus transferred to main account' });
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Transfer failed' });
|
|
}
|
|
}
|
|
async function getAdminReferralConfig(_req, res) {
|
|
try {
|
|
const config = await referralService.getProgramSettingsForAdmin();
|
|
res.json(config);
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed' });
|
|
}
|
|
}
|
|
async function updateAdminReferralSettings(req, res) {
|
|
try {
|
|
const body = req.body;
|
|
await referralRepository.updateSettings({
|
|
enabled: body.enabled,
|
|
defaultReferrerUserId: body.defaultReferrerUserId,
|
|
bonusWithdrawable: body.bonusWithdrawable,
|
|
referrerMustBeVerified: body.referrerMustBeVerified,
|
|
referrerMustHaveDeposited: body.referrerMustHaveDeposited,
|
|
referredMustBeVerified: body.referredMustBeVerified,
|
|
commissionHoldDays: body.commissionHoldDays,
|
|
minReferredPredictions: body.minReferredPredictions,
|
|
commissionMaxPendingDays: body.commissionMaxPendingDays,
|
|
sybilDetectionEnabled: body.sybilDetectionEnabled,
|
|
sybilReviewThreshold: body.sybilReviewThreshold,
|
|
sybilBlockThreshold: body.sybilBlockThreshold,
|
|
maxReferralsPerDay: body.maxReferralsPerDay,
|
|
maxCommissionAmountPerReferrerMonthRaw: body.maxCommissionAmountPerReferrerMonthRaw,
|
|
maxCommissionsPerReferrerMonth: body.maxCommissionsPerReferrerMonth,
|
|
slackAlertsEnabled: body.slackAlertsEnabled,
|
|
depositCommissionsEnabled: body.depositCommissionsEnabled,
|
|
withdrawalCommissionsEnabled: body.withdrawalCommissionsEnabled,
|
|
minFirstDepositRaw: body.minFirstDepositRaw,
|
|
commissionWalletTypeId: body.commissionWalletTypeId,
|
|
});
|
|
const config = await referralService.getProgramSettingsForAdmin();
|
|
res.json(config);
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Update failed' });
|
|
}
|
|
}
|
|
async function upsertReferralLevel(req, res) {
|
|
try {
|
|
const { levelNumber, commissionPercent, isActive, monthlyMaxAmountRaw, monthlyMaxCount, } = req.body;
|
|
if (!levelNumber || commissionPercent == null) {
|
|
res.status(400).json({ error: 'levelNumber and commissionPercent are required' });
|
|
return;
|
|
}
|
|
await referralRepository.upsertLevel(levelNumber, commissionPercent, isActive ?? true, monthlyMaxAmountRaw ?? '0', monthlyMaxCount ?? 0);
|
|
res.json({ message: 'Level saved' });
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Failed' });
|
|
}
|
|
}
|
|
async function deleteReferralLevel(req, res) {
|
|
try {
|
|
const levelNumber = parseInt(req.params.levelNumber, 10);
|
|
if (!levelNumber) {
|
|
res.status(400).json({ error: 'Invalid level number' });
|
|
return;
|
|
}
|
|
await referralRepository.deleteLevel(levelNumber);
|
|
res.json({ message: 'Level deleted' });
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Failed' });
|
|
}
|
|
}
|
|
async function getReferralStats(_req, res) {
|
|
try {
|
|
const stats = await referralRepository.getAdminStats();
|
|
res.json(stats);
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed' });
|
|
}
|
|
}
|
|
async function fundBonusPool(req, res) {
|
|
try {
|
|
const { walletTypeId, amountRaw } = req.body;
|
|
if (!walletTypeId || !amountRaw) {
|
|
res.status(400).json({ error: 'walletTypeId and amountRaw are required' });
|
|
return;
|
|
}
|
|
const amount = BigInt(amountRaw);
|
|
if (amount <= 0n) {
|
|
res.status(400).json({ error: 'Amount must be positive' });
|
|
return;
|
|
}
|
|
await bonusPoolRepository.fundPool(walletTypeId, amount);
|
|
const pools = await bonusPoolRepository.listPools();
|
|
res.json({ message: 'Pool funded', pools });
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Failed' });
|
|
}
|
|
}
|
|
async function getReferralReviewQueue(req, res) {
|
|
try {
|
|
const limit = req.query.limit ? Number(req.query.limit) : 50;
|
|
const offset = req.query.offset ? Number(req.query.offset) : 0;
|
|
const queue = await referralService.listReferralReviewQueue({ limit, offset });
|
|
res.json(queue);
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed' });
|
|
}
|
|
}
|
|
async function approveReferralCommission(req, res) {
|
|
try {
|
|
const payoutId = req.params.payoutId;
|
|
const { note } = req.body;
|
|
await referralService.approveReferralCommissionReview({
|
|
payoutId,
|
|
reviewerId: req.user.userId,
|
|
note,
|
|
});
|
|
res.json({ message: 'Commission approved and paid' });
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Approval failed' });
|
|
}
|
|
}
|
|
async function rejectReferralCommission(req, res) {
|
|
try {
|
|
const payoutId = req.params.payoutId;
|
|
const { reason } = req.body;
|
|
if (!reason?.trim()) {
|
|
res.status(400).json({ error: 'reason is required' });
|
|
return;
|
|
}
|
|
await referralService.rejectReferralCommissionReview({
|
|
payoutId,
|
|
reviewerId: req.user.userId,
|
|
reason: reason.trim(),
|
|
});
|
|
res.json({ message: 'Commission rejected' });
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Rejection failed' });
|
|
}
|
|
}
|
|
async function getReferralFraudDashboard(_req, res) {
|
|
try {
|
|
const dashboard = await referralService.getReferralFraudDashboard();
|
|
res.json(dashboard);
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed' });
|
|
}
|
|
}
|
|
async function clawbackReferralCommission(req, res) {
|
|
try {
|
|
const payoutId = req.params.payoutId;
|
|
const { reason } = req.body;
|
|
if (!reason?.trim()) {
|
|
res.status(400).json({ error: 'reason is required' });
|
|
return;
|
|
}
|
|
const result = await referralService.clawbackReferralCommission({
|
|
payoutId,
|
|
adminId: req.user.userId,
|
|
reason: reason.trim(),
|
|
});
|
|
res.json(result);
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Clawback failed' });
|
|
}
|
|
}
|
|
async function exportReferralClawbacksCsv(req, res) {
|
|
try {
|
|
const fromRaw = req.query.from ? String(req.query.from) : undefined;
|
|
const toRaw = req.query.to ? String(req.query.to) : undefined;
|
|
const from = fromRaw ? new Date(fromRaw) : undefined;
|
|
const to = toRaw ? new Date(toRaw) : undefined;
|
|
if (from && Number.isNaN(from.getTime())) {
|
|
res.status(400).json({ error: 'Invalid from date (use ISO 8601)' });
|
|
return;
|
|
}
|
|
if (to && Number.isNaN(to.getTime())) {
|
|
res.status(400).json({ error: 'Invalid to date (use ISO 8601)' });
|
|
return;
|
|
}
|
|
const csv = await (0, referralExportService_1.buildClawbacksCsv)({ from, to });
|
|
const filename = `referral-clawbacks-${new Date().toISOString().slice(0, 10)}.csv`;
|
|
res.setHeader('Content-Type', 'text/csv; charset=utf-8');
|
|
res.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
|
|
res.send(csv);
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Export failed' });
|
|
}
|
|
}
|
|
//# sourceMappingURL=referralController.js.map
|