Skip to content
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

fix: add default rates data #38375

Merged
merged 29 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
060df3d
fix add default rates data
nkdengineer Mar 15, 2024
4ca4290
fix optimistic data
nkdengineer Mar 15, 2024
385893f
fix remove pending field
nkdengineer Mar 15, 2024
5e03c10
fix remove tax rates pending
nkdengineer Mar 15, 2024
f0a87ff
fix add taxRates data if failed
nkdengineer Mar 15, 2024
578d2d9
fix grey out row once offline
nkdengineer Mar 15, 2024
23e9eff
fix use default tax const
nkdengineer Mar 16, 2024
8ca415f
fix add taxes data to param
nkdengineer Mar 19, 2024
1948ce0
Merge branch 'main' into fix/38234
nkdengineer Mar 19, 2024
47a2dc7
fix lint
nkdengineer Mar 19, 2024
b4e2493
fix import type
nkdengineer Mar 19, 2024
f49fd7e
fix offline mode issue
nkdengineer Mar 20, 2024
bd33bd3
fix failure data
nkdengineer Mar 20, 2024
b7e8947
fix lint
nkdengineer Mar 20, 2024
2437b2c
fix refactor the default taxes data const
nkdengineer Mar 20, 2024
713782d
fix typo
nkdengineer Mar 20, 2024
95af3e3
fix lint
nkdengineer Mar 20, 2024
016e4f1
fix use array reduce
nkdengineer Mar 20, 2024
c915f96
fix remove reduce
nkdengineer Mar 20, 2024
d26c90d
fix use taxFields as string
nkdengineer Mar 21, 2024
469be4c
fix revert change
nkdengineer Mar 21, 2024
2d751ea
fix use taxFields as string
nkdengineer Mar 21, 2024
ec240ac
fix merge main
nkdengineer Mar 21, 2024
0cd290e
fix refactor optimistic data
nkdengineer Mar 21, 2024
0b1005e
fix lint
nkdengineer Mar 21, 2024
dc5dfda
fix combine two data in optimistic data
nkdengineer Mar 22, 2024
3472f14
fix lint
nkdengineer Mar 22, 2024
c923ed6
fix rename taxt rate 1
nkdengineer Mar 22, 2024
250baa9
fix do not import type in CONST
nkdengineer Mar 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4132,6 +4132,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 @@ -3664,6 +3665,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,
},
},
],
};
nkdengineer marked this conversation as resolved.
Show resolved Hide resolved
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
const shouldAddDefaultTaxRatesData = (!policy?.taxRates || isEmptyObject(policy.taxRates)) && enabled;
const onyxData: OnyxData = {
optimisticData: [
{
Expand All @@ -3678,6 +3735,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) {
},
},
},
...(shouldAddDefaultTaxRatesData ? taxRatesData.optimisticData ?? [] : []),
nkdengineer marked this conversation as resolved.
Show resolved Hide resolved
],
successData: [
{
Expand All @@ -3689,6 +3747,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) {
},
},
},
...(shouldAddDefaultTaxRatesData ? taxRatesData.successData ?? [] : []),
],
failureData: [
{
Expand All @@ -3703,11 +3762,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
Loading