Skip to content

Commit 9cb8e8a

Browse files
authored
Merge pull request #54278 from bernhardoj/fix/53413-can't-interact-with-failed-to-delete-workspace-item
Fix can't interact with workspace item that is previously failed to delete
2 parents 523edfe + 9f00014 commit 9cb8e8a

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

src/libs/API/parameters/CreateWorkspaceFromIOUPaymentParams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type CreateWorkspaceFromIOUPaymentParams = {
1212
customUnitRateID: string;
1313
iouReportID: string;
1414
memberData: string;
15-
reportActionID: string;
15+
reportActionID: string | undefined;
1616
};
1717

1818
export default CreateWorkspaceFromIOUPaymentParams;

src/libs/actions/Policy/Policy.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ function deleteWorkspace(policyID: string, policyName: string) {
397397
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
398398
value: {
399399
avatarURL: policy?.avatarURL,
400+
pendingAction: null,
400401
},
401402
},
402403
];
@@ -2622,14 +2623,15 @@ function createWorkspaceFromIOUPayment(iouReport: OnyxEntry<Report>): WorkspaceF
26222623
optimisticData.push({
26232624
onyxMethod: Onyx.METHOD.MERGE,
26242625
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oldChatReportID}`,
2625-
value: {[reportPreview?.reportActionID]: null},
2626+
value: {[reportPreview.reportActionID]: null},
26262627
});
26272628
failureData.push({
26282629
onyxMethod: Onyx.METHOD.MERGE,
26292630
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oldChatReportID}`,
2630-
value: {[reportPreview?.reportActionID]: reportPreview},
2631+
value: {[reportPreview.reportActionID]: reportPreview},
26312632
});
26322633
}
2634+
26332635
// To optimistically remove the GBR from the DM we need to update the hasOutstandingChildRequest param to false
26342636
optimisticData.push({
26352637
onyxMethod: Onyx.METHOD.MERGE,
@@ -2664,13 +2666,10 @@ function createWorkspaceFromIOUPayment(iouReport: OnyxEntry<Report>): WorkspaceF
26642666
},
26652667
},
26662668
});
2667-
}
2668-
2669-
if (reportPreview?.reportActionID) {
26702669
failureData.push({
26712670
onyxMethod: Onyx.METHOD.MERGE,
26722671
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${memberData.workspaceChatReportID}`,
2673-
value: {[reportPreview?.reportActionID]: null},
2672+
value: {[reportPreview.reportActionID]: null},
26742673
});
26752674
}
26762675

tests/actions/PolicyTest.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
77
import type {Policy as PolicyType, Report, ReportAction, ReportActions} from '@src/types/onyx';
88
import type {Participant} from '@src/types/onyx/Report';
99
import createRandomPolicy from '../utils/collections/policies';
10+
import createRandomReport from '../utils/collections/reports';
1011
import * as TestHelper from '../utils/TestHelper';
1112
import type {MockFetch} from '../utils/TestHelper';
1213
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
@@ -25,8 +26,10 @@ describe('actions/Policy', () => {
2526
});
2627
});
2728

29+
let mockFetch: MockFetch;
2830
beforeEach(() => {
2931
global.fetch = TestHelper.getGlobalFetchMock();
32+
mockFetch = fetch as MockFetch;
3033
return Onyx.clear().then(waitForBatchedUpdates);
3134
});
3235

@@ -222,4 +225,69 @@ describe('actions/Policy', () => {
222225
expect(policy?.autoReportingFrequency).toBe(autoReportingFrequency);
223226
});
224227
});
228+
229+
describe('deleteWorkspace', () => {
230+
it('should apply failure data when deleteWorkspace fails', async () => {
231+
// Given a policy
232+
const fakePolicy = createRandomPolicy(0);
233+
const fakeReport = {
234+
...createRandomReport(0),
235+
stateNum: CONST.REPORT.STATE_NUM.OPEN,
236+
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
237+
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
238+
policyName: fakePolicy.name,
239+
};
240+
const fakeReimbursementAccount = {errors: {}};
241+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
242+
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`, fakeReport);
243+
await Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, fakeReimbursementAccount);
244+
245+
// When deleting a workspace fails
246+
mockFetch?.fail?.();
247+
Policy.deleteWorkspace(fakePolicy.id, fakePolicy.name);
248+
249+
await waitForBatchedUpdates();
250+
251+
// Then it should apply the correct failure data
252+
await new Promise<void>((resolve) => {
253+
const connection = Onyx.connect({
254+
key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`,
255+
callback: (policy) => {
256+
Onyx.disconnect(connection);
257+
expect(policy?.pendingAction).toBeUndefined();
258+
expect(policy?.avatarURL).toBe(fakePolicy.avatarURL);
259+
resolve();
260+
},
261+
});
262+
});
263+
264+
// Unarchive the report
265+
await new Promise<void>((resolve) => {
266+
const connection = Onyx.connect({
267+
key: `${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`,
268+
callback: (report) => {
269+
Onyx.disconnect(connection);
270+
expect(report?.stateNum).toBe(fakeReport.stateNum);
271+
expect(report?.statusNum).toBe(fakeReport.statusNum);
272+
expect(report?.policyName).toBe(fakeReport.policyName);
273+
expect(report?.oldPolicyName).toBe(fakePolicy.name);
274+
expect(report?.private_isArchived).toBeUndefined();
275+
resolve();
276+
},
277+
});
278+
});
279+
280+
// Restore the reimbursement account errors
281+
await new Promise<void>((resolve) => {
282+
const connection = Onyx.connect({
283+
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
284+
callback: (reimbursementAccount) => {
285+
Onyx.disconnect(connection);
286+
expect(reimbursementAccount?.errors).not.toBeUndefined();
287+
resolve();
288+
},
289+
});
290+
});
291+
});
292+
});
225293
});

0 commit comments

Comments
 (0)