feat: uncategorize transaciton catch validation errors

This commit is contained in:
Ahmed Bouhuolia
2024-03-10 03:08:24 +02:00
parent b71c79fef5
commit 2c98376162
3 changed files with 36 additions and 17 deletions

View File

@@ -9,9 +9,6 @@ export class DeleteCashflowTransactionOnUncategorize {
@Inject()
private deleteCashflowTransactionService: DeleteCashflowTransaction;
@Inject()
private tenancy: HasTenancyService;
/**
* Attaches events with handlers.
*/
@@ -31,20 +28,13 @@ export class DeleteCashflowTransactionOnUncategorize {
oldUncategorizedTransaction,
trx,
}: ICashflowTransactionUncategorizedPayload) {
const { CashflowTransaction } = this.tenancy.models(tenantId);
// Deletes the cashflow transaction.
if (
oldUncategorizedTransaction.categorizeRefType === 'CashflowTransaction'
) {
await CashflowTransaction.query()
.findById(oldUncategorizedTransaction.categorizeRefId)
.patch({
uncategorizedTransactionId: null,
});
await this.deleteCashflowTransactionService.deleteCashflowTransaction(
tenantId,
oldUncategorizedTransaction.categorizeRefId
);
}

View File

@@ -1,11 +1,15 @@
import { Service } from 'typedi';
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import { ICommandCashflowDeletingPayload } from '@/interfaces';
import { ServiceError } from '@/exceptions';
import { ERRORS } from '../constants';
import HasTenancyService from '@/services/Tenancy/TenancyService';
@Service()
export class PreventDeleteTransactionOnDelete {
@Inject()
private tenancy: HasTenancyService;
/**
* Attaches events with handlers.
*/
@@ -27,11 +31,22 @@ export class PreventDeleteTransactionOnDelete {
oldCashflowTransaction,
trx,
}: ICommandCashflowDeletingPayload) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
if (oldCashflowTransaction.uncategorizedTransactionId) {
throw new ServiceError(
ERRORS.CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED,
'Cannot delete cashflow transaction converted from uncategorized transaction.'
);
const foundTransactions = await UncategorizedCashflowTransaction.query(
trx
).where({
categorized: true,
categorizeRefId: oldCashflowTransaction.id,
categorizeRefType: 'CashflowTransaction',
});
// Throw the error if the cashflow transaction still linked to uncategorized transaction.
if (foundTransactions.length > 0) {
throw new ServiceError(
ERRORS.CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED,
'Cannot delete cashflow transaction converted from uncategorized transaction.'
);
}
}
}
}