feat: get bank account meta summary

This commit is contained in:
Ahmed Bouhuolia
2024-07-02 19:21:26 +02:00
parent 8a09de9771
commit 91730d204e
22 changed files with 476 additions and 69 deletions

View File

@@ -0,0 +1,53 @@
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { Server } from 'socket.io';
import { Inject, Service } from 'typedi';
@Service()
export class GetBankAccountSummary {
@Inject()
private tenancy: HasTenancyService;
/**
* Retrieves the bank account meta summary
* @param {number} tenantId
* @param {number} bankAccountId
* @returns
*/
public async getBankAccountSummary(tenantId: number, bankAccountId: number) {
const {
Account,
UncategorizedCashflowTransaction,
RecognizedBankTransaction,
} = this.tenancy.models(tenantId);
const bankAccount = await Account.query()
.findById(bankAccountId)
.throwIfNotFound();
const uncategorizedTranasctionsCount =
await UncategorizedCashflowTransaction.query()
.where('accountId', bankAccountId)
.count('id as total')
.first();
const recognizedTransactionsCount = await RecognizedBankTransaction.query()
.whereExists(
UncategorizedCashflowTransaction.query().where(
'accountId',
bankAccountId
)
)
.count('id as total')
.first();
const totalUncategorizedTransactions =
uncategorizedTranasctionsCount?.total;
const totalRecognizedTransactions = recognizedTransactionsCount?.total;
return {
name: bankAccount.name,
totalUncategorizedTransactions,
totalRecognizedTransactions,
};
}
}