mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
feat: Categorize the bank synced transactions
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import HasTenancyService from '../Tenancy/TenancyService';
|
||||
import events from '@/subscribers/events';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import UnitOfWork from '../UnitOfWork';
|
||||
import {
|
||||
ICashflowTransactionCategorizedPayload,
|
||||
ICashflowTransactionUncategorizingPayload,
|
||||
ICategorizeCashflowTransactioDTO,
|
||||
} from '@/interfaces';
|
||||
import { Knex } from 'knex';
|
||||
import { transformCategorizeTransToCashflow } from './utils';
|
||||
import { CommandCashflowValidator } from './CommandCasflowValidator';
|
||||
import NewCashflowTransactionService from './NewCashflowTransactionService';
|
||||
|
||||
@Service()
|
||||
export class CategorizeCashflowTransaction {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private eventPublisher: EventPublisher;
|
||||
|
||||
@Inject()
|
||||
private uow: UnitOfWork;
|
||||
|
||||
@Inject()
|
||||
private commandValidators: CommandCashflowValidator;
|
||||
|
||||
@Inject()
|
||||
private createCashflow: NewCashflowTransactionService;
|
||||
|
||||
/**
|
||||
* Categorize the given cashflow transaction.
|
||||
* @param {number} tenantId
|
||||
* @param {ICategorizeCashflowTransactioDTO} categorizeDTO
|
||||
*/
|
||||
public async categorize(
|
||||
tenantId: number,
|
||||
uncategorizedTransactionId: number,
|
||||
categorizeDTO: ICategorizeCashflowTransactioDTO
|
||||
) {
|
||||
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
||||
|
||||
// Retrieves the uncategorized transaction or throw an error.
|
||||
const transaction = await UncategorizedCashflowTransaction.query()
|
||||
.findById(uncategorizedTransactionId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Validates the transaction shouldn't be categorized before.
|
||||
this.commandValidators.validateTransactionShouldNotCategorized(transaction);
|
||||
|
||||
// Edits the cashflow transaction under UOW env.
|
||||
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
||||
// Triggers `onTransactionCategorizing` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.cashflow.onTransactionCategorizing,
|
||||
{
|
||||
tenantId,
|
||||
trx,
|
||||
} as ICashflowTransactionUncategorizingPayload
|
||||
);
|
||||
// Transformes the categorize DTO to the cashflow transaction.
|
||||
const cashflowTransactionDTO = transformCategorizeTransToCashflow(
|
||||
transaction,
|
||||
categorizeDTO
|
||||
);
|
||||
// Creates a new cashflow transaction.
|
||||
const cashflowTransaction =
|
||||
await this.createCashflow.newCashflowTransaction(
|
||||
tenantId,
|
||||
cashflowTransactionDTO
|
||||
);
|
||||
// Updates the uncategorized transaction as categorized.
|
||||
await UncategorizedCashflowTransaction.query(trx)
|
||||
.findById(uncategorizedTransactionId)
|
||||
.patch({
|
||||
categorized: true,
|
||||
categorizeRefType: 'CashflowTransaction',
|
||||
categorizeRefId: cashflowTransaction.id,
|
||||
});
|
||||
// Triggers `onCashflowTransactionCategorized` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.cashflow.onTransactionCategorized,
|
||||
{
|
||||
tenantId,
|
||||
// cashflowTransaction,
|
||||
trx,
|
||||
} as ICashflowTransactionCategorizedPayload
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user