mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
99 lines
2.8 KiB
TypeScript
99 lines
2.8 KiB
TypeScript
import { Inject, Service } from 'typedi';
|
|
import events from '@/subscribers/events';
|
|
import {
|
|
IBankRuleEventCreatedPayload,
|
|
IBankRuleEventDeletedPayload,
|
|
IBankRuleEventEditedPayload,
|
|
} from '../../Rules/types';
|
|
import { IImportFileCommitedEventPayload } from '@/interfaces/Import';
|
|
import { Import } from '@/system/models';
|
|
|
|
@Service()
|
|
export class TriggerRecognizedTransactions {
|
|
@Inject('agenda')
|
|
private agenda: any;
|
|
|
|
/**
|
|
* Constructor method.
|
|
*/
|
|
public attach(bus) {
|
|
bus.subscribe(
|
|
events.bankRules.onCreated,
|
|
this.recognizedTransactionsOnRuleCreated.bind(this)
|
|
);
|
|
bus.subscribe(
|
|
events.bankRules.onEdited,
|
|
this.recognizedTransactionsOnRuleEdited.bind(this)
|
|
);
|
|
bus.subscribe(
|
|
events.bankRules.onDeleted,
|
|
this.recognizedTransactionsOnRuleDeleted.bind(this)
|
|
);
|
|
bus.subscribe(
|
|
events.import.onImportCommitted,
|
|
this.triggerRecognizeTransactionsOnImportCommitted.bind(this)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Triggers the recognize uncategorized transactions job on rule created.
|
|
* @param {IBankRuleEventCreatedPayload} payload -
|
|
*/
|
|
private async recognizedTransactionsOnRuleCreated({
|
|
tenantId,
|
|
createRuleDTO,
|
|
}: IBankRuleEventCreatedPayload) {
|
|
const payload = { tenantId };
|
|
|
|
// Cannot run recognition if the option is not enabled.
|
|
if (createRuleDTO.recognition) {
|
|
return;
|
|
}
|
|
await this.agenda.now('recognize-uncategorized-transactions-job', payload);
|
|
}
|
|
|
|
/**
|
|
* Triggers the recognize uncategorized transactions job on rule edited.
|
|
* @param {IBankRuleEventEditedPayload} payload -
|
|
*/
|
|
private async recognizedTransactionsOnRuleEdited({
|
|
tenantId,
|
|
editRuleDTO,
|
|
}: IBankRuleEventEditedPayload) {
|
|
const payload = { tenantId };
|
|
|
|
// Cannot run recognition if the option is not enabled.
|
|
if (!editRuleDTO.recognition) {
|
|
return;
|
|
}
|
|
await this.agenda.now('recognize-uncategorized-transactions-job', payload);
|
|
}
|
|
|
|
/**
|
|
* Triggers the recognize uncategorized transactions job on rule deleted.
|
|
* @param {IBankRuleEventDeletedPayload} payload -
|
|
*/
|
|
private async recognizedTransactionsOnRuleDeleted({
|
|
tenantId,
|
|
}: IBankRuleEventDeletedPayload) {
|
|
const payload = { tenantId };
|
|
await this.agenda.now('recognize-uncategorized-transactions-job', payload);
|
|
}
|
|
|
|
/**
|
|
* Triggers the recognize bank transactions once the imported file commit.
|
|
* @param {IImportFileCommitedEventPayload} payload -
|
|
*/
|
|
private async triggerRecognizeTransactionsOnImportCommitted({
|
|
tenantId,
|
|
importId,
|
|
meta,
|
|
}: IImportFileCommitedEventPayload) {
|
|
const importFile = await Import.query().findOne({ importId });
|
|
const batch = importFile.paramsParsed.batch;
|
|
const payload = { tenantId, batch };
|
|
|
|
await this.agenda.now('recognize-uncategorized-transactions-job', payload);
|
|
}
|
|
}
|