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

View File

@@ -1,11 +1,15 @@
import { Service } from 'typedi'; import { Inject, Service } from 'typedi';
import events from '@/subscribers/events'; import events from '@/subscribers/events';
import { ICommandCashflowDeletingPayload } from '@/interfaces'; import { ICommandCashflowDeletingPayload } from '@/interfaces';
import { ServiceError } from '@/exceptions'; import { ServiceError } from '@/exceptions';
import { ERRORS } from '../constants'; import { ERRORS } from '../constants';
import HasTenancyService from '@/services/Tenancy/TenancyService';
@Service() @Service()
export class PreventDeleteTransactionOnDelete { export class PreventDeleteTransactionOnDelete {
@Inject()
private tenancy: HasTenancyService;
/** /**
* Attaches events with handlers. * Attaches events with handlers.
*/ */
@@ -27,11 +31,22 @@ export class PreventDeleteTransactionOnDelete {
oldCashflowTransaction, oldCashflowTransaction,
trx, trx,
}: ICommandCashflowDeletingPayload) { }: ICommandCashflowDeletingPayload) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
if (oldCashflowTransaction.uncategorizedTransactionId) { if (oldCashflowTransaction.uncategorizedTransactionId) {
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( throw new ServiceError(
ERRORS.CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED, ERRORS.CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED,
'Cannot delete cashflow transaction converted from uncategorized transaction.' 'Cannot delete cashflow transaction converted from uncategorized transaction.'
); );
} }
} }
}
} }

View File

@@ -56,7 +56,21 @@ function AccountDeleteTransactionAlert({
response: { response: {
data: { errors }, data: { errors },
}, },
}) => {}, }) => {
if (
errors.find(
(e) =>
e.type ===
'CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED',
)
) {
AppToaster.show({
message:
'Cannot delete transaction converted from uncategorized transaction but you uncategorize it.',
intent: Intent.DANGER,
});
}
},
) )
.finally(() => { .finally(() => {
closeAlert(name); closeAlert(name);