Skip to content

Commit c288636

Browse files
authored
Merge pull request Expensify#74124 from s77rt/default-policy-after-delete
2 parents fe70529 + c015dce commit c288636

6 files changed

Lines changed: 49 additions & 17 deletions

File tree

src/libs/actions/Policy/Policy.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx';
66
import Onyx from 'react-native-onyx';
77
import type {TupleToUnion, ValueOf} from 'type-fest';
88
import type {ReportExportType} from '@components/ButtonWithDropdownMenu/types';
9+
import type {LocaleContextProps} from '@components/LocaleContextProvider';
910
import * as API from '@libs/API';
1011
import type {
1112
AddBillingCardAndRequestWorkspaceOwnerChangeParams,
@@ -360,6 +361,7 @@ type DeleteWorkspaceActionParams = {
360361
reimbursementAccountError: Errors | undefined;
361362
bankAccountList: OnyxEntry<BankAccountList>;
362363
lastUsedPaymentMethods?: LastPaymentMethod;
364+
localeCompare: LocaleContextProps['localeCompare'];
363365
};
364366

365367
/**
@@ -381,6 +383,7 @@ function deleteWorkspace(params: DeleteWorkspaceActionParams) {
381383
reimbursementAccountError,
382384
lastUsedPaymentMethods,
383385
bankAccountList,
386+
localeCompare,
384387
} = params;
385388

386389
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
@@ -469,12 +472,17 @@ function deleteWorkspace(params: DeleteWorkspaceActionParams) {
469472
];
470473

471474
if (policyID === activePolicyID) {
472-
const personalPolicyID = PolicyUtils.getPersonalPolicy()?.id;
475+
const mostRecentlyCreatedGroupPolicy = Object.values(deprecatedAllPolicies ?? {})
476+
.filter((p) => p && p.id !== activePolicyID && p.type !== CONST.POLICY.TYPE.PERSONAL && p.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE)
477+
.sort((policyA, policyB) => localeCompare(policyB?.created ?? '', policyA?.created ?? ''))
478+
.at(0);
479+
const newActivePolicyID = mostRecentlyCreatedGroupPolicy?.id ?? PolicyUtils.getPersonalPolicy()?.id;
480+
473481
// @ts-expect-error - will be solved in https://github.com/Expensify/App/issues/73830
474482
optimisticData.push({
475483
onyxMethod: Onyx.METHOD.MERGE,
476484
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
477-
value: personalPolicyID,
485+
value: newActivePolicyID,
478486
});
479487

480488
failureData.push({
@@ -2164,6 +2172,7 @@ function buildPolicyData(options: BuildPolicyDataOptions) {
21642172
harvesting: {
21652173
enabled: !shouldEnableWorkflowsByDefault,
21662174
},
2175+
created: DateUtils.getDBTime(),
21672176
customUnits,
21682177
areCategoriesEnabled: true,
21692178
areCompanyCardsEnabled: true,

src/pages/workspace/WorkspaceOverviewPage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type WorkspaceOverviewPageProps = WithPolicyProps & PlatformStackScreenProps<Wor
6767

6868
function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: WorkspaceOverviewPageProps) {
6969
const styles = useThemeStyles();
70-
const {translate} = useLocalize();
70+
const {translate, localeCompare} = useLocalize();
7171
const {shouldUseNarrowLayout} = useResponsiveLayout();
7272
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
7373
const illustrations = useThemeIllustrations();
@@ -232,6 +232,7 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa
232232
reimbursementAccountError,
233233
bankAccountList,
234234
lastUsedPaymentMethods: lastPaymentMethod,
235+
localeCompare,
235236
});
236237
if (isOffline) {
237238
setIsDeleteModalOpen(false);
@@ -246,6 +247,7 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa
246247
transactionViolations,
247248
reimbursementAccountError,
248249
lastPaymentMethod,
250+
localeCompare,
249251
isOffline,
250252
activePolicyID,
251253
bankAccountList,

src/pages/workspace/WorkspacesListPage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ function WorkspacesListPage() {
217217
reimbursementAccountError,
218218
bankAccountList,
219219
lastUsedPaymentMethods: lastPaymentMethod,
220+
localeCompare,
220221
});
221222
if (isOffline) {
222223
setIsDeleteModalOpen(false);

src/types/onyx/Policy.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,9 @@ type Policy = OnyxCommon.OnyxValueWithOfflineFeedback<
17271727
/** When this policy was last modified */
17281728
lastModified?: string;
17291729

1730+
/** When this policy was created */
1731+
created?: string;
1732+
17301733
/** The custom units data for this policy */
17311734
customUnits?: Record<string, CustomUnit>;
17321735

tests/actions/IOUTest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ import createRandomTransaction from '../utils/collections/transaction';
112112
import getOnyxValue from '../utils/getOnyxValue';
113113
import PusherHelper from '../utils/PusherHelper';
114114
import type {MockFetch} from '../utils/TestHelper';
115-
import {getGlobalFetchMock, getOnyxData, setPersonalDetails, signInWithTestUser, translateLocal} from '../utils/TestHelper';
115+
import {getGlobalFetchMock, getOnyxData, localeCompare, setPersonalDetails, signInWithTestUser, translateLocal} from '../utils/TestHelper';
116116
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
117117
import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';
118118
import waitForNetworkPromises from '../utils/waitForNetworkPromises';
@@ -5511,6 +5511,7 @@ describe('actions/IOU', () => {
55115511
reimbursementAccountError: undefined,
55125512
bankAccountList: {},
55135513
lastUsedPaymentMethods: undefined,
5514+
localeCompare,
55145515
});
55155516
}
55165517
return waitForBatchedUpdates();

tests/actions/PolicyTest.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import {getOnboardingMessages} from '@libs/actions/Welcome/OnboardingFlow';
55
import {WRITE_COMMANDS} from '@libs/API/types';
66
// eslint-disable-next-line no-restricted-syntax
77
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
8-
// eslint-disable-next-line no-restricted-syntax
9-
import * as PolicyUtils from '@libs/PolicyUtils';
108
import CONST from '@src/CONST';
119
import IntlStore from '@src/languages/IntlStore';
1210
import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager';
@@ -1071,6 +1069,7 @@ describe('actions/Policy', () => {
10711069
reimbursementAccountError: {},
10721070
bankAccountList: {},
10731071
lastUsedPaymentMethods: undefined,
1072+
localeCompare: TestHelper.localeCompare,
10741073
});
10751074

10761075
await waitForBatchedUpdates();
@@ -1167,6 +1166,7 @@ describe('actions/Policy', () => {
11671166
reimbursementAccountError: undefined,
11681167
bankAccountList: {},
11691168
lastUsedPaymentMethods: undefined,
1169+
localeCompare: TestHelper.localeCompare,
11701170
});
11711171

11721172
await waitForBatchedUpdates();
@@ -1181,28 +1181,42 @@ describe('actions/Policy', () => {
11811181
expect(violations?.every((violation) => violation.type !== CONST.VIOLATION_TYPES.VIOLATION)).toBe(true);
11821182
});
11831183

1184-
it('should update active policy ID to personal policy when deleting the active policy', async () => {
1185-
const personalPolicy = createRandomPolicy(0, CONST.POLICY.TYPE.PERSONAL);
1186-
const teamPolicy = createRandomPolicy(1, CONST.POLICY.TYPE.TEAM);
1184+
it('should update active policy ID to most recently created group policy when deleting the active policy', async () => {
1185+
const personalPolicy = createRandomPolicy(1, CONST.POLICY.TYPE.PERSONAL);
1186+
personalPolicy.created = '2020-01-01 10:00:00';
1187+
personalPolicy.pendingAction = null;
1188+
1189+
const randomGroupPolicy = createRandomPolicy(2, CONST.POLICY.TYPE.TEAM);
1190+
randomGroupPolicy.created = '2021-01-01 10:00:00';
1191+
personalPolicy.pendingAction = null;
1192+
1193+
const randomGroupPolicy2 = createRandomPolicy(3, CONST.POLICY.TYPE.CORPORATE);
1194+
randomGroupPolicy2.created = '2022-01-01 10:00:00';
1195+
randomGroupPolicy2.pendingAction = null;
1196+
1197+
const mostRecentlyCreatedGroupPolicy = createRandomPolicy(0, CONST.POLICY.TYPE.TEAM);
1198+
mostRecentlyCreatedGroupPolicy.created = '3000-01-01 10:00:00';
1199+
mostRecentlyCreatedGroupPolicy.pendingAction = null;
11871200

11881201
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${personalPolicy.id}`, personalPolicy);
1189-
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${teamPolicy.id}`, teamPolicy);
1190-
await Onyx.merge(ONYXKEYS.NVP_ACTIVE_POLICY_ID, teamPolicy.id);
1202+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${randomGroupPolicy.id}`, randomGroupPolicy);
1203+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${randomGroupPolicy2.id}`, randomGroupPolicy2);
1204+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${mostRecentlyCreatedGroupPolicy.id}`, mostRecentlyCreatedGroupPolicy);
1205+
await Onyx.merge(ONYXKEYS.NVP_ACTIVE_POLICY_ID, randomGroupPolicy.id);
11911206
await waitForBatchedUpdates();
11921207

1193-
jest.spyOn(PolicyUtils, 'getPersonalPolicy').mockReturnValue(personalPolicy);
1194-
11951208
Policy.deleteWorkspace({
1196-
policyID: teamPolicy.id,
1197-
activePolicyID: teamPolicy.id,
1198-
policyName: teamPolicy.name,
1209+
policyID: randomGroupPolicy.id,
1210+
activePolicyID: randomGroupPolicy.id,
1211+
policyName: randomGroupPolicy.name,
11991212
lastAccessedWorkspacePolicyID: undefined,
12001213
policyCardFeeds: undefined,
12011214
reportsToArchive: [],
12021215
transactionViolations: undefined,
12031216
reimbursementAccountError: undefined,
12041217
bankAccountList: {},
12051218
lastUsedPaymentMethods: undefined,
1219+
localeCompare: TestHelper.localeCompare,
12061220
});
12071221
await waitForBatchedUpdates();
12081222

@@ -1216,7 +1230,7 @@ describe('actions/Policy', () => {
12161230
});
12171231
});
12181232

1219-
expect(activePolicyID).toBe(personalPolicy.id);
1233+
expect(activePolicyID).toBe(mostRecentlyCreatedGroupPolicy.id);
12201234
});
12211235

12221236
it('should reset lastAccessedWorkspacePolicyID when deleting the last accessed workspace', async () => {
@@ -1238,6 +1252,7 @@ describe('actions/Policy', () => {
12381252
reimbursementAccountError: undefined,
12391253
bankAccountList: {},
12401254
lastUsedPaymentMethods: undefined,
1255+
localeCompare: TestHelper.localeCompare,
12411256
});
12421257
await waitForBatchedUpdates();
12431258

@@ -1275,6 +1290,7 @@ describe('actions/Policy', () => {
12751290
reimbursementAccountError: undefined,
12761291
bankAccountList: {},
12771292
lastUsedPaymentMethods: undefined,
1293+
localeCompare: TestHelper.localeCompare,
12781294
});
12791295
await waitForBatchedUpdates();
12801296

0 commit comments

Comments
 (0)