mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 14:38:45 +00:00
* chore(scripts/twitter): Refactored twitter to use event enums * chore(scripts/marketplace): Refactored marketplace to use event enums * chore(scripts/messages): Refactored messages to use event enums * chore(scripts/contacts): Refactored contacts to use event enums * chore(scripts/calls): Refactored calls to use event enums * chore(scripts/notes): Refactored notes to use event enums * chore(scripts/photos): Refactored photos to use event enums * chore(scripts/bank): Refactored bank to use event enums * chore(scripts/phone): Refactored phone to use event enums * chore(events): Removed old events object * refactor(phone/nui-events): Attached NUI events to respective enum * tweak(event-enums): Added npwd prefix to all events * tweak(phone/serve): Added craco to override CRA without eject Also bumped react-scripts to 4.0.1, with seemingly no issues * fix(typings/twitter): Fixed method enums for twitter
130 lines
4.0 KiB
TypeScript
130 lines
4.0 KiB
TypeScript
import { ESX } from './server';
|
|
import { getSource } from './functions';
|
|
import { pool } from './db';
|
|
import { Transfer, IBankCredentials, BankEvents } from '../../typings/bank';
|
|
import { getIdentifier } 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(BankEvents.TRANSACTION_ALERT, getSource(), false);
|
|
} else if (transfer.transferAmount < bankBalance) {
|
|
emitNet(BankEvents.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(BankEvents.FETCH_TRANSACTIONS, async () => {
|
|
const _source = getSource();
|
|
try {
|
|
const _identifier = getIdentifier(_source);
|
|
const transfer = await fetchAllTransactions(_identifier);
|
|
|
|
emitNet(BankEvents.SEND_TRANSFERS, _source, transfer);
|
|
} catch (e) {
|
|
bankLogger.error(`Failed to fetch transactions, ${e.message}`, {
|
|
source: _source,
|
|
});
|
|
}
|
|
});
|
|
|
|
onNet(BankEvents.ADD_TRANSFER, async (transfer: Transfer) => {
|
|
const _source = getSource();
|
|
const xTarget = ESX.GetPlayerFromId(transfer.targetID);
|
|
try {
|
|
const _identifier = getIdentifier(_source);
|
|
await addTransfer(_identifier, transfer);
|
|
|
|
// we need to create a notification for bank. that's why i have commented out.
|
|
// as well as creating a check if the player has enough money etc....
|
|
|
|
//emitNet(events.BANK_TRANSACTION_NOTIFICATION, xTarget, transferNotify);
|
|
|
|
emitNet(BankEvents.ADD_TRANSFER_SUCCESS, _source);
|
|
emitNet(BankEvents.TRANSACTION_ALERT, _source, {
|
|
message: 'BANK_ALERT_TRANSFER_SUCCESS',
|
|
type: 'success',
|
|
});
|
|
} catch (e) {
|
|
bankLogger.error(`Failed to add transaction, ${e.message}`, {
|
|
source: _source,
|
|
});
|
|
emitNet(BankEvents.TRANSACTION_ALERT, _source, {
|
|
message: 'APPS_BANK_ALERT_TRANSFER_FAILURE',
|
|
type: 'error',
|
|
});
|
|
}
|
|
});
|
|
|
|
onNet(BankEvents.GET_CREDENTIALS, () => {
|
|
const _source = getSource();
|
|
try {
|
|
const credentials = fetchCredentials();
|
|
emitNet(BankEvents.SEND_CREDENTIALS, _source, credentials);
|
|
} catch (e) {
|
|
bankLogger.error(`Failed to fetch credentials, ${e.message}`, {
|
|
source: _source,
|
|
});
|
|
}
|
|
});
|