Skip to content

Commit

Permalink
fix(db/invoices): use UpdateBalance db function to pay invoice
Browse files Browse the repository at this point in the history
resolves an issue where the name for the invoice transaction would not show
  • Loading branch information
LukeWasTakenn committed Sep 2, 2024
1 parent 794cd8e commit 037a66a
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions server/accounts/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { OxAccount, OxAccountRole, OxCreateInvoice } from 'types';
import locales from '../../common/locales';
import { getRandomInt } from '@overextended/ox_lib';
import { CanPerformAction } from './roles';
import { GetAccountById, RemoveAccountBalance } from 'accounts';
import { GetAccountById } from 'accounts';

const addBalance = `UPDATE accounts SET balance = balance + ? WHERE id = ?`;
const removeBalance = `UPDATE accounts SET balance = balance - ? WHERE id = ?`;
Expand Down Expand Up @@ -39,7 +39,8 @@ export async function UpdateBalance(
action: 'add' | 'remove',
overdraw: boolean,
message?: string,
note?: string
note?: string,
actorId?: number
) {
amount = parseInt(String(amount));

Expand All @@ -58,7 +59,7 @@ export async function UpdateBalance(
return (
success &&
(await conn.update(addTransaction, [
null,
actorId || null,
addAction ? null : id,
addAction ? id : null,
amount,
Expand Down Expand Up @@ -296,17 +297,17 @@ export async function UpdateInvoice(invoiceId: number, charId: number) {

if (!hasPermission) return 'no_permission';

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

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

const removedBalance = await RemoveAccountBalance({
id: invoice.toAccount,
amount: invoice.amount,
message: locales('invoice_payment'),
});
const success = await UpdateBalance(
invoice.toAccount,
invoice.amount,
'remove',
false,
locales('invoice_payment'),
undefined,
charId
);

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

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

0 comments on commit 037a66a

Please sign in to comment.