Skip to content

Commit 0113849

Browse files
authored
Merge pull request Expensify#66653 from thelullabyy/fix/64795/work-space-delete-offline
Not here page opens after entering amount when submit expense after workspace is deleted offline
2 parents cbf4c01 + 4f62558 commit 0113849

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/libs/actions/Policy/Policy.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,21 @@ function deleteWorkspace(policyID: string, policyName: string) {
387387
},
388388
];
389389

390+
if (policyID === activePolicyID) {
391+
const personalPolicyID = PolicyUtils.getPersonalPolicy()?.id;
392+
optimisticData.push({
393+
onyxMethod: Onyx.METHOD.MERGE,
394+
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
395+
value: personalPolicyID,
396+
});
397+
398+
failureData.push({
399+
onyxMethod: Onyx.METHOD.MERGE,
400+
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
401+
value: activePolicyID,
402+
});
403+
}
404+
390405
const reportsToArchive = Object.values(allReports ?? {}).filter(
391406
(report) => ReportUtils.isPolicyRelatedReport(report, policyID) && (ReportUtils.isChatRoom(report) || ReportUtils.isPolicyExpenseChat(report) || ReportUtils.isTaskReport(report)),
392407
);

tests/actions/PolicyTest.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {getOnboardingMessages} from '@libs/actions/Welcome/OnboardingFlow';
55
import {translateLocal} from '@libs/Localize';
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';
810
import CONST from '@src/CONST';
911
import IntlStore from '@src/languages/IntlStore';
1012
import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager';
@@ -645,6 +647,33 @@ describe('actions/Policy', () => {
645647

646648
expect(violations?.every((violation) => violation.type !== CONST.VIOLATION_TYPES.VIOLATION)).toBe(true);
647649
});
650+
651+
it('should update active policy ID to personal policy when deleting the active policy', async () => {
652+
const personalPolicy = createRandomPolicy(0, CONST.POLICY.TYPE.PERSONAL);
653+
const teamPolicy = createRandomPolicy(1, CONST.POLICY.TYPE.TEAM);
654+
655+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${personalPolicy.id}`, personalPolicy);
656+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${teamPolicy.id}`, teamPolicy);
657+
await Onyx.merge(ONYXKEYS.NVP_ACTIVE_POLICY_ID, teamPolicy.id);
658+
await waitForBatchedUpdates();
659+
660+
jest.spyOn(PolicyUtils, 'getPersonalPolicy').mockReturnValue(personalPolicy);
661+
662+
Policy.deleteWorkspace(teamPolicy.id, teamPolicy.name);
663+
await waitForBatchedUpdates();
664+
665+
const activePolicyID: OnyxEntry<string> = await new Promise((resolve) => {
666+
const connection = Onyx.connect({
667+
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
668+
callback: (policyID) => {
669+
Onyx.disconnect(connection);
670+
resolve(policyID);
671+
},
672+
});
673+
});
674+
675+
expect(activePolicyID).toBe(personalPolicy.id);
676+
});
648677
});
649678

650679
const TEST_EMAIL = 'esh@gmail.com';

0 commit comments

Comments
 (0)