Skip to content

Commit

Permalink
refactor(invoices): rename columns and properties for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWasTakenn committed Aug 30, 2024
1 parent 1a9bfd4 commit 2deb06d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
18 changes: 9 additions & 9 deletions server/accounts/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,20 @@ export async function UpdateInvoice(invoiceId: number, playerId: number) {

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` = ?',
const invoice = await db.row<{ amount: number; payerId?: number; toAccount: number }>(
'SELECT `amount`, `payerId`, `toAccount` 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');
const hasPermission = await player.hasAccountPermission(invoice.toAccount, 'payInvoice');

if (!hasPermission) return 'no_permission';

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

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

Expand All @@ -308,21 +308,21 @@ export async function UpdateInvoice(invoiceId: number, playerId: number) {
}

export async function CreateInvoice(invoice: OxCreateInvoice) {
const player = OxPlayer.get(invoice.creatorId);
const player = OxPlayer.get(invoice.actorId);

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

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

if (!hasPermission) return 'no_permission';

const targetAccount = await GetAccountById(invoice.toId);
const targetAccount = await GetAccountById(invoice.toAccount);

if (!targetAccount) return 'no_target_account';

return db.insert(
'INSERT INTO accounts_invoices (`creatorId`, `fromId`, `toId`, `amount`, `message`, `dueDate`) VALUES (?, ?, ?, ?, ?, ?)',
[invoice.creatorId, invoice.fromId, invoice.toId, invoice.amount, invoice.message, invoice.dueDate]
'INSERT INTO accounts_invoices (`actorId`, `fromAccount`, `toAccount`, `amount`, `message`, `dueDate`) VALUES (?, ?, ?, ?, ?, ?)',
[invoice.actorId, invoice.fromAccount, invoice.toAccount, invoice.amount, invoice.message, invoice.dueDate]
);
}

Expand Down
4 changes: 2 additions & 2 deletions server/player/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ export class OxPlayer extends ClassInterface {
return await PayAccountInvoice(invoiceId, +this.source);
}

async createInvoice(data: Omit<OxCreateInvoice, 'creatorId'>) {
async createInvoice(data: Omit<OxCreateInvoice, 'actorId'>) {
if (!this.charId) return;

const invoice = {
creatorId: +this.source,
actorId: +this.source,
...data,
} satisfies OxCreateInvoice;

Expand Down
26 changes: 13 additions & 13 deletions sql/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -212,25 +212,25 @@ 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,
`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,
`sentAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP() NOT NULL,
`dueDate` TIMESTAMP NOT NULL,
`paidAt` TIMESTAMP NULL,
`actorId` INT UNSIGNED NOT NULL,
`payerId` INT UNSIGNED NULL,
`fromAccount` INT UNSIGNED NOT NULL,
`toAccount` INT UNSIGNED NOT NULL,
`amount` INT UNSIGNED NOT NULL,
`message` VARCHAR(255) NULL,
`sentAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP() NOT NULL,
`dueDate` TIMESTAMP NOT NULL,
`paidAt` TIMESTAMP NULL,
CONSTRAINT `accounts_invoices_accounts_id_fk`
FOREIGN KEY (`fromId`) REFERENCES `accounts` (`id`),
FOREIGN KEY (`fromAccount`) REFERENCES `accounts` (`id`),
CONSTRAINT `accounts_invoices_accounts_id_fk_2`
FOREIGN KEY (`toId`) REFERENCES `accounts` (`id`),
FOREIGN KEY (`toAccount`) REFERENCES `accounts` (`id`),
CONSTRAINT `accounts_invoices_characters_charId_fk`
FOREIGN KEY (`payerId`) REFERENCES `characters` (`charId`),
CONSTRAINT `accounts_invoices_characters_charId_fk_2`
FOREIGN KEY (`creatorId`) REFERENCES `characters` (`charId`)
FOREIGN KEY (`actorId`) REFERENCES `characters` (`charId`)
);

/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
Expand Down
12 changes: 6 additions & 6 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ export interface OxAccountPermissions {

export interface OxAccountInvoice {
id: number;
creatorId: number;
actorId: number;
payerId?: number;
fromId: number;
toId: number;
fromAccount: number;
toAccount: number;
amount: number;
message?: string;
sentAt: number;
Expand All @@ -169,9 +169,9 @@ export interface OxAccountInvoice {
}

export interface OxCreateInvoice {
creatorId: number;
fromId: number;
toId: number;
actorId: number;
fromAccount: number;
toAccount: number;
amount: number;
message: string;
dueDate: string;
Expand Down

0 comments on commit 2deb06d

Please sign in to comment.