mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10: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);
|
||||
}
|
||||
}
|
||||
@@ -27,9 +27,12 @@ export class BankRulesApplication {
|
||||
* Creates new bank rule.
|
||||
* @param {number} tenantId
|
||||
* @param {ICreateBankRuleDTO} createRuleDTO
|
||||
* @returns
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public createBankRule(tenantId: number, createRuleDTO: ICreateBankRuleDTO) {
|
||||
public createBankRule(
|
||||
tenantId: number,
|
||||
createRuleDTO: ICreateBankRuleDTO
|
||||
): Promise<void> {
|
||||
return this.createBankRuleService.createBankRule(tenantId, createRuleDTO);
|
||||
}
|
||||
|
||||
@@ -37,13 +40,13 @@ export class BankRulesApplication {
|
||||
* Edits the given bank rule.
|
||||
* @param {number} tenantId
|
||||
* @param {IEditBankRuleDTO} editRuleDTO
|
||||
* @returns
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public editBankRule(
|
||||
tenantId: number,
|
||||
ruleId: number,
|
||||
editRuleDTO: IEditBankRuleDTO
|
||||
) {
|
||||
): Promise<void> {
|
||||
return this.editBankRuleService.editBankRule(tenantId, ruleId, editRuleDTO);
|
||||
}
|
||||
|
||||
@@ -51,9 +54,9 @@ export class BankRulesApplication {
|
||||
* Deletes the given bank rule.
|
||||
* @param {number} tenantId
|
||||
* @param {number} ruleId
|
||||
* @returns
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public deleteBankRule(tenantId: number, ruleId: number) {
|
||||
public deleteBankRule(tenantId: number, ruleId: number): Promise<void> {
|
||||
return this.deleteBankRuleService.deleteBankRule(tenantId, ruleId);
|
||||
}
|
||||
|
||||
@@ -61,9 +64,9 @@ export class BankRulesApplication {
|
||||
* Retrieves the given bank rule.
|
||||
* @param {number} tenantId
|
||||
* @param {number} ruleId
|
||||
* @returns
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
public getBankRule(tenantId: number, ruleId: number) {
|
||||
public getBankRule(tenantId: number, ruleId: number): Promise<any> {
|
||||
return this.getBankRuleService.getBankRule(tenantId, ruleId);
|
||||
}
|
||||
|
||||
@@ -71,9 +74,9 @@ export class BankRulesApplication {
|
||||
* Retrieves the bank rules of the given account.
|
||||
* @param {number} tenantId
|
||||
* @param {number} accountId
|
||||
* @returns
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
public getBankRules(tenantId: number) {
|
||||
public getBankRules(tenantId: number): Promise<any> {
|
||||
return this.getBankRulesService.getBankRules(tenantId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,8 +36,12 @@ export class CreateBankRuleService {
|
||||
* Creates a new bank rule.
|
||||
* @param {number} tenantId
|
||||
* @param {ICreateBankRuleDTO} createRuleDTO
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public createBankRule(tenantId: number, createRuleDTO: ICreateBankRuleDTO) {
|
||||
public createBankRule(
|
||||
tenantId: number,
|
||||
createRuleDTO: ICreateBankRuleDTO
|
||||
): Promise<void> {
|
||||
const { BankRule } = this.tenancy.models(tenantId);
|
||||
|
||||
const transformDTO = this.transformDTO(createRuleDTO);
|
||||
@@ -45,6 +49,7 @@ export class CreateBankRuleService {
|
||||
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
||||
// Triggers `onBankRuleCreating` event.
|
||||
await this.eventPublisher.emitAsync(events.bankRules.onCreating, {
|
||||
tenantId,
|
||||
createRuleDTO,
|
||||
trx,
|
||||
} as IBankRuleEventCreatingPayload);
|
||||
@@ -55,6 +60,7 @@ export class CreateBankRuleService {
|
||||
|
||||
// Triggers `onBankRuleCreated` event.
|
||||
await this.eventPublisher.emitAsync(events.bankRules.onCreated, {
|
||||
tenantId,
|
||||
createRuleDTO,
|
||||
trx,
|
||||
} as IBankRuleEventCreatedPayload);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Knex } from 'knex';
|
||||
import UnitOfWork from '@/services/UnitOfWork';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import UnitOfWork from '@/services/UnitOfWork';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import events from '@/subscribers/events';
|
||||
import {
|
||||
@@ -26,7 +26,7 @@ export class DeleteBankRuleSerivce {
|
||||
* @param {number} ruleId
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async deleteBankRule(tenantId: number, ruleId: number) {
|
||||
public async deleteBankRule(tenantId: number, ruleId: number): Promise<void> {
|
||||
const { BankRule } = this.tenancy.models(tenantId);
|
||||
|
||||
const oldBankRule = await BankRule.query()
|
||||
@@ -36,6 +36,7 @@ export class DeleteBankRuleSerivce {
|
||||
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
||||
// Triggers `onBankRuleDeleting` event.
|
||||
await this.eventPublisher.emitAsync(events.bankRules.onDeleting, {
|
||||
tenantId,
|
||||
oldBankRule,
|
||||
ruleId,
|
||||
trx,
|
||||
@@ -45,6 +46,7 @@ export class DeleteBankRuleSerivce {
|
||||
|
||||
// Triggers `onBankRuleDeleted` event.
|
||||
await await this.eventPublisher.emitAsync(events.bankRules.onDeleted, {
|
||||
tenantId,
|
||||
ruleId,
|
||||
trx,
|
||||
} as IBankRuleEventDeletedPayload);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import UnitOfWork from '@/services/UnitOfWork';
|
||||
import events from '@/subscribers/events';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import {
|
||||
IBankRuleEventEditedPayload,
|
||||
IBankRuleEventEditingPayload,
|
||||
@@ -56,6 +56,7 @@ export class EditBankRuleService {
|
||||
async (trx?: Knex.Transaction) => {
|
||||
// Triggers `onBankRuleEditing` event.
|
||||
await this.eventPublisher.emitAsync(events.bankRules.onEditing, {
|
||||
tenantId,
|
||||
oldBankRule,
|
||||
ruleId,
|
||||
editRuleDTO,
|
||||
@@ -63,12 +64,13 @@ export class EditBankRuleService {
|
||||
} as IBankRuleEventEditingPayload);
|
||||
|
||||
// Updates the given bank rule.
|
||||
await BankRule.query()
|
||||
await BankRule.query(trx)
|
||||
.findById(ruleId)
|
||||
.patch({ ...tranformDTO });
|
||||
|
||||
// Triggers `onBankRuleEdited` event.
|
||||
await this.eventPublisher.emitAsync(events.bankRules.onEdited, {
|
||||
tenantId,
|
||||
oldBankRule,
|
||||
ruleId,
|
||||
editRuleDTO,
|
||||
|
||||
@@ -16,9 +16,9 @@ export class GetBankRuleService {
|
||||
* Retrieves the bank rule.
|
||||
* @param {number} tenantId
|
||||
* @param {number} ruleId
|
||||
* @returns
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
async getBankRule(tenantId: number, ruleId: number) {
|
||||
async getBankRule(tenantId: number, ruleId: number): Promise<any> {
|
||||
const { BankRule } = this.tenancy.models(tenantId);
|
||||
|
||||
const bankRule = await BankRule.query()
|
||||
|
||||
@@ -15,9 +15,9 @@ export class GetBankRulesService {
|
||||
* Retrieves the bank rules of the given account.
|
||||
* @param {number} tenantId
|
||||
* @param {number} accountId
|
||||
* @returns
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
public async getBankRules(tenantId: number) {
|
||||
public async getBankRules(tenantId: number): Promise<any> {
|
||||
const { BankRule } = this.tenancy.models(tenantId);
|
||||
|
||||
const bankRule = await BankRule.query();
|
||||
|
||||
@@ -33,32 +33,38 @@ export interface ICreateBankRuleDTO extends IBankRuleCommonDTO {}
|
||||
export interface IEditBankRuleDTO extends IBankRuleCommonDTO {}
|
||||
|
||||
export interface IBankRuleEventCreatingPayload {
|
||||
tenantId: number;
|
||||
createRuleDTO: ICreateBankRuleDTO;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
export interface IBankRuleEventCreatedPayload {
|
||||
tenantId: number;
|
||||
createRuleDTO: ICreateBankRuleDTO;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IBankRuleEventEditingPayload {
|
||||
tenantId: number;
|
||||
ruleId: number;
|
||||
oldBankRule: any;
|
||||
editRuleDTO: IEditBankRuleDTO;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
export interface IBankRuleEventEditedPayload {
|
||||
tenantId: number;
|
||||
ruleId: number;
|
||||
editRuleDTO: IEditBankRuleDTO;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IBankRuleEventDeletingPayload {
|
||||
tenantId: number;
|
||||
oldBankRule: any;
|
||||
ruleId: number;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
export interface IBankRuleEventDeletedPayload {
|
||||
tenantId: number;
|
||||
ruleId: number;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { Service } from "typedi";
|
||||
|
||||
|
||||
@Service()
|
||||
export class CategorizeRecognizedTransactionService {
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user