fix: improvements to bank matching transactions

This commit is contained in:
Ahmed Bouhuolia
2024-07-06 19:10:07 +02:00
parent cd9039fe16
commit b7487f19d3
18 changed files with 188 additions and 233 deletions

View File

@@ -110,6 +110,7 @@ import { ValidateMatchingOnPaymentMadeDelete } from '@/services/Banking/Matching
import { ValidateMatchingOnCashflowDelete } from '@/services/Banking/Matching/events/ValidateMatchingOnCashflowDelete';
import { RecognizeSyncedBankTranasctions } from '@/services/Banking/Plaid/subscribers/RecognizeSyncedBankTransactions';
import { UnlinkBankRuleOnDeleteBankRule } from '@/services/Banking/Rules/events/UnlinkBankRuleOnDeleteBankRule';
import { DecrementUncategorizedTransactionOnMatching } from '@/services/Banking/Matching/events/DecrementUncategorizedTransactionsOnMatch';
export default () => {
return new EventPublisher();
@@ -258,6 +259,7 @@ export const susbcribers = () => {
// Bank Rules
TriggerRecognizedTransactions,
UnlinkBankRuleOnDeleteBankRule,
DecrementUncategorizedTransactionOnMatching,
// Validate matching
ValidateMatchingOnCashflowDelete,
@@ -266,7 +268,7 @@ export const susbcribers = () => {
ValidateMatchingOnPaymentReceivedDelete,
ValidateMatchingOnPaymentMadeDelete,
// Plaid
// Plaid
RecognizeSyncedBankTranasctions,
];
};

View File

@@ -105,8 +105,34 @@ export default class UncategorizedCashflowTransaction extends mixin(
* Filters the excluded transactions.
*/
excluded(query) {
query.whereNotNull('excluded_at')
}
query.whereNotNull('excluded_at');
},
/**
* Filter out the recognized transactions.
* @param query
*/
recognized(query) {
query.whereNotNull('recognizedTransactionId');
},
/**
* Filter out the not recognized transactions.
* @param query
*/
notRecognized(query) {
query.whereNull('recognizedTransactionId');
},
categorized(query) {
query.whereNotNull('categorizeRefType');
query.whereNotNull('categorizeRefId');
},
notCategorized(query) {
query.whereNull('categorizeRefType');
query.whereNull('categorizeRefId');
},
};
}

View File

@@ -1,6 +1,6 @@
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { Server } from 'socket.io';
import { Inject, Service } from 'typedi';
import { initialize } from 'objection';
import HasTenancyService from '@/services/Tenancy/TenancyService';
@Service()
export class GetBankAccountSummary {
@@ -14,22 +14,41 @@ export class GetBankAccountSummary {
* @returns
*/
public async getBankAccountSummary(tenantId: number, bankAccountId: number) {
const knex = this.tenancy.knex(tenantId);
const {
Account,
UncategorizedCashflowTransaction,
RecognizedBankTransaction,
} = this.tenancy.models(tenantId);
await initialize(knex, [
UncategorizedCashflowTransaction,
RecognizedBankTransaction,
]);
const bankAccount = await Account.query()
.findById(bankAccountId)
.throwIfNotFound();
// Retrieves the uncategorized transactions count of the given bank account.
const uncategorizedTranasctionsCount =
await UncategorizedCashflowTransaction.query()
.where('accountId', bankAccountId)
.count('id as total')
.first();
await UncategorizedCashflowTransaction.query().onBuild((q) => {
// Include just the given account.
q.where('accountId', bankAccountId);
// Only the not excluded.
q.modify('notExcluded');
// Only the not categorized.
q.modify('notCategorized');
// Only the not matched bank transactions.
q.withGraphJoined('matchedBankTransactions');
q.whereNull('matchedBankTransactions.id');
// Count the results.
q.count('uncategorized_cashflow_transactions.id as total');
q.first();
});
// Retrieves the recognized transactions count of the given bank account.
const recognizedTransactionsCount = await RecognizedBankTransaction.query()
@@ -43,8 +62,8 @@ export class GetBankAccountSummary {
.first();
const totalUncategorizedTransactions =
uncategorizedTranasctionsCount?.total;
const totalRecognizedTransactions = recognizedTransactionsCount?.total;
uncategorizedTranasctionsCount?.total || 0;
const totalRecognizedTransactions = recognizedTransactionsCount?.total || 0;
return {
name: bankAccount.name,

View File

@@ -31,6 +31,7 @@ export class UnmatchMatchedBankTransaction {
return this.uow.withTransaction(tenantId, async (trx) => {
await this.eventPublisher.emitAsync(events.bankMatch.onUnmatching, {
tenantId,
uncategorizedTransactionId,
trx,
} as IBankTransactionUnmatchingEventPayload);
@@ -40,6 +41,7 @@ export class UnmatchMatchedBankTransaction {
await this.eventPublisher.emitAsync(events.bankMatch.onUnmatched, {
tenantId,
uncategorizedTransactionId,
trx,
} as IBankTransactionUnmatchingEventPayload);
});

View File

@@ -1,6 +1,7 @@
import { Knex } from 'knex';
import { Inject, Service } from 'typedi';
import { ServiceError } from '@/exceptions';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { Inject, Service } from 'typedi';
import { ERRORS } from './types';
@Service()
@@ -18,12 +19,13 @@ export class ValidateTransactionMatched {
public async validateTransactionNoMatchLinking(
tenantId: number,
referenceType: string,
referenceId: number
referenceId: number,
trx?: Knex.Transaction
) {
const { MatchedBankTransaction } = this.tenancy.models(tenantId);
const foundMatchedTransaction =
await MatchedBankTransaction.query().findOne({
await MatchedBankTransaction.query(trx).findOne({
referenceType,
referenceId,
});

View File

@@ -0,0 +1,69 @@
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import {
IBankTransactionMatchedEventPayload,
IBankTransactionUnmatchedEventPayload,
} from '../types';
import HasTenancyService from '@/services/Tenancy/TenancyService';
@Service()
export class DecrementUncategorizedTransactionOnMatching {
@Inject()
private tenancy: HasTenancyService;
/**
* Constructor method.
*/
public attach(bus) {
bus.subscribe(
events.bankMatch.onMatched,
this.decrementUnCategorizedTransactionsOnMatching.bind(this)
);
bus.subscribe(
events.bankMatch.onUnmatched,
this.incrementUnCategorizedTransactionsOnUnmatching.bind(this)
);
}
/**
* Validates the cashflow transaction whether matched with bank transaction on deleting.
* @param {IManualJournalDeletingPayload}
*/
public async decrementUnCategorizedTransactionsOnMatching({
tenantId,
uncategorizedTransactionId,
matchTransactionsDTO,
trx,
}: IBankTransactionMatchedEventPayload) {
const { UncategorizedCashflowTransaction, Account } =
this.tenancy.models(tenantId);
const transaction = await UncategorizedCashflowTransaction.query().findById(
uncategorizedTransactionId
);
//
await Account.query(trx)
.findById(transaction.accountId)
.decrement('uncategorizedTransactions', 1);
}
/**
* Validates the cashflow transaction whether matched with bank transaction on deleting.
* @param {IManualJournalDeletingPayload}
*/
public async incrementUnCategorizedTransactionsOnUnmatching({
tenantId,
uncategorizedTransactionId,
trx,
}: IBankTransactionUnmatchedEventPayload) {
const { UncategorizedCashflowTransaction, Account } =
this.tenancy.models(tenantId);
const transaction = await UncategorizedCashflowTransaction.query().findById(
uncategorizedTransactionId
);
//
await Account.query(trx)
.findById(transaction.accountId)
.decrement('uncategorizedTransactions', 1);
}
}

View File

@@ -1,5 +1,5 @@
import { Inject, Service } from 'typedi';
import { IManualJournalDeletingPayload } from '@/interfaces';
import { ICommandCashflowDeletingPayload, IManualJournalDeletingPayload } from '@/interfaces';
import events from '@/subscribers/events';
import { ValidateTransactionMatched } from '../ValidateTransactionsMatched';
@@ -24,13 +24,14 @@ export class ValidateMatchingOnCashflowDelete {
*/
public async validateMatchingOnCashflowDeleting({
tenantId,
oldManualJournal,
oldCashflowTransaction,
trx,
}: IManualJournalDeletingPayload) {
}: ICommandCashflowDeletingPayload) {
await this.validateNoMatchingLinkedService.validateTransactionNoMatchLinking(
tenantId,
'ManualJournal',
oldManualJournal.id
'CashflowTransaction',
oldCashflowTransaction.id,
trx
);
}
}

View File

@@ -30,7 +30,8 @@ export class ValidateMatchingOnExpenseDelete {
await this.validateNoMatchingLinkedService.validateTransactionNoMatchLinking(
tenantId,
'Expense',
oldExpense.id
oldExpense.id,
trx
);
}
}

View File

@@ -30,7 +30,8 @@ export class ValidateMatchingOnManualJournalDelete {
await this.validateNoMatchingLinkedService.validateTransactionNoMatchLinking(
tenantId,
'ManualJournal',
oldManualJournal.id
oldManualJournal.id,
trx
);
}
}

View File

@@ -33,7 +33,8 @@ export class ValidateMatchingOnPaymentMadeDelete {
await this.validateNoMatchingLinkedService.validateTransactionNoMatchLinking(
tenantId,
'PaymentMade',
oldBillPayment.id
oldBillPayment.id,
trx
);
}
}

View File

@@ -30,7 +30,8 @@ export class ValidateMatchingOnPaymentReceivedDelete {
await this.validateNoMatchingLinkedService.validateTransactionNoMatchLinking(
tenantId,
'PaymentReceive',
oldPaymentReceive.id
oldPaymentReceive.id,
trx
);
}
}

View File

@@ -16,10 +16,14 @@ export interface IBankTransactionMatchedEventPayload {
export interface IBankTransactionUnmatchingEventPayload {
tenantId: number;
uncategorizedTransactionId: number;
trx?: Knex.Transaction;
}
export interface IBankTransactionUnmatchedEventPayload {
tenantId: number;
uncategorizedTransactionId: number;
trx?: Knex.Transaction;
}
export interface IMatchTransactionDTO {