diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 690a55daa1d0..f514a9b27158 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -199,6 +199,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 */ @@ -224,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; @@ -241,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); @@ -1621,6 +1618,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, @@ -1706,8 +1705,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, @@ -1796,6 +1808,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); } @@ -4597,7 +4617,6 @@ export { setPolicyCustomTaxName, clearPolicyErrorField, isCurrencySupportedForDirectReimbursement, - getPrimaryPolicy, getInvoicePrimaryWorkspace, createDraftWorkspace, savePreferredExportMethod, 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, diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 2ede9f5e5228..0bcd22e05bb7 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); + }, + }); + }); + + // 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(