Skip to content

Commit

Permalink
Merge pull request #51927 from huult/50563-fix-offline-category-updat…
Browse files Browse the repository at this point in the history
…e-gray-out-and-name

fix gray-out and name issue in offline category update
  • Loading branch information
youssef-lr authored Dec 4, 2024
2 parents 731cab1 + 425e743 commit 35e4761
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/libs/CategoryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {LocaleContextProps} from '@components/LocaleContextProvider';
import CONST from '@src/CONST';
import type {Policy, TaxRate, TaxRatesWithDefault} from '@src/types/onyx';
import type {ApprovalRule, ExpenseRule} from '@src/types/onyx/Policy';
import type {ApprovalRule, ExpenseRule, MccGroup} from '@src/types/onyx/Policy';
import * as CurrencyUtils from './CurrencyUtils';

function formatDefaultTaxRateText(translate: LocaleContextProps['translate'], taxID: string, taxRate: TaxRate, policyTaxRates?: TaxRatesWithDefault) {
Expand Down Expand Up @@ -68,4 +68,19 @@ function getCategoryDefaultTaxRate(expenseRules: ExpenseRule[], categoryName: st
return categoryDefaultTaxRate;
}

export {formatDefaultTaxRateText, formatRequireReceiptsOverText, getCategoryApproverRule, getCategoryExpenseRule, getCategoryDefaultTaxRate};
function updateCategoryInMccGroup(mccGroups: Record<string, MccGroup>, oldCategoryName: string, newCategoryName: string, shouldClearPendingAction?: boolean) {
if (oldCategoryName === newCategoryName) {
return mccGroups;
}

const updatedGroups: Record<string, MccGroup> = {};

for (const [key, group] of Object.entries(mccGroups || {})) {
updatedGroups[key] =
group.category === oldCategoryName ? {...group, category: newCategoryName, pendingAction: shouldClearPendingAction ? null : CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE} : group;
}

return updatedGroups;
}

export {formatDefaultTaxRateText, formatRequireReceiptsOverText, getCategoryApproverRule, getCategoryExpenseRule, getCategoryDefaultTaxRate, updateCategoryInMccGroup};
15 changes: 14 additions & 1 deletion src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, PolicyCategories, PolicyCategory, RecentlyUsedCategories, Report} from '@src/types/onyx';
import type {ApprovalRule, ExpenseRule} from '@src/types/onyx/Policy';
import type {ApprovalRule, ExpenseRule, MccGroup} from '@src/types/onyx/Policy';
import type {PolicyCategoryExpenseLimitType} from '@src/types/onyx/PolicyCategory';
import type {OnyxData} from '@src/types/onyx/Request';

Expand Down Expand Up @@ -549,8 +549,12 @@ function renamePolicyCategory(policyID: string, policyCategory: {oldName: string
const policyCategoryExpenseRule = CategoryUtils.getCategoryExpenseRule(policy?.rules?.expenseRules ?? [], policyCategory.oldName);
const approvalRules = policy?.rules?.approvalRules ?? [];
const expenseRules = policy?.rules?.expenseRules ?? [];
const mccGroup = policy?.mccGroup ?? {};
const updatedApprovalRules: ApprovalRule[] = lodashCloneDeep(approvalRules);
const updatedExpenseRules: ExpenseRule[] = lodashCloneDeep(expenseRules);
const clonedMccGroup: Record<string, MccGroup> = lodashCloneDeep(mccGroup);
const updatedMccGroup = CategoryUtils.updateCategoryInMccGroup(clonedMccGroup, policyCategory.oldName, policyCategory.newName);
const updatedMccGroupWithClearedPendingAction = CategoryUtils.updateCategoryInMccGroup(clonedMccGroup, policyCategory.oldName, policyCategory.newName, true);

if (policyCategoryExpenseRule) {
const ruleIndex = updatedExpenseRules.findIndex((rule) => rule.id === policyCategoryExpenseRule.id);
Expand Down Expand Up @@ -603,10 +607,18 @@ function renamePolicyCategory(policyID: string, policyCategory: {oldName: string
approvalRules: updatedApprovalRules,
expenseRules: updatedExpenseRules,
},
mccGroup: updatedMccGroup,
},
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
mccGroup: updatedMccGroupWithClearedPendingAction,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`,
Expand Down Expand Up @@ -646,6 +658,7 @@ function renamePolicyCategory(policyID: string, policyCategory: {oldName: string
rules: {
approvalRules,
},
mccGroup,
},
},
],
Expand Down
1 change: 1 addition & 0 deletions src/types/onyx/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1929,4 +1929,5 @@ export type {
ApprovalRule,
ExpenseRule,
NetSuiteConnectionConfig,
MccGroup,
};

0 comments on commit 35e4761

Please sign in to comment.