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 - Open Categories - Default spend categories are missing when workspace is created offline #56349

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
42 changes: 21 additions & 21 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2640,27 +2640,27 @@ const CONST = {
ARE_RULES_ENABLED: 'areRulesEnabled',
ARE_PER_DIEM_RATES_ENABLED: 'arePerDiemRatesEnabled',
},
DEFAULT_CATEGORIES: [
'Advertising',
'Benefits',
'Car',
'Equipment',
'Fees',
'Home Office',
'Insurance',
'Interest',
'Labor',
'Maintenance',
'Materials',
'Meals and Entertainment',
'Office Supplies',
'Other',
'Professional Services',
'Rent',
'Taxes',
'Travel',
'Utilities',
],
DEFAULT_CATEGORIES: {
ADVERTISING: 'Advertising',
BENEFITS: 'Benefits',
CAR: 'Car',
EQUIPMENT: 'Equipment',
FEES: 'Fees',
HOME_OFFICE: 'Home Office',
INSURANCE: 'Insurance',
INTEREST: 'Interest',
LABOR: 'Labor',
MAINTENANCE: 'Maintenance',
MATERIALS: 'Materials',
MEALS_AND_ENTERTAINMENT: 'Meals and Entertainment',
OFFICE_SUPPLIES: 'Office Supplies',
OTHER: 'Other',
PROFESSIONAL_SERVICES: 'Professional Services',
RENT: 'Rent',
TAXES: 'Taxes',
TRAVEL: 'Travel',
UTILITIES: 'Utilities',
},
OWNERSHIP_ERRORS: {
NO_BILLING_CARD: 'noBillingCard',
AMOUNT_OWED: 'amountOwed',
Expand Down
84 changes: 84 additions & 0 deletions src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,89 @@ function buildOptimisticPolicyCategories(policyID: string, categories: readonly
return onyxData;
}

function buildOptimisticMccGroup() {
const optimisticMccGroup: Record<'mccGroup', Record<string, MccGroup>> = {
mccGroup: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FitseTLT can help me by checking if we do the same when we update the MccGroup, my first thought is that we should create a const, but haven't looked much, can you please check?

Copy link
Contributor Author

@FitseTLT FitseTLT Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we update mcc group we only update the category of a specific groupID as in here

...mccGroup,
[groupID]: {
category,
groupID,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
},
},

but I have now updated the category part to take the values from default workspace categories const 👍

airlines: {
category: CONST.POLICY.DEFAULT_CATEGORIES.TRAVEL,
groupID: 'airlines',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
commuter: {
category: CONST.POLICY.DEFAULT_CATEGORIES.CAR,
groupID: 'commuter',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
gas: {
category: CONST.POLICY.DEFAULT_CATEGORIES.CAR,
groupID: 'gas',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
goods: {
category: CONST.POLICY.DEFAULT_CATEGORIES.MATERIALS,
groupID: 'goods',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
groceries: {
category: CONST.POLICY.DEFAULT_CATEGORIES.MEALS_AND_ENTERTAINMENT,
groupID: 'groceries',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
hotel: {
category: CONST.POLICY.DEFAULT_CATEGORIES.TRAVEL,
groupID: 'hotel',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
mail: {
category: CONST.POLICY.DEFAULT_CATEGORIES.OFFICE_SUPPLIES,
groupID: 'mail',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
meals: {
category: CONST.POLICY.DEFAULT_CATEGORIES.MEALS_AND_ENTERTAINMENT,
groupID: 'meals',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
rental: {
category: CONST.POLICY.DEFAULT_CATEGORIES.TRAVEL,
groupID: 'rental',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
services: {
category: CONST.POLICY.DEFAULT_CATEGORIES.PROFESSIONAL_SERVICES,
groupID: 'services',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
taxi: {
category: CONST.POLICY.DEFAULT_CATEGORIES.TRAVEL,
groupID: 'taxi',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
uncategorized: {
category: CONST.POLICY.DEFAULT_CATEGORIES.OTHER,
groupID: 'uncategorized',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
utilities: {
category: CONST.POLICY.DEFAULT_CATEGORIES.UTILITIES,
groupID: 'utilities',
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
},
};

const successMccGroup: Record<'mccGroup', Record<string, Partial<MccGroup>>> = {mccGroup: {}};
Object.keys(optimisticMccGroup.mccGroup).forEach((key) => (successMccGroup.mccGroup[key] = {pendingAction: null}));

const mccGroupData = {
optimisticData: optimisticMccGroup,
successData: successMccGroup,
failureData: {mccGroup: null},
};

return mccGroupData;
}

function updateImportSpreadsheetData(categoriesLength: number) {
const onyxData: OnyxData = {
successData: [
Expand Down Expand Up @@ -1369,6 +1452,7 @@ function getPolicyCategoriesData(policyID: string | undefined) {

export {
buildOptimisticPolicyCategories,
buildOptimisticMccGroup,
buildOptimisticPolicyRecentlyUsedCategories,
clearCategoryErrors,
createPolicyCategory,
Expand Down
11 changes: 7 additions & 4 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import type {Errors} from '@src/types/onyx/OnyxCommon';
import type {Attributes, CompanyAddress, CustomUnit, NetSuiteCustomList, NetSuiteCustomSegment, Rate, TaxRate} from '@src/types/onyx/Policy';
import type {OnyxData} from '@src/types/onyx/Request';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import {buildOptimisticPolicyCategories} from './Category';
import {buildOptimisticMccGroup, buildOptimisticPolicyCategories} from './Category';

type ReportCreationData = Record<
string,
Expand Down Expand Up @@ -1751,7 +1751,8 @@ function buildPolicyData(
pendingChatMembers,
} = ReportUtils.buildOptimisticWorkspaceChats(policyID, workspaceName, expenseReportId);

const optimisticCategoriesData = buildOptimisticPolicyCategories(policyID, CONST.POLICY.DEFAULT_CATEGORIES);
const optimisticCategoriesData = buildOptimisticPolicyCategories(policyID, Object.values(CONST.POLICY.DEFAULT_CATEGORIES));
const optimisticMccGroupData = buildOptimisticMccGroup();

const shouldSetCreatedWorkspaceAsActivePolicy = !!activePolicyID && allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`]?.type === CONST.POLICY.TYPE.PERSONAL;

Expand Down Expand Up @@ -1801,6 +1802,7 @@ function buildPolicyData(
},
avatarURL: file?.uri,
originalFileName: file?.name,
...optimisticMccGroupData.optimisticData,
},
},
{
Expand Down Expand Up @@ -1881,6 +1883,7 @@ function buildPolicyData(
description: null,
type: null,
},
...optimisticMccGroupData.successData,
},
},
{
Expand Down Expand Up @@ -1942,7 +1945,7 @@ function buildPolicyData(
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {employeeList: null},
value: {employeeList: null, ...optimisticMccGroupData.failureData},
},
{
onyxMethod: Onyx.METHOD.SET,
Expand Down Expand Up @@ -2133,7 +2136,7 @@ function createDraftWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policy
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES_DRAFT}${policyID}`,
value: CONST.POLICY.DEFAULT_CATEGORIES.reduce<Record<string, PolicyCategory>>((acc, category) => {
value: Object.values(CONST.POLICY.DEFAULT_CATEGORIES).reduce<Record<string, PolicyCategory>>((acc, category) => {
acc[category] = {
name: category,
enabled: true,
Expand Down
Loading