From b57de0738641ad7857ac9775b6c9650417d9f42a Mon Sep 17 00:00:00 2001 From: Michal Muzyk Date: Wed, 3 Apr 2024 13:08:32 +0200 Subject: [PATCH] fix: workspace currency can be changed without existing distance rates --- src/libs/actions/Policy.ts | 70 ++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 3c34e823ac9a..f075b83ced15 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -1507,7 +1507,7 @@ function updateGeneralSettings(policyID: string, name: string, currency: string) const distanceUnit = Object.values(policy?.customUnits ?? {}).find((unit) => unit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE); const customUnitID = distanceUnit?.customUnitID; - if (!policy || !customUnitID) { + if (!policy) { return; } @@ -1516,22 +1516,24 @@ function updateGeneralSettings(policyID: string, name: string, currency: string) const finallyRates: Record = {}; const failureRates: Record = {}; - for (const rateID of Object.keys(currentRates)) { - optimisticRates[rateID] = { - ...currentRates[rateID], - pendingFields: {currency: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}, - currency, - }; - finallyRates[rateID] = { - ...currentRates[rateID], - pendingFields: {currency: null}, - currency, - }; - failureRates[rateID] = { - ...currentRates[rateID], - pendingFields: {currency: null}, - errorFields: {currency: ErrorUtils.getMicroSecondOnyxError('common.genericErrorMessage')}, - }; + if (customUnitID) { + for (const rateID of Object.keys(currentRates)) { + optimisticRates[rateID] = { + ...currentRates[rateID], + pendingFields: {currency: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}, + currency, + }; + finallyRates[rateID] = { + ...currentRates[rateID], + pendingFields: {currency: null}, + currency, + }; + failureRates[rateID] = { + ...currentRates[rateID], + pendingFields: {currency: null}, + errorFields: {currency: ErrorUtils.getMicroSecondOnyxError('common.genericErrorMessage')}, + }; + } } const optimisticData: OnyxUpdate[] = [ @@ -1553,12 +1555,14 @@ function updateGeneralSettings(policyID: string, name: string, currency: string) }, name, outputCurrency: currency, - customUnits: { - [customUnitID]: { - ...distanceUnit, - rates: optimisticRates, + ...(customUnitID && { + customUnits: { + [customUnitID]: { + ...distanceUnit, + rates: optimisticRates, + }, }, - }, + }), }, }, ]; @@ -1570,11 +1574,14 @@ function updateGeneralSettings(policyID: string, name: string, currency: string) pendingFields: { generalSettings: null, }, - customUnits: { - [customUnitID]: { - rates: finallyRates, + ...(customUnitID && { + customUnits: { + [customUnitID]: { + ...distanceUnit, + rates: finallyRates, + }, }, - }, + }), }, }, ]; @@ -1587,11 +1594,14 @@ function updateGeneralSettings(policyID: string, name: string, currency: string) errorFields: { generalSettings: ErrorUtils.getMicroSecondOnyxError('workspace.editor.genericFailureMessage'), }, - customUnits: { - [customUnitID]: { - rates: failureRates, + ...(customUnitID && { + customUnits: { + [customUnitID]: { + ...distanceUnit, + rates: failureRates, + }, }, - }, + }), }, }, ];