From 107ad5e322096c15d4929517d2d51b4e37c0c749 Mon Sep 17 00:00:00 2001 From: GiroudMathias Date: Sun, 5 Mar 2023 20:47:29 +0100 Subject: [PATCH 1/5] fix: order invoices by createdAt DESC --- src/server/services/invoice/invoice.db.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/server/services/invoice/invoice.db.ts b/src/server/services/invoice/invoice.db.ts index 4f71bda3..1842421e 100644 --- a/src/server/services/invoice/invoice.db.ts +++ b/src/server/services/invoice/invoice.db.ts @@ -14,7 +14,11 @@ export class InvoiceDB { identifier: string, pagination: GetInvoicesInput, ): Promise { - return await InvoiceModel.findAll({ where: { toIdentifier: identifier }, ...pagination }); + return await InvoiceModel.findAll({ + where: { toIdentifier: identifier }, + ...pagination, + order: [['createdAt', 'DESC']], + }); } async getReceivedInvoicesCount(identifier: string): Promise { From 615f667a5ce02e5e7877334d56c1e53ad608393b Mon Sep 17 00:00:00 2001 From: GiroudMathias Date: Sun, 12 Mar 2023 19:07:50 +0100 Subject: [PATCH 2/5] fix: error on addCash / removeCash exports --- src/server/services/cash/cash.controller.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/server/services/cash/cash.controller.ts b/src/server/services/cash/cash.controller.ts index fd0638e2..3475a634 100644 --- a/src/server/services/cash/cash.controller.ts +++ b/src/server/services/cash/cash.controller.ts @@ -31,9 +31,9 @@ export class CashController { } @Export(ServerExports.AddCash) - async addCash(req: Request, res: Response) { + async addCash(req: Request, res: Response) { try { - await this._cashService.handleAddCash(req.data.source, req.data.amount); + await this._cashService.handleAddCash(req.source, req.data); res({ status: 'ok', data: true }); } catch (error) { res({ status: 'error', errorMsg: error.message }); @@ -41,9 +41,9 @@ export class CashController { } @Export(ServerExports.RemoveCash) - async removeCash(req: Request, res: Response) { + async removeCash(req: Request, res: Response) { try { - await this._cashService.handleRemoveCash(req.data.source, req.data.amount); + await this._cashService.handleRemoveCash(req.source, req.data); res({ status: 'ok', data: true }); } catch (error) { res({ status: 'error', errorMsg: error.message }); From 80d89003a7b97f795b588e905b2385f9fc4b8c1d Mon Sep 17 00:00:00 2001 From: GiroudMathias Date: Sun, 12 Mar 2023 23:00:48 +0100 Subject: [PATCH 3/5] fix: addMoneyByIdentifier is toAccount --- src/server/services/account/account.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/services/account/account.service.ts b/src/server/services/account/account.service.ts index 322f948f..c6587d43 100644 --- a/src/server/services/account/account.service.ts +++ b/src/server/services/account/account.service.ts @@ -637,7 +637,7 @@ export class AccountService { { amount, message, - fromAccount: account?.toJSON(), + toAccount: account?.toJSON(), type: TransactionType.Incoming, }, t, @@ -668,7 +668,7 @@ export class AccountService { { amount, message, - fromAccount: account?.toJSON(), + toAccount: account?.toJSON(), type: TransactionType.Incoming, }, t, From 7031905a4e90317e038f5b489f17ef4d7f0da458 Mon Sep 17 00:00:00 2001 From: GiroudMathias Date: Sun, 12 Mar 2023 23:01:19 +0100 Subject: [PATCH 4/5] feat: allow fromIdentifier on addBankBalance --- src/server/services/account/account.controller.ts | 5 ++++- src/server/services/account/account.service.ts | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/server/services/account/account.controller.ts b/src/server/services/account/account.controller.ts index 32f1752b..bb94f321 100644 --- a/src/server/services/account/account.controller.ts +++ b/src/server/services/account/account.controller.ts @@ -220,7 +220,10 @@ export class AccountController { } @Export(ServerExports.AddBankBalance) - async addBankBalance(req: Request<{ amount: number; message: string }>, res: Response) { + async addBankBalance( + req: Request<{ amount: number; message: string; fromIdentifier?: string }>, + res: Response, + ) { try { await this._accountService.addMoney(req); res({ status: 'ok', data: {} }); diff --git a/src/server/services/account/account.service.ts b/src/server/services/account/account.service.ts index c6587d43..35b28cb1 100644 --- a/src/server/services/account/account.service.ts +++ b/src/server/services/account/account.service.ts @@ -585,7 +585,7 @@ export class AccountService { async addMoney(req: Request) { logger.silly(`Adding money to ${req.source} ..`); - const { amount, message } = req.data; + const { amount, message, fromIdentifier } = req.data; if (amount <= 0) { throw new ServerError(GenericErrors.BadInput); @@ -595,17 +595,27 @@ export class AccountService { const t = await sequelize.transaction(); try { + let fromAccount = undefined; const account = await this._accountDB.getDefaultAccountByIdentifier(user.getIdentifier()); if (!account) { throw new ServerError(GenericErrors.NotFound); } + if (fromIdentifier) { + logger.silly(`Adding money from ${fromIdentifier} ..`); + fromAccount = await this._accountDB.getDefaultAccountByIdentifier(fromIdentifier); + if (!fromAccount) { + throw new ServerError(GenericErrors.NotFound); + } + await this._accountDB.decrement(fromAccount, amount, t); + } await this._accountDB.increment(account, amount, t); await this._transactionService.handleCreateTransaction( { amount, message, + fromAccount: fromAccount?.toJSON(), toAccount: account?.toJSON(), type: TransactionType.Incoming, }, From b5d0c08f750589c1ff656a15985d5c2997da8b61 Mon Sep 17 00:00:00 2001 From: GiroudMathias Date: Sun, 12 Mar 2023 23:12:44 +0100 Subject: [PATCH 5/5] feat: allow toIdentifier on addBankBalance and addBankBalanceByIdentifie --- .../services/account/account.controller.ts | 2 +- .../services/account/account.service.ts | 39 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/server/services/account/account.controller.ts b/src/server/services/account/account.controller.ts index bb94f321..d75b89e7 100644 --- a/src/server/services/account/account.controller.ts +++ b/src/server/services/account/account.controller.ts @@ -303,7 +303,7 @@ export class AccountController { @Export(ServerExports.RemoveBankBalance) async removeBankBalance( - req: Request<{ amount: number; message: string }>, + req: Request<{ amount: number; message: string; toIdentifier?: string }>, res: Response, ) { try { diff --git a/src/server/services/account/account.service.ts b/src/server/services/account/account.service.ts index 35b28cb1..176de594 100644 --- a/src/server/services/account/account.service.ts +++ b/src/server/services/account/account.service.ts @@ -629,24 +629,35 @@ export class AccountService { async addMoneyByIdentifier(req: Request) { logger.silly(`Adding money by identifier to ${req.data.identifier} ..`); - const { amount, message, identifier } = req.data; + const { amount, message, identifier, fromIdentifier } = req.data; if (amount <= 0) { throw new ServerError(GenericErrors.BadInput); } const t = await sequelize.transaction(); try { + let fromAccount = undefined; const account = await this._accountDB.getDefaultAccountByIdentifier(identifier ?? ''); if (!account) { throw new ServerError(GenericErrors.NotFound); } + if (fromIdentifier) { + logger.silly(`Adding money from ${fromIdentifier} ..`); + fromAccount = await this._accountDB.getDefaultAccountByIdentifier(fromIdentifier); + if (!fromAccount) { + throw new ServerError(GenericErrors.NotFound); + } + await this._accountDB.decrement(fromAccount, amount, t); + } + await this._accountDB.increment(account, amount, t); await this._transactionService.handleCreateTransaction( { amount, message, + fromAccount: fromAccount?.toJSON(), toAccount: account?.toJSON(), type: TransactionType.Incoming, }, @@ -690,7 +701,7 @@ export class AccountService { } async removeMoney(req: Request) { - const { amount, message } = req.data; + const { amount, message, toIdentifier } = req.data; logger.silly(`Removing ${amount} money from ${req.source}...`); if (amount <= 0) { @@ -701,11 +712,21 @@ export class AccountService { const t = await sequelize.transaction(); try { + let toAccount = undefined; const account = await this._accountDB.getDefaultAccountByIdentifier(user.getIdentifier()); if (!account) { throw new ServerError(GenericErrors.NotFound); } + if (toIdentifier) { + logger.silly(`Adding money to ${toIdentifier} ..`); + toAccount = await this._accountDB.getDefaultAccountByIdentifier(toIdentifier); + if (!toAccount) { + throw new ServerError(GenericErrors.NotFound); + } + await this._accountDB.decrement(toAccount, amount, t); + } + await account.update( { balance: account.getDataValue('balance') - amount, @@ -718,6 +739,7 @@ export class AccountService { amount: amount, message: message, fromAccount: account?.toJSON(), + toAccount: toAccount?.toJSON(), type: TransactionType.Outgoing, }, t, @@ -729,7 +751,7 @@ export class AccountService { } async removeMoneyByIdentifier(req: Request) { - const { amount, identifier, message } = req.data; + const { amount, identifier, message, toIdentifier } = req.data; logger.silly(`Removing ${amount} money by identifier from ${identifier} ..`); if (amount <= 0) { @@ -738,17 +760,28 @@ export class AccountService { const t = await sequelize.transaction(); try { + let toAccount = undefined; const account = await this._accountDB.getDefaultAccountByIdentifier(identifier ?? ''); if (!account) { throw new ServerError(GenericErrors.NotFound); } + if (toIdentifier) { + logger.silly(`Adding money to ${toIdentifier} ..`); + toAccount = await this._accountDB.getDefaultAccountByIdentifier(toIdentifier); + if (!toAccount) { + throw new ServerError(GenericErrors.NotFound); + } + await this._accountDB.decrement(toAccount, amount, t); + } + await this._accountDB.decrement(account, amount, t); await this._transactionService.handleCreateTransaction( { amount, message, fromAccount: account?.toJSON(), + toAccount: toAccount?.toJSON(), type: TransactionType.Outgoing, }, t,