feat: matching uncategorized transactions

This commit is contained in:
Ahmed Bouhuolia
2024-06-19 22:40:10 +02:00
parent 6c4b0cdac5
commit d3230767dd
15 changed files with 672 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
import { PromisePool } from '@supercharge/promise-pool';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import UnitOfWork from '@/services/UnitOfWork';
import events from '@/subscribers/events';
import { Knex } from 'knex';
import { Inject, Service } from 'typedi';
import {
IBankTransactionMatchedEventPayload,
IBankTransactionMatchingEventPayload,
IMatchTransactionDTO,
} from './types';
@Service()
export class MatchBankTransactions {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private uow: UnitOfWork;
@Inject()
private eventPublisher: EventPublisher;
/**
* Matches the given uncategorized transaction to the given references.
* @param {number} tenantId
* @param {number} uncategorizedTransactionId
*/
public matchTransaction(
tenantId: number,
uncategorizedTransactionId: number,
matchTransactionsDTO: IMatchTransactionDTO
) {
const { matchedTransactions } = matchTransactionsDTO;
const { MatchBankTransaction } = this.tenancy.models(tenantId);
//
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers the event `onSaleInvoiceCreated`.
await this.eventPublisher.emitAsync(events.bankMatch.onMatching, {
tenantId,
uncategorizedTransactionId,
matchTransactionsDTO,
trx,
} as IBankTransactionMatchingEventPayload);
//
await PromisePool.withConcurrency(10)
.for(matchedTransactions)
.process(async (matchedTransaction) => {
await MatchBankTransaction.query(trx).insert({
uncategorizedTransactionId,
referenceType: matchedTransaction.referenceType,
referenceId: matchedTransaction.referenceId,
amount: matchedTransaction.amount,
});
});
// Triggers the event `onSaleInvoiceCreated`.
await this.eventPublisher.emitAsync(events.bankMatch.onMatched, {
tenantId,
uncategorizedTransactionId,
matchTransactionsDTO,
trx,
} as IBankTransactionMatchedEventPayload);
});
}
}