mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 05:40:31 +00:00
feat: recognize uncategorized transactions
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import UncategorizedCashflowTransaction from '@/models/UncategorizedCashflowTransaction';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import { transformToMapBy } from '@/utils';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { PromisePool } from '@supercharge/promise-pool';
|
||||
|
||||
@Service()
|
||||
export class RecognizeedTranasctionsService {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
/**
|
||||
* Regonized the uncategorized transactions.
|
||||
* @param {number} tenantId
|
||||
*/
|
||||
public async recognizeTransactions(tenantId: number) {
|
||||
const { UncategorizedCashflowTransaction, BankRule } =
|
||||
this.tenancy.models(tenantId);
|
||||
|
||||
const uncategorizedTranasctions =
|
||||
await UncategorizedCashflowTransaction.query().where(
|
||||
'regonized_transaction_id',
|
||||
null
|
||||
);
|
||||
|
||||
const bankRules = await BankRule.query();
|
||||
const bankRulesByAccountId = transformToMapBy(bankRules, 'accountId');
|
||||
|
||||
console.log(bankRulesByAccountId);
|
||||
|
||||
const regonizeTransaction = (
|
||||
transaction: UncategorizedCashflowTransaction
|
||||
) => {};
|
||||
|
||||
await PromisePool.withConcurrency(MIGRATION_CONCURRENCY)
|
||||
.for(uncategorizedTranasctions)
|
||||
.process((transaction: UncategorizedCashflowTransaction, index, pool) => {
|
||||
return regonizeTransaction(transaction);
|
||||
});
|
||||
}
|
||||
|
||||
public async regonizeTransaction(
|
||||
uncategorizedTransaction: UncategorizedCashflowTransaction
|
||||
) {}
|
||||
}
|
||||
|
||||
const MIGRATION_CONCURRENCY = 10;
|
||||
@@ -0,0 +1,32 @@
|
||||
import Container, { Service } from 'typedi';
|
||||
import { RegonizeTranasctionsService } from './RecognizeTranasctionsService';
|
||||
|
||||
@Service()
|
||||
export class RegonizeTransactionsJob {
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor(agenda) {
|
||||
agenda.define(
|
||||
'regonize-uncategorized-transactions-job',
|
||||
{ priority: 'high', concurrency: 2 },
|
||||
this.handler
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers sending invoice mail.
|
||||
*/
|
||||
private handler = async (job, done: Function) => {
|
||||
const { tenantId } = job.attrs.data;
|
||||
const regonizeTransactions = Container.get(RegonizeTranasctionsService);
|
||||
|
||||
try {
|
||||
await regonizeTransactions.regonizeTransactions(tenantId);
|
||||
done();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
done(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import events from '@/subscribers/events';
|
||||
import {
|
||||
IBankRuleEventCreatedPayload,
|
||||
IBankRuleEventEditedPayload,
|
||||
} from '../../Rules/types';
|
||||
|
||||
@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.recognizedTransactionsOnRuleCreated.bind(this)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers the recognize uncategorized transactions job.
|
||||
* @param {IBankRuleEventEditedPayload | IBankRuleEventCreatedPayload} payload -
|
||||
*/
|
||||
private async recognizedTransactionsOnRuleCreated({
|
||||
tenantId,
|
||||
}: IBankRuleEventEditedPayload | IBankRuleEventCreatedPayload) {
|
||||
const payload = { tenantId };
|
||||
await this.agenda.now('recognize-uncategorized-transactions-job', payload);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user