From 3e07767a46977f3d83ef164b4b0d30724ec8334a Mon Sep 17 00:00:00 2001 From: Ishpaul Singh Date: Sat, 9 Nov 2024 20:00:18 +0530 Subject: [PATCH 1/6] fixes offline delete --- src/libs/actions/Policy/Policy.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index d87f0321bab0..905226428da0 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -198,6 +198,12 @@ Onyx.connect({ callback: (val) => (allRecentlyUsedCurrencies = val ?? []), }); +let activePolicyID: OnyxEntry; +Onyx.connect({ + key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, + callback: (value) => (activePolicyID = value), +}); + /** * Stores in Onyx the policy ID of the last workspace that was accessed by the user */ @@ -1620,6 +1626,8 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName const optimisticCategoriesData = buildOptimisticPolicyCategories(policyID, CONST.POLICY.DEFAULT_CATEGORIES); + const shouldSetCreatedWorkspaceAsActivePolicy = !!activePolicyID && allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`]?.type === CONST.POLICY.TYPE.PERSONAL; + const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.SET, @@ -1705,8 +1713,21 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName key: `${ONYXKEYS.COLLECTION.REPORT_DRAFT}${expenseChatReportID}`, value: null, }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_DRAFT}${adminsChatReportID}`, + value: null, + }, ]; + if (shouldSetCreatedWorkspaceAsActivePolicy) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.SET, + key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, + value: policyID, + }); + } + const successData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -1795,6 +1816,14 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName }, ]; + if (shouldSetCreatedWorkspaceAsActivePolicy) { + failureData.push({ + onyxMethod: Onyx.METHOD.SET, + key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, + value: activePolicyID ?? '', + }); + } + if (optimisticCategoriesData.optimisticData) { optimisticData.push(...optimisticCategoriesData.optimisticData); } From 63f9e48737eb140a4723cf4bdde140ed7e86db9f Mon Sep 17 00:00:00 2001 From: Ishpaul Singh Date: Sat, 9 Nov 2024 20:13:47 +0530 Subject: [PATCH 2/6] fix es lint --- src/libs/actions/Policy/Policy.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 905226428da0..14028d764171 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -198,10 +198,10 @@ Onyx.connect({ callback: (val) => (allRecentlyUsedCurrencies = val ?? []), }); -let activePolicyID: OnyxEntry; +let nvpActivePolicyID: OnyxEntry; Onyx.connect({ key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, - callback: (value) => (activePolicyID = value), + callback: (value) => (nvpActivePolicyID = value), }); /** @@ -1626,7 +1626,7 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName const optimisticCategoriesData = buildOptimisticPolicyCategories(policyID, CONST.POLICY.DEFAULT_CATEGORIES); - const shouldSetCreatedWorkspaceAsActivePolicy = !!activePolicyID && allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`]?.type === CONST.POLICY.TYPE.PERSONAL; + const shouldSetCreatedWorkspaceAsActivePolicy = !!nvpActivePolicyID && allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${nvpActivePolicyID}`]?.type === CONST.POLICY.TYPE.PERSONAL; const optimisticData: OnyxUpdate[] = [ { @@ -1820,7 +1820,7 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName failureData.push({ onyxMethod: Onyx.METHOD.SET, key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, - value: activePolicyID ?? '', + value: nvpActivePolicyID ?? '', }); } From 58e0f20dfccbb3b9cd716578992aa683ab7d5a6d Mon Sep 17 00:00:00 2001 From: Ishpaul Singh Date: Tue, 12 Nov 2024 22:20:24 +0530 Subject: [PATCH 3/6] clean up as per requests --- src/libs/actions/Policy/Policy.ts | 21 ++++++------------- .../MoneyRequestParticipantsSelector.tsx | 2 +- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index ebb3309fdf76..6ad8ca07821c 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -199,10 +199,10 @@ Onyx.connect({ callback: (val) => (allRecentlyUsedCurrencies = val ?? []), }); -let nvpActivePolicyID: OnyxEntry; +let activePolicyID: OnyxEntry; Onyx.connect({ key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, - callback: (value) => (nvpActivePolicyID = value), + callback: (value) => (activePolicyID = value), }); /** @@ -230,15 +230,6 @@ function getPolicy(policyID: string | undefined): OnyxEntry { return allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]; } -/** - * Returns a primary policy for the user - */ -function getPrimaryPolicy(activePolicyID: OnyxEntry, currentUserLogin: string | undefined): Policy | undefined { - const activeAdminWorkspaces = PolicyUtils.getActiveAdminWorkspaces(allPolicies, currentUserLogin); - const primaryPolicy: Policy | null | undefined = activeAdminWorkspaces.find((policy) => policy.id === activePolicyID); - return primaryPolicy ?? activeAdminWorkspaces.at(0); -} - /** Check if the policy has invoicing company details */ function hasInvoicingDetails(policy: OnyxEntry): boolean { return !!policy?.invoice?.companyName && !!policy?.invoice?.companyWebsite; @@ -247,8 +238,8 @@ function hasInvoicingDetails(policy: OnyxEntry): boolean { /** * Returns a primary invoice workspace for the user */ -function getInvoicePrimaryWorkspace(activePolicyID: OnyxEntry, currentUserLogin: string | undefined): Policy | undefined { - if (PolicyUtils.canSendInvoiceFromWorkspace(activePolicyID)) { +function getInvoicePrimaryWorkspace(currentUserLogin: string | undefined): Policy | undefined { + if (PolicyUtils.canSendInvoiceFromWorkspace(activePolicyID ?? '-1')) { return allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID ?? '-1'}`]; } const activeAdminWorkspaces = PolicyUtils.getActiveAdminWorkspaces(allPolicies, currentUserLogin); @@ -1627,7 +1618,7 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName const optimisticCategoriesData = buildOptimisticPolicyCategories(policyID, CONST.POLICY.DEFAULT_CATEGORIES); - const shouldSetCreatedWorkspaceAsActivePolicy = !!nvpActivePolicyID && allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${nvpActivePolicyID}`]?.type === CONST.POLICY.TYPE.PERSONAL; + const shouldSetCreatedWorkspaceAsActivePolicy = !!activePolicyID && allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`]?.type === CONST.POLICY.TYPE.PERSONAL; const optimisticData: OnyxUpdate[] = [ { @@ -1821,7 +1812,7 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName failureData.push({ onyxMethod: Onyx.METHOD.SET, key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, - value: nvpActivePolicyID ?? '', + value: activePolicyID ?? '', }); } diff --git a/src/pages/iou/request/MoneyRequestParticipantsSelector.tsx b/src/pages/iou/request/MoneyRequestParticipantsSelector.tsx index df8d7797b41f..4478951555ef 100644 --- a/src/pages/iou/request/MoneyRequestParticipantsSelector.tsx +++ b/src/pages/iou/request/MoneyRequestParticipantsSelector.tsx @@ -241,7 +241,7 @@ function MoneyRequestParticipantsSelector({ ]; if (iouType === CONST.IOU.TYPE.INVOICE) { - const policyID = option.item && ReportUtils.isInvoiceRoom(option.item) ? option.policyID : Policy.getInvoicePrimaryWorkspace(activePolicyID, currentUserLogin)?.id; + const policyID = option.item && ReportUtils.isInvoiceRoom(option.item) ? option.policyID : Policy.getInvoicePrimaryWorkspace(currentUserLogin)?.id; newParticipants.push({ policyID, isSender: true, From 53aa6466e0d43b7035d2b888471e0091cb71fe81 Mon Sep 17 00:00:00 2001 From: Ishpaul Singh Date: Tue, 12 Nov 2024 22:21:08 +0530 Subject: [PATCH 4/6] remove undefined variable --- src/libs/actions/Policy/Policy.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 6ad8ca07821c..f514a9b27158 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -4617,7 +4617,6 @@ export { setPolicyCustomTaxName, clearPolicyErrorField, isCurrencySupportedForDirectReimbursement, - getPrimaryPolicy, getInvoicePrimaryWorkspace, createDraftWorkspace, savePreferredExportMethod, From 72879a3326a3b306a0845783e01155841225954d Mon Sep 17 00:00:00 2001 From: Ishpaul Singh Date: Tue, 12 Nov 2024 22:49:48 +0530 Subject: [PATCH 5/6] unit tests --- tests/actions/PolicyTest.ts | 17 +++++++++++++++++ tests/utils/collections/policies.ts | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 2ede9f5e5228..fd88e75e01d3 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -6,6 +6,7 @@ import * as Policy from '@src/libs/actions/Policy/Policy'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy as PolicyType, Report, ReportAction, ReportActions} from '@src/types/onyx'; import type {Participant} from '@src/types/onyx/Report'; +import createRandomPolicy from '../utils/collections/policies'; import * as TestHelper from '../utils/TestHelper'; import type {MockFetch} from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; @@ -33,6 +34,9 @@ describe('actions/Policy', () => { it('creates a new workspace', async () => { (fetch as MockFetch)?.pause?.(); Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID}); + const fakePolicy = createRandomPolicy(0, CONST.POLICY.TYPE.PERSONAL); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); + Onyx.set(`${ONYXKEYS.NVP_ACTIVE_POLICY_ID}`, fakePolicy.id); await waitForBatchedUpdates(); let adminReportID; @@ -52,6 +56,19 @@ describe('actions/Policy', () => { }); }); + const activePolicyID: OnyxEntry = await new Promise((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.NVP_ACTIVE_POLICY_ID}`, + callback: (id) => { + Onyx.disconnect(connection); + resolve(id); + }, + }); + }); + console.log(activePolicyID, policyID, fakePolicy), "dsaad---------------------"; + // check if NVP_ACTIVE_POLICY_ID is updated to created policy id + expect(activePolicyID).toBe(policyID); + // check if policy was created with correct values expect(policy?.id).toBe(policyID); expect(policy?.name).toBe(WORKSPACE_NAME); diff --git a/tests/utils/collections/policies.ts b/tests/utils/collections/policies.ts index 47bf996afb7e..bda1c3242997 100644 --- a/tests/utils/collections/policies.ts +++ b/tests/utils/collections/policies.ts @@ -3,11 +3,11 @@ import type {ValueOf} from 'type-fest'; import CONST from '@src/CONST'; import type {Policy} from '@src/types/onyx'; -export default function createRandomPolicy(index: number): Policy { +export default function createRandomPolicy(index: number, type?: ValueOf): Policy { return { id: index.toString(), name: randWord(), - type: rand(Object.values(CONST.POLICY.TYPE)), + type: type ?? rand(Object.values(CONST.POLICY.TYPE)), autoReporting: randBoolean(), isPolicyExpenseChatEnabled: randBoolean(), autoReportingFrequency: rand( From 84edcc12c4d153792843773b820f84d917ffbaf0 Mon Sep 17 00:00:00 2001 From: Ishpaul Singh Date: Tue, 12 Nov 2024 22:50:23 +0530 Subject: [PATCH 6/6] remove console log --- tests/actions/PolicyTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index fd88e75e01d3..0bcd22e05bb7 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -65,7 +65,7 @@ describe('actions/Policy', () => { }, }); }); - console.log(activePolicyID, policyID, fakePolicy), "dsaad---------------------"; + // check if NVP_ACTIVE_POLICY_ID is updated to created policy id expect(activePolicyID).toBe(policyID);