-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix: maintain tax assign when tax code changes #47327
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import INPUT_IDS from '@src/types/form/WorkspaceNewTaxForm'; | |
import {default as INPUT_IDS_TAX_CODE} from '@src/types/form/WorkspaceTaxCodeForm'; | ||
import type {Policy, TaxRate, TaxRates} from '@src/types/onyx'; | ||
import type * as OnyxCommon from '@src/types/onyx/OnyxCommon'; | ||
import type {CustomUnit, Rate} from '@src/types/onyx/Policy'; | ||
import type {OnyxData} from '@src/types/onyx/Request'; | ||
|
||
let allPolicies: OnyxCollection<Policy>; | ||
|
@@ -486,6 +487,33 @@ function renamePolicyTax(policyID: string, taxID: string, newName: string) { | |
function setPolicyTaxCode(policyID: string, oldTaxCode: string, newTaxCode: string) { | ||
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]; | ||
const originalTaxRate = {...policy?.taxRates?.taxes[oldTaxCode]}; | ||
const customUnits = Object.values(policy?.customUnits ?? {}); | ||
const optimisticCustomUnit = { | ||
customUnits: { | ||
...customUnits.reduce((units, customUnit) => { | ||
// eslint-disable-next-line no-param-reassign | ||
units[customUnit.customUnitID] = { | ||
rates: { | ||
...Object.keys(customUnit.rates).reduce((rates, rateID) => { | ||
if (customUnit.rates[rateID].attributes?.taxRateExternalID === oldTaxCode) { | ||
// eslint-disable-next-line no-param-reassign | ||
rates[rateID] = { | ||
attributes: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also do we need to spread the old rate here? to make sure we don't remove everything but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a merge call, so I think we don't need to include everything inside. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yep |
||
taxRateExternalID: newTaxCode, | ||
}, | ||
}; | ||
} | ||
return rates; | ||
}, {} as Record<string, Rate>), | ||
}, | ||
}; | ||
return units; | ||
}, {} as Record<string, Partial<CustomUnit>>), | ||
}, | ||
}; | ||
const failureCustomUnit = { | ||
customUnits: policy?.customUnits, | ||
}; | ||
const oldDefaultExternalID = policy?.taxRates?.defaultExternalID; | ||
const oldForeignTaxDefault = policy?.taxRates?.foreignTaxDefault; | ||
const onyxData: OnyxData = { | ||
|
@@ -508,6 +536,7 @@ function setPolicyTaxCode(policyID: string, oldTaxCode: string, newTaxCode: stri | |
}, | ||
}, | ||
}, | ||
...(!!customUnits && optimisticCustomUnit), | ||
}, | ||
}, | ||
], | ||
|
@@ -552,6 +581,7 @@ function setPolicyTaxCode(policyID: string, oldTaxCode: string, newTaxCode: stri | |
}, | ||
}, | ||
}, | ||
...(!!customUnits && failureCustomUnit), | ||
}, | ||
}, | ||
], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm misunderstanding but wouldn't this be better as a map instead of a reduce? Same with the rates.reduce below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know, we can't add properties to an object using
map
, so I need to usereduce
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
customUnits is an array though right? Could do something here like
Seems a little cleaner? And could use a forEach for the rates object?
what do you think @rushatgabhane
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree with ^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But since
customUnits
in optimistic data is of typeRecord<string, any>
, after this we will have to either do anArray.reduce
orObject.assign
to the updatedcustomUnits
anyway.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh yeah, fair. Okay then I think this works for me! Thank you for the explanation, that was helpful!