feat: wip categorize the cashflow transactions

This commit is contained in:
Ahmed Bouhuolia
2024-03-06 22:15:31 +02:00
parent a17b4f6a8a
commit d87d674aba
27 changed files with 768 additions and 144 deletions

View File

@@ -80,10 +80,7 @@ export default class NewCashflowTransactionController extends BaseController {
public get categorizeCashflowTransactionValidationSchema() {
return [
check('date').exists().isISO8601().toDate(),
oneOf([
check('to_account_id').exists().isInt().toInt(),
check('from_account_id').exists().isInt().toInt(),
]),
check('credit_account_id').exists().isInt().toInt(),
check('transaction_number').optional(),
check('transaction_type').exists(),
check('reference_no').optional(),

View File

@@ -235,8 +235,7 @@ export interface ICashflowTransactionSchema {
export interface ICashflowTransactionInput extends ICashflowTransactionSchema {}
export interface ICategorizeCashflowTransactioDTO {
fromAccountId: number;
toAccountId: number;
creditAccountId: number;
referenceNo: string;
transactionNumber: string;
transactionType: string;

View File

@@ -318,7 +318,7 @@ export default class Account extends mixin(TenantModel, [
to: 'uncategorized_cashflow_transactions.accountId',
},
filter: (query) => {
query.filter('categorized', false);
query.where('categorized', false);
},
},
};

View File

@@ -1,6 +1,6 @@
/* eslint-disable global-require */
import TenantModel from 'models/TenantModel';
import { Model } from 'objection';
import { Model, ModelOptions, QueryContext } from 'objection';
import Account from './Account';
export default class UncategorizedCashflowTransaction extends TenantModel {
@@ -95,6 +95,19 @@ export default class UncategorizedCashflowTransaction extends TenantModel {
.increment('uncategorized_transactions', 1);
}
public async $afterUpdate(
opt: ModelOptions,
queryContext: QueryContext
): void | Promise<any> {
await super.$afterUpdate(opt, queryContext);
if (this.id && this.categorized) {
await Account.query(queryContext.transaction)
.findById(this.accountId)
.decrement('uncategorized_transactions', 1);
}
}
/**
*
* @param queryContext
@@ -102,7 +115,7 @@ export default class UncategorizedCashflowTransaction extends TenantModel {
public async $afterDelete(queryContext) {
await super.$afterDelete(queryContext);
await Account.query()
await Account.query(queryContext.transaction)
.findById(this.accountId)
.decrement('uncategorized_transactions', 1);
}

View File

@@ -79,13 +79,14 @@ export class CategorizeCashflowTransaction {
cashflowTransactionDTO
);
// Updates the uncategorized transaction as categorized.
await UncategorizedCashflowTransaction.query(trx)
.findById(uncategorizedTransactionId)
.patch({
await UncategorizedCashflowTransaction.query(trx).patchAndFetchById(
uncategorizedTransactionId,
{
categorized: true,
categorizeRefType: 'CashflowTransaction',
categorizeRefId: cashflowTransaction.id,
});
}
);
// Triggers `onCashflowTransactionCategorized` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionCategorized,

View File

@@ -31,6 +31,7 @@ export default class GetCashflowTransactionsService {
.withGraphFetched('entries.cashflowAccount')
.withGraphFetched('entries.creditAccount')
.withGraphFetched('transactions.account')
.orderBy('date', 'DESC')
.throwIfNotFound();
this.throwErrorCashflowTranscationNotFound(cashflowTransaction);

View File

@@ -24,7 +24,8 @@ export class GetUncategorizedTransactions {
.where('accountId', accountId)
.where('categorized', false)
.withGraphFetched('account')
.pagination(0, 10);
.orderBy('date', 'DESC')
.pagination(0, 1000);
const data = await this.transformer.transform(
tenantId,

View File

@@ -8,6 +8,7 @@ export class UncategorizedTransactionTransformer extends Transformer {
*/
public includeAttributes = (): string[] => {
return [
'formattedAmount',
'formattedDate',
'formattetDepositAmount',
'formattedWithdrawalAmount',
@@ -23,6 +24,17 @@ export class UncategorizedTransactionTransformer extends Transformer {
return this.formatDate(transaction.date);
}
/**
* Formatted amount.
* @param transaction
* @returns {string}
*/
public formattedAmount(transaction) {
return formatNumber(transaction.amount, {
currencyCode: transaction.currencyCode,
});
}
/**
* Formatted deposit amount.
* @param transaction

View File

@@ -55,7 +55,7 @@ export const transformCategorizeTransToCashflow = (
referenceNo: categorizeDTO.referenceNo || uncategorizeModel.referenceNo,
description: categorizeDTO.description || uncategorizeModel.description,
cashflowAccountId: uncategorizeModel.accountId,
creditAccountId: categorizeDTO.fromAccountId || categorizeDTO.toAccountId,
creditAccountId: categorizeDTO.creditAccountId,
exchangeRate: categorizeDTO.exchangeRate || 1,
currencyCode: uncategorizeModel.currencyCode,
amount: uncategorizeModel.amount,