Skip to content

Commit

Permalink
fix(server/invoice): actually remove balance when paying an invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWasTakenn committed Aug 30, 2024
1 parent ff40c02 commit 03e0714
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"date_of_birth": "Date of birth",
"withdraw": "Withdraw",
"deposit": "Deposit",
"transfer": "Bank transfer"
"transfer": "Bank transfer",
"invoice_payment": "Invoice payment"
}
16 changes: 12 additions & 4 deletions server/accounts/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { OxAccount, OxAccountInvoice, OxAccountRole, OxCreateInvoice } from
import locales from '../../common/locales';
import { getRandomInt } from '@overextended/ox_lib';
import { CanPerformAction } from './roles';
import { GetAccountById } from 'accounts';
import { GetAccountById, RemoveAccountBalance } from 'accounts';

const addBalance = `UPDATE accounts SET balance = balance + ? WHERE id = ?`;
const removeBalance = `UPDATE accounts SET balance = balance - ? WHERE id = ?`;
Expand Down Expand Up @@ -278,8 +278,8 @@ export function UpdateAccountAccess(accountId: number, id: number, role?: string
);
}

export async function UpdateInvoice(invoiceId: number, playerId: number) {
const player = OxPlayer.getFromCharId(playerId);
export async function UpdateInvoice(invoiceId: number, charId: number) {
const player = OxPlayer.getFromCharId(charId);

if (!player?.charId) return 'no_charId';

Expand All @@ -298,7 +298,15 @@ export async function UpdateInvoice(invoiceId: number, playerId: number) {

const account = (await GetAccountById(invoice.toAccount))!;

if (account.balance > invoice.amount) return 'insufficient_balance';
if (invoice.amount > account.balance) return 'insufficient_balance';

const removedBalance = await RemoveAccountBalance({
id: invoice.toAccount,
amount: invoice.amount,
message: locales('invoice_payment'),
});

if (!removedBalance || typeof removedBalance === 'string') return removedBalance;

return db.update('UPDATE `accounts_invoices` SET `payerId` = ?, `paidAt` = ? WHERE `id` = ?', [
player.charId,
Expand Down

0 comments on commit 03e0714

Please sign in to comment.