feat: run re-recognizing bank transactions on edit bank rule

This commit is contained in:
Ahmed Bouhuolia
2024-08-12 20:07:01 +02:00
parent 193a86cf30
commit cf4bb3007e
6 changed files with 24 additions and 8 deletions

View File

@@ -116,6 +116,6 @@ const determineFieldType = (field: string): string => {
case 'description': case 'description':
case 'payee': case 'payee':
default: default:
return 'unknown'; return 'text';
} }
}; };

View File

@@ -1,4 +1,5 @@
import { Inject, Service } from 'typedi'; import { Inject, Service } from 'typedi';
import { isEqual, omit } from 'lodash';
import events from '@/subscribers/events'; import events from '@/subscribers/events';
import { import {
IBankRuleEventCreatedPayload, IBankRuleEventCreatedPayload,
@@ -55,10 +56,22 @@ export class TriggerRecognizedTransactions {
private async recognizedTransactionsOnRuleEdited({ private async recognizedTransactionsOnRuleEdited({
tenantId, tenantId,
editRuleDTO, editRuleDTO,
oldBankRule,
bankRule,
ruleId, ruleId,
}: IBankRuleEventEditedPayload) { }: IBankRuleEventEditedPayload) {
const payload = { tenantId, ruleId }; const payload = { tenantId, ruleId };
// Cannot continue if the new and old bank rule values are the same,
// after excluding `createdAt` and `updatedAt` dates.
if (
isEqual(
omit(bankRule, ['createdAt', 'updatedAt']),
omit(oldBankRule, ['createdAt', 'updatedAt'])
)
) {
return;
}
await this.agenda.now( await this.agenda.now(
'rerecognize-uncategorized-transactions-job', 'rerecognize-uncategorized-transactions-job',
payload payload

View File

@@ -47,6 +47,7 @@ export class EditBankRuleService {
const oldBankRule = await BankRule.query() const oldBankRule = await BankRule.query()
.findById(ruleId) .findById(ruleId)
.withGraphFetched('conditions')
.throwIfNotFound(); .throwIfNotFound();
const tranformDTO = this.transformDTO(editRuleDTO); const tranformDTO = this.transformDTO(editRuleDTO);
@@ -64,15 +65,15 @@ export class EditBankRuleService {
} as IBankRuleEventEditingPayload); } as IBankRuleEventEditingPayload);
// Updates the given bank rule. // Updates the given bank rule.
await BankRule.query(trx).upsertGraphAndFetch({ const bankRule = await BankRule.query(trx).upsertGraphAndFetch({
...tranformDTO, ...tranformDTO,
id: ruleId, id: ruleId,
}); });
// Triggers `onBankRuleEdited` event. // Triggers `onBankRuleEdited` event.
await this.eventPublisher.emitAsync(events.bankRules.onEdited, { await this.eventPublisher.emitAsync(events.bankRules.onEdited, {
tenantId, tenantId,
oldBankRule, oldBankRule,
bankRule,
ruleId, ruleId,
editRuleDTO, editRuleDTO,
trx, trx,

View File

@@ -110,6 +110,8 @@ export interface IBankRuleEventEditingPayload {
export interface IBankRuleEventEditedPayload { export interface IBankRuleEventEditedPayload {
tenantId: number; tenantId: number;
ruleId: number; ruleId: number;
oldBankRule: IBankRule;
bankRule: IBankRule;
editRuleDTO: IEditBankRuleDTO; editRuleDTO: IEditBankRuleDTO;
trx?: Knex.Transaction; trx?: Knex.Transaction;
} }

View File

@@ -177,12 +177,12 @@ function RuleFormConditions() {
setFieldValue('conditions', _conditions); setFieldValue('conditions', _conditions);
}; };
const handleConditionFieldChange = (item) => { const handleConditionFieldChange = R.curry((index, item) => {
const defaultComparator = getDefaultFieldConditionByFieldKey(item.value); const defaultComparator = getDefaultFieldConditionByFieldKey(item.value);
setFieldValue(`conditions[${index}].field`, item.value); setFieldValue(`conditions[${index}].field`, item.value);
setFieldValue(`conditions[${index}].comparator`, defaultComparator); setFieldValue(`conditions[${index}].comparator`, defaultComparator);
}; });
return ( return (
<Box style={{ marginBottom: 15 }}> <Box style={{ marginBottom: 15 }}>
@@ -199,7 +199,7 @@ function RuleFormConditions() {
name={`conditions[${index}].field`} name={`conditions[${index}].field`}
items={Fields} items={Fields}
popoverProps={{ minimal: true, inline: false }} popoverProps={{ minimal: true, inline: false }}
onItemChange={handleConditionFieldChange} onItemChange={handleConditionFieldChange(index)}
fastField fastField
/> />
</FFormGroup> </FFormGroup>

View File

@@ -84,6 +84,6 @@ export const getDefaultFieldConditionByFieldKey = (fieldKey?: string) => {
case 'amount': case 'amount':
return 'bigger_or_equal'; return 'bigger_or_equal';
default: default:
return 'equals'; return 'contains';
} }
}; };