mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 15:20:34 +00:00
fix: re-recognize transactions when bank rule is edited (closes #809)
This commit is contained in:
@@ -15,8 +15,13 @@ export const RecognizeUncategorizedTransactionsJob =
|
|||||||
export const RecognizeUncategorizedTransactionsQueue =
|
export const RecognizeUncategorizedTransactionsQueue =
|
||||||
'recognize-uncategorized-transactions-queue';
|
'recognize-uncategorized-transactions-queue';
|
||||||
|
|
||||||
|
|
||||||
export interface RecognizeUncategorizedTransactionsJobPayload extends TenantJobPayload {
|
export interface RecognizeUncategorizedTransactionsJobPayload extends TenantJobPayload {
|
||||||
ruleId: number,
|
ruleId: number,
|
||||||
transactionsCriteria: any;
|
transactionsCriteria?: RecognizeTransactionsCriteria;
|
||||||
|
/**
|
||||||
|
* When true, first reverts recognized transactions before recognizing again.
|
||||||
|
* Used when a bank rule is edited to ensure transactions previously recognized
|
||||||
|
* by lower-priority rules are re-evaluated against the updated rule.
|
||||||
|
*/
|
||||||
|
shouldRevert?: boolean;
|
||||||
}
|
}
|
||||||
@@ -93,6 +93,10 @@ export class RecognizeTranasctionsService {
|
|||||||
q.whereIn('id', rulesIds);
|
q.whereIn('id', rulesIds);
|
||||||
}
|
}
|
||||||
q.withGraphFetched('conditions');
|
q.withGraphFetched('conditions');
|
||||||
|
|
||||||
|
// Order by the 'order' field to ensure higher priority rules (lower order values)
|
||||||
|
// are matched first.
|
||||||
|
q.orderBy('order', 'asc');
|
||||||
});
|
});
|
||||||
|
|
||||||
const bankRulesByAccountId = transformToMapBy(
|
const bankRulesByAccountId = transformToMapBy(
|
||||||
|
|||||||
@@ -69,10 +69,13 @@ export class TriggerRecognizedTransactionsSubscriber {
|
|||||||
const tenantPayload = await this.tenancyContect.getTenantJobPayload();
|
const tenantPayload = await this.tenancyContect.getTenantJobPayload();
|
||||||
const payload = {
|
const payload = {
|
||||||
ruleId: bankRule.id,
|
ruleId: bankRule.id,
|
||||||
|
shouldRevert: true,
|
||||||
...tenantPayload,
|
...tenantPayload,
|
||||||
} as RecognizeUncategorizedTransactionsJobPayload;
|
} as RecognizeUncategorizedTransactionsJobPayload;
|
||||||
|
|
||||||
// Re-recognize the transactions based on the new rules.
|
// Re-recognize the transactions based on the new rules.
|
||||||
|
// Setting shouldRevert to true ensures that transactions previously recognized
|
||||||
|
// by this or lower-priority rules are re-evaluated against the updated rule.
|
||||||
await this.recognizeTransactionsQueue.add(
|
await this.recognizeTransactionsQueue.add(
|
||||||
RecognizeUncategorizedTransactionsJob,
|
RecognizeUncategorizedTransactionsJob,
|
||||||
payload,
|
payload,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { Processor, WorkerHost } from '@nestjs/bullmq';
|
|||||||
import { Scope } from '@nestjs/common';
|
import { Scope } from '@nestjs/common';
|
||||||
import { ClsService, UseCls } from 'nestjs-cls';
|
import { ClsService, UseCls } from 'nestjs-cls';
|
||||||
import { RecognizeTranasctionsService } from '../commands/RecognizeTranasctions.service';
|
import { RecognizeTranasctionsService } from '../commands/RecognizeTranasctions.service';
|
||||||
|
import { RevertRecognizedTransactionsService } from '../commands/RevertRecognizedTransactions.service';
|
||||||
import {
|
import {
|
||||||
RecognizeUncategorizedTransactionsJobPayload,
|
RecognizeUncategorizedTransactionsJobPayload,
|
||||||
RecognizeUncategorizedTransactionsQueue,
|
RecognizeUncategorizedTransactionsQueue,
|
||||||
@@ -15,10 +16,12 @@ import {
|
|||||||
export class RegonizeTransactionsPrcessor extends WorkerHost {
|
export class RegonizeTransactionsPrcessor extends WorkerHost {
|
||||||
/**
|
/**
|
||||||
* @param {RecognizeTranasctionsService} recognizeTranasctionsService -
|
* @param {RecognizeTranasctionsService} recognizeTranasctionsService -
|
||||||
|
* @param {RevertRecognizedTransactionsService} revertRecognizedTransactionsService -
|
||||||
* @param {ClsService} clsService -
|
* @param {ClsService} clsService -
|
||||||
*/
|
*/
|
||||||
constructor(
|
constructor(
|
||||||
private readonly recognizeTranasctionsService: RecognizeTranasctionsService,
|
private readonly recognizeTranasctionsService: RecognizeTranasctionsService,
|
||||||
|
private readonly revertRecognizedTransactionsService: RevertRecognizedTransactionsService,
|
||||||
private readonly clsService: ClsService,
|
private readonly clsService: ClsService,
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
@@ -29,12 +32,21 @@ export class RegonizeTransactionsPrcessor extends WorkerHost {
|
|||||||
*/
|
*/
|
||||||
@UseCls()
|
@UseCls()
|
||||||
async process(job: Job<RecognizeUncategorizedTransactionsJobPayload>) {
|
async process(job: Job<RecognizeUncategorizedTransactionsJobPayload>) {
|
||||||
const { ruleId, transactionsCriteria } = job.data;
|
const { ruleId, transactionsCriteria, shouldRevert } = job.data;
|
||||||
|
|
||||||
this.clsService.set('organizationId', job.data.organizationId);
|
this.clsService.set('organizationId', job.data.organizationId);
|
||||||
this.clsService.set('userId', job.data.userId);
|
this.clsService.set('userId', job.data.userId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// If shouldRevert is true, first revert recognized transactions before re-recognizing.
|
||||||
|
// This is used when a bank rule is edited to ensure transactions previously recognized
|
||||||
|
// by lower-priority rules are re-evaluated against the updated rule.
|
||||||
|
if (shouldRevert) {
|
||||||
|
await this.revertRecognizedTransactionsService.revertRecognizedTransactions(
|
||||||
|
ruleId,
|
||||||
|
transactionsCriteria,
|
||||||
|
);
|
||||||
|
}
|
||||||
await this.recognizeTranasctionsService.recognizeTransactions(
|
await this.recognizeTranasctionsService.recognizeTransactions(
|
||||||
ruleId,
|
ruleId,
|
||||||
transactionsCriteria,
|
transactionsCriteria,
|
||||||
|
|||||||
Reference in New Issue
Block a user