mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-10 18:01:59 +00:00
81 lines
2.4 KiB
TypeScript
81 lines
2.4 KiB
TypeScript
import {
|
|
CategorizeTransactionAsExpenseDTO,
|
|
ICashflowTransactionCategorizedPayload,
|
|
} from '@/interfaces';
|
|
import { Inject, Service } from 'typedi';
|
|
import UnitOfWork from '../UnitOfWork';
|
|
import events from '@/subscribers/events';
|
|
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
|
import HasTenancyService from '../Tenancy/TenancyService';
|
|
import { Knex } from 'knex';
|
|
import { CreateExpense } from '../Expenses/CRUD/CreateExpense';
|
|
|
|
@Service()
|
|
export class CategorizeTransactionAsExpense {
|
|
@Inject()
|
|
private uow: UnitOfWork;
|
|
|
|
@Inject()
|
|
private tenancy: HasTenancyService;
|
|
|
|
@Inject()
|
|
private eventPublisher: EventPublisher;
|
|
|
|
@Inject()
|
|
private createExpenseService: CreateExpense;
|
|
|
|
/**
|
|
* Categorize the transaction as expense transaction.
|
|
* @param {number} tenantId
|
|
* @param {number} cashflowTransactionId
|
|
* @param {CategorizeTransactionAsExpenseDTO} transactionDTO
|
|
*/
|
|
public async categorize(
|
|
tenantId: number,
|
|
cashflowTransactionId: number,
|
|
transactionDTO: CategorizeTransactionAsExpenseDTO
|
|
) {
|
|
const { CashflowTransaction } = this.tenancy.models(tenantId);
|
|
|
|
const transaction = await CashflowTransaction.query()
|
|
.findById(cashflowTransactionId)
|
|
.throwIfNotFound();
|
|
|
|
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
|
// Triggers `onTransactionUncategorizing` event.
|
|
await this.eventPublisher.emitAsync(
|
|
events.cashflow.onTransactionCategorizingAsExpense,
|
|
{
|
|
tenantId,
|
|
trx,
|
|
} as ICashflowTransactionCategorizedPayload
|
|
);
|
|
// Creates a new expense transaction.
|
|
const expenseTransaction = await this.createExpenseService.newExpense(
|
|
tenantId,
|
|
{
|
|
|
|
},
|
|
1
|
|
);
|
|
// Updates the item on the storage and fetches the updated once.
|
|
const cashflowTransaction = await CashflowTransaction.query(
|
|
trx
|
|
).patchAndFetchById(cashflowTransactionId, {
|
|
categorizeRefType: 'Expense',
|
|
categorizeRefId: expenseTransaction.id,
|
|
uncategorized: true,
|
|
});
|
|
// Triggers `onTransactionUncategorized` event.
|
|
await this.eventPublisher.emitAsync(
|
|
events.cashflow.onTransactionCategorizedAsExpense,
|
|
{
|
|
tenantId,
|
|
cashflowTransaction,
|
|
trx,
|
|
} as ICashflowTransactionUncategorizedPayload
|
|
);
|
|
});
|
|
}
|
|
}
|