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] Scan QAB is not displayed for offline created workspace #52288

Merged
merged 8 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 31 additions & 12 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ Onyx.connect({
callback: (val) => (allRecentlyUsedCurrencies = val ?? []),
});

let activePolicyID: OnyxEntry<string>;
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
*/
Expand All @@ -224,15 +230,6 @@ function getPolicy(policyID: string | undefined): OnyxEntry<Policy> {
return allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
}

/**
* Returns a primary policy for the user
*/
function getPrimaryPolicy(activePolicyID: OnyxEntry<string>, 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<Policy>): boolean {
return !!policy?.invoice?.companyName && !!policy?.invoice?.companyWebsite;
Expand All @@ -241,8 +238,8 @@ function hasInvoicingDetails(policy: OnyxEntry<Policy>): boolean {
/**
* Returns a primary invoice workspace for the user
*/
function getInvoicePrimaryWorkspace(activePolicyID: OnyxEntry<string>, 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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -4597,7 +4617,6 @@ export {
setPolicyCustomTaxName,
clearPolicyErrorField,
isCurrencySupportedForDirectReimbursement,
getPrimaryPolicy,
getInvoicePrimaryWorkspace,
createDraftWorkspace,
savePreferredExportMethod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions tests/actions/PolicyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -52,6 +56,19 @@ describe('actions/Policy', () => {
});
});

const activePolicyID: OnyxEntry<string> = 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);
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/collections/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof CONST.POLICY.TYPE>): 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(
Expand Down
Loading