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 'payee':
default:
return 'unknown';
return 'text';
}
};

View File

@@ -1,4 +1,5 @@
import { Inject, Service } from 'typedi';
import { isEqual, omit } from 'lodash';
import events from '@/subscribers/events';
import {
IBankRuleEventCreatedPayload,
@@ -55,10 +56,22 @@ export class TriggerRecognizedTransactions {
private async recognizedTransactionsOnRuleEdited({
tenantId,
editRuleDTO,
oldBankRule,
bankRule,
ruleId,
}: IBankRuleEventEditedPayload) {
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(
'rerecognize-uncategorized-transactions-job',
payload

View File

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

View File

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

View File

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

View File

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