Skip to content

Commit

Permalink
Merge pull request #38375 from nkdengineer/fix/38234
Browse files Browse the repository at this point in the history
fix: add default rates data
  • Loading branch information
luacmartins authored Mar 22, 2024
2 parents 570a001 + 250baa9 commit 7dc0cfc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4133,6 +4133,22 @@ const CONST = {
SESSION_STORAGE_KEYS: {
INITIAL_URL: 'INITIAL_URL',
},
DEFAULT_TAX: {
defaultExternalID: 'id_TAX_EXEMPT',
defaultValue: '0%',
foreignTaxDefault: 'id_TAX_EXEMPT',
name: 'Tax',
taxes: {
id_TAX_EXEMPT: {
name: 'Tax exempt',
value: '0%',
},
id_TAX_RATE_1: {
name: 'Tax Rate 1',
value: '5%',
},
},
},
} as const;

type Country = keyof typeof CONST.ALL_COUNTRIES;
Expand Down
1 change: 1 addition & 0 deletions src/libs/API/parameters/EnablePolicyTaxesParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type EnablePolicyTaxesParams = {
policyID: string;
enabled: boolean;
taxFields?: string;
};

export default EnablePolicyTaxesParams;
64 changes: 63 additions & 1 deletion src/libs/actions/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import type {
ReimbursementAccount,
Report,
ReportAction,
TaxRatesWithDefault,
Transaction,
} from '@src/types/onyx';
import type {ErrorFields, Errors, OnyxValueWithOfflineFeedback, PendingAction} from '@src/types/onyx/OnyxCommon';
Expand Down Expand Up @@ -3669,6 +3670,62 @@ function enablePolicyTags(policyID: string, enabled: boolean) {
}

function enablePolicyTaxes(policyID: string, enabled: boolean) {
const defaultTaxRates: TaxRatesWithDefault = CONST.DEFAULT_TAX;
const taxRatesData: OnyxData = {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
taxRates: {
...defaultTaxRates,
taxes: {
...Object.keys(defaultTaxRates.taxes).reduce(
(prevTaxesData, taxKey) => ({
...prevTaxesData,
[taxKey]: {
...defaultTaxRates.taxes[taxKey],
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
}),
{},
),
},
},
},
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
taxRates: {
taxes: {
...Object.keys(defaultTaxRates.taxes).reduce(
(prevTaxesData, taxKey) => ({
...prevTaxesData,
[taxKey]: {pendingAction: null},
}),
{},
),
},
},
},
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
taxRates: undefined,
},
},
],
};
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
const shouldAddDefaultTaxRatesData = (!policy?.taxRates || isEmptyObject(policy.taxRates)) && enabled;
const onyxData: OnyxData = {
optimisticData: [
{
Expand All @@ -3683,6 +3740,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) {
},
},
},
...(shouldAddDefaultTaxRatesData ? taxRatesData.optimisticData ?? [] : []),
],
successData: [
{
Expand All @@ -3694,6 +3752,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) {
},
},
},
...(shouldAddDefaultTaxRatesData ? taxRatesData.successData ?? [] : []),
],
failureData: [
{
Expand All @@ -3708,11 +3767,14 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) {
},
},
},
...(shouldAddDefaultTaxRatesData ? taxRatesData.failureData ?? [] : []),
],
};

const parameters: EnablePolicyTaxesParams = {policyID, enabled};

if (shouldAddDefaultTaxRatesData) {
parameters.taxFields = JSON.stringify(defaultTaxRates);
}
API.write(WRITE_COMMANDS.ENABLE_POLICY_TAXES, parameters, onyxData);

if (enabled) {
Expand Down

0 comments on commit 7dc0cfc

Please sign in to comment.