feat: retrieve the matching transactions

This commit is contained in:
Ahmed Bouhuolia
2024-06-20 10:20:18 +02:00
parent d3230767dd
commit b6deb842ff
11 changed files with 507 additions and 149 deletions

View File

@@ -2,20 +2,32 @@ import { Inject, Service } from 'typedi';
import * as R from 'ramda';
import { PromisePool } from '@supercharge/promise-pool';
import { GetMatchedTransactionsFilter } from './types';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { GetMatchedTransactionInvoicesTransformer } from './GetMatchedTransactionInvoicesTransformer';
import { GetMatchedTransactionBillsTransformer } from './GetMatchedTransactionBillsTransformer';
import { GetMatchedTransactionExpensesTransformer } from './GetMatchedTransactionExpensesTransformer';
import { GetMatchedTransactionManualJournalsTransformer } from './GetMatchedTransactionManualJournalsTransformer';
import { GetMatchedTransactionsByExpenses } from './GetMatchedTransactionsByExpenses';
import { GetMatchedTransactionsByBills } from './GetMatchedTransactionsByBills';
import { GetMatchedTransactionsByManualJournals } from './GetMatchedTransactionsByManualJournals';
@Service()
export class GetMatchedTransactions {
@Inject()
private tenancy: HasTenancyService;
private getMatchedInvoicesService: GetMatchedTransactionsByExpenses;
@Inject()
private transformer: TransformerInjectable;
private getMatchedBillsService: GetMatchedTransactionsByBills;
@Inject()
private getMatchedManualJournalService: GetMatchedTransactionsByManualJournals;
@Inject()
private getMatchedExpensesService: GetMatchedTransactionsByExpenses;
get registered() {
return [
{ type: 'SaleInvoice', service: this.getMatchedInvoicesService },
{ type: 'Bill', service: this.getMatchedBillsService },
{ type: 'Expense', service: this.getMatchedExpensesService },
{ type: 'ManualJournal', service: this.getMatchedManualJournalService },
];
}
/**
* Retrieves the matched transactions.
@@ -26,111 +38,17 @@ export class GetMatchedTransactions {
tenantId: number,
filter: GetMatchedTransactionsFilter
) {
const registered = [
{
type: 'SaleInvoice',
callback: this.getSaleInvoicesMatchedTransactions.bind(this),
},
{
type: 'Bill',
callback: this.getBillsMatchedTransactions.bind(this),
},
{
type: 'Expense',
callback: this.getExpensesMatchedTransactions.bind(this),
},
{
type: 'ManualJournal',
callback: this.getManualJournalsMatchedTransactions.bind(this),
},
];
const filtered = filter.transactionType
? registered.filter((item) => item.type === filter.transactionType)
: registered;
? this.registered.filter((item) => item.type === filter.transactionType)
: this.registered;
const matchedTransactions = await PromisePool.withConcurrency(2)
.for(filtered)
.process(async ({ type, callback }) => {
return callback(tenantId, filter);
.process(async ({ type, service }) => {
return service.getMatchedTransactions(tenantId, filter);
});
return R.compose(R.flatten)(matchedTransactions?.results);
}
/**
*
* @param {number} tenantId -
* @param {GetMatchedTransactionsFilter} filter -
*/
async getSaleInvoicesMatchedTransactions(
tenantId: number,
filter: GetMatchedTransactionsFilter
) {
const { SaleInvoice } = this.tenancy.models(tenantId);
const invoices = await SaleInvoice.query();
return this.transformer.transform(
tenantId,
invoices,
new GetMatchedTransactionInvoicesTransformer()
);
}
/**
*
* @param {number} tenantId -
* @param {GetMatchedTransactionsFilter} filter -
*/
async getBillsMatchedTransactions(
tenantId: number,
filter: GetMatchedTransactionsFilter
) {
const { Bill } = this.tenancy.models(tenantId);
const bills = await Bill.query();
return this.transformer.transform(
tenantId,
bills,
new GetMatchedTransactionBillsTransformer()
);
}
/**
*
* @param {number} tenantId
* @param {GetMatchedTransactionsFilter} filter
* @returns
*/
async getExpensesMatchedTransactions(
tenantId: number,
filter: GetMatchedTransactionsFilter
) {
const { Expense } = this.tenancy.models(tenantId);
const expenses = await Expense.query();
return this.transformer.transform(
tenantId,
expenses,
new GetMatchedTransactionManualJournalsTransformer()
);
}
async getManualJournalsMatchedTransactions(
tenantId: number,
filter: GetMatchedTransactionsFilter
) {
const { ManualJournal } = this.tenancy.models(tenantId);
const manualJournals = await ManualJournal.query();
return this.transformer.transform(
tenantId,
manualJournals,
new GetMatchedTransactionManualJournalsTransformer()
);
}
}
interface MatchedTransaction {