import Casino from './Casino.js';
import Mysql from './dbDrivers/mysql.js';
const mysql = Mysql({
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
host: process.env.MYSQL_HOST,
});
export default class TestCasino extends Casino {
static async getSession(sessionId) {
const session = await mysql.queryRow(
'SELECT * FROM game_sessions_100hp WHERE session_id = ?', [sessionId],
);
return session ? { userId: session.user_id, currency: session.currency } : undefined;
}
static async getBalance(userId, currency) {
const balance = await mysql.queryRow('SELECT * FROM balances WHERE ? AND ?', [
{
user_id: userId,
}, {
currency,
},
]);
if (!balance) {
throw {
data: '',
message: 'Session not found',
code: 604,
};
}
return {
balance: balance.amount,
};
}
static async getUser(userId) {
const user = await mysql.queryRow('SELECT * FROM users WHERE ?', [
{
id: userId,
},
]);
const segmentsMeta = await mysql.query(
'SELECT * FROM users_segments WHERE ?', { user_id: userId },
);
if (!user) {
throw {
data: '',
message: 'Session not found',
code: 604,
};
}
return {
userName: user.user_name,
status: user.is_blocked ? 'blocked' : 'active',
type: user.is_test ? 'test' : 'real',
segment: segmentsMeta.map(s => s.segment_name),
};
}
static async makeDeposit(userId, currency, amount, txId) {
const conn = await mysql.getConnection();
try {
await conn.query('START TRANSACTION');
await conn.query('INSERT INTO deposits SET ?', {
user_id: userId,
currency,
amount,
tx_id: txId,
});
await conn.query('UPDATE balances SET amount = amount + ? WHERE ? and ?', [amount, {
user_id: userId,
}, { currency }]);
await conn.query('COMMIT');
} catch (e) {
await conn.query('ROLLBACK');
throw e;
} finally {
await conn.release();
}
return TestCasino.getBalance(userId, currency);
}
static async makeWithdrawal(userId, currency, amount, txId) {
const conn = await mysql.getConnection();
try {
await conn.query('START TRANSACTION');
await conn.query('INSERT INTO withdrawals SET ?', {
user_id: userId,
currency,
amount,
tx_id: txId,
});
const { affectedRows } = await conn.query('UPDATE balances SET amount = amount - ? WHERE ? and ? and amount > ?', [amount, {
user_id: userId,
}, { currency }, amount]);
if (affectedRows === 0) {
throw {
data: '',
message: 'Insufficient balance',
code: 603,
};
}
await conn.query('COMMIT');
} catch (e) {
await conn.query('ROLLBACK');
throw e;
} finally {
await conn.release();
}
return TestCasino.getBalance(userId, currency);
}
static async makeRollback(userId, txId) {
const [
deposit, withdrawal,
] = await Promise.all([
mysql.queryRow('SELECT * FROM deposits WHERE ? and ?', [{ user_id: userId }, { tx_id: txId }]),
mysql.queryRow('SELECT * FROM withdrawals WHERE ? and ?', [{ user_id: userId }, { tx_id: txId }]),
]);
if (!deposit && !withdrawal) {
throw {
data: '',
message: 'Transaction not found',
code: 605,
};
}
if (deposit && withdrawal) {
throw new Error('Deposit and withdrawal found for the same transaction');
}
const conn = await mysql.getConnection();
try {
await conn.query('START TRANSACTION');
if (deposit) {
const { affectedRows } = await conn.query('UPDATE deposits SET canceled = 1 WHERE ? and canceled = 0', [{ id: deposit.id }]);
console.log('deposit refund', affectedRows, deposit.id);
if (affectedRows === 0) {
throw {
data: '',
message: 'Transaction already settled',
code: 609,
};
}
await conn.query('UPDATE balances SET amount = amount - ? WHERE ? and ?', [deposit.amount, {
user_id: userId }, {
currency: deposit.currency,
}]);
}
if (withdrawal) {
const { affectedRows } = await conn.query('UPDATE withdrawals SET canceled = 1 WHERE ? and canceled = 0', [{ id: withdrawal.id }]);
if (affectedRows === 0) {
throw {
data: '',
message: 'Transaction already refunded',
code: 608,
};
}
await conn.query('UPDATE balances SET amount = amount + ? WHERE ? and ?', [withdrawal.amount, {
user_id: userId }, {
currency: withdrawal.currency,
}]);
}
await conn.query('COMMIT');
} catch (e) {
await conn.query('ROLLBACK');
throw e;
} finally {
await conn.release();
}
return TestCasino.getBalance(userId, deposit?.currency || withdrawal.currency);
}
async makeDepositWithPromo(userId, currency, amount, txId, promoId) {
await TestCasino.makeDeposit(userId, currency, amount, txId);
console.log(`Made promo deposit: ${userId}, ${currency}, ${amount}, ${txId}, ${promoId}`);
return TestCasino.getBalance(userId, currency);
}
}