Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - Category rules - default tax rate field is blank for a moment after editing category name #49867

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/libs/CategoryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ function getCategoryApproverRule(approvalRules: ApprovalRule[], categoryName: st
return approverRule;
}

function getCategoryExpenseRule(expenseRules: ExpenseRule[], categoryName: string) {
const expenseRule = expenseRules?.find((rule) =>
rule.applyWhen.find(({condition, field, value}) => condition === CONST.POLICY.RULE_CONDITIONS.MATCHES && field === CONST.POLICY.FIELDS.CATEGORY && value === categoryName),
);
return expenseRule;
}

function getCategoryDefaultTaxRate(expenseRules: ExpenseRule[], categoryName: string, defaultTaxRate?: string) {
const categoryDefaultTaxRate = expenseRules?.find((rule) => rule.applyWhen.some((when) => when.value === categoryName))?.tax?.field_id_TAX?.externalID;

Expand All @@ -61,4 +68,4 @@ function getCategoryDefaultTaxRate(expenseRules: ExpenseRule[], categoryName: st
return categoryDefaultTaxRate;
}

export {formatDefaultTaxRateText, formatRequireReceiptsOverText, getCategoryApproverRule, getCategoryDefaultTaxRate};
export {formatDefaultTaxRateText, formatRequireReceiptsOverText, getCategoryApproverRule, getCategoryExpenseRule, getCategoryDefaultTaxRate};
23 changes: 18 additions & 5 deletions src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,14 +524,26 @@ function renamePolicyCategory(policyID: string, policyCategory: {oldName: string
const policy = PolicyUtils.getPolicy(policyID);
const policyCategoryToUpdate = allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`]?.[policyCategory.oldName];

const policyCategoryRule = CategoryUtils.getCategoryApproverRule(policy?.rules?.approvalRules ?? [], policyCategory.oldName);
const policyCategoryApproverRule = CategoryUtils.getCategoryApproverRule(policy?.rules?.approvalRules ?? [], policyCategory.oldName);
const policyCategoryExpenseRule = CategoryUtils.getCategoryExpenseRule(policy?.rules?.expenseRules ?? [], policyCategory.oldName);
const approvalRules = policy?.rules?.approvalRules ?? [];
const expenseRules = policy?.rules?.expenseRules ?? [];
const updatedApprovalRules: ApprovalRule[] = lodashCloneDeep(approvalRules);
const updatedExpenseRules: ExpenseRule[] = lodashCloneDeep(expenseRules);

if (policyCategoryExpenseRule) {
const ruleIndex = updatedExpenseRules.findIndex((rule) => rule.id === policyCategoryExpenseRule.id);
policyCategoryExpenseRule.applyWhen = policyCategoryExpenseRule.applyWhen.map((applyWhen) => ({
...applyWhen,
...(applyWhen.field === CONST.POLICY.FIELDS.CATEGORY && applyWhen.value === policyCategory.oldName && {value: policyCategory.newName}),
}));
updatedExpenseRules[ruleIndex] = policyCategoryExpenseRule;
}

// Its related by name, so the corresponding rule has to be updated to handle offline scenario
if (policyCategoryRule) {
const indexToUpdate = updatedApprovalRules.findIndex((rule) => rule.id === policyCategoryRule.id);
policyCategoryRule.applyWhen = policyCategoryRule.applyWhen.map((ruleCondition) => {
if (policyCategoryApproverRule) {
const indexToUpdate = updatedApprovalRules.findIndex((rule) => rule.id === policyCategoryApproverRule.id);
policyCategoryApproverRule.applyWhen = policyCategoryApproverRule.applyWhen.map((ruleCondition) => {
const {value, field, condition} = ruleCondition;

if (value === policyCategory.oldName && field === CONST.POLICY.FIELDS.CATEGORY && condition === CONST.POLICY.RULE_CONDITIONS.MATCHES) {
Expand All @@ -540,7 +552,7 @@ function renamePolicyCategory(policyID: string, policyCategory: {oldName: string

return ruleCondition;
});
updatedApprovalRules[indexToUpdate] = policyCategoryRule;
updatedApprovalRules[indexToUpdate] = policyCategoryApproverRule;
}

const onyxData: OnyxData = {
Expand Down Expand Up @@ -568,6 +580,7 @@ function renamePolicyCategory(policyID: string, policyCategory: {oldName: string
value: {
rules: {
approvalRules: updatedApprovalRules,
expenseRules: updatedExpenseRules,
},
},
},
Expand Down
Loading