Skip to content

Commit

Permalink
Merge pull request #39484 from MrMuzyk/fix/optional-custom-unit-id-wh…
Browse files Browse the repository at this point in the history
…en-changing-workspace-currency

fix: workspace currency can be changed without existing distance rates
  • Loading branch information
mountiny authored Apr 4, 2024
2 parents 62000d8 + b57de07 commit 64b5661
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions src/libs/actions/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,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;
}

Expand All @@ -1524,22 +1524,24 @@ function updateGeneralSettings(policyID: string, name: string, currency: string)
const finallyRates: Record<string, Rate> = {};
const failureRates: Record<string, Rate> = {};

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[] = [
Expand All @@ -1561,12 +1563,14 @@ function updateGeneralSettings(policyID: string, name: string, currency: string)
},
name,
outputCurrency: currency,
customUnits: {
[customUnitID]: {
...distanceUnit,
rates: optimisticRates,
...(customUnitID && {
customUnits: {
[customUnitID]: {
...distanceUnit,
rates: optimisticRates,
},
},
},
}),
},
},
];
Expand All @@ -1578,11 +1582,14 @@ function updateGeneralSettings(policyID: string, name: string, currency: string)
pendingFields: {
generalSettings: null,
},
customUnits: {
[customUnitID]: {
rates: finallyRates,
...(customUnitID && {
customUnits: {
[customUnitID]: {
...distanceUnit,
rates: finallyRates,
},
},
},
}),
},
},
];
Expand All @@ -1595,11 +1602,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,
},
},
},
}),
},
},
];
Expand Down

0 comments on commit 64b5661

Please sign in to comment.