feat: remove uncategorized transaction from expenses

This commit is contained in:
Ahmed Bouhuolia
2024-03-07 20:58:44 +02:00
parent b9a00418fa
commit 83fbb7225d
12 changed files with 53 additions and 110 deletions

View File

@@ -182,7 +182,6 @@ export default class Expense extends mixin(TenantModel, [
const ExpenseCategory = require('models/ExpenseCategory');
const Media = require('models/Media');
const Branch = require('models/Branch');
const UncategorizedCashflowTransaction = require('models/UncategorizedCashflowTransaction');
return {
paymentAccount: {
@@ -235,18 +234,6 @@ export default class Expense extends mixin(TenantModel, [
query.where('model_name', 'Expense');
},
},
/**
* Retrieves the related uncategorized cashflow transaction.
*/
categorized: {
relation: Model.BelongsToOneRelation,
modelClass: UncategorizedCashflowTransaction.default,
join: {
from: 'expenses_transactions.categorizedTransactionId',
to: 'uncategorized_cashflow_transactions.id',
},
}
};
}

View File

@@ -4,7 +4,11 @@ import { Model, ModelOptions, QueryContext } from 'objection';
import Account from './Account';
export default class UncategorizedCashflowTransaction extends TenantModel {
amount: number;
id!: number;
amount!: number;
categorized!: boolean;
accountId!: number;
/**
* Table name.
*/
@@ -19,6 +23,18 @@ export default class UncategorizedCashflowTransaction extends TenantModel {
return ['createdAt', 'updatedAt'];
}
/**
* Virtual attributes.
*/
static get virtualAttributes() {
return [
'withdrawal',
'deposit',
'isDepositTransaction',
'isWithdrawalTransaction',
];
}
/**
* Retrieves the withdrawal amount.
* @returns {number}
@@ -49,18 +65,6 @@ export default class UncategorizedCashflowTransaction extends TenantModel {
return 0 < this.withdrawal;
}
/**
* Virtual attributes.
*/
static get virtualAttributes() {
return [
'withdrawal',
'deposit',
'isDepositTransaction',
'isWithdrawalTransaction',
];
}
/**
* Relationship mapping.
*/
@@ -83,40 +87,54 @@ export default class UncategorizedCashflowTransaction extends TenantModel {
}
/**
*
* @param queryContext
* Updates the count of uncategorized transactions for the associated account
* based on the specified operation.
* @param {QueryContext} queryContext - The query context for the transaction.
* @param {boolean} increment - Indicates whether to increment or decrement the count.
*/
private async updateUncategorizedTransactionCount(
queryContext: QueryContext,
increment: boolean
) {
const operation = increment ? 'increment' : 'decrement';
const amount = increment ? 1 : -1;
await Account.query(queryContext.transaction)
.findById(this.accountId)
[operation]('uncategorized_transactions', amount);
}
/**
* Runs after insert.
* @param {QueryContext} queryContext
*/
public async $afterInsert(queryContext) {
await super.$afterInsert(queryContext);
// Increments the uncategorized transactions count of the associated account.
await Account.query(queryContext.transaction)
.findById(this.accountId)
.increment('uncategorized_transactions', 1);
await this.updateUncategorizedTransactionCount(queryContext, true);
}
/**
* Runs after update.
* @param {ModelOptions} opt
* @param {QueryContext} queryContext
*/
public async $afterUpdate(
opt: ModelOptions,
queryContext: QueryContext
): void | Promise<any> {
): Promise<any> {
await super.$afterUpdate(opt, queryContext);
if (this.id && this.categorized) {
await Account.query(queryContext.transaction)
.findById(this.accountId)
.decrement('uncategorized_transactions', 1);
await this.updateUncategorizedTransactionCount(queryContext, false);
}
}
/**
*
* @param queryContext
* Runs after delete.
* @param {QueryContext} queryContext
*/
public async $afterDelete(queryContext) {
public async $afterDelete(queryContext: QueryContext) {
await super.$afterDelete(queryContext);
await Account.query(queryContext.transaction)
.findById(this.accountId)
.decrement('uncategorized_transactions', 1);
await this.updateUncategorizedTransactionCount(queryContext, false);
}
}