fix: Bank rules conditions column

This commit is contained in:
Ahmed Bouhuolia
2024-07-01 10:48:11 +02:00
parent 5bbcb7913d
commit da0fab9a58
4 changed files with 84 additions and 13 deletions

View File

@@ -39,6 +39,7 @@ export class BankRule extends TenantModel {
*/ */
static get relationMappings() { static get relationMappings() {
const { BankRuleCondition } = require('models/BankRuleCondition'); const { BankRuleCondition } = require('models/BankRuleCondition');
const Account = require('models/Account');
return { return {
/** /**
@@ -52,6 +53,15 @@ export class BankRule extends TenantModel {
to: 'bank_rule_conditions.ruleId', to: 'bank_rule_conditions.ruleId',
}, },
}, },
assignAccount: {
relation: Model.BelongsToOneRelation,
modelClass: Account.default,
join: {
from: 'bank_rules.assignAccountId',
to: 'accounts.id'
}
}
}; };
} }
} }

View File

@@ -13,14 +13,16 @@ export class GetBankRulesService {
/** /**
* Retrieves the bank rules of the given account. * Retrieves the bank rules of the given account.
* @param {number} tenantId * @param {number} tenantId
* @param {number} accountId * @param {number} accountId
* @returns {Promise<any>} * @returns {Promise<any>}
*/ */
public async getBankRules(tenantId: number): Promise<any> { public async getBankRules(tenantId: number): Promise<any> {
const { BankRule } = this.tenancy.models(tenantId); const { BankRule } = this.tenancy.models(tenantId);
const bankRule = await BankRule.query(); const bankRule = await BankRule.query()
.withGraphFetched('conditions')
.withGraphFetched('assignAccount');
return this.transformer.transform( return this.transformer.transform(
tenantId, tenantId,

View File

@@ -1,4 +1,6 @@
import { upperFirst, camelCase } from 'lodash';
import { Transformer } from '@/lib/Transformer/Transformer'; import { Transformer } from '@/lib/Transformer/Transformer';
import { getTransactionTypeLabel } from '@/utils/transactions-types';
export class GetBankRulesTransformer extends Transformer { export class GetBankRulesTransformer extends Transformer {
/** /**
@@ -6,6 +8,44 @@ export class GetBankRulesTransformer extends Transformer {
* @returns {Array} * @returns {Array}
*/ */
public includeAttributes = (): string[] => { public includeAttributes = (): string[] => {
return []; return [
'assignAccountName',
'assignCategoryFormatted',
'conditionsFormatted',
];
}; };
/**
* Get the assign account name.
* @param bankRule
* @returns {string}
*/
protected assignAccountName(bankRule: any) {
return bankRule.assignAccount.name;
}
/**
* Assigned category formatted.
* @returns {string}
*/
protected assignCategoryFormatted(bankRule: any) {
const assignCategory = upperFirst(camelCase(bankRule.assignCategory));
return getTransactionTypeLabel(assignCategory);
}
/**
* Get the bank rule formatted conditions.
* @param bankRule
* @returns {string}
*/
protected conditionsFormatted(bankRule: any) {
return bankRule.conditions
.map((condition) => {
const field =
condition.field.charAt(0).toUpperCase() + condition.field.slice(1);
return `${field} ${condition.comparator} ${condition.value}`;
})
.join(bankRule.conditionsType === 'and' ? ' and ' : ' or ');
}
} }

View File

@@ -2,17 +2,36 @@
import { useMemo } from 'react'; import { useMemo } from 'react';
import { Intent, Tag } from '@blueprintjs/core'; import { Intent, Tag } from '@blueprintjs/core';
const applyToTypeAccessor = (rule) => {
return rule.apply_if_transaction_type === 'deposit' ? (
<Tag round intent={Intent.SUCCESS}>
Deposits
</Tag>
) : (
<Tag round intent={Intent.DANGER}>
Withdrawals
</Tag>
);
};
const conditionsAccessor = (rule) => (
<span style={{ fontSize: 12, color: '#5F6B7C' }}>
{rule.conditions_formatted}
</span>
);
const applyToAccessor = (rule) => (
<Tag intent={Intent.NONE} minimal>
{rule.assign_account_name}
</Tag>
);
export const useBankRulesTableColumns = () => { export const useBankRulesTableColumns = () => {
return useMemo( return useMemo(
() => [ () => [
{ {
Header: 'Apply to', Header: 'Apply to',
accessor: (rule) => accessor: applyToTypeAccessor,
rule.apply_if_transaction_type === 'deposit' ? (
<Tag round intent={Intent.SUCCESS}>Deposits</Tag>
) : (
<Tag round intent={Intent.DANGER}>Withdrawals</Tag>
),
}, },
{ {
Header: 'Rule Name', Header: 'Rule Name',
@@ -20,15 +39,15 @@ export const useBankRulesTableColumns = () => {
}, },
{ {
Header: 'Categorize As', Header: 'Categorize As',
accessor: () => 'Expense', accessor: 'assign_category_formatted',
}, },
{ {
Header: 'Apply To', Header: 'Apply To',
accessor: () => <Tag intent={Intent.NONE} minimal>All Accounts</Tag>, accessor: applyToAccessor,
}, },
{ {
Header: 'Conditions', Header: 'Conditions',
accessor: () => '', accessor: conditionsAccessor,
}, },
], ],
[], [],