mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 14:38:45 +00:00
- Logs out to a file w/ relevant meta data - Provides better readability for end user This should help provide better troubleshooting for both development and end users. This also fixes any previous catches that were attempting to print stack traces, causing the [object Object] message to appear. Co-authored-by: Christopher Gjelten <[email protected]>
120 lines
3.6 KiB
TypeScript
120 lines
3.6 KiB
TypeScript
import events from '../utils/events';
|
|
import { ESX } from './server';
|
|
import { getSource } from './functions';
|
|
import { pool } from './db';
|
|
import {
|
|
Transfer,
|
|
IBankCredentials,
|
|
} from '../../phone/src/common/typings/bank';
|
|
import { useIdentifier } from './functions';
|
|
import { mainLogger } from './sv_logger';
|
|
|
|
const bankLogger = mainLogger.child({ module: 'bank' });
|
|
|
|
async function fetchAllTransactions(identifier: string): Promise<Transfer[]> {
|
|
const query =
|
|
'SELECT * FROM npwd_bank_transfers WHERE identifier = ? ORDER BY id DESC';
|
|
const [results] = await pool.query(query, [identifier]);
|
|
const transactions = <Transfer[]>results;
|
|
|
|
bankLogger.verbose(
|
|
`Fetched all transactions (${identifier}) - ${JSON.stringify(transactions)}`
|
|
);
|
|
|
|
return transactions;
|
|
}
|
|
|
|
function fetchCredentials(): IBankCredentials {
|
|
const name = ESX.GetPlayerFromId(getSource()).getName();
|
|
const balance = ESX.GetPlayerFromId(getSource()).getAccount('bank').money;
|
|
|
|
const result = {
|
|
name,
|
|
balance,
|
|
};
|
|
|
|
bankLogger.verbose(`Fetching banking credentials - ${name} - ${balance}`);
|
|
|
|
return result;
|
|
}
|
|
|
|
async function addTransfer(
|
|
identifier: string,
|
|
transfer: Transfer
|
|
): Promise<any> {
|
|
const xPlayer = ESX.GetPlayerFromId(getSource());
|
|
const bankBalance = xPlayer.getAccount('bank').money;
|
|
const sourceName = xPlayer.getName();
|
|
|
|
const xTarget = ESX.GetPlayerFromId(transfer.targetID);
|
|
|
|
if (transfer.transferAmount > bankBalance) {
|
|
emitNet(events.BANK_TRANSACTION_ALERT, getSource(), false);
|
|
} else if (transfer.transferAmount < bankBalance) {
|
|
emitNet(events.BANK_TRANSACTION_ALERT, getSource(), true);
|
|
|
|
xTarget.addAccountMoney('bank', transfer.transferAmount);
|
|
xPlayer.removeAccountMoney('bank', transfer.transferAmount);
|
|
|
|
const query =
|
|
'INSERT INTO npwd_bank_transfers (identifier, target, amount, message, type, source) VALUES (?, ?, ?, ?, ?, ?)';
|
|
|
|
const [results] = await pool.query(query, [
|
|
identifier,
|
|
transfer.targetID,
|
|
transfer.transferAmount,
|
|
transfer.message,
|
|
'Transfer',
|
|
sourceName,
|
|
]);
|
|
const insertData = <any>results;
|
|
return await getTransfer(insertData.insertId);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets a transfer from its ID
|
|
* @param transferId - transfer id
|
|
*/
|
|
async function getTransfer(transferId: number): Promise<Transfer> {
|
|
const query = 'SELECT * FROM npwd_bank_transfers WHERE id = ?';
|
|
const [results] = await pool.query(query, [transferId]);
|
|
const transfers = <Transfer[]>results;
|
|
const transfer = transfers[0];
|
|
return { ...transfer };
|
|
}
|
|
|
|
onNet(events.BANK_FETCH_TRANSACTIONS, async () => {
|
|
try {
|
|
const _source = (global as any).source;
|
|
const _identifier = await useIdentifier();
|
|
const transfer = await fetchAllTransactions(_identifier);
|
|
|
|
emitNet(events.BANK_SEND_TRANSFERS, _source, transfer);
|
|
} catch (e) {
|
|
bankLogger.error(`Failed to fetch transactions, ${e.message}`);
|
|
}
|
|
});
|
|
|
|
onNet(events.BANK_ADD_TRANSFER, async (transfer: Transfer) => {
|
|
const xTarget = ESX.GetPlayerFromId(transfer.targetID);
|
|
try {
|
|
const _identifier = await useIdentifier();
|
|
const transferNotify = await addTransfer(_identifier, transfer);
|
|
emitNet(events.BANK_TRANSACTION_NOTIFICATION, xTarget, transferNotify);
|
|
emitNet(events.BANK_ADD_TRANSFER_SUCCESS, getSource());
|
|
} catch (e) {
|
|
bankLogger.error(`Failed to add transaction, ${e.message}`);
|
|
emitNet(events.BANK_TRANSACTION_ALERT, getSource(), false);
|
|
}
|
|
});
|
|
|
|
onNet(events.BANK_GET_CREDENTIALS, () => {
|
|
try {
|
|
const credentials = fetchCredentials();
|
|
emitNet(events.BANK_SEND_CREDENTIALS, getSource(), credentials);
|
|
} catch (e) {
|
|
bankLogger.error(`Failed to fetch credentials, ${e.message}`);
|
|
}
|
|
});
|