diff --git a/src/libs/actions/Policy/ReportField.ts b/src/libs/actions/Policy/ReportField.ts index bc19c9d46d89..670f393850c7 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,43 +20,10 @@ 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'; -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; 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; }; function openPolicyReportFieldsPage(policyID: string) { @@ -170,8 +167,13 @@ function deleteReportFieldsListValue({valueIndexes, listValues, disabledListValu /** * Creates a new report field. */ -function createReportField({name, type, initialValue, listValues, disabledListValues, policyID, policyExpenseReportIDs}: CreateReportFieldParams) { - const previousFieldList = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.fieldList ?? {}; +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); const optimisticReportFieldDataForPolicy: Omit, 'value'> = { @@ -191,7 +193,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 +215,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 +241,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 +255,19 @@ 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) { + 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))); @@ -284,7 +290,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 +299,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 +309,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 +321,7 @@ function deleteReportFields(policyID: string, reportFieldsToUpdate: string[]) { }; const parameters: DeletePolicyReportField = { - policyID, + policyID: policy?.id, reportFields: JSON.stringify(Object.values(updatedReportFields)), }; @@ -325,8 +331,13 @@ 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) { + if (!policy) { + Log.warn('Policy data is not present'); + return; + } + + const previousFieldList = policy?.fieldList ?? {}; const fieldKey = ReportUtils.getReportFieldKey(reportFieldID); const updatedReportField: PolicyReportField = { ...previousFieldList[fieldKey], @@ -335,7 +346,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 +358,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 +370,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 +384,20 @@ 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) { + if (!policy) { + Log.warn('Policy data is not present'); + return; + } + + const previousFieldList = policy?.fieldList ?? {}; const fieldKey = ReportUtils.getReportFieldKey(reportFieldID); const reportField = previousFieldList[fieldKey]; @@ -400,7 +416,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 +428,7 @@ function updateReportFieldListValueEnabled(policyID: string, reportFieldID: stri }; const parameters: EnableWorkspaceReportFieldListValueParams = { - policyID, + policyID: policy?.id, reportFields: JSON.stringify([updatedReportField]), }; @@ -422,8 +438,13 @@ 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) { + if (!policy) { + Log.warn('Policy data is not present'); + return; + } + + const previousFieldList = policy?.fieldList ?? {}; const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID); const reportField = previousFieldList[reportFieldKey]; const updatedReportField = cloneDeep(reportField); @@ -435,7 +456,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 +468,7 @@ function addReportFieldListValue(policyID: string, reportFieldID: string, valueN }; const parameters: CreateWorkspaceReportFieldListValueParams = { - policyID, + policyID: policy?.id, reportFields: JSON.stringify([updatedReportField]), }; @@ -457,8 +478,13 @@ 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) { + if (!policy) { + Log.warn('Policy data is not present'); + return; + } + + const previousFieldList = policy?.fieldList ?? {}; const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID); const reportField = previousFieldList[reportFieldKey]; const updatedReportField = cloneDeep(reportField); @@ -480,7 +506,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 +518,7 @@ function removeReportFieldListValue(policyID: string, reportFieldID: string, val }; const parameters: RemoveWorkspaceReportFieldListValueParams = { - policyID, + policyID: policy?.id, reportFields: JSON.stringify([updatedReportField]), }; diff --git a/src/pages/workspace/reports/CreateReportFieldsPage.tsx b/src/pages/workspace/reports/CreateReportFieldsPage.tsx index 9273c6db124c..e4b5f10ae53b 100644 --- a/src/pages/workspace/reports/CreateReportFieldsPage.tsx +++ b/src/pages/workspace/reports/CreateReportFieldsPage.tsx @@ -64,7 +64,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] : '', @@ -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 e2a8f4e885bf..f2834dbf0a13 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], @@ -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 4fdae0c8452b..efb153e2ad94 100644 --- a/src/pages/workspace/reports/ReportFieldsInitialValuePage.tsx +++ b/src/pages/workspace/reports/ReportFieldsInitialValuePage.tsx @@ -49,15 +49,15 @@ function ReportFieldsInitialValuePage({ const submitForm = useCallback( (values: FormOnyxValues) => { if (currentInitialValue !== values.initialValue) { - updateReportFieldInitialValue(policyID, reportFieldID, values.initialValue); + updateReportFieldInitialValue({policy, reportFieldID, newInitialValue: values.initialValue}); } Navigation.goBack(); }, - [policyID, reportFieldID, currentInitialValue], + [currentInitialValue, policy, reportFieldID], ); 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 a0582e91b925..0b05752294d2 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; } @@ -112,7 +112,7 @@ function ReportFieldsListValuesPage({ disabledListValues, }); }, - [disabledListValues, policyID, reportFieldID], + [disabledListValues, policy, reportFieldID], ); useSearchBackPress({ @@ -179,7 +179,7 @@ function ReportFieldsListValuesPage({ }, []); if (reportFieldID) { - removeReportFieldListValue(policyID, reportFieldID, valuesToDelete); + removeReportFieldListValue({policy, reportFieldID, valueIndexes: valuesToDelete}); } else { deleteReportFieldsListValue({ valueIndexes: valuesToDelete, @@ -252,7 +252,7 @@ function ReportFieldsListValuesPage({ setSelectedValues({}); if (reportFieldID) { - updateReportFieldListValueEnabledReportField(policyID, reportFieldID, valuesToDisable, false); + updateReportFieldListValueEnabledReportField({policy, reportFieldID, valueIndexes: valuesToDisable, enabled: false}); return; } @@ -288,7 +288,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; } diff --git a/tests/actions/ReportFieldTest.ts b/tests/actions/ReportFieldTest.ts index 5df387fa4655..2b2560120823 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({