feat: auto fill categorize form from recognized transaction

This commit is contained in:
Ahmed Bouhuolia
2024-07-01 15:48:40 +02:00
parent 79616cf1eb
commit c95eec565d
9 changed files with 212 additions and 26 deletions

View File

@@ -0,0 +1,22 @@
import moment from 'moment';
import * as R from 'ramda';
import UncategorizedCashflowTransaction from '@/models/UncategorizedCashflowTransaction';
import { MatchedTransactionPOJO } from './types';
export const sortClosestMatchTransactions = (
uncategorizedTransaction: UncategorizedCashflowTransaction,
matches: MatchedTransactionPOJO[]
) => {
return R.sortWith([
// Sort by amount difference (closest to uncategorized transaction amount first)
R.ascend((match: MatchedTransactionPOJO) =>
Math.abs(match.amount - uncategorizedTransaction.amount)
),
// Sort by date difference (closest to uncategorized transaction date first)
R.ascend((match: MatchedTransactionPOJO) =>
Math.abs(
moment(match.date).diff(moment(uncategorizedTransaction.date), 'days')
)
),
])(matches);
};

View File

@@ -20,6 +20,7 @@ import NewCashflowTransactionService from './NewCashflowTransactionService';
import GetCashflowAccountsService from './GetCashflowAccountsService';
import { GetCashflowTransactionService } from './GetCashflowTransactionsService';
import { GetRecognizedTransactionsService } from './GetRecongizedTransactions';
import { GetRecognizedTransactionService } from './GetRecognizedTransaction';
@Service()
export class CashflowApplication {
@@ -56,6 +57,9 @@ export class CashflowApplication {
@Inject()
private getRecognizedTranasctionsService: GetRecognizedTransactionsService;
@Inject()
private getRecognizedTransactionService: GetRecognizedTransactionService;
/**
* Creates a new cashflow transaction.
* @param {number} tenantId
@@ -234,4 +238,20 @@ export class CashflowApplication {
filter
);
}
/**
* Retrieves the recognized transaction of the given uncategorized transaction.
* @param {number} tenantId
* @param {number} uncategorizedTransactionId
* @returns
*/
public getRecognizedTransaction(
tenantId: number,
uncategorizedTransactionId: number
) {
return this.getRecognizedTransactionService.getRecognizedTransaction(
tenantId,
uncategorizedTransactionId
);
}
}

View File

@@ -0,0 +1,40 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import { GetRecognizedTransactionTransformer } from './GetRecognizedTransactionTransformer';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
@Service()
export class GetRecognizedTransactionService {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private transformer: TransformerInjectable;
/**
* Retrieves the recognized transaction of the given uncategorized transaction.
* @param {number} tenantId
* @param {number} uncategorizedTransactionId
*/
public async getRecognizedTransaction(
tenantId: number,
uncategorizedTransactionId: number
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
const uncategorizedTransaction =
await UncategorizedCashflowTransaction.query()
.findById(uncategorizedTransactionId)
.withGraphFetched('matchedBankTransactions')
.withGraphFetched('recognizedTransaction.assignAccount')
.withGraphFetched('recognizedTransaction.bankRule')
.withGraphFetched('account')
.throwIfNotFound();
return this.transformer.transform(
tenantId,
uncategorizedTransaction,
new GetRecognizedTransactionTransformer()
);
}
}