Skip to content

Commit

Permalink
feat: creating and paying invoices
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWasTakenn committed Aug 30, 2024
1 parent 29191ca commit 80e789a
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type {
GetAccountRole,
RemoveAccountAccess,
SetAccountAccess,
PayAccountInvoice,
CreateAccountInvoice,
} from 'server/accounts';
import type { OxPlayer } from 'server/player/class';
import type { GetCharIdFromStateId } from 'server/player/db';
Expand Down Expand Up @@ -44,6 +46,8 @@ interface OxServer extends OxCommon {
GenerateVehiclePlate: typeof OxVehicle.generatePlate;
SetGroupPermission: typeof SetGroupPermission;
RemoveGroupPermission: typeof RemoveGroupPermission;
PayAccountInvoice: typeof PayAccountInvoice;
CreateAccoutnInvoice: typeof CreateAccountInvoice;
}

export const Ox = OxCore as OxServer;
Expand Down
53 changes: 52 additions & 1 deletion server/accounts/db.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Connection, GetConnection, db } from 'db';
import { OxPlayer } from 'player/class';
import type { OxAccount, OxAccountRole } from 'types';
import type { OxAccount, OxAccountInvoice, OxAccountRole } from 'types';
import locales from '../../common/locales';
import { getRandomInt } from '@overextended/ox_lib';
import { CanPerformAction } from './roles';
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 @@ -276,3 +277,53 @@ export function UpdateAccountAccess(accountId: number, id: number, role?: string
[accountId, id, role]
);
}

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

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

const invoice = await db.row<{ amount: number; payerId?: number; toId: number }>(
'SELECT `amount`, `payerId`, `toId` FROM `accounts_invoices` WHERE `id` = ?',
[invoiceId]
);

if (!invoice) return 'no_invoice';

if (invoice.payerId) return 'invoice_paid';

const hasPermission = await player.hasAccountPermission(invoice.toId, 'payInvoice');

if (!hasPermission) return 'no_permission';

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

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

return db.update('UPDATE `accounts_invoices` SET `payerId` = ?, `paidAt` = ? WHERE `id` = ?', [
player.charId,
new Date(),
invoiceId,
]);
}

export async function CreateInvoice(invoice: Omit<OxAccountInvoice, 'id'>) {
const player = OxPlayer.get(invoice.creatorId);

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

console.log(JSON.stringify(invoice, null, 2));

const hasPermission = await player.hasAccountPermission(invoice.fromId, 'sendInvoice');

if (!hasPermission) return 'no_permission';

const targetAccount = await GetAccountById(invoice.toId);

if (!targetAccount) return 'no_target_account';

return db.insert(
'INSERT INTO accounts_invoices (`creatorId`, `fromId`, `toId`, `amount`, `message`) VALUES (?, ?, ?, ?, ?)',
[invoice.creatorId, invoice.fromId, invoice.toId, invoice.amount, invoice.message]
);
}
15 changes: 15 additions & 0 deletions server/accounts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { OxAccountInvoice } from 'types';
import {
CreateNewAccount,
DeleteAccount,
Expand All @@ -11,6 +12,8 @@ import {
SelectDefaultAccount,
UpdateBalance,
WithdrawMoney,
UpdateInvoice,
CreateInvoice,
} from './db';
import { GetCharIdFromStateId } from 'player/db';

Expand Down Expand Up @@ -114,6 +117,16 @@ export async function RemoveAccountAccess(accountId: number, id: number | string
return charId && UpdateAccountAccess(accountId, charId);
}

export async function PayAccountInvoice(invoiceId: number, charId: number) {
return UpdateInvoice(invoiceId, charId);
}

export async function CreateAccountInvoice(invoice: Omit<OxAccountInvoice, 'id'>) {
return CreateInvoice(invoice);
}

export async function DeleteAccountInvoice(invoiceId: number) {}

exports('GetAccountById', GetAccountById);
exports('GetCharacterAccount', GetCharacterAccount);
exports('GetGroupAccount', GetGroupAccount);
Expand All @@ -130,3 +143,5 @@ exports('DepositMoney', DepositMoney);
exports('WithdrawMoney', WithdrawMoney);
exports('SetAccountAccess', SetAccountAccess);
exports('RemoveAccountAccess', RemoveAccountAccess);
exports('PayAccountInvoice', PayAccountInvoice);
exports('CreateAccountInvoice', CreateAccountInvoice);
27 changes: 26 additions & 1 deletion server/player/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ import {
UpdateCharacterGroup,
SetActiveGroup,
} from 'groups/db';
import { GetAccountRole, GetCharacterAccount, GetCharacterAccounts } from 'accounts';
import {
CreateAccountInvoice,
GetAccountRole,
GetCharacterAccount,
GetCharacterAccounts,
PayAccountInvoice,
} from 'accounts';
import type {
Character,
Dict,
Expand Down Expand Up @@ -202,6 +208,25 @@ export class OxPlayer extends ClassInterface {
);
}

async payInvoice(invoiceId: number) {
if (!this.charId) return;
return await PayAccountInvoice(invoiceId, this.charId);
}

async createInvoice(data: { fromAccountId: number; toAccountId: number; amount: number; message: string }) {
if (!this.charId) return;

const invoice = {
creatorId: this.charId,
fromId: data.fromAccountId,
toId: data.toAccountId,
amount: data.amount,
message: data.message,
};

return await CreateAccountInvoice(invoice);
}

setActiveGroup(groupName?: string, temp?: boolean) {
if (!this.charId || (groupName && !(groupName in this.#groups))) return false;

Expand Down
22 changes: 14 additions & 8 deletions sql/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ CREATE TABLE `account_roles` (
`viewHistory` TINYINT(1) NOT NULL DEFAULT '0',
`manageAccount` TINYINT(1) NOT NULL DEFAULT '0',
`closeAccount` TINYINT(1) NOT NULL DEFAULT '0',
`sendInvoice` TINYINT(1) NOT NULL DEFAULT '0',
`payInvoice` TINYINT(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE INDEX `name` (`name`)
);
Expand Down Expand Up @@ -210,27 +212,31 @@ CREATE TABLE IF NOT EXISTS `accounts_transactions` (

CREATE TABLE IF NOT EXISTS `accounts_invoices`
(
`id` INT UNSIGNED AUTO_INCREMENT
`id` INT UNSIGNED AUTO_INCREMENT
PRIMARY KEY,
`payerId` INT UNSIGNED NULL,
`fromId` INT UNSIGNED NOT NULL,
`toId` INT UNSIGNED NOT NULL,
`amount` INT UNSIGNED NOT NULL,
`message` VARCHAR(255) NULL,
`date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP() NOT NULL,
`creatorId` INT UNSIGNED NOT NULL,
`payerId` INT UNSIGNED NULL,
`fromId` INT UNSIGNED NOT NULL,
`toId` INT UNSIGNED NOT NULL,
`amount` INT UNSIGNED NOT NULL,
`message` VARCHAR(255) NULL,
`date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP() NOT NULL,
CONSTRAINT `accounts_invoices_accounts_id_fk`
FOREIGN KEY (`fromId`) REFERENCES `accounts` (`id`),
CONSTRAINT `accounts_invoices_accounts_id_fk_2`
FOREIGN KEY (`toId`) REFERENCES `accounts` (`id`),
CONSTRAINT `accounts_invoices_characters_charId_fk`
FOREIGN KEY (`payerId`) REFERENCES `characters` (`charId`)
FOREIGN KEY (`payerId`) REFERENCES `characters` (`charId`),
CONSTRAINT `accounts_invoices_characters_charId_fk_2`
FOREIGN KEY (`creatorId`) REFERENCES `characters` (`charId`)
);







/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;

/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */;
Expand Down
14 changes: 14 additions & 0 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,18 @@ export interface OxAccountPermissions {
viewHistory: boolean;
manageAccount: boolean;
closeAccount: boolean;
sendInvoice: boolean;
payInvoice: boolean;
}

export interface OxAccountInvoice {
id: number;
creatorId: number;
payerId?: number;
fromId: number;
toId: number;
amount: number;
message?: string;
sentAt?: number;
paidAt?: number;
}

0 comments on commit 80e789a

Please sign in to comment.