diff --git a/src/libs/CategoryUtils.ts b/src/libs/CategoryUtils.ts index f27b32360a84..479ae557eab6 100644 --- a/src/libs/CategoryUtils.ts +++ b/src/libs/CategoryUtils.ts @@ -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; @@ -61,4 +68,4 @@ function getCategoryDefaultTaxRate(expenseRules: ExpenseRule[], categoryName: st return categoryDefaultTaxRate; } -export {formatDefaultTaxRateText, formatRequireReceiptsOverText, getCategoryApproverRule, getCategoryDefaultTaxRate}; +export {formatDefaultTaxRateText, formatRequireReceiptsOverText, getCategoryApproverRule, getCategoryExpenseRule, getCategoryDefaultTaxRate}; diff --git a/src/libs/actions/Policy/Category.ts b/src/libs/actions/Policy/Category.ts index a9f6e376b80a..682610e7336b 100644 --- a/src/libs/actions/Policy/Category.ts +++ b/src/libs/actions/Policy/Category.ts @@ -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) { @@ -540,7 +552,7 @@ function renamePolicyCategory(policyID: string, policyCategory: {oldName: string return ruleCondition; }); - updatedApprovalRules[indexToUpdate] = policyCategoryRule; + updatedApprovalRules[indexToUpdate] = policyCategoryApproverRule; } const onyxData: OnyxData = { @@ -568,6 +580,7 @@ function renamePolicyCategory(policyID: string, policyCategory: {oldName: string value: { rules: { approvalRules: updatedApprovalRules, + expenseRules: updatedExpenseRules, }, }, },