fix(Accounts): accounts chart children not nested.

This commit is contained in:
a.bouhuolia
2021-05-17 17:27:47 +02:00
parent bd7c9db5f6
commit 256d915f06

View File

@@ -10,7 +10,7 @@ import {
IFilterMeta, IFilterMeta,
IAccountResponse, IAccountResponse,
IAccountsTransactionsFilter, IAccountsTransactionsFilter,
IAccountTransaction IAccountTransaction,
} from 'interfaces'; } from 'interfaces';
import { import {
EventDispatcher, EventDispatcher,
@@ -20,7 +20,7 @@ import DynamicListingService from 'services/DynamicListing/DynamicListService';
import events from 'subscribers/events'; import events from 'subscribers/events';
import AccountTypesUtils from 'lib/AccountTypes'; import AccountTypesUtils from 'lib/AccountTypes';
import { ERRORS } from './constants'; import { ERRORS } from './constants';
import { transaction } from 'objection'; import { flatToNestedArray } from 'utils';
@Service() @Service()
export default class AccountsService { export default class AccountsService {
@@ -632,18 +632,17 @@ export default class AccountsService {
}; };
} }
runningBalanceFromClosing( runningBalanceFromClosing(
transactions: IAccountTransaction[], transactions: IAccountTransaction[],
closingBalance: number, closingBalance: number,
accountNormal: string, accountNormal: string
) { ) {
let remain = closingBalance; let remain = closingBalance;
return transactions.map((entry, index) => { return transactions.map((entry, index) => {
const { credit, debit } = entry; const { credit, debit } = entry;
const amount = accountNormal === 'credit' ? const amount =
credit - debit : debit - credit; accountNormal === 'credit' ? credit - debit : debit - credit;
const runningBalance = Math.min(amount, remain); const runningBalance = Math.min(amount, remain);
const output = { const output = {
@@ -663,12 +662,15 @@ export default class AccountsService {
*/ */
public async getAccountsTransactions( public async getAccountsTransactions(
tenantId: number, tenantId: number,
filter: IAccountsTransactionsFilter, filter: IAccountsTransactionsFilter
): Promise<{ transactions: IAccountTransaction }> { ): Promise<{ transactions: IAccountTransaction }> {
const { AccountTransaction } = this.tenancy.models(tenantId); const { AccountTransaction } = this.tenancy.models(tenantId);
// Retrieve the given account or throw not found error. // Retrieve the given account or throw not found error.
const account = await this.getAccountOrThrowError(tenantId, filter.accountId); const account = await this.getAccountOrThrowError(
tenantId,
filter.accountId
);
this.logger.info('[accounts] trying to get accounts transactions list.'); this.logger.info('[accounts] trying to get accounts transactions list.');
const transactions = await AccountTransaction.query().onBuild((query) => { const transactions = await AccountTransaction.query().onBuild((query) => {
@@ -685,8 +687,8 @@ export default class AccountsService {
transactions: this.runningBalanceFromClosing( transactions: this.runningBalanceFromClosing(
transactions, transactions,
account.amount, account.amount,
account.accountNormal, account.accountNormal
) ),
}; };
} }
@@ -715,10 +717,14 @@ export default class AccountsService {
key: 'base_currency', key: 'base_currency',
}); });
return accounts.map((account) => ({ const _accounts = accounts.map((account) => ({
...account.toJSON(), ...account.toJSON(),
currencyCode: baseCurrency, currencyCode: baseCurrency,
})); }));
return flatToNestedArray(_accounts, {
id: 'id',
parentId: 'parent_account_id',
});
} }
/** /**