mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
fix: group matches to get possible and perfect matches
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import * as R from 'ramda';
|
||||
import moment from 'moment';
|
||||
import { PromisePool } from '@supercharge/promise-pool';
|
||||
import { GetMatchedTransactionsFilter, MatchedTransactionsPOJO } from './types';
|
||||
import { GetMatchedTransactionsByExpenses } from './GetMatchedTransactionsByExpenses';
|
||||
import { GetMatchedTransactionsByBills } from './GetMatchedTransactionsByBills';
|
||||
import { GetMatchedTransactionsByManualJournals } from './GetMatchedTransactionsByManualJournals';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
|
||||
@Service()
|
||||
export class GetMatchedTransactions {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private getMatchedInvoicesService: GetMatchedTransactionsByExpenses;
|
||||
|
||||
@@ -36,11 +41,20 @@ export class GetMatchedTransactions {
|
||||
* Retrieves the matched transactions.
|
||||
* @param {number} tenantId -
|
||||
* @param {GetMatchedTransactionsFilter} filter -
|
||||
* @returns {Promise<MatchedTransactionsPOJO>}
|
||||
*/
|
||||
public async getMatchedTransactions(
|
||||
tenantId: number,
|
||||
uncategorizedTransactionId: number,
|
||||
filter: GetMatchedTransactionsFilter
|
||||
): Promise<MatchedTransactionsPOJO> {
|
||||
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
||||
|
||||
const uncategorizedTransaction =
|
||||
await UncategorizedCashflowTransaction.query()
|
||||
.findById(uncategorizedTransactionId)
|
||||
.throwIfNotFound();
|
||||
|
||||
const filtered = filter.transactionType
|
||||
? this.registered.filter((item) => item.type === filter.transactionType)
|
||||
: this.registered;
|
||||
@@ -50,6 +64,39 @@ export class GetMatchedTransactions {
|
||||
.process(async ({ type, service }) => {
|
||||
return service.getMatchedTransactions(tenantId, filter);
|
||||
});
|
||||
return R.compose(R.flatten)(matchedTransactions?.results);
|
||||
|
||||
const { perfectMatches, possibleMatches } = this.groupMatchedResults(
|
||||
uncategorizedTransaction,
|
||||
matchedTransactions
|
||||
);
|
||||
return {
|
||||
perfectMatches,
|
||||
possibleMatches,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Groups the given results for getting perfect and possible matches
|
||||
* based on the given uncategorized transaction.
|
||||
* @param uncategorizedTransaction
|
||||
* @param matchedTransactions
|
||||
* @returns {MatchedTransactionsPOJO}
|
||||
*/
|
||||
private groupMatchedResults(
|
||||
uncategorizedTransaction,
|
||||
matchedTransactions
|
||||
): MatchedTransactionsPOJO {
|
||||
const results = R.compose(R.flatten)(matchedTransactions?.results);
|
||||
|
||||
const perfectMatches = R.filter(
|
||||
(match) =>
|
||||
match.amount === uncategorizedTransaction.amount &&
|
||||
moment(match.date).isSame(uncategorizedTransaction.date, 'day'),
|
||||
results
|
||||
);
|
||||
|
||||
const possibleMatches = R.difference(results, perfectMatches);
|
||||
|
||||
return { perfectMatches, possibleMatches };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,10 +23,12 @@ export class MatchBankTransactionsApplication {
|
||||
*/
|
||||
public getMatchedTransactions(
|
||||
tenantId: number,
|
||||
uncategorizedTransactionId: number,
|
||||
filter: GetMatchedTransactionsFilter
|
||||
) {
|
||||
return this.getMatchedTransactionsService.getMatchedTransactions(
|
||||
tenantId,
|
||||
uncategorizedTransactionId,
|
||||
filter
|
||||
);
|
||||
}
|
||||
|
||||
@@ -49,7 +49,10 @@ export interface MatchedTransactionPOJO {
|
||||
transactionId: number;
|
||||
}
|
||||
|
||||
export type MatchedTransactionsPOJO = Array<MatchedTransactionPOJO[]>;
|
||||
export type MatchedTransactionsPOJO = {
|
||||
perfectMatches: Array<MatchedTransactionPOJO[]>;
|
||||
possibleMatches: Array<MatchedTransactionPOJO[]>;
|
||||
};
|
||||
|
||||
export const ERRORS = {
|
||||
RESOURCE_TYPE_MATCHING_TRANSACTION_INVALID:
|
||||
@@ -59,5 +62,5 @@ export const ERRORS = {
|
||||
TOTAL_MATCHING_TRANSACTIONS_INVALID: 'TOTAL_MATCHING_TRANSACTIONS_INVALID',
|
||||
TRANSACTION_ALREADY_MATCHED: 'TRANSACTION_ALREADY_MATCHED',
|
||||
CANNOT_MATCH_EXCLUDED_TRANSACTION: 'CANNOT_MATCH_EXCLUDED_TRANSACTION',
|
||||
CANNOT_DELETE_TRANSACTION_MATCHED: 'CANNOT_DELETE_TRANSACTION_MATCHED'
|
||||
CANNOT_DELETE_TRANSACTION_MATCHED: 'CANNOT_DELETE_TRANSACTION_MATCHED',
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user