From 060df3dd86df687314affaa19d5270eaeee06702 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 15 Mar 2024 14:15:15 +0700 Subject: [PATCH 01/27] fix add default rates data --- src/CONST.ts | 17 +++++++++++++++++ src/libs/actions/Policy.ts | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/CONST.ts b/src/CONST.ts index fa44cda20720..ce035fe5d852 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -985,6 +985,23 @@ const CONST = { TOOLTIP_MAX_LINES: 3, + TAX_DEFAULT_RATES: { + 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%', + }, + }, + }, + LOGIN_TYPE: { PHONE: 'phone', EMAIL: 'email', diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 6699f8756005..fb58a53f27c0 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3415,6 +3415,19 @@ function enablePolicyTags(policyID: string, enabled: boolean) { } function enablePolicyTaxes(policyID: string, enabled: boolean) { + const taxRatesData: OnyxData = { + optimisticData: [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + value: { + taxRates: CONST.TAX_DEFAULT_RATES, + }, + }, + ], + }; + const policy = ReportUtils.getPolicy(policyID); + const shouldAddDefaultTaxRatesData = isEmptyObject(policy.taxRates) && enabled; const onyxData: OnyxData = { optimisticData: [ { @@ -3426,9 +3439,11 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { }, pendingFields: { tax: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, + taxRates: shouldAddDefaultTaxRatesData ? CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE : null, }, }, }, + ...(shouldAddDefaultTaxRatesData ? taxRatesData.optimisticData ?? [] : []), ], successData: [ { @@ -3437,6 +3452,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { value: { pendingFields: { tax: null, + taxRates: null, }, }, }, @@ -3451,6 +3467,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { }, pendingFields: { tax: null, + taxRates: null, }, }, }, From 4ca42901041d52b819487fdc11bda223cc454eca Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 15 Mar 2024 15:27:35 +0700 Subject: [PATCH 02/27] fix optimistic data --- src/CONST.ts | 26 ++++++++++++-------------- src/libs/actions/Policy.ts | 19 ++++++++++++++++++- src/types/onyx/Policy.ts | 4 ++-- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index ce035fe5d852..e6a2f72990c5 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -985,20 +985,18 @@ const CONST = { TOOLTIP_MAX_LINES: 3, - TAX_DEFAULT_RATES: { - 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%', - }, + TAXES: { + DEFAULT: { + EXTERNAL_ID: 'id_TAX_EXEMPT', + FOREIGN_ID: 'id_TAX_EXEMPT', + VALUE: '0%', + EXEMPT_ID: 'id_TAX_EXEMPT', + NAME: 'Tax', + EXEMPT_NAME: 'Tax exempt', + EXEMPT_VALUE: '0%', + TAX_RATE_1_ID: 'id_TAX_RATE_1', + TAX_RATE_1_NAME: 'Tax Rate 1', + TAX_RATE_1_VALUE: '5%', }, }, diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index fb58a53f27c0..008c211876e7 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3421,7 +3421,24 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { - taxRates: CONST.TAX_DEFAULT_RATES, + taxRates: { + defaultExternalID: CONST.TAXES.DEFAULT.EXTERNAL_ID, + defaultValue: CONST.TAXES.DEFAULT.VALUE, + foreignTaxDefault: CONST.TAXES.DEFAULT.FOREIGN_ID, + name: CONST.TAXES.DEFAULT.NAME, + taxes: { + [CONST.TAXES.DEFAULT.EXEMPT_ID]: { + name: CONST.TAXES.DEFAULT.EXEMPT_NAME, + value: CONST.TAXES.DEFAULT.EXEMPT_VALUE, + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + [CONST.TAXES.DEFAULT.TAX_RATE_1_ID]: { + name: CONST.TAXES.DEFAULT.TAX_RATE_1_NAME, + value: CONST.TAXES.DEFAULT.TAX_RATE_1_VALUE, + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + }, + }, }, }, ], diff --git a/src/types/onyx/Policy.ts b/src/types/onyx/Policy.ts index 688300933a5e..ad7d2bb120b1 100644 --- a/src/types/onyx/Policy.ts +++ b/src/types/onyx/Policy.ts @@ -32,7 +32,7 @@ type DisabledFields = { reimbursable?: boolean; }; -type TaxRate = { +type TaxRate = OnyxCommon.OnyxValueWithOfflineFeedback<{ /** Name of the a tax rate. */ name: string; @@ -47,7 +47,7 @@ type TaxRate = { /** Indicates if the tax rate is disabled. */ isDisabled?: boolean; -}; +}>; type TaxRates = Record; From 385893f267aaad4bce113202f9d1527da8949d2f Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 15 Mar 2024 15:29:06 +0700 Subject: [PATCH 03/27] fix remove pending field --- src/libs/actions/Policy.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 008c211876e7..861cf45b1c09 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3456,7 +3456,6 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { }, pendingFields: { tax: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, - taxRates: shouldAddDefaultTaxRatesData ? CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE : null, }, }, }, From 5e03c1003d47ca80ac5ce1f76ff82d9ae54e9d6a Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 15 Mar 2024 15:30:00 +0700 Subject: [PATCH 04/27] fix remove tax rates pending --- src/libs/actions/Policy.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 861cf45b1c09..32eb33c9ea98 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3468,7 +3468,6 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { value: { pendingFields: { tax: null, - taxRates: null, }, }, }, From f0a87ff9b4ca61aa704597480a353150f3194b51 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 15 Mar 2024 15:31:52 +0700 Subject: [PATCH 05/27] fix add taxRates data if failed --- src/libs/actions/Policy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 32eb33c9ea98..fc8b56226ee6 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3480,9 +3480,9 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { tax: { trackingEnabled: !enabled, }, + taxRates: undefined, pendingFields: { tax: null, - taxRates: null, }, }, }, From 578d2d95a38d6fdfe72303371cd28957a5a7801d Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 15 Mar 2024 15:58:36 +0700 Subject: [PATCH 06/27] fix grey out row once offline --- src/pages/workspace/taxes/WorkspaceTaxesPage.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/workspace/taxes/WorkspaceTaxesPage.tsx b/src/pages/workspace/taxes/WorkspaceTaxesPage.tsx index 18123d109645..91c5087aafb2 100644 --- a/src/pages/workspace/taxes/WorkspaceTaxesPage.tsx +++ b/src/pages/workspace/taxes/WorkspaceTaxesPage.tsx @@ -70,6 +70,7 @@ function WorkspaceTaxesPage({policy, route}: WorkspaceTaxesPageProps) { text: value.name, alternateText: textForDefault(key), keyForList: key, + pendingAction: value.pendingAction, isSelected: !!selectedTaxesIDs.includes(key), isDisabledCheckbox: key === defaultExternalID, rightElement: ( From 23e9effd4c3b2b59c962fd374c823ead42fb4666 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Sat, 16 Mar 2024 09:26:04 +0700 Subject: [PATCH 07/27] fix use default tax const --- src/CONST.ts | 26 ++++++++++++++++---------- src/libs/actions/Policy.ts | 19 +------------------ 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index e6a2f72990c5..ea3f06f81908 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -987,16 +987,22 @@ const CONST = { TAXES: { DEFAULT: { - EXTERNAL_ID: 'id_TAX_EXEMPT', - FOREIGN_ID: 'id_TAX_EXEMPT', - VALUE: '0%', - EXEMPT_ID: 'id_TAX_EXEMPT', - NAME: 'Tax', - EXEMPT_NAME: 'Tax exempt', - EXEMPT_VALUE: '0%', - TAX_RATE_1_ID: 'id_TAX_RATE_1', - TAX_RATE_1_NAME: 'Tax Rate 1', - TAX_RATE_1_VALUE: '5%', + defaultExternalID: 'id_TAX_EXEMPT', + defaultValue: '0%', + foreignTaxDefault: 'id_TAX_EXEMPT', + name: 'Tax', + taxes: { + id_TAX_EXEMPT: { + name: 'Tax exempt', + value: '0%', + pendingAction: 'add', + }, + id_TAX_RATE_1: { + name: 'Tax rate 1', + value: '5%', + pendingAction: 'add', + }, + }, }, }, diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index fc8b56226ee6..96331736ab2e 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3421,24 +3421,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { - taxRates: { - defaultExternalID: CONST.TAXES.DEFAULT.EXTERNAL_ID, - defaultValue: CONST.TAXES.DEFAULT.VALUE, - foreignTaxDefault: CONST.TAXES.DEFAULT.FOREIGN_ID, - name: CONST.TAXES.DEFAULT.NAME, - taxes: { - [CONST.TAXES.DEFAULT.EXEMPT_ID]: { - name: CONST.TAXES.DEFAULT.EXEMPT_NAME, - value: CONST.TAXES.DEFAULT.EXEMPT_VALUE, - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, - [CONST.TAXES.DEFAULT.TAX_RATE_1_ID]: { - name: CONST.TAXES.DEFAULT.TAX_RATE_1_NAME, - value: CONST.TAXES.DEFAULT.TAX_RATE_1_VALUE, - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, - }, - }, + taxRates: CONST.TAXES.DEFAULT, }, }, ], From 8ca415f5e493de8f32fe7fd6a4cf1188e8fcf945 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Tue, 19 Mar 2024 17:27:17 +0700 Subject: [PATCH 08/27] fix add taxes data to param --- src/CONST.ts | 41 +++++++++---------- .../API/parameters/EnablePolicyTaxesParams.ts | 3 ++ src/libs/actions/Policy.ts | 8 ++-- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index ea3f06f81908..61fce0346683 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -985,27 +985,6 @@ const CONST = { TOOLTIP_MAX_LINES: 3, - TAXES: { - DEFAULT: { - defaultExternalID: 'id_TAX_EXEMPT', - defaultValue: '0%', - foreignTaxDefault: 'id_TAX_EXEMPT', - name: 'Tax', - taxes: { - id_TAX_EXEMPT: { - name: 'Tax exempt', - value: '0%', - pendingAction: 'add', - }, - id_TAX_RATE_1: { - name: 'Tax rate 1', - value: '5%', - pendingAction: 'add', - }, - }, - }, - }, - LOGIN_TYPE: { PHONE: 'phone', EMAIL: 'email', @@ -4054,8 +4033,28 @@ const CONST = { }, } as const; +const DEFAULT_TAXES_DATA = { + defaultExternalID: 'id_TAX_EXEMPT', + defaultValue: '0%', + foreignTaxDefault: 'id_TAX_EXEMPT', + name: 'Tax', + taxes: { + id_TAX_EXEMPT: { + name: 'Tax exempt', + value: '0%', + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + id_TAX_RATE_1: { + name: 'Tax rate 1', + value: '5%', + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + }, +}; + type Country = keyof typeof CONST.ALL_COUNTRIES; export type {Country}; +export {DEFAULT_TAXES_DATA}; export default CONST; diff --git a/src/libs/API/parameters/EnablePolicyTaxesParams.ts b/src/libs/API/parameters/EnablePolicyTaxesParams.ts index 4a235d5d6a1f..13f5e94ce0ca 100644 --- a/src/libs/API/parameters/EnablePolicyTaxesParams.ts +++ b/src/libs/API/parameters/EnablePolicyTaxesParams.ts @@ -1,6 +1,9 @@ +import { Policy } from "@src/types/onyx"; + type EnablePolicyTaxesParams = { policyID: string; enabled: boolean; + taxFields?: Partial }; export default EnablePolicyTaxesParams; diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 96331736ab2e..b36fe11d3514 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -57,7 +57,7 @@ import * as PhoneNumber from '@libs/PhoneNumber'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; -import CONST from '@src/CONST'; +import CONST, {DEFAULT_TAXES_DATA} from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; @@ -3421,7 +3421,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { - taxRates: CONST.TAXES.DEFAULT, + taxRates: DEFAULT_TAXES_DATA, }, }, ], @@ -3473,7 +3473,9 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { }; const parameters: EnablePolicyTaxesParams = {policyID, enabled}; - + if (shouldAddDefaultTaxRatesData) { + parameters.taxFields = DEFAULT_TAXES_DATA; + } API.write(WRITE_COMMANDS.ENABLE_POLICY_TAXES, parameters, onyxData); if (enabled) { From 47a2dc702a8f27522c7d5baef760a68584b59b7b Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Tue, 19 Mar 2024 17:33:29 +0700 Subject: [PATCH 09/27] fix lint --- src/pages/workspace/taxes/WorkspaceTaxesPage.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pages/workspace/taxes/WorkspaceTaxesPage.tsx b/src/pages/workspace/taxes/WorkspaceTaxesPage.tsx index 014aba048ad7..d0724f4592ba 100644 --- a/src/pages/workspace/taxes/WorkspaceTaxesPage.tsx +++ b/src/pages/workspace/taxes/WorkspaceTaxesPage.tsx @@ -73,7 +73,6 @@ function WorkspaceTaxesPage({policy, route}: WorkspaceTaxesPageProps) { text: value.name, alternateText: textForDefault(key), keyForList: key, - pendingAction: value.pendingAction, isSelected: !!selectedTaxesIDs.includes(key), isDisabledCheckbox: key === defaultExternalID, pendingAction: value.pendingAction, From b4e249348099eca9a60629d2294ab5ceaf1aa5f6 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Tue, 19 Mar 2024 17:34:57 +0700 Subject: [PATCH 10/27] fix import type --- src/libs/API/parameters/EnablePolicyTaxesParams.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/API/parameters/EnablePolicyTaxesParams.ts b/src/libs/API/parameters/EnablePolicyTaxesParams.ts index 13f5e94ce0ca..dd4630078f05 100644 --- a/src/libs/API/parameters/EnablePolicyTaxesParams.ts +++ b/src/libs/API/parameters/EnablePolicyTaxesParams.ts @@ -1,9 +1,9 @@ -import { Policy } from "@src/types/onyx"; +import type {Policy} from '@src/types/onyx'; type EnablePolicyTaxesParams = { policyID: string; enabled: boolean; - taxFields?: Partial + taxFields?: Partial; }; export default EnablePolicyTaxesParams; From f49fd7e5313719e89c40384e1af8ffbd65a80f50 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Wed, 20 Mar 2024 23:56:52 +0700 Subject: [PATCH 11/27] fix offline mode issue --- src/CONST.ts | 15 +++++++++++++-- src/libs/actions/Policy.ts | 25 ++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index a6b01160282d..383b98f79aea 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -4074,7 +4074,7 @@ const CONST = { }, } as const; -const DEFAULT_TAXES_DATA = { +const DEFAULT_TAXES_OPTIMISTIC_DATA = { defaultExternalID: 'id_TAX_EXEMPT', defaultValue: '0%', foreignTaxDefault: 'id_TAX_EXEMPT', @@ -4093,9 +4093,20 @@ const DEFAULT_TAXES_DATA = { }, }; +const DEFAULT_TAXES_SUCCESS_DATA = { + taxes: { + id_TAX_EXEMPT: { + pendingAction: null, + }, + id_TAX_RATE_1: { + pendingAction: null, + }, + }, +}; + type Country = keyof typeof CONST.ALL_COUNTRIES; export type {Country}; -export {DEFAULT_TAXES_DATA}; +export {DEFAULT_TAXES_OPTIMISTIC_DATA, DEFAULT_TAXES_SUCCESS_DATA}; export default CONST; diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 2bfa38326a9b..32018bd337e7 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -59,7 +59,7 @@ import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; -import CONST, {DEFAULT_TAXES_DATA} from '@src/CONST'; +import CONST, {DEFAULT_TAXES_DATA, DEFAULT_TAXES_OPTIMISTIC_DATA, DEFAULT_TAXES_SUCCESS_DATA} from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; @@ -3571,7 +3571,25 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { - taxRates: DEFAULT_TAXES_DATA, + taxRates: DEFAULT_TAXES_OPTIMISTIC_DATA, + }, + }, + ], + successData: [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + value: { + taxRates: DEFAULT_TAXES_SUCCESS_DATA, + }, + }, + ], + failureData: [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + value: { + taxRates: undefined, }, }, ], @@ -3604,6 +3622,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { }, }, }, + ...(shouldAddDefaultTaxRatesData ? taxRatesData.successData ?? [] : []), ], failureData: [ { @@ -3613,12 +3632,12 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { tax: { trackingEnabled: !enabled, }, - taxRates: undefined, pendingFields: { tax: null, }, }, }, + ...(shouldAddDefaultTaxRatesData ? taxRatesData.successData ?? [] : []), ], }; From bd33bd36a35ab89c5ca8086a6bc01a2a51dd80e7 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Wed, 20 Mar 2024 23:58:44 +0700 Subject: [PATCH 12/27] fix failure data --- src/libs/actions/Policy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 32018bd337e7..339cd0a639ff 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3637,7 +3637,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { }, }, }, - ...(shouldAddDefaultTaxRatesData ? taxRatesData.successData ?? [] : []), + ...(shouldAddDefaultTaxRatesData ? taxRatesData.failureData ?? [] : []), ], }; From b7e8947bc091b4eeb3a195283784524b80016bce Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 21 Mar 2024 00:00:51 +0700 Subject: [PATCH 13/27] fix lint --- src/libs/actions/Policy.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 339cd0a639ff..36aa642daecc 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -59,7 +59,7 @@ import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; -import CONST, {DEFAULT_TAXES_DATA, DEFAULT_TAXES_OPTIMISTIC_DATA, DEFAULT_TAXES_SUCCESS_DATA} from '@src/CONST'; +import CONST, {DEFAULT_TAXES_OPTIMISTIC_DATA, DEFAULT_TAXES_SUCCESS_DATA} from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; @@ -3643,7 +3643,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { const parameters: EnablePolicyTaxesParams = {policyID, enabled}; if (shouldAddDefaultTaxRatesData) { - parameters.taxFields = DEFAULT_TAXES_DATA; + parameters.taxFields = DEFAULT_TAXES_OPTIMISTIC_DATA; } API.write(WRITE_COMMANDS.ENABLE_POLICY_TAXES, parameters, onyxData); From 2437b2ce4cf42f37800f1c6faaa6380fc362ab00 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 21 Mar 2024 02:34:51 +0700 Subject: [PATCH 14/27] fix refactor the default taxes data const --- src/CONST.ts | 47 ++++++++++++++------------------------ src/libs/actions/Policy.ts | 37 +++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index 383b98f79aea..0a996e3df5af 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -4072,41 +4072,28 @@ const CONST = { SESSION_STORAGE_KEYS: { INITIAL_URL: 'INITIAL_URL', }, -} as const; - -const DEFAULT_TAXES_OPTIMISTIC_DATA = { - defaultExternalID: 'id_TAX_EXEMPT', - defaultValue: '0%', - foreignTaxDefault: 'id_TAX_EXEMPT', - name: 'Tax', - taxes: { - id_TAX_EXEMPT: { - name: 'Tax exempt', - value: '0%', - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, - id_TAX_RATE_1: { - name: 'Tax rate 1', - value: '5%', - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, - }, -}; - -const DEFAULT_TAXES_SUCCESS_DATA = { - taxes: { - id_TAX_EXEMPT: { - pendingAction: null, - }, - id_TAX_RATE_1: { - pendingAction: null, + DEFAULT_TAX: { + RATES: { + 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; export type {Country}; -export {DEFAULT_TAXES_OPTIMISTIC_DATA, DEFAULT_TAXES_SUCCESS_DATA}; export default CONST; diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 36aa642daecc..c902947e27ec 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -59,7 +59,7 @@ import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; -import CONST, {DEFAULT_TAXES_OPTIMISTIC_DATA, DEFAULT_TAXES_SUCCESS_DATA} from '@src/CONST'; +import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; @@ -3571,7 +3571,23 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { - taxRates: DEFAULT_TAXES_OPTIMISTIC_DATA, + taxRates: CONST.DEFAULT_TAX.RATES, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + value: { + taxRates: { + taxes: { + id_TAX_EXEMPT: { + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + id_TAX_RATE_1: { + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + }, + }, }, }, ], @@ -3580,7 +3596,16 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { - taxRates: DEFAULT_TAXES_SUCCESS_DATA, + taxRates: { + taxes: { + id_TAX_EXEMPT: { + pendingAction: null, + }, + id_TAX_RATE_1: { + pendingAction: null, + }, + }, + }, }, }, ], @@ -3594,8 +3619,8 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { }, ], }; - const policy = ReportUtils.getPolicy(policyID); - const shouldAddDefaultTaxRatesData = isEmptyObject(policy.taxRates) && enabled; + const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]; + const shouldAddDefaultTaxRatesData = (!policy?.taxRates || isEmptyObject(policy.taxRates)) && enabled; const onyxData: OnyxData = { optimisticData: [ { @@ -3643,7 +3668,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { const parameters: EnablePolicyTaxesParams = {policyID, enabled}; if (shouldAddDefaultTaxRatesData) { - parameters.taxFields = DEFAULT_TAXES_OPTIMISTIC_DATA; + parameters.taxFields = CONST.DEFAULT_TAX.RATES; } API.write(WRITE_COMMANDS.ENABLE_POLICY_TAXES, parameters, onyxData); From 713782d57166134e1ad512f10d371ae4280daf46 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 21 Mar 2024 02:36:15 +0700 Subject: [PATCH 15/27] fix typo --- src/CONST.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index 0a996e3df5af..98f861f49f74 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -4089,8 +4089,8 @@ const CONST = { }, }, }, - } as const, -}; + }, +} as const; type Country = keyof typeof CONST.ALL_COUNTRIES; From 95af3e3c0374cf943e4e0f7fec2dce8dcc14672d Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 21 Mar 2024 02:56:36 +0700 Subject: [PATCH 16/27] fix lint --- src/libs/actions/Policy.ts | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index c902947e27ec..7f6149c21e80 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -1,7 +1,7 @@ import {PUBLIC_DOMAINS} from 'expensify-common/lib/CONST'; import ExpensiMark from 'expensify-common/lib/ExpensiMark'; import Str from 'expensify-common/lib/str'; -import {escapeRegExp} from 'lodash'; +import {escapeRegExp, reduce} from 'lodash'; import lodashClone from 'lodash/clone'; import lodashUnion from 'lodash/union'; import type {NullishDeep, OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx'; @@ -3580,12 +3580,14 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { value: { taxRates: { taxes: { - id_TAX_EXEMPT: { - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, - id_TAX_RATE_1: { - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, + ...reduce( + Object.keys(CONST.DEFAULT_TAX.RATES.taxes), + (prevTaxesData, taxKey) => ({ + ...prevTaxesData, + [taxKey]: {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}, + }), + {}, + ), }, }, }, @@ -3598,12 +3600,14 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { value: { taxRates: { taxes: { - id_TAX_EXEMPT: { - pendingAction: null, - }, - id_TAX_RATE_1: { - pendingAction: null, - }, + ...reduce( + Object.keys(CONST.DEFAULT_TAX.RATES.taxes), + (prevTaxesData, taxKey) => ({ + ...prevTaxesData, + [taxKey]: {pendingAction: null}, + }), + {}, + ), }, }, }, From 016e4f1ed4a6fad26533776e40dd71fcd52ce58b Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 21 Mar 2024 03:03:17 +0700 Subject: [PATCH 17/27] fix use array reduce --- src/libs/actions/Policy.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 7f6149c21e80..231233ac75da 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3580,8 +3580,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { value: { taxRates: { taxes: { - ...reduce( - Object.keys(CONST.DEFAULT_TAX.RATES.taxes), + ...Object.keys(CONST.DEFAULT_TAX.RATES.taxes).reduce( (prevTaxesData, taxKey) => ({ ...prevTaxesData, [taxKey]: {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}, @@ -3600,8 +3599,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { value: { taxRates: { taxes: { - ...reduce( - Object.keys(CONST.DEFAULT_TAX.RATES.taxes), + ...Object.keys(CONST.DEFAULT_TAX.RATES.taxes).reduce( (prevTaxesData, taxKey) => ({ ...prevTaxesData, [taxKey]: {pendingAction: null}, From c915f96964fd99b9b2fcc6c76ffcf75eeb3c2a33 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 21 Mar 2024 03:09:31 +0700 Subject: [PATCH 18/27] fix remove reduce --- src/libs/actions/Policy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 231233ac75da..0f88fd996d62 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -1,7 +1,7 @@ import {PUBLIC_DOMAINS} from 'expensify-common/lib/CONST'; import ExpensiMark from 'expensify-common/lib/ExpensiMark'; import Str from 'expensify-common/lib/str'; -import {escapeRegExp, reduce} from 'lodash'; +import {escapeRegExp} from 'lodash'; import lodashClone from 'lodash/clone'; import lodashUnion from 'lodash/union'; import type {NullishDeep, OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx'; From d26c90d5fc0587ea50167c6fff9ff01626ec3484 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 22 Mar 2024 00:43:36 +0700 Subject: [PATCH 19/27] fix use taxFields as string --- src/libs/actions/Policy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 0f88fd996d62..3b20e07b1dcd 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3670,7 +3670,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { const parameters: EnablePolicyTaxesParams = {policyID, enabled}; if (shouldAddDefaultTaxRatesData) { - parameters.taxFields = CONST.DEFAULT_TAX.RATES; + parameters.taxFields = JSON.stringify(CONST.DEFAULT_TAX.RATES); } API.write(WRITE_COMMANDS.ENABLE_POLICY_TAXES, parameters, onyxData); From 469be4c6887a8c17913c9c0b9d96032e02aabb86 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 22 Mar 2024 00:49:37 +0700 Subject: [PATCH 20/27] fix revert change --- src/libs/actions/Policy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 3b20e07b1dcd..0f88fd996d62 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3670,7 +3670,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { const parameters: EnablePolicyTaxesParams = {policyID, enabled}; if (shouldAddDefaultTaxRatesData) { - parameters.taxFields = JSON.stringify(CONST.DEFAULT_TAX.RATES); + parameters.taxFields = CONST.DEFAULT_TAX.RATES; } API.write(WRITE_COMMANDS.ENABLE_POLICY_TAXES, parameters, onyxData); From 2d751eabaeba08203fe1c94454e3daf5447ea8b4 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 22 Mar 2024 00:58:30 +0700 Subject: [PATCH 21/27] fix use taxFields as string --- src/libs/API/parameters/EnablePolicyTaxesParams.ts | 4 +--- src/libs/actions/Policy.ts | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libs/API/parameters/EnablePolicyTaxesParams.ts b/src/libs/API/parameters/EnablePolicyTaxesParams.ts index dd4630078f05..5517bd442231 100644 --- a/src/libs/API/parameters/EnablePolicyTaxesParams.ts +++ b/src/libs/API/parameters/EnablePolicyTaxesParams.ts @@ -1,9 +1,7 @@ -import type {Policy} from '@src/types/onyx'; - type EnablePolicyTaxesParams = { policyID: string; enabled: boolean; - taxFields?: Partial; + taxFields?: string; }; export default EnablePolicyTaxesParams; diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 0f88fd996d62..3b20e07b1dcd 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3670,7 +3670,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { const parameters: EnablePolicyTaxesParams = {policyID, enabled}; if (shouldAddDefaultTaxRatesData) { - parameters.taxFields = CONST.DEFAULT_TAX.RATES; + parameters.taxFields = JSON.stringify(CONST.DEFAULT_TAX.RATES); } API.write(WRITE_COMMANDS.ENABLE_POLICY_TAXES, parameters, onyxData); From 0cd290ef4a2a3f8a68630ffedbf703178ad61b07 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 22 Mar 2024 03:24:58 +0700 Subject: [PATCH 22/27] fix refactor optimistic data --- src/CONST.ts | 26 ++++++++++++-------------- src/libs/actions/Policy.ts | 20 +++++++++----------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index 6d74633d452a..df3af60a2300 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -4133,20 +4133,18 @@ const CONST = { INITIAL_URL: 'INITIAL_URL', }, DEFAULT_TAX: { - RATES: { - 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%', - }, + 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%', }, }, }, diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 58312a437d9f..86df999cecc5 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3666,23 +3666,20 @@ function enablePolicyTags(policyID: string, enabled: boolean) { function enablePolicyTaxes(policyID: string, enabled: boolean) { const taxRatesData: OnyxData = { optimisticData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, - value: { - taxRates: CONST.DEFAULT_TAX.RATES, - }, - }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { taxRates: { + ...CONST.DEFAULT_TAX, taxes: { - ...Object.keys(CONST.DEFAULT_TAX.RATES.taxes).reduce( + ...Object.keys(CONST.DEFAULT_TAX.taxes).reduce( (prevTaxesData, taxKey) => ({ ...prevTaxesData, - [taxKey]: {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}, + [taxKey]: { + ...CONST.DEFAULT_TAX.taxes[taxKey], + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, }), {}, ), @@ -3698,7 +3695,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { value: { taxRates: { taxes: { - ...Object.keys(CONST.DEFAULT_TAX.RATES.taxes).reduce( + ...Object.keys(CONST.DEFAULT_TAX.taxes).reduce( (prevTaxesData, taxKey) => ({ ...prevTaxesData, [taxKey]: {pendingAction: null}, @@ -3720,6 +3717,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { }, ], }; + console.log('111111111111111111', {taxRatesData}); const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]; const shouldAddDefaultTaxRatesData = (!policy?.taxRates || isEmptyObject(policy.taxRates)) && enabled; const onyxData: OnyxData = { @@ -3769,7 +3767,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { const parameters: EnablePolicyTaxesParams = {policyID, enabled}; if (shouldAddDefaultTaxRatesData) { - parameters.taxFields = JSON.stringify(CONST.DEFAULT_TAX.RATES); + parameters.taxFields = JSON.stringify(CONST.DEFAULT_TAX); } API.write(WRITE_COMMANDS.ENABLE_POLICY_TAXES, parameters, onyxData); From 0b1005e65296ca8a77d92e22612ff318b0a64852 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 22 Mar 2024 03:29:48 +0700 Subject: [PATCH 23/27] fix lint --- src/libs/actions/Policy.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 86df999cecc5..4f5095e33591 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3666,20 +3666,23 @@ function enablePolicyTags(policyID: string, enabled: boolean) { function enablePolicyTaxes(policyID: string, enabled: boolean) { const taxRatesData: OnyxData = { optimisticData: [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + value: { + taxRates: CONST.DEFAULT_TAX, + }, + }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { taxRates: { - ...CONST.DEFAULT_TAX, taxes: { ...Object.keys(CONST.DEFAULT_TAX.taxes).reduce( (prevTaxesData, taxKey) => ({ ...prevTaxesData, - [taxKey]: { - ...CONST.DEFAULT_TAX.taxes[taxKey], - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, + [taxKey]: {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}, }), {}, ), @@ -3717,7 +3720,6 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { }, ], }; - console.log('111111111111111111', {taxRatesData}); const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]; const shouldAddDefaultTaxRatesData = (!policy?.taxRates || isEmptyObject(policy.taxRates)) && enabled; const onyxData: OnyxData = { From dc5dfda2dd4d5553d24116673d364b8838b38fe6 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 22 Mar 2024 14:49:24 +0700 Subject: [PATCH 24/27] fix combine two data in optimistic data --- src/CONST.ts | 3 ++- src/libs/actions/Policy.ts | 13 +++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index df3af60a2300..2d33886b61f6 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -3,6 +3,7 @@ import dateAdd from 'date-fns/add'; import dateSubtract from 'date-fns/sub'; import Config from 'react-native-config'; import * as KeyCommand from 'react-native-key-command'; +import type {TaxRatesWithDefault} from '@src/types/onyx'; import * as Url from './libs/Url'; import SCREENS from './SCREENS'; @@ -4147,7 +4148,7 @@ const CONST = { value: '5%', }, }, - }, + } as TaxRatesWithDefault, } as const; type Country = keyof typeof CONST.ALL_COUNTRIES; diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 4f5095e33591..3675e83e5f8f 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -3666,23 +3666,20 @@ function enablePolicyTags(policyID: string, enabled: boolean) { function enablePolicyTaxes(policyID: string, enabled: boolean) { const taxRatesData: OnyxData = { optimisticData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, - value: { - taxRates: CONST.DEFAULT_TAX, - }, - }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { taxRates: { + ...CONST.DEFAULT_TAX, taxes: { ...Object.keys(CONST.DEFAULT_TAX.taxes).reduce( (prevTaxesData, taxKey) => ({ ...prevTaxesData, - [taxKey]: {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}, + [taxKey]: { + ...CONST.DEFAULT_TAX.taxes[taxKey], + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, }), {}, ), From 3472f1470c90acd1168be7549be7aadf31ffa0c4 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 22 Mar 2024 14:56:09 +0700 Subject: [PATCH 25/27] fix lint --- src/CONST.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CONST.ts b/src/CONST.ts index 2d33886b61f6..74bbd9eafcb8 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -3,9 +3,9 @@ import dateAdd from 'date-fns/add'; import dateSubtract from 'date-fns/sub'; import Config from 'react-native-config'; import * as KeyCommand from 'react-native-key-command'; -import type {TaxRatesWithDefault} from '@src/types/onyx'; import * as Url from './libs/Url'; import SCREENS from './SCREENS'; +import type {TaxRatesWithDefault} from './types/onyx'; type RateAndUnit = { unit: string; From c923ed63fbfc77a2e697ab208d440aed15f93fcb Mon Sep 17 00:00:00 2001 From: nkdengineer <161821005+nkdengineer@users.noreply.github.com> Date: Sat, 23 Mar 2024 03:55:39 +0700 Subject: [PATCH 26/27] fix rename taxt rate 1 Co-authored-by: Carlos Martins --- src/CONST.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CONST.ts b/src/CONST.ts index 74bbd9eafcb8..64d5af46cc4c 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -4144,7 +4144,7 @@ const CONST = { value: '0%', }, id_TAX_RATE_1: { - name: 'Tax rate 1', + name: 'Tax Rate 1', value: '5%', }, }, From 250baa9926cdf492785002100bad60455ccb567e Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Sat, 23 Mar 2024 04:09:43 +0700 Subject: [PATCH 27/27] fix do not import type in CONST --- src/CONST.ts | 3 +-- src/libs/actions/Policy.ts | 12 +++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index 64d5af46cc4c..a00be3f94ba4 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -5,7 +5,6 @@ import Config from 'react-native-config'; import * as KeyCommand from 'react-native-key-command'; import * as Url from './libs/Url'; import SCREENS from './SCREENS'; -import type {TaxRatesWithDefault} from './types/onyx'; type RateAndUnit = { unit: string; @@ -4148,7 +4147,7 @@ const CONST = { value: '5%', }, }, - } as TaxRatesWithDefault, + }, } as const; type Country = keyof typeof CONST.ALL_COUNTRIES; diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 3675e83e5f8f..afb2d307b2a5 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -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'; @@ -3664,6 +3665,7 @@ function enablePolicyTags(policyID: string, enabled: boolean) { } function enablePolicyTaxes(policyID: string, enabled: boolean) { + const defaultTaxRates: TaxRatesWithDefault = CONST.DEFAULT_TAX; const taxRatesData: OnyxData = { optimisticData: [ { @@ -3671,13 +3673,13 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { taxRates: { - ...CONST.DEFAULT_TAX, + ...defaultTaxRates, taxes: { - ...Object.keys(CONST.DEFAULT_TAX.taxes).reduce( + ...Object.keys(defaultTaxRates.taxes).reduce( (prevTaxesData, taxKey) => ({ ...prevTaxesData, [taxKey]: { - ...CONST.DEFAULT_TAX.taxes[taxKey], + ...defaultTaxRates.taxes[taxKey], pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }, }), @@ -3695,7 +3697,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { value: { taxRates: { taxes: { - ...Object.keys(CONST.DEFAULT_TAX.taxes).reduce( + ...Object.keys(defaultTaxRates.taxes).reduce( (prevTaxesData, taxKey) => ({ ...prevTaxesData, [taxKey]: {pendingAction: null}, @@ -3766,7 +3768,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) { const parameters: EnablePolicyTaxesParams = {policyID, enabled}; if (shouldAddDefaultTaxRatesData) { - parameters.taxFields = JSON.stringify(CONST.DEFAULT_TAX); + parameters.taxFields = JSON.stringify(defaultTaxRates); } API.write(WRITE_COMMANDS.ENABLE_POLICY_TAXES, parameters, onyxData);