mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
feat: exclude/unexclude the uncategorized transactions
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import UnitOfWork from '@/services/UnitOfWork';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { validateTransactionNotCategorized } from './utils';
|
||||
|
||||
@Service()
|
||||
export class ExcludeBankTransaction {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private uow: UnitOfWork;
|
||||
|
||||
/**
|
||||
* Marks the given bank transaction as excluded.
|
||||
* @param {number} tenantId
|
||||
* @param {number} bankTransactionId
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async excludeBankTransaction(
|
||||
tenantId: number,
|
||||
bankTransactionId: number
|
||||
) {
|
||||
const { UncategorizeCashflowTransaction } = this.tenancy.models(tenantId);
|
||||
|
||||
const oldUncategorizedTransaction =
|
||||
await UncategorizeCashflowTransaction.query()
|
||||
.findById(bankTransactionId)
|
||||
.throwIfNotFound();
|
||||
|
||||
validateTransactionNotCategorized(oldUncategorizedTransaction);
|
||||
|
||||
return this.uow.withTransaction(tenantId, async (trx) => {
|
||||
await UncategorizeCashflowTransaction.query(trx)
|
||||
.findById(bankTransactionId)
|
||||
.patch({
|
||||
excluded: true,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { ExcludeBankTransaction } from './ExcludeBankTransaction';
|
||||
import { UnexcludeBankTransaction } from './UnexcludeBankTransaction';
|
||||
|
||||
@Service()
|
||||
export class ExcludeBankTransactionsApplication {
|
||||
@Inject()
|
||||
private excludeBankTransactionService: ExcludeBankTransaction;
|
||||
|
||||
@Inject()
|
||||
private unexcludeBankTransactionService: UnexcludeBankTransaction;
|
||||
|
||||
/**
|
||||
* Marks a bank transaction as excluded.
|
||||
* @param {number} tenantId - The ID of the tenant.
|
||||
* @param {number} bankTransactionId - The ID of the bank transaction to exclude.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public excludeBankTransaction(tenantId: number, bankTransactionId: number) {
|
||||
return this.excludeBankTransactionService.excludeBankTransaction(
|
||||
tenantId,
|
||||
bankTransactionId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks a bank transaction as not excluded.
|
||||
* @param {number} tenantId - The ID of the tenant.
|
||||
* @param {number} bankTransactionId - The ID of the bank transaction to exclude.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public unexcludeBankTransaction(tenantId: number, bankTransactionId: number) {
|
||||
return this.unexcludeBankTransactionService.unexcludeBankTransaction(
|
||||
tenantId,
|
||||
bankTransactionId
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import UnitOfWork from '@/services/UnitOfWork';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { validateTransactionNotCategorized } from './utils';
|
||||
|
||||
@Service()
|
||||
export class UnexcludeBankTransaction {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private uow: UnitOfWork;
|
||||
|
||||
/**
|
||||
* Marks the given bank transaction as excluded.
|
||||
* @param {number} tenantId
|
||||
* @param {number} bankTransactionId
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async unexcludeBankTransaction(
|
||||
tenantId: number,
|
||||
bankTransactionId: number
|
||||
) {
|
||||
const { UncategorizeCashflowTransaction } = this.tenancy.models(tenantId);
|
||||
|
||||
const oldUncategorizedTransaction =
|
||||
await UncategorizeCashflowTransaction.query()
|
||||
.findById(bankTransactionId)
|
||||
.throwIfNotFound();
|
||||
|
||||
validateTransactionNotCategorized(oldUncategorizedTransaction);
|
||||
|
||||
return this.uow.withTransaction(tenantId, async (trx) => {
|
||||
await UncategorizeCashflowTransaction.query(trx)
|
||||
.findById(bankTransactionId)
|
||||
.patch({
|
||||
excluded: false,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
14
packages/server/src/services/Banking/Exclude/utils.ts
Normal file
14
packages/server/src/services/Banking/Exclude/utils.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { ServiceError } from '@/exceptions';
|
||||
import UncategorizedCashflowTransaction from '@/models/UncategorizedCashflowTransaction';
|
||||
|
||||
const ERRORS = {
|
||||
TRANSACTION_ALREADY_CATEGORIZED: 'TRANSACTION_ALREADY_CATEGORIZED',
|
||||
};
|
||||
|
||||
export const validateTransactionNotCategorized = (
|
||||
transaction: UncategorizedCashflowTransaction
|
||||
) => {
|
||||
if (transaction.categorized) {
|
||||
throw new ServiceError(ERRORS.TRANSACTION_ALREADY_CATEGORIZED);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user