diff --git a/packages/server/src/interfaces/CashflowService.ts b/packages/server/src/interfaces/CashflowService.ts index 2174c2390..c1f55a86f 100644 --- a/packages/server/src/interfaces/CashflowService.ts +++ b/packages/server/src/interfaces/CashflowService.ts @@ -130,7 +130,7 @@ export interface ICommandCashflowDeletedPayload { export interface ICashflowTransactionCategorizedPayload { tenantId: number; - uncategorizedTransactions: any; + uncategorizedTransactions: Array; cashflowTransaction: ICashflowTransaction; oldUncategorizedTransactions: Array; categorizeDTO: any; diff --git a/packages/server/src/models/UncategorizedCashflowTransaction.ts b/packages/server/src/models/UncategorizedCashflowTransaction.ts index 029825583..2f3f0a50e 100644 --- a/packages/server/src/models/UncategorizedCashflowTransaction.ts +++ b/packages/server/src/models/UncategorizedCashflowTransaction.ts @@ -31,7 +31,7 @@ export default class UncategorizedCashflowTransaction extends mixin( /** * Timestamps columns. */ - static get timestamps() { + get timestamps() { return ['createdAt', 'updatedAt']; } diff --git a/packages/server/src/services/Banking/Matching/events/DecrementUncategorizedTransactionsOnMatch.ts b/packages/server/src/services/Banking/Matching/events/DecrementUncategorizedTransactionsOnMatch.ts index 8acda3d4d..5e37d71d1 100644 --- a/packages/server/src/services/Banking/Matching/events/DecrementUncategorizedTransactionsOnMatch.ts +++ b/packages/server/src/services/Banking/Matching/events/DecrementUncategorizedTransactionsOnMatch.ts @@ -37,12 +37,13 @@ export class DecrementUncategorizedTransactionOnMatching { const { UncategorizedCashflowTransaction, Account } = this.tenancy.models(tenantId); - const transactions = await UncategorizedCashflowTransaction.query().whereIn( - 'id', - uncategorizedTransactionIds - ); + const uncategorizedTransactions = + await UncategorizedCashflowTransaction.query().whereIn( + 'id', + uncategorizedTransactionIds + ); await PromisePool.withConcurrency(1) - .for(transactions) + .for(uncategorizedTransactions) .process(async (transaction) => { await Account.query(trx) .findById(transaction.accountId) diff --git a/packages/server/src/services/Cashflow/UncategorizeCashflowTransaction.ts b/packages/server/src/services/Cashflow/UncategorizeCashflowTransaction.ts index 9d9c9b359..963bc80a2 100644 --- a/packages/server/src/services/Cashflow/UncategorizeCashflowTransaction.ts +++ b/packages/server/src/services/Cashflow/UncategorizeCashflowTransaction.ts @@ -75,7 +75,7 @@ export class UncategorizeCashflowTransaction { categorizeRefType: null, }); const uncategorizedTransactions = - await UncategorizedCashflowTransaction.query().whereIn( + await UncategorizedCashflowTransaction.query(trx).whereIn( 'id', oldUncategoirzedTransactionsIds ); diff --git a/packages/server/src/services/Cashflow/subscribers/DecrementUncategorizedTransactionOnCategorize.ts b/packages/server/src/services/Cashflow/subscribers/DecrementUncategorizedTransactionOnCategorize.ts index cf610b582..a1570a390 100644 --- a/packages/server/src/services/Cashflow/subscribers/DecrementUncategorizedTransactionOnCategorize.ts +++ b/packages/server/src/services/Cashflow/subscribers/DecrementUncategorizedTransactionOnCategorize.ts @@ -5,6 +5,7 @@ import { ICashflowTransactionCategorizedPayload, ICashflowTransactionUncategorizedPayload, } from '@/interfaces'; +import PromisePool from '@supercharge/promise-pool'; @Service() export class DecrementUncategorizedTransactionOnCategorize { @@ -35,13 +36,17 @@ export class DecrementUncategorizedTransactionOnCategorize { public async decrementUnCategorizedTransactionsOnCategorized({ tenantId, uncategorizedTransactions, + trx }: ICashflowTransactionCategorizedPayload) { const { Account } = this.tenancy.models(tenantId); - const accountIds = uncategorizedTransactions.map((a) => a.id); - await Account.query() - .whereIn('id', accountIds) - .decrement('uncategorizedTransactions', 1); + await PromisePool.withConcurrency(1) + .for(uncategorizedTransactions) + .process(async (uncategorizedTransaction) => { + await Account.query(trx) + .findById(uncategorizedTransaction.accountId) + .decrement('uncategorizedTransactions', 1); + }); } /** @@ -51,15 +56,17 @@ export class DecrementUncategorizedTransactionOnCategorize { public async incrementUnCategorizedTransactionsOnUncategorized({ tenantId, uncategorizedTransactions, + trx }: ICashflowTransactionUncategorizedPayload) { const { Account } = this.tenancy.models(tenantId); - const uncategorizedTransactionIds = uncategorizedTransactions?.map( - (t) => t.id - ); - await Account.query() - .whereIn('id', uncategorizedTransactionIds) - .increment('uncategorizedTransactions', 1); + await PromisePool.withConcurrency(1) + .for(uncategorizedTransactions) + .process(async (uncategorizedTransaction) => { + await Account.query(trx) + .findById(uncategorizedTransaction.accountId) + .increment('uncategorizedTransactions', 1); + }); } /** diff --git a/packages/server/src/services/Cashflow/subscribers/DeleteCashflowTransactionOnUncategorize.ts b/packages/server/src/services/Cashflow/subscribers/DeleteCashflowTransactionOnUncategorize.ts index 95a41e88a..4bbdc3fac 100644 --- a/packages/server/src/services/Cashflow/subscribers/DeleteCashflowTransactionOnUncategorize.ts +++ b/packages/server/src/services/Cashflow/subscribers/DeleteCashflowTransactionOnUncategorize.ts @@ -45,7 +45,7 @@ export class DeleteCashflowTransactionOnUncategorize { trx ); }); - if (result.errors) { + if (result.errors.length > 0) { throw new ServiceError('SOMETHING_WRONG'); } }