games_app/backend/dist/services/bonusService.js
2026-11-15 17:36:00 +02:00

103 lines
4.2 KiB
JavaScript

"use strict";
/**
* User bonus account operations: balances and transfer to main when allowed.
*/
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.BonusError = void 0;
exports.getUserBonusBalances = getUserBonusBalances;
exports.transferBonusToMain = transferBonusToMain;
exports.isBonusWithdrawable = isBonusWithdrawable;
const ledgerRepository = __importStar(require("../repositories/ledgerRepository"));
const referralRepository = __importStar(require("../repositories/referralRepository"));
const walletRepository = __importStar(require("../repositories/walletRepository"));
const auditLogRepository = __importStar(require("../repositories/auditLogRepository"));
const crypto_1 = require("crypto");
const userAdminStatsService_1 = require("./userAdminStatsService");
const securityService_1 = require("./securityService");
class BonusError extends Error {
constructor(message) {
super(message);
this.name = 'BonusError';
}
}
exports.BonusError = BonusError;
async function getUserBonusBalances(userId) {
const types = await walletRepository.getWalletTypes();
const result = [];
for (const wt of types) {
const account = await ledgerRepository.getAccount(userId, wt.id, 'bonus');
result.push({
walletTypeId: wt.id,
walletCode: wt.code,
available: account?.available_balance ?? '0',
});
}
return result;
}
async function transferBonusToMain(input) {
await (0, securityService_1.assertUserCanMoveFunds)(input.userId);
const settings = await referralRepository.getSettings();
if (!settings.bonus_withdrawable) {
throw new BonusError('Bonus balance cannot be transferred to main account (platform policy)');
}
let amount;
try {
amount = BigInt(input.amountRaw);
}
catch {
throw new BonusError('Invalid amount');
}
if (amount <= 0n)
throw new BonusError('Amount must be positive');
const bonusAccount = await ledgerRepository.getOrCreateBonusAccount(input.userId, input.walletTypeId);
const mainAccount = await ledgerRepository.getOrCreateAccount(input.userId, input.walletTypeId, 'main');
const refId = (0, crypto_1.randomUUID)();
await ledgerRepository.debit(bonusAccount.id, amount, 'bonus_to_main', 'bonus_transfer', refId);
await ledgerRepository.credit(mainAccount.id, amount, 'bonus_to_main', 'bonus_transfer', refId);
await auditLogRepository.create({
userId: input.userId,
action: 'bonus_transferred_to_main',
resourceType: 'bonus_transfer',
resourceId: refId,
metadata: { wallet_type_id: input.walletTypeId, amount: amount.toString() },
});
(0, userAdminStatsService_1.queueUserAdminStatsRefresh)(input.userId);
}
async function isBonusWithdrawable() {
const settings = await referralRepository.getSettings();
return settings.bonus_withdrawable;
}
//# sourceMappingURL=bonusService.js.map