Skip to content

Commit d59cda4

Browse files
authored
Merge pull request Expensify#82360 from bernhardoj/chore/66574-refactor-policy-to-use-useonyx-2
Refactor leaveWorkspace to accept policy object
2 parents 0afbe9c + 221acc0 commit d59cda4

5 files changed

Lines changed: 54 additions & 17 deletions

File tree

src/libs/actions/Policy/Policy.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,19 +1173,18 @@ function setWorkspaceReimbursement({
11731173
API.write(WRITE_COMMANDS.SET_WORKSPACE_REIMBURSEMENT, params, {optimisticData, failureData, successData});
11741174
}
11751175

1176-
function leaveWorkspace(currentUserAccountID: number, policyID?: string) {
1177-
if (!policyID) {
1176+
function leaveWorkspace(currentUserAccountID: number, policy: OnyxEntry<Policy>) {
1177+
if (!policy) {
11781178
return;
11791179
}
1180-
const policy = deprecatedAllPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
1181-
const workspaceChats = ReportUtils.getAllWorkspaceReports(policyID);
1180+
const workspaceChats = ReportUtils.getAllWorkspaceReports(policy.id);
11821181

11831182
const optimisticData: Array<
11841183
OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY | typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_METADATA | typeof ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS>
11851184
> = [
11861185
{
11871186
onyxMethod: Onyx.METHOD.MERGE,
1188-
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
1187+
key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`,
11891188
value: {
11901189
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE,
11911190
employeeList: {
@@ -1200,7 +1199,7 @@ function leaveWorkspace(currentUserAccountID: number, policyID?: string) {
12001199
const successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY | typeof ONYXKEYS.COLLECTION.REPORT_METADATA | typeof ONYXKEYS.COLLECTION.REPORT>> = [
12011200
{
12021201
onyxMethod: Onyx.METHOD.MERGE,
1203-
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
1202+
key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`,
12041203
value: null,
12051204
},
12061205
];
@@ -1209,7 +1208,7 @@ function leaveWorkspace(currentUserAccountID: number, policyID?: string) {
12091208
> = [
12101209
{
12111210
onyxMethod: Onyx.METHOD.MERGE,
1212-
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
1211+
key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`,
12131212
value: {
12141213
pendingAction: policy?.pendingAction ?? null,
12151214
employeeList: {
@@ -1317,7 +1316,7 @@ function leaveWorkspace(currentUserAccountID: number, policyID?: string) {
13171316
}
13181317

13191318
const params: LeavePolicyParams = {
1320-
policyID,
1319+
policyID: policy.id,
13211320
email: deprecatedSessionEmail,
13221321
};
13231322
API.write(WRITE_COMMANDS.LEAVE_POLICY, params, {optimisticData, successData, failureData});

src/pages/workspace/WorkspaceOverviewPage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,14 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa
275275
]);
276276

277277
const handleLeaveWorkspace = useCallback(() => {
278-
if (!policyID) {
278+
if (!policy) {
279279
return;
280280
}
281281

282-
leaveWorkspace(currentUserPersonalDetails.accountID, policyID);
282+
leaveWorkspace(currentUserPersonalDetails.accountID, policy);
283283
setIsLeaveModalOpen(false);
284284
goBackFromInvalidPolicy();
285-
}, [currentUserPersonalDetails.accountID, policyID]);
285+
}, [currentUserPersonalDetails.accountID, policy]);
286286

287287
const hideDeleteWorkspaceErrorModal = () => {
288288
setIsDeleteWorkspaceErrorModalOpen(false);

src/pages/workspace/WorkspacesListPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,11 @@ function WorkspacesListPage() {
247247
};
248248

249249
const confirmLeaveAndHideModal = () => {
250-
if (!policyIDToLeave) {
250+
if (!policyToLeave) {
251251
return;
252252
}
253253

254-
leaveWorkspace(currentUserPersonalDetails.accountID, policyIDToLeave);
254+
leaveWorkspace(currentUserPersonalDetails.accountID, policyToLeave);
255255
setIsLeaveModalOpen(false);
256256
};
257257

tests/actions/IOUTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9148,7 +9148,7 @@ describe('actions/IOU', () => {
91489148
}
91499149

91509150
deleteWorkspace({
9151-
policies: {[policy.id]: policy},
9151+
policies: {[`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`]: policy},
91529152
policyID: policy.id,
91539153
personalPolicyID: undefined,
91549154
activePolicyID: undefined,

tests/actions/PolicyTest.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -823,11 +823,12 @@ describe('actions/Policy', () => {
823823
it("should remove all non-owned workspace chats and keep the user's own workspace chat when leaving a workspace", async () => {
824824
await Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID});
825825
const policyID = Policy.generatePolicyID();
826-
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {
826+
const policy: PolicyType = {
827827
...createRandomPolicy(0, CONST.POLICY.TYPE.TEAM),
828828
id: policyID,
829829
name: WORKSPACE_NAME,
830-
});
830+
};
831+
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, policy);
831832
await waitForBatchedUpdates();
832833

833834
const ownWorkspaceChat: Report = {
@@ -856,7 +857,7 @@ describe('actions/Policy', () => {
856857
const getAllWorkspaceReportsSpy = jest.spyOn(ReportUtils, 'getAllWorkspaceReports').mockReturnValue([ownWorkspaceChat, ...nonOwnedWorkspaceChats]);
857858
const apiWriteSpy = jest.spyOn(require('@libs/API'), 'write').mockImplementation(() => Promise.resolve());
858859

859-
Policy.leaveWorkspace(ESH_ACCOUNT_ID, policyID);
860+
Policy.leaveWorkspace(ESH_ACCOUNT_ID, policy);
860861
await waitForBatchedUpdates();
861862

862863
expect(apiWriteSpy).toHaveBeenCalledWith(
@@ -1929,6 +1930,43 @@ describe('actions/Policy', () => {
19291930
});
19301931
});
19311932

1933+
describe('leaveWorkspace', () => {
1934+
it('should leave workspace and archive reports', async () => {
1935+
// Given a policy and a report in Onyx
1936+
const policy = createRandomPolicy(1);
1937+
const report = {
1938+
...createRandomReport(1, undefined),
1939+
policyID: policy.id,
1940+
};
1941+
1942+
// Set initial state in Onyx
1943+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy);
1944+
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report);
1945+
1946+
// When leaveWorkspace is called
1947+
Policy.leaveWorkspace(1, policy);
1948+
1949+
await waitForBatchedUpdates();
1950+
1951+
// Then the policy should be removed
1952+
const policyAfter = await getOnyxValue(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`);
1953+
expect(policyAfter).toBeFalsy();
1954+
1955+
// And the report should be closed and archived
1956+
const reportAfter = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`);
1957+
expect(reportAfter).toMatchObject({
1958+
statusNum: CONST.REPORT.STATUS_NUM.CLOSED,
1959+
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
1960+
});
1961+
1962+
const reportNameValuePairsAfter = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`);
1963+
expect(reportNameValuePairsAfter?.private_isArchived).toBeTruthy();
1964+
1965+
const reportMetadataAfter = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${report.reportID}`);
1966+
expect(reportMetadataAfter?.pendingChatMembers).toBeFalsy();
1967+
});
1968+
});
1969+
19321970
describe('generateDefaultWorkspaceName', () => {
19331971
beforeAll(() => {
19341972
Onyx.set(ONYXKEYS.COLLECTION.POLICY, {});

0 commit comments

Comments
 (0)