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 workflows default for workspaces #50391

Merged
merged 16 commits into from
Mar 3, 2025
8 changes: 7 additions & 1 deletion src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,8 @@ function buildPolicyData(
const optimisticCategoriesData = buildOptimisticPolicyCategories(policyID, Object.values(CONST.POLICY.DEFAULT_CATEGORIES));
const optimisticMccGroupData = buildOptimisticMccGroup();

const shouldEnableWorkflowsByDefault =
!introSelected?.choice || introSelected.choice === CONST.ONBOARDING_CHOICES.MANAGE_TEAM || introSelected.choice === CONST.ONBOARDING_CHOICES.LOOKING_AROUND;
const shouldSetCreatedWorkspaceAsActivePolicy = !!activePolicyID && allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`]?.type === CONST.POLICY.TYPE.PERSONAL;

const optimisticData: OnyxUpdate[] = [
Expand All @@ -1794,7 +1796,7 @@ function buildPolicyData(
areCategoriesEnabled: true,
areTagsEnabled: false,
areDistanceRatesEnabled: false,
areWorkflowsEnabled: false,
areWorkflowsEnabled: shouldEnableWorkflowsByDefault,
areReportFieldsEnabled: false,
areConnectionsEnabled: false,
employeeList: {
Expand Down Expand Up @@ -3145,6 +3147,7 @@ function enablePolicyWorkflows(policyID: string, enabled: boolean) {
? {
approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL,
autoReporting: false,
autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT,
harvesting: {
enabled: false,
},
Expand All @@ -3157,6 +3160,7 @@ function enablePolicyWorkflows(policyID: string, enabled: boolean) {
? {
approvalMode: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
autoReporting: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
autoReportingFrequency: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
harvesting: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
reimbursementChoice: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
}
Expand Down Expand Up @@ -3194,6 +3198,7 @@ function enablePolicyWorkflows(policyID: string, enabled: boolean) {
? {
approvalMode: policy?.approvalMode,
autoReporting: policy?.autoReporting,
autoReportingFrequency: policy?.autoReportingFrequency,
harvesting: policy?.harvesting,
reimbursementChoice: policy?.reimbursementChoice,
}
Expand All @@ -3204,6 +3209,7 @@ function enablePolicyWorkflows(policyID: string, enabled: boolean) {
? {
approvalMode: null,
autoReporting: null,
autoReportingFrequency: null,
harvesting: null,
reimbursementChoice: null,
}
Expand Down
67 changes: 67 additions & 0 deletions tests/actions/PolicyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,44 @@ describe('actions/Policy', () => {
expect(reportAction.pendingAction).toBeFalsy();
});
});

it('create a new workspace with enabled workflow if the onboarding purpose is newDotManageTeam or newDotLookingAround', async () => {
Onyx.merge(`${ONYXKEYS.NVP_INTRO_SELECTED}`, {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM});
await waitForBatchedUpdates();

const policyID = Policy.generatePolicyID();
// When a new workspace is created with introSelected set to TRACK_WORKSPACE
Policy.createWorkspace(ESH_EMAIL, true, WORKSPACE_NAME, policyID);
await waitForBatchedUpdates();

await TestHelper.getOnyxData({
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
waitForCollectionCallback: false,
callback: (policy) => {
// Then the workflows feature is enabled
expect(policy?.areWorkflowsEnabled).toBeTruthy();
},
});
});

it('create a new workspace with disabled workflow if the onboarding purpose is not newDotManageTeam or newDotLookingAround', async () => {
Onyx.merge(`${ONYXKEYS.NVP_INTRO_SELECTED}`, {choice: CONST.ONBOARDING_CHOICES.PERSONAL_SPEND});
await waitForBatchedUpdates();

const policyID = Policy.generatePolicyID();
// When a new workspace is created with introSelected set to TRACK_WORKSPACE
Policy.createWorkspace(ESH_EMAIL, true, WORKSPACE_NAME, policyID);
await waitForBatchedUpdates();

await TestHelper.getOnyxData({
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
waitForCollectionCallback: false,
callback: (policy) => {
// Chekck if the autoReportingFrequency is updated to instand
expect(policy?.areWorkflowsEnabled).toBeFalsy();
},
});
});
});

describe('upgradeToCorporate', () => {
Expand Down Expand Up @@ -375,4 +413,33 @@ describe('actions/Policy', () => {
});
});
});

describe('enablePolicyWorkflows', () => {
it('should update delay submission to instant when disabling the workflows feature', async () => {
(fetch as MockFetch)?.pause?.();
Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID});
const fakePolicy: PolicyType = {
...createRandomPolicy(0, CONST.POLICY.TYPE.TEAM),
areWorkflowsEnabled: true,
autoReporting: true,
autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.IMMEDIATE,
};
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
await waitForBatchedUpdates();

// Disable the workflow feature
Policy.enablePolicyWorkflows(fakePolicy.id, false);
await waitForBatchedUpdates();

await TestHelper.getOnyxData({
key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`,
waitForCollectionCallback: false,
callback: (policy) => {
// Chekck if the autoReportingFrequency is updated to instand
expect(policy?.areWorkflowsEnabled).toBeFalsy();
expect(policy?.autoReportingFrequency).toBe(CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT);
},
});
});
});
});
3 changes: 1 addition & 2 deletions tests/utils/TestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {fireEvent, screen} from '@testing-library/react-native';
import {Str} from 'expensify-common';
import {Linking} from 'react-native';
import Onyx from 'react-native-onyx';
import type {ConnectOptions} from 'react-native-onyx/dist/types';
import type {ConnectOptions, OnyxKey} from 'react-native-onyx/dist/types';
import type {ApiCommand, ApiRequestCommandParameters} from '@libs/API/types';
import {translateLocal} from '@libs/Localize';
import Pusher from '@libs/Pusher';
Expand All @@ -13,7 +13,6 @@ import * as Session from '@src/libs/actions/Session';
import HttpUtils from '@src/libs/HttpUtils';
import * as NumberUtils from '@src/libs/NumberUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import type {OnyxKey} from '@src/ONYXKEYS';
import appSetup from '@src/setup';
import type {Response as OnyxResponse, PersonalDetails, Report} from '@src/types/onyx';
import waitForBatchedUpdates from './waitForBatchedUpdates';
Expand Down