From 98516301518eb6ee076ccc4afc97af15c67f77ff Mon Sep 17 00:00:00 2001 From: allgandalf Date: Mon, 8 Sep 2025 12:08:30 +0530 Subject: [PATCH 1/6] remove usage of Onyx.connect --- ...eateWorkspaceReportFieldListValueParams.ts | 2 +- .../CreateWorkspaceReportFieldParams.ts | 2 +- .../API/parameters/DeletePolicyReportField.ts | 2 +- ...ableWorkspaceReportFieldListValueParams.ts | 2 +- ...moveWorkspaceReportFieldListValueParams.ts | 2 +- ...eWorkspaceReportFieldInitialValueParams.ts | 2 +- src/libs/actions/Policy/ReportField.ts | 126 +++++++++--------- tests/actions/ReportFieldTest.ts | 85 ++++++------ 8 files changed, 109 insertions(+), 114 deletions(-) diff --git a/src/libs/API/parameters/CreateWorkspaceReportFieldListValueParams.ts b/src/libs/API/parameters/CreateWorkspaceReportFieldListValueParams.ts index 950287bc5d04..a4b8dd81b777 100644 --- a/src/libs/API/parameters/CreateWorkspaceReportFieldListValueParams.ts +++ b/src/libs/API/parameters/CreateWorkspaceReportFieldListValueParams.ts @@ -1,5 +1,5 @@ type CreateWorkspaceReportFieldListValueParams = { - policyID: string; + policyID: string | undefined; /** * Stringified JSON object with type of following structure: * Array diff --git a/src/libs/API/parameters/CreateWorkspaceReportFieldParams.ts b/src/libs/API/parameters/CreateWorkspaceReportFieldParams.ts index 33692d210959..d1435c747c72 100644 --- a/src/libs/API/parameters/CreateWorkspaceReportFieldParams.ts +++ b/src/libs/API/parameters/CreateWorkspaceReportFieldParams.ts @@ -1,5 +1,5 @@ type CreateWorkspaceReportFieldParams = { - policyID: string; + policyID: string | undefined; /** * Stringified JSON object with type of following structure: * Array diff --git a/src/libs/API/parameters/DeletePolicyReportField.ts b/src/libs/API/parameters/DeletePolicyReportField.ts index d79e9b07249e..56da61990fc9 100644 --- a/src/libs/API/parameters/DeletePolicyReportField.ts +++ b/src/libs/API/parameters/DeletePolicyReportField.ts @@ -1,5 +1,5 @@ type DeletePolicyReportField = { - policyID: string; + policyID: string | undefined; /** * Stringified JSON object with type of following structure: * Array diff --git a/src/libs/API/parameters/EnableWorkspaceReportFieldListValueParams.ts b/src/libs/API/parameters/EnableWorkspaceReportFieldListValueParams.ts index 7c54a2f4c68b..21d5a25f11f7 100644 --- a/src/libs/API/parameters/EnableWorkspaceReportFieldListValueParams.ts +++ b/src/libs/API/parameters/EnableWorkspaceReportFieldListValueParams.ts @@ -1,5 +1,5 @@ type EnableWorkspaceReportFieldListValueParams = { - policyID: string; + policyID: string | undefined; /** * Stringified JSON object with type of following structure: * Array diff --git a/src/libs/API/parameters/RemoveWorkspaceReportFieldListValueParams.ts b/src/libs/API/parameters/RemoveWorkspaceReportFieldListValueParams.ts index 94d90a8dbaae..934ca399d665 100644 --- a/src/libs/API/parameters/RemoveWorkspaceReportFieldListValueParams.ts +++ b/src/libs/API/parameters/RemoveWorkspaceReportFieldListValueParams.ts @@ -1,5 +1,5 @@ type RemoveWorkspaceReportFieldListValueParams = { - policyID: string; + policyID: string | undefined; /** * Stringified JSON object with type of following structure: * Array diff --git a/src/libs/API/parameters/UpdateWorkspaceReportFieldInitialValueParams.ts b/src/libs/API/parameters/UpdateWorkspaceReportFieldInitialValueParams.ts index a72781ff1c37..628e3ac8b960 100644 --- a/src/libs/API/parameters/UpdateWorkspaceReportFieldInitialValueParams.ts +++ b/src/libs/API/parameters/UpdateWorkspaceReportFieldInitialValueParams.ts @@ -1,5 +1,5 @@ type UpdateWorkspaceReportFieldInitialValueParams = { - policyID: string; + policyID: string | undefined; /** * Stringified JSON object with type of following structure: * Array diff --git a/src/libs/actions/Policy/ReportField.ts b/src/libs/actions/Policy/ReportField.ts index 5aac8c1a1c3b..8bb69ee9b5f2 100644 --- a/src/libs/actions/Policy/ReportField.ts +++ b/src/libs/actions/Policy/ReportField.ts @@ -1,5 +1,5 @@ import cloneDeep from 'lodash/cloneDeep'; -import type {NullishDeep, OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx'; +import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import * as API from '@libs/API'; import type { @@ -24,39 +24,6 @@ import type {Policy, PolicyReportField, Report} from '@src/types/onyx'; import type {OnyxValueWithOfflineFeedback} from '@src/types/onyx/OnyxCommon'; import type {OnyxData} from '@src/types/onyx/Request'; -const allPolicies: OnyxCollection = {}; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.POLICY, - callback: (value, key) => { - if (!key) { - return; - } - if (value === null || value === undefined) { - // If we are deleting a policy, we have to check every report linked to that policy - // and unset the draft indicator (pencil icon) alongside removing any draft comments. Clearing these values will keep the newly archived chats from being displayed in the LHN. - // More info: https://github.com/Expensify/App/issues/14260 - const policyID = key.replace(ONYXKEYS.COLLECTION.POLICY, ''); - const policyReports = ReportUtils.getAllPolicyReports(policyID); - const cleanUpMergeQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT}${string}`, NullishDeep> = {}; - const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${string}` | `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; - policyReports.forEach((policyReport) => { - if (!policyReport) { - return; - } - const {reportID} = policyReport; - cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`] = null; - cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}`] = null; - }); - Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, cleanUpMergeQueries); - Onyx.multiSet(cleanUpSetQueries); - delete allPolicies[key]; - return; - } - - allPolicies[key] = value; - }, -}); - type CreateReportFieldsListValueParams = { valueName: string; listValues: string[]; @@ -84,8 +51,38 @@ type DeleteReportFieldsListValueParams = { type CreateReportFieldParams = Pick & { listValues: string[]; disabledListValues: boolean[]; - policyID: string; policyExpenseReports: Array> | undefined; + policy: OnyxEntry; +}; + +type DeleteReportFieldsParams = { + reportFieldsToUpdate: string[]; + policy: OnyxEntry; +}; + +type RemoveReportFieldListValueParams = { + valueIndexes: number[]; + reportFieldID: string; + policy: OnyxEntry; +}; + +type AddReportFieldListValueParams = { + valueName: string; + reportFieldID: string; + policy: OnyxEntry; +}; + +type UpdateReportFieldListValueEnabledParams = { + valueIndexes: number[]; + enabled: boolean; + reportFieldID: string; + policy: OnyxEntry; +}; + +type UpdateReportFieldInitialValueParams = { + newInitialValue: string; + reportFieldID: string; + policy: OnyxEntry; }; function openPolicyReportFieldsPage(policyID: string) { @@ -170,8 +167,8 @@ function deleteReportFieldsListValue({valueIndexes, listValues, disabledListValu /** * Creates a new report field. */ -function createReportField({name, type, initialValue, listValues, disabledListValues, policyID, policyExpenseReports}: CreateReportFieldParams) { - const previousFieldList = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.fieldList ?? {}; +function createReportField({name, type, initialValue, listValues, disabledListValues, policyExpenseReports, policy}: CreateReportFieldParams) { + const previousFieldList = policy?.fieldList ?? {}; const fieldID = WorkspaceReportFieldUtils.generateFieldID(name); const fieldKey = ReportUtils.getReportFieldKey(fieldID); const optimisticReportFieldDataForPolicy: Omit, 'value'> = { @@ -191,7 +188,7 @@ function createReportField({name, type, initialValue, listValues, disabledListVa const optimisticData = [ { - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`, onyxMethod: Onyx.METHOD.MERGE, value: { fieldList: { @@ -213,7 +210,7 @@ function createReportField({name, type, initialValue, listValues, disabledListVa const failureData = [ { - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`, onyxMethod: Onyx.METHOD.MERGE, value: { fieldList: { @@ -239,7 +236,7 @@ function createReportField({name, type, initialValue, listValues, disabledListVa optimisticData, successData: [ { - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`, onyxMethod: Onyx.METHOD.MERGE, value: { fieldList: { @@ -253,15 +250,14 @@ function createReportField({name, type, initialValue, listValues, disabledListVa }; const parameters: CreateWorkspaceReportFieldParams = { - policyID, + policyID: policy?.id, reportFields: JSON.stringify([optimisticReportFieldDataForPolicy]), }; API.write(WRITE_COMMANDS.CREATE_WORKSPACE_REPORT_FIELD, parameters, onyxData); } -function deleteReportFields(policyID: string, reportFieldsToUpdate: string[]) { - const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]; +function deleteReportFields({policy, reportFieldsToUpdate}: DeleteReportFieldsParams) { const allReportFields = policy?.fieldList ?? {}; const updatedReportFields = Object.fromEntries(Object.entries(allReportFields).filter(([key]) => !reportFieldsToUpdate.includes(key))); @@ -284,7 +280,7 @@ function deleteReportFields(policyID: string, reportFieldsToUpdate: string[]) { optimisticData: [ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`, value: { fieldList: optimisticReportFields, }, @@ -293,7 +289,7 @@ function deleteReportFields(policyID: string, reportFieldsToUpdate: string[]) { successData: [ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`, value: { fieldList: successReportFields, errorFields: null, @@ -303,7 +299,7 @@ function deleteReportFields(policyID: string, reportFieldsToUpdate: string[]) { failureData: [ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`, value: { fieldList: failureReportFields, errorFields: { @@ -315,7 +311,7 @@ function deleteReportFields(policyID: string, reportFieldsToUpdate: string[]) { }; const parameters: DeletePolicyReportField = { - policyID, + policyID: policy?.id, reportFields: JSON.stringify(Object.values(updatedReportFields)), }; @@ -325,8 +321,8 @@ function deleteReportFields(policyID: string, reportFieldsToUpdate: string[]) { /** * Updates the initial value of a report field. */ -function updateReportFieldInitialValue(policyID: string, reportFieldID: string, newInitialValue: string) { - const previousFieldList = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.fieldList ?? {}; +function updateReportFieldInitialValue({policy, reportFieldID, newInitialValue}: UpdateReportFieldInitialValueParams) { + const previousFieldList = policy?.fieldList ?? {}; const fieldKey = ReportUtils.getReportFieldKey(reportFieldID); const updatedReportField: PolicyReportField = { ...previousFieldList[fieldKey], @@ -335,7 +331,7 @@ function updateReportFieldInitialValue(policyID: string, reportFieldID: string, const onyxData: OnyxData = { optimisticData: [ { - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`, onyxMethod: Onyx.METHOD.MERGE, value: { fieldList: { @@ -347,7 +343,7 @@ function updateReportFieldInitialValue(policyID: string, reportFieldID: string, ], successData: [ { - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`, onyxMethod: Onyx.METHOD.MERGE, value: { fieldList: { @@ -359,7 +355,7 @@ function updateReportFieldInitialValue(policyID: string, reportFieldID: string, ], failureData: [ { - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`, onyxMethod: Onyx.METHOD.MERGE, value: { fieldList: { @@ -373,15 +369,15 @@ function updateReportFieldInitialValue(policyID: string, reportFieldID: string, ], }; const parameters: UpdateWorkspaceReportFieldInitialValueParams = { - policyID, + policyID: policy?.id, reportFields: JSON.stringify([updatedReportField]), }; API.write(WRITE_COMMANDS.UPDATE_WORKSPACE_REPORT_FIELD_INITIAL_VALUE, parameters, onyxData); } -function updateReportFieldListValueEnabled(policyID: string, reportFieldID: string, valueIndexes: number[], enabled: boolean) { - const previousFieldList = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.fieldList ?? {}; +function updateReportFieldListValueEnabled({policy, reportFieldID, valueIndexes, enabled}: UpdateReportFieldListValueEnabledParams) { + const previousFieldList = policy?.fieldList ?? {}; const fieldKey = ReportUtils.getReportFieldKey(reportFieldID); const reportField = previousFieldList[fieldKey]; @@ -400,7 +396,7 @@ function updateReportFieldListValueEnabled(policyID: string, reportFieldID: stri const onyxData: OnyxData = { optimisticData: [ { - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`, onyxMethod: Onyx.METHOD.MERGE, value: { fieldList: { @@ -412,7 +408,7 @@ function updateReportFieldListValueEnabled(policyID: string, reportFieldID: stri }; const parameters: EnableWorkspaceReportFieldListValueParams = { - policyID, + policyID: policy?.id, reportFields: JSON.stringify([updatedReportField]), }; @@ -422,8 +418,8 @@ function updateReportFieldListValueEnabled(policyID: string, reportFieldID: stri /** * Adds a new option to the list type report field on a workspace. */ -function addReportFieldListValue(policyID: string, reportFieldID: string, valueName: string) { - const previousFieldList = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.fieldList ?? {}; +function addReportFieldListValue({policy, reportFieldID, valueName}: AddReportFieldListValueParams) { + const previousFieldList = policy?.fieldList ?? {}; const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID); const reportField = previousFieldList[reportFieldKey]; const updatedReportField = cloneDeep(reportField); @@ -435,7 +431,7 @@ function addReportFieldListValue(policyID: string, reportFieldID: string, valueN const onyxData: OnyxData = { optimisticData: [ { - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`, onyxMethod: Onyx.METHOD.MERGE, value: { fieldList: { @@ -447,7 +443,7 @@ function addReportFieldListValue(policyID: string, reportFieldID: string, valueN }; const parameters: CreateWorkspaceReportFieldListValueParams = { - policyID, + policyID: policy?.id, reportFields: JSON.stringify([updatedReportField]), }; @@ -457,8 +453,8 @@ function addReportFieldListValue(policyID: string, reportFieldID: string, valueN /** * Removes a list value from the workspace report fields. */ -function removeReportFieldListValue(policyID: string, reportFieldID: string, valueIndexes: number[]) { - const previousFieldList = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.fieldList ?? {}; +function removeReportFieldListValue({policy, reportFieldID, valueIndexes}: RemoveReportFieldListValueParams) { + const previousFieldList = policy?.fieldList ?? {}; const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID); const reportField = previousFieldList[reportFieldKey]; const updatedReportField = cloneDeep(reportField); @@ -480,7 +476,7 @@ function removeReportFieldListValue(policyID: string, reportFieldID: string, val const onyxData: OnyxData = { optimisticData: [ { - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + key: `${ONYXKEYS.COLLECTION.POLICY}${policy?.id}`, onyxMethod: Onyx.METHOD.MERGE, value: { fieldList: { @@ -492,7 +488,7 @@ function removeReportFieldListValue(policyID: string, reportFieldID: string, val }; const parameters: RemoveWorkspaceReportFieldListValueParams = { - policyID, + policyID: policy?.id, reportFields: JSON.stringify([updatedReportField]), }; diff --git a/tests/actions/ReportFieldTest.ts b/tests/actions/ReportFieldTest.ts index 5035eb222106..c791d006bb10 100644 --- a/tests/actions/ReportFieldTest.ts +++ b/tests/actions/ReportFieldTest.ts @@ -53,7 +53,7 @@ describe('actions/ReportField', () => { Onyx.set(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT, {}); await waitForBatchedUpdates(); - const policyID = Policy.generatePolicyID(); + const fakePolicy = createRandomPolicy(0); const reportFieldName = 'Test Field'; const reportFieldID = generateFieldID(reportFieldName); const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID); @@ -73,7 +73,7 @@ describe('actions/ReportField', () => { isTax: false, }; const createReportFieldArguments: CreateReportFieldParams = { - policyID, + policy: fakePolicy, name: reportFieldName, type: CONST.REPORT_FIELD_TYPES.TEXT, initialValue: 'Default Value', @@ -85,7 +85,7 @@ describe('actions/ReportField', () => { ReportField.createReportField(createReportFieldArguments); await waitForBatchedUpdates(); - let policy: OnyxEntry = await connectToFetchPolicy(policyID); + let policy: OnyxEntry = await connectToFetchPolicy(fakePolicy.id); // check if the new report field was added to the policy expect(policy?.fieldList).toStrictEqual({ @@ -96,7 +96,7 @@ describe('actions/ReportField', () => { mockFetch.resume(); await waitForBatchedUpdates(); - policy = await connectToFetchPolicy(policyID); + policy = await connectToFetchPolicy(fakePolicy.id); // Check if the policy pending action was cleared expect(policy?.fieldList?.[reportFieldKey]?.pendingAction).toBeFalsy(); @@ -107,7 +107,7 @@ describe('actions/ReportField', () => { Onyx.set(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT, {}); await waitForBatchedUpdates(); - const policyID = Policy.generatePolicyID(); + const fakePolicy = createRandomPolicy(0); const reportFieldName = 'Test Field 2'; const reportFieldID = generateFieldID(reportFieldName); const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID); @@ -128,7 +128,7 @@ describe('actions/ReportField', () => { isTax: false, }; const createReportFieldArguments: CreateReportFieldParams = { - policyID, + policy: fakePolicy, name: reportFieldName, type: CONST.REPORT_FIELD_TYPES.DATE, initialValue: defaultDate, @@ -140,7 +140,7 @@ describe('actions/ReportField', () => { ReportField.createReportField(createReportFieldArguments); await waitForBatchedUpdates(); - let policy: OnyxEntry = await connectToFetchPolicy(policyID); + let policy: OnyxEntry = await connectToFetchPolicy(fakePolicy.id); // check if the new report field was added to the policy expect(policy?.fieldList).toStrictEqual({ @@ -151,7 +151,7 @@ describe('actions/ReportField', () => { mockFetch.resume(); await waitForBatchedUpdates(); - policy = await connectToFetchPolicy(policyID); + policy = await connectToFetchPolicy(fakePolicy.id); // Check if the policy pending action was cleared expect(policy?.fieldList?.[reportFieldKey]?.pendingAction).toBeFalsy(); @@ -165,7 +165,7 @@ describe('actions/ReportField', () => { }); await waitForBatchedUpdates(); - const policyID = Policy.generatePolicyID(); + const fakePolicy = createRandomPolicy(0); const reportFieldName = 'Test Field 3'; const reportFieldID = generateFieldID(reportFieldName); const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID); @@ -185,7 +185,7 @@ describe('actions/ReportField', () => { pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }; const createReportFieldArguments: CreateReportFieldParams = { - policyID, + policy: fakePolicy, name: reportFieldName, type: CONST.REPORT_FIELD_TYPES.LIST, initialValue: '', @@ -197,7 +197,7 @@ describe('actions/ReportField', () => { ReportField.createReportField(createReportFieldArguments); await waitForBatchedUpdates(); - let policy: OnyxEntry = await connectToFetchPolicy(policyID); + let policy: OnyxEntry = await connectToFetchPolicy(fakePolicy.id); // check if the new report field was added to the policy expect(policy?.fieldList).toStrictEqual({ @@ -208,7 +208,7 @@ describe('actions/ReportField', () => { mockFetch.resume(); await waitForBatchedUpdates(); - policy = await connectToFetchPolicy(policyID); + policy = await connectToFetchPolicy(fakePolicy.id); // Check if the policy pending action was cleared expect(policy?.fieldList?.[reportFieldKey].pendingAction).toBeFalsy(); @@ -249,7 +249,7 @@ describe('actions/ReportField', () => { [reportFieldKey]: fakeReportField, }); - ReportField.deleteReportFields(fakePolicy.id, [reportFieldKey]); + ReportField.deleteReportFields({policy, reportFieldsToUpdate: [reportFieldKey]}); await waitForBatchedUpdates(); // Check for success data @@ -297,7 +297,7 @@ describe('actions/ReportField', () => { // Check for failure data mockFetch.fail(); - ReportField.deleteReportFields(policyID, [reportFieldKey]); + ReportField.deleteReportFields({policy, reportFieldsToUpdate: [reportFieldKey]}); await waitForBatchedUpdates(); mockFetch.resume(); @@ -316,7 +316,7 @@ describe('actions/ReportField', () => { it('updates the initial value of a text report field', async () => { mockFetch.pause(); - const policyID = Policy.generatePolicyID(); + const fakePolicy = createRandomPolicy(0); const reportFieldName = 'Test Field'; const oldInitialValue = 'Old initial value'; const newInitialValue = 'New initial value'; @@ -335,15 +335,15 @@ describe('actions/ReportField', () => { externalIDs: [], isTax: false, }; - const fakePolicy = createRandomPolicy(Number(policyID)); - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {...fakePolicy, fieldList: {[reportFieldKey]: reportField}}); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy?.id}`, {...fakePolicy, fieldList: {[reportFieldKey]: reportField}}); await waitForBatchedUpdates(); + let policy: OnyxEntry = await connectToFetchPolicy(fakePolicy.id); - ReportField.updateReportFieldInitialValue(policyID, reportFieldID, newInitialValue); + ReportField.updateReportFieldInitialValue({policy, reportFieldID, newInitialValue}); await waitForBatchedUpdates(); - let policy: OnyxEntry = await connectToFetchPolicy(policyID); + policy = await connectToFetchPolicy(fakePolicy.id); // check if the updated report field was set to the policy expect(policy?.fieldList).toStrictEqual({ @@ -358,7 +358,7 @@ describe('actions/ReportField', () => { mockFetch.resume(); await waitForBatchedUpdates(); - policy = await connectToFetchPolicy(policyID); + policy = await connectToFetchPolicy(fakePolicy.id); // Check if the policy pending action was cleared expect(policy?.fieldList?.[reportFieldKey].pendingAction).toBeFalsy(); @@ -367,7 +367,7 @@ describe('actions/ReportField', () => { it('updates the initial value of a text report field when api returns an error', async () => { mockFetch.pause(); - const policyID = Policy.generatePolicyID(); + const fakePolicy = createRandomPolicy(0); const reportFieldName = 'Test Field'; const oldInitialValue = 'Old initial value'; const newInitialValue = 'New initial value'; @@ -386,15 +386,16 @@ describe('actions/ReportField', () => { externalIDs: [], isTax: false, }; - const fakePolicy = createRandomPolicy(Number(policyID)); - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {...fakePolicy, fieldList: {[reportFieldKey]: reportField}}); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy?.id}`, {...fakePolicy, fieldList: {[reportFieldKey]: reportField}}); await waitForBatchedUpdates(); - ReportField.updateReportFieldInitialValue(policyID, reportFieldID, newInitialValue); + let policy: OnyxEntry = await connectToFetchPolicy(fakePolicy.id); + + ReportField.updateReportFieldInitialValue({policy, reportFieldID, newInitialValue}); await waitForBatchedUpdates(); - let policy: OnyxEntry = await connectToFetchPolicy(policyID); + policy = await connectToFetchPolicy(fakePolicy.id); // check if the updated report field was set to the policy expect(policy?.fieldList).toStrictEqual({ @@ -410,7 +411,7 @@ describe('actions/ReportField', () => { mockFetch.resume(); await waitForBatchedUpdates(); - policy = await connectToFetchPolicy(policyID); + policy = await connectToFetchPolicy(fakePolicy.id); // check if the updated report field was reset in the policy expect(policy?.fieldList).toStrictEqual({ @@ -425,7 +426,7 @@ describe('actions/ReportField', () => { it('updates the enabled flag of report field list values', async () => { mockFetch.pause(); - const policyID = Policy.generatePolicyID(); + const fakePolicy = createRandomPolicy(0); const reportFieldName = 'Test Field'; const valueIndexesTpUpdate = [1, 2]; const reportFieldID = generateFieldID(reportFieldName); @@ -444,15 +445,14 @@ describe('actions/ReportField', () => { isTax: false, value: CONST.REPORT_FIELD_TYPES.LIST, }; - const fakePolicy = createRandomPolicy(Number(policyID)); - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {...fakePolicy, fieldList: {[reportFieldKey]: reportField}}); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy?.id}`, {...fakePolicy, fieldList: {[reportFieldKey]: reportField}}); await waitForBatchedUpdates(); - - ReportField.updateReportFieldListValueEnabled(policyID, reportFieldID, valueIndexesTpUpdate, false); + let policy: OnyxEntry = await connectToFetchPolicy(fakePolicy.id); + ReportField.updateReportFieldListValueEnabled({policy, reportFieldID, valueIndexes: valueIndexesTpUpdate, enabled: false}); await waitForBatchedUpdates(); - const policy: OnyxEntry = await connectToFetchPolicy(policyID); + policy = await connectToFetchPolicy(fakePolicy.id); // check if the new report field was added to the policy optimistically expect(policy?.fieldList).toStrictEqual({ @@ -469,7 +469,7 @@ describe('actions/ReportField', () => { it('adds a new value to a report field list', async () => { mockFetch.pause(); - const policyID = Policy.generatePolicyID(); + const fakePolicy = createRandomPolicy(0); const reportFieldName = 'Test Field'; const reportFieldID = generateFieldID(reportFieldName); const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID); @@ -487,16 +487,15 @@ describe('actions/ReportField', () => { isTax: false, value: CONST.REPORT_FIELD_TYPES.LIST, }; - const fakePolicy = createRandomPolicy(Number(policyID)); const newListValueName = 'Value 4'; - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {...fakePolicy, fieldList: {[reportFieldKey]: reportField}}); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy?.id}`, {...fakePolicy, fieldList: {[reportFieldKey]: reportField}}); await waitForBatchedUpdates(); - - ReportField.addReportFieldListValue(policyID, reportFieldID, newListValueName); + let policy: OnyxEntry = await connectToFetchPolicy(fakePolicy.id); + ReportField.addReportFieldListValue({policy, reportFieldID, valueName: newListValueName}); await waitForBatchedUpdates(); - const policy: OnyxEntry = await connectToFetchPolicy(policyID); + policy = await connectToFetchPolicy(fakePolicy.id); // Check if the new report field was added to the policy optimistically expect(policy?.fieldList).toStrictEqual({ [reportFieldKey]: { @@ -512,7 +511,6 @@ describe('actions/ReportField', () => { it('removes list values from a report field list', async () => { mockFetch.pause(); - const policyID = Policy.generatePolicyID(); const reportFieldName = 'Test Field'; const reportFieldID = generateFieldID(reportFieldName); const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID); @@ -530,15 +528,16 @@ describe('actions/ReportField', () => { isTax: false, value: CONST.REPORT_FIELD_TYPES.LIST, }; - const fakePolicy = createRandomPolicy(Number(policyID)); + const fakePolicy = createRandomPolicy(0); - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {...fakePolicy, fieldList: {[reportFieldKey]: reportField}}); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy?.id}`, {...fakePolicy, fieldList: {[reportFieldKey]: reportField}}); await waitForBatchedUpdates(); - ReportField.removeReportFieldListValue(policyID, reportFieldID, [1, 2]); + let policy: OnyxEntry = await connectToFetchPolicy(fakePolicy.id); + ReportField.removeReportFieldListValue({policy, reportFieldID, valueIndexes: [1, 2]}); await waitForBatchedUpdates(); - const policy: OnyxEntry = await connectToFetchPolicy(policyID); + policy = await connectToFetchPolicy(fakePolicy.id); // Check if the values were removed from the report field optimistically expect(policy?.fieldList).toStrictEqual({ From 88956e4b28f47b583fb5a576f4139c99f85a07ae Mon Sep 17 00:00:00 2001 From: allgandalf Date: Mon, 8 Sep 2025 12:14:10 +0530 Subject: [PATCH 2/6] update props on UI layer --- src/pages/workspace/reports/CreateReportFieldsPage.tsx | 2 +- .../workspace/reports/ReportFieldsAddListValuePage.tsx | 2 +- .../workspace/reports/ReportFieldsInitialValuePage.tsx | 4 ++-- .../workspace/reports/ReportFieldsListValuesPage.tsx | 8 ++++---- src/pages/workspace/reports/ReportFieldsSettingsPage.tsx | 2 +- .../workspace/reports/ReportFieldsValueSettingsPage.tsx | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/pages/workspace/reports/CreateReportFieldsPage.tsx b/src/pages/workspace/reports/CreateReportFieldsPage.tsx index e25b4e849fd6..0e62e5396289 100644 --- a/src/pages/workspace/reports/CreateReportFieldsPage.tsx +++ b/src/pages/workspace/reports/CreateReportFieldsPage.tsx @@ -52,7 +52,7 @@ function WorkspaceCreateReportFieldsPage({ const submitForm = useCallback( (values: FormOnyxValues) => { createReportField({ - policyID, + policy, name: values[INPUT_IDS.NAME], type: values[INPUT_IDS.TYPE], initialValue: !(values[INPUT_IDS.TYPE] === CONST.REPORT_FIELD_TYPES.LIST && availableListValuesLength === 0) ? values[INPUT_IDS.INITIAL_VALUE] : '', diff --git a/src/pages/workspace/reports/ReportFieldsAddListValuePage.tsx b/src/pages/workspace/reports/ReportFieldsAddListValuePage.tsx index e2a8f4e885bf..fa6ebaa87487 100644 --- a/src/pages/workspace/reports/ReportFieldsAddListValuePage.tsx +++ b/src/pages/workspace/reports/ReportFieldsAddListValuePage.tsx @@ -58,7 +58,7 @@ function ReportFieldsAddListValuePage({ const createValue = useCallback( (values: FormOnyxValues) => { if (reportFieldID) { - addReportFieldListValue(policyID, reportFieldID, values[INPUT_IDS.VALUE_NAME]); + addReportFieldListValue({policy, reportFieldID, valueName: values[INPUT_IDS.VALUE_NAME]}); } else { createReportFieldsListValue({ valueName: values[INPUT_IDS.VALUE_NAME], diff --git a/src/pages/workspace/reports/ReportFieldsInitialValuePage.tsx b/src/pages/workspace/reports/ReportFieldsInitialValuePage.tsx index 4fdae0c8452b..220daa9c8015 100644 --- a/src/pages/workspace/reports/ReportFieldsInitialValuePage.tsx +++ b/src/pages/workspace/reports/ReportFieldsInitialValuePage.tsx @@ -49,7 +49,7 @@ function ReportFieldsInitialValuePage({ const submitForm = useCallback( (values: FormOnyxValues) => { if (currentInitialValue !== values.initialValue) { - updateReportFieldInitialValue(policyID, reportFieldID, values.initialValue); + updateReportFieldInitialValue({policy, reportFieldID, newInitialValue: values.initialValue}); } Navigation.goBack(); }, @@ -57,7 +57,7 @@ function ReportFieldsInitialValuePage({ ); const submitListValueUpdate = (value: string) => { - updateReportFieldInitialValue(policyID, reportFieldID, currentInitialValue === value ? '' : value); + updateReportFieldInitialValue({policy, reportFieldID, newInitialValue: currentInitialValue === value ? '' : value}); Navigation.goBack(); }; diff --git a/src/pages/workspace/reports/ReportFieldsListValuesPage.tsx b/src/pages/workspace/reports/ReportFieldsListValuesPage.tsx index 8f740f12f6f8..7c050809bd28 100644 --- a/src/pages/workspace/reports/ReportFieldsListValuesPage.tsx +++ b/src/pages/workspace/reports/ReportFieldsListValuesPage.tsx @@ -102,7 +102,7 @@ function ReportFieldsListValuesPage({ const updateReportFieldListValueEnabled = useCallback( (value: boolean, valueIndex: number) => { if (reportFieldID) { - updateReportFieldListValueEnabledReportField(policyID, reportFieldID, [Number(valueIndex)], value); + updateReportFieldListValueEnabledReportField({policy, reportFieldID, valueIndexes: [Number(valueIndex)], enabled: value}); return; } @@ -179,7 +179,7 @@ function ReportFieldsListValuesPage({ }, []); if (reportFieldID) { - removeReportFieldListValue(policyID, reportFieldID, valuesToDelete); + removeReportFieldListValue({policy, reportFieldID, valueIndexes: valuesToDelete}); } else { deleteReportFieldsListValue({ valueIndexes: valuesToDelete, @@ -250,7 +250,7 @@ function ReportFieldsListValuesPage({ setSelectedValues({}); if (reportFieldID) { - updateReportFieldListValueEnabledReportField(policyID, reportFieldID, valuesToDisable, false); + updateReportFieldListValueEnabledReportField({policy, reportFieldID, valueIndexes: valuesToDisable, enabled: false}); return; } @@ -286,7 +286,7 @@ function ReportFieldsListValuesPage({ setSelectedValues({}); if (reportFieldID) { - updateReportFieldListValueEnabledReportField(policyID, reportFieldID, valuesToEnable, true); + updateReportFieldListValueEnabledReportField({policy, reportFieldID, valueIndexes: valuesToEnable, enabled: true}); return; } diff --git a/src/pages/workspace/reports/ReportFieldsSettingsPage.tsx b/src/pages/workspace/reports/ReportFieldsSettingsPage.tsx index c6d15e81a7b7..b249d05bb46f 100644 --- a/src/pages/workspace/reports/ReportFieldsSettingsPage.tsx +++ b/src/pages/workspace/reports/ReportFieldsSettingsPage.tsx @@ -50,7 +50,7 @@ function ReportFieldsSettingsPage({ const listValues = Object.values(policy?.fieldList?.[reportFieldKey]?.values ?? {})?.sort(localeCompare); const deleteReportFieldAndHideModal = () => { - deleteReportFields(policyID, [reportFieldKey]); + deleteReportFields({policy, reportFieldsToUpdate: [reportFieldKey]}); setIsDeleteModalVisible(false); Navigation.goBack(); }; diff --git a/src/pages/workspace/reports/ReportFieldsValueSettingsPage.tsx b/src/pages/workspace/reports/ReportFieldsValueSettingsPage.tsx index f5cdd727438e..cad65b3c1156 100644 --- a/src/pages/workspace/reports/ReportFieldsValueSettingsPage.tsx +++ b/src/pages/workspace/reports/ReportFieldsValueSettingsPage.tsx @@ -66,7 +66,7 @@ function ReportFieldsValueSettingsPage({ } const deleteListValueAndHideModal = () => { if (reportFieldID) { - removeReportFieldListValue(policyID, reportFieldID, [valueIndex]); + removeReportFieldListValue({policy, reportFieldID, valueIndexes: [valueIndex]}); } else { deleteReportFieldsListValue({ valueIndexes: [valueIndex], @@ -80,7 +80,7 @@ function ReportFieldsValueSettingsPage({ const updateListValueEnabled = (value: boolean) => { if (reportFieldID) { - updateReportFieldListValueEnabled(policyID, reportFieldID, [Number(valueIndex)], value); + updateReportFieldListValueEnabled({policy, reportFieldID, valueIndexes: [Number(valueIndex)], enabled: value}); return; } From 582f6c48d44d40d9fe53b2cfe9f720ee80aaa21c Mon Sep 17 00:00:00 2001 From: allgandalf Date: Wed, 8 Oct 2025 17:24:16 +0530 Subject: [PATCH 3/6] fix post merge --- src/libs/actions/Policy/ReportField.ts | 38 ++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Policy/ReportField.ts b/src/libs/actions/Policy/ReportField.ts index d83859347e0c..19f75bdc4b92 100644 --- a/src/libs/actions/Policy/ReportField.ts +++ b/src/libs/actions/Policy/ReportField.ts @@ -1,5 +1,5 @@ import cloneDeep from 'lodash/cloneDeep'; -import type {NullishDeep, OnyxCollection, OnyxUpdate} from 'react-native-onyx'; +import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import * as API from '@libs/API'; import type { @@ -20,7 +20,7 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {WorkspaceReportFieldForm} from '@src/types/form/WorkspaceReportFieldForm'; import INPUT_IDS from '@src/types/form/WorkspaceReportFieldForm'; -import type {Policy, PolicyReportField, Report} from '@src/types/onyx'; +import type {Policy, PolicyReportField} from '@src/types/onyx'; import type {OnyxValueWithOfflineFeedback} from '@src/types/onyx/OnyxCommon'; import type {OnyxData} from '@src/types/onyx/Request'; @@ -51,6 +51,38 @@ type DeleteReportFieldsListValueParams = { type CreateReportFieldParams = Pick & { listValues: string[]; disabledListValues: boolean[]; + policyExpenseReportIDs: Array | undefined; + policy: OnyxEntry; +}; + +type DeleteReportFieldsParams = { + reportFieldsToUpdate: string[]; + policy: OnyxEntry; +}; + +type RemoveReportFieldListValueParams = { + valueIndexes: number[]; + reportFieldID: string; + policy: OnyxEntry; +}; + +type AddReportFieldListValueParams = { + valueName: string; + reportFieldID: string; + policy: OnyxEntry; +}; + +type UpdateReportFieldListValueEnabledParams = { + valueIndexes: number[]; + enabled: boolean; + reportFieldID: string; + policy: OnyxEntry; +}; + +type UpdateReportFieldInitialValueParams = { + newInitialValue: string; + reportFieldID: string; + policy: OnyxEntry; policyID: string; policyExpenseReportIDs: Array | undefined; }; @@ -137,7 +169,7 @@ function deleteReportFieldsListValue({valueIndexes, listValues, disabledListValu /** * Creates a new report field. */ -function createReportField({name, type, initialValue, listValues, disabledListValues, policyExpenseReports, policy}: CreateReportFieldParams) { +function createReportField({name, type, initialValue, listValues, disabledListValues, policyExpenseReportIDs, policy}: CreateReportFieldParams) { const previousFieldList = policy?.fieldList ?? {}; const fieldID = WorkspaceReportFieldUtils.generateFieldID(name); const fieldKey = ReportUtils.getReportFieldKey(fieldID); From 286e1f50ad426960f82ce434183af3b42e2697fc Mon Sep 17 00:00:00 2001 From: allgandalf Date: Wed, 8 Oct 2025 17:33:11 +0530 Subject: [PATCH 4/6] fix post merge --- src/libs/actions/Policy/ReportField.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libs/actions/Policy/ReportField.ts b/src/libs/actions/Policy/ReportField.ts index 19f75bdc4b92..1f27369c9cd1 100644 --- a/src/libs/actions/Policy/ReportField.ts +++ b/src/libs/actions/Policy/ReportField.ts @@ -83,8 +83,6 @@ type UpdateReportFieldInitialValueParams = { newInitialValue: string; reportFieldID: string; policy: OnyxEntry; - policyID: string; - policyExpenseReportIDs: Array | undefined; }; function openPolicyReportFieldsPage(policyID: string) { From 8b68d5ec85c8254c1054a32f7529645ffa2d4ee5 Mon Sep 17 00:00:00 2001 From: allgandalf Date: Wed, 8 Oct 2025 17:42:24 +0530 Subject: [PATCH 5/6] fix lint errors --- src/pages/workspace/reports/CreateReportFieldsPage.tsx | 2 +- src/pages/workspace/reports/ReportFieldsAddListValuePage.tsx | 2 +- src/pages/workspace/reports/ReportFieldsInitialValuePage.tsx | 2 +- src/pages/workspace/reports/ReportFieldsListValuesPage.tsx | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pages/workspace/reports/CreateReportFieldsPage.tsx b/src/pages/workspace/reports/CreateReportFieldsPage.tsx index c1777cff2ca8..e4b5f10ae53b 100644 --- a/src/pages/workspace/reports/CreateReportFieldsPage.tsx +++ b/src/pages/workspace/reports/CreateReportFieldsPage.tsx @@ -74,7 +74,7 @@ function WorkspaceCreateReportFieldsPage({ }); Navigation.goBack(); }, - [availableListValuesLength, formDraft, policyExpenseReportIDs, policyID], + [availableListValuesLength, formDraft, policy, policyExpenseReportIDs], ); const validateForm = useCallback( diff --git a/src/pages/workspace/reports/ReportFieldsAddListValuePage.tsx b/src/pages/workspace/reports/ReportFieldsAddListValuePage.tsx index fa6ebaa87487..f2834dbf0a13 100644 --- a/src/pages/workspace/reports/ReportFieldsAddListValuePage.tsx +++ b/src/pages/workspace/reports/ReportFieldsAddListValuePage.tsx @@ -69,7 +69,7 @@ function ReportFieldsAddListValuePage({ Keyboard.dismiss(); Navigation.goBack(); }, - [formDraft, policyID, reportFieldID], + [formDraft, policy, reportFieldID], ); return ( diff --git a/src/pages/workspace/reports/ReportFieldsInitialValuePage.tsx b/src/pages/workspace/reports/ReportFieldsInitialValuePage.tsx index 220daa9c8015..efb153e2ad94 100644 --- a/src/pages/workspace/reports/ReportFieldsInitialValuePage.tsx +++ b/src/pages/workspace/reports/ReportFieldsInitialValuePage.tsx @@ -53,7 +53,7 @@ function ReportFieldsInitialValuePage({ } Navigation.goBack(); }, - [policyID, reportFieldID, currentInitialValue], + [currentInitialValue, policy, reportFieldID], ); const submitListValueUpdate = (value: string) => { diff --git a/src/pages/workspace/reports/ReportFieldsListValuesPage.tsx b/src/pages/workspace/reports/ReportFieldsListValuesPage.tsx index 16c2e31cd1c8..93bff19de7ad 100644 --- a/src/pages/workspace/reports/ReportFieldsListValuesPage.tsx +++ b/src/pages/workspace/reports/ReportFieldsListValuesPage.tsx @@ -112,7 +112,7 @@ function ReportFieldsListValuesPage({ disabledListValues, }); }, - [disabledListValues, policyID, reportFieldID], + [disabledListValues, policy, reportFieldID], ); useSearchBackPress({ From 6eb5d37b2ba64c34d90186015a77c06630f51a39 Mon Sep 17 00:00:00 2001 From: allgandalf Date: Tue, 21 Oct 2025 10:35:21 +0530 Subject: [PATCH 6/6] return early if policy data doesn't exist --- ...eateWorkspaceReportFieldListValueParams.ts | 2 +- .../CreateWorkspaceReportFieldParams.ts | 2 +- .../API/parameters/DeletePolicyReportField.ts | 2 +- ...ableWorkspaceReportFieldListValueParams.ts | 2 +- ...moveWorkspaceReportFieldListValueParams.ts | 2 +- ...eWorkspaceReportFieldInitialValueParams.ts | 2 +- src/libs/actions/Policy/ReportField.ts | 30 +++++++++++++++++++ 7 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/libs/API/parameters/CreateWorkspaceReportFieldListValueParams.ts b/src/libs/API/parameters/CreateWorkspaceReportFieldListValueParams.ts index a4b8dd81b777..950287bc5d04 100644 --- a/src/libs/API/parameters/CreateWorkspaceReportFieldListValueParams.ts +++ b/src/libs/API/parameters/CreateWorkspaceReportFieldListValueParams.ts @@ -1,5 +1,5 @@ type CreateWorkspaceReportFieldListValueParams = { - policyID: string | undefined; + policyID: string; /** * Stringified JSON object with type of following structure: * Array diff --git a/src/libs/API/parameters/CreateWorkspaceReportFieldParams.ts b/src/libs/API/parameters/CreateWorkspaceReportFieldParams.ts index d1435c747c72..33692d210959 100644 --- a/src/libs/API/parameters/CreateWorkspaceReportFieldParams.ts +++ b/src/libs/API/parameters/CreateWorkspaceReportFieldParams.ts @@ -1,5 +1,5 @@ type CreateWorkspaceReportFieldParams = { - policyID: string | undefined; + policyID: string; /** * Stringified JSON object with type of following structure: * Array diff --git a/src/libs/API/parameters/DeletePolicyReportField.ts b/src/libs/API/parameters/DeletePolicyReportField.ts index 56da61990fc9..d79e9b07249e 100644 --- a/src/libs/API/parameters/DeletePolicyReportField.ts +++ b/src/libs/API/parameters/DeletePolicyReportField.ts @@ -1,5 +1,5 @@ type DeletePolicyReportField = { - policyID: string | undefined; + policyID: string; /** * Stringified JSON object with type of following structure: * Array diff --git a/src/libs/API/parameters/EnableWorkspaceReportFieldListValueParams.ts b/src/libs/API/parameters/EnableWorkspaceReportFieldListValueParams.ts index 21d5a25f11f7..7c54a2f4c68b 100644 --- a/src/libs/API/parameters/EnableWorkspaceReportFieldListValueParams.ts +++ b/src/libs/API/parameters/EnableWorkspaceReportFieldListValueParams.ts @@ -1,5 +1,5 @@ type EnableWorkspaceReportFieldListValueParams = { - policyID: string | undefined; + policyID: string; /** * Stringified JSON object with type of following structure: * Array diff --git a/src/libs/API/parameters/RemoveWorkspaceReportFieldListValueParams.ts b/src/libs/API/parameters/RemoveWorkspaceReportFieldListValueParams.ts index 934ca399d665..94d90a8dbaae 100644 --- a/src/libs/API/parameters/RemoveWorkspaceReportFieldListValueParams.ts +++ b/src/libs/API/parameters/RemoveWorkspaceReportFieldListValueParams.ts @@ -1,5 +1,5 @@ type RemoveWorkspaceReportFieldListValueParams = { - policyID: string | undefined; + policyID: string; /** * Stringified JSON object with type of following structure: * Array diff --git a/src/libs/API/parameters/UpdateWorkspaceReportFieldInitialValueParams.ts b/src/libs/API/parameters/UpdateWorkspaceReportFieldInitialValueParams.ts index 628e3ac8b960..a72781ff1c37 100644 --- a/src/libs/API/parameters/UpdateWorkspaceReportFieldInitialValueParams.ts +++ b/src/libs/API/parameters/UpdateWorkspaceReportFieldInitialValueParams.ts @@ -1,5 +1,5 @@ type UpdateWorkspaceReportFieldInitialValueParams = { - policyID: string | undefined; + policyID: string; /** * Stringified JSON object with type of following structure: * Array diff --git a/src/libs/actions/Policy/ReportField.ts b/src/libs/actions/Policy/ReportField.ts index 1f27369c9cd1..670f393850c7 100644 --- a/src/libs/actions/Policy/ReportField.ts +++ b/src/libs/actions/Policy/ReportField.ts @@ -168,6 +168,11 @@ function deleteReportFieldsListValue({valueIndexes, listValues, disabledListValu * Creates a new report field. */ function createReportField({name, type, initialValue, listValues, disabledListValues, policyExpenseReportIDs, policy}: CreateReportFieldParams) { + if (!policy) { + Log.warn('Policy data is not present'); + return; + } + const previousFieldList = policy?.fieldList ?? {}; const fieldID = WorkspaceReportFieldUtils.generateFieldID(name); const fieldKey = ReportUtils.getReportFieldKey(fieldID); @@ -258,6 +263,11 @@ function createReportField({name, type, initialValue, listValues, disabledListVa } function deleteReportFields({policy, reportFieldsToUpdate}: DeleteReportFieldsParams) { + if (!policy) { + Log.warn('Policy data is not present'); + return; + } + const allReportFields = policy?.fieldList ?? {}; const updatedReportFields = Object.fromEntries(Object.entries(allReportFields).filter(([key]) => !reportFieldsToUpdate.includes(key))); @@ -322,6 +332,11 @@ function deleteReportFields({policy, reportFieldsToUpdate}: DeleteReportFieldsPa * Updates the initial value of a report field. */ function updateReportFieldInitialValue({policy, reportFieldID, newInitialValue}: UpdateReportFieldInitialValueParams) { + if (!policy) { + Log.warn('Policy data is not present'); + return; + } + const previousFieldList = policy?.fieldList ?? {}; const fieldKey = ReportUtils.getReportFieldKey(reportFieldID); const updatedReportField: PolicyReportField = { @@ -377,6 +392,11 @@ function updateReportFieldInitialValue({policy, reportFieldID, newInitialValue}: } function updateReportFieldListValueEnabled({policy, reportFieldID, valueIndexes, enabled}: UpdateReportFieldListValueEnabledParams) { + if (!policy) { + Log.warn('Policy data is not present'); + return; + } + const previousFieldList = policy?.fieldList ?? {}; const fieldKey = ReportUtils.getReportFieldKey(reportFieldID); const reportField = previousFieldList[fieldKey]; @@ -419,6 +439,11 @@ function updateReportFieldListValueEnabled({policy, reportFieldID, valueIndexes, * Adds a new option to the list type report field on a workspace. */ function addReportFieldListValue({policy, reportFieldID, valueName}: AddReportFieldListValueParams) { + if (!policy) { + Log.warn('Policy data is not present'); + return; + } + const previousFieldList = policy?.fieldList ?? {}; const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID); const reportField = previousFieldList[reportFieldKey]; @@ -454,6 +479,11 @@ function addReportFieldListValue({policy, reportFieldID, valueName}: AddReportFi * Removes a list value from the workspace report fields. */ function removeReportFieldListValue({policy, reportFieldID, valueIndexes}: RemoveReportFieldListValueParams) { + if (!policy) { + Log.warn('Policy data is not present'); + return; + } + const previousFieldList = policy?.fieldList ?? {}; const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID); const reportField = previousFieldList[reportFieldKey];