Skip to content

Commit 7a5a6e7

Browse files
Refactor: isolate buildOptimisticChangePolicyData from Onyx.connect ONYXKEYS.COLLECTION.REPORT
1 parent 30d83c7 commit 7a5a6e7

3 files changed

Lines changed: 79 additions & 42 deletions

File tree

src/libs/actions/Report.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5816,6 +5816,7 @@ function navigateToTrainingModal(isChangePolicyTrainingModalDismissed: boolean,
58165816

58175817
function buildOptimisticChangePolicyData(
58185818
report: Report,
5819+
parentReport: OnyxEntry<Report>,
58195820
policy: Policy,
58205821
currentUserAccountID: number,
58215822
email: string,
@@ -6031,7 +6032,6 @@ function buildOptimisticChangePolicyData(
60316032
});
60326033

60336034
// Update the expense chat report
6034-
const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oldWorkspaceChatReportID}`];
60356035
const lastMessageText = getLastVisibleMessage(oldWorkspaceChatReportID, isReportLastVisibleArchived, {
60366036
[oldReportPreviewActionID]: updatedReportPreviewAction as ReportAction,
60376037
})?.lastMessageText;
@@ -6053,7 +6053,7 @@ function buildOptimisticChangePolicyData(
60536053
failureData.push({
60546054
onyxMethod: Onyx.METHOD.MERGE,
60556055
key: `${ONYXKEYS.COLLECTION.REPORT}${oldWorkspaceChatReportID}`,
6056-
value: chatReport,
6056+
value: parentReport,
60576057
});
60586058
}
60596059

@@ -6277,6 +6277,7 @@ function buildOptimisticChangePolicyData(
62776277
*/
62786278
function changeReportPolicy(
62796279
report: Report,
6280+
parentReport: OnyxEntry<Report>,
62806281
policy: Policy,
62816282
accountID: number,
62826283
email: string,
@@ -6292,6 +6293,7 @@ function changeReportPolicy(
62926293

62936294
const {optimisticData, successData, failureData, optimisticReportPreviewAction, optimisticMovedReportAction} = buildOptimisticChangePolicyData(
62946295
report,
6296+
parentReport,
62956297
policy,
62966298
accountID,
62976299
email,
@@ -6317,18 +6319,31 @@ function changeReportPolicy(
63176319
/**
63186320
* Invites the submitter to the new report policy, changes the policy of a report and all its child reports, and moves the report to the new policy's expense chat
63196321
*/
6320-
function changeReportPolicyAndInviteSubmitter(
6321-
report: Report,
6322-
policy: Policy,
6323-
currentUserAccountID: number,
6324-
email: string,
6325-
hasViolationsParam: boolean,
6326-
isChangePolicyTrainingModalDismissed: boolean,
6327-
isASAPSubmitBetaEnabled: boolean,
6328-
employeeList: PolicyEmployeeList | undefined,
6329-
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
6330-
isReportLastVisibleArchived: boolean | undefined,
6331-
) {
6322+
function changeReportPolicyAndInviteSubmitter({
6323+
report,
6324+
parentReport,
6325+
policy,
6326+
currentUserAccountID,
6327+
email,
6328+
hasViolationsParam,
6329+
isChangePolicyTrainingModalDismissed,
6330+
isASAPSubmitBetaEnabled,
6331+
employeeList,
6332+
formatPhoneNumber,
6333+
isReportLastVisibleArchived,
6334+
}: {
6335+
report: Report;
6336+
parentReport: OnyxEntry<Report>;
6337+
policy: Policy;
6338+
currentUserAccountID: number;
6339+
email: string;
6340+
hasViolationsParam: boolean;
6341+
isChangePolicyTrainingModalDismissed: boolean;
6342+
isASAPSubmitBetaEnabled: boolean;
6343+
employeeList: PolicyEmployeeList | undefined;
6344+
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'];
6345+
isReportLastVisibleArchived: boolean | undefined;
6346+
}) {
63326347
if (!report.reportID || !policy?.id || report.policyID === policy.id || !isExpenseReport(report) || !report.ownerAccountID) {
63336348
return;
63346349
}
@@ -6362,6 +6377,7 @@ function changeReportPolicyAndInviteSubmitter(
63626377
optimisticMovedReportAction,
63636378
} = buildOptimisticChangePolicyData(
63646379
report,
6380+
parentReport,
63656381
policy,
63666382
currentUserAccountID,
63676383
email,

src/pages/ReportChangeWorkspacePage.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function ReportChangeWorkspacePage({report, route}: ReportChangeWorkspacePagePro
5454
const {translate, formatPhoneNumber, localeCompare} = useLocalize();
5555
const reportTransactions = useReportTransactions(reportID);
5656

57+
const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`, {canBeMissing: true});
5758
const [policies, fetchStatus] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false});
5859
const [reportNextStep] = useOnyx(`${ONYXKEYS.COLLECTION.NEXT_STEP}${reportID}`, {canBeMissing: true});
5960
const [isChangePolicyTrainingModalDismissed = false] = useOnyx(ONYXKEYS.NVP_DISMISSED_PRODUCT_TRAINING, {canBeMissing: true, selector: changePolicyTrainingModalDismissedSelector});
@@ -94,21 +95,23 @@ function ReportChangeWorkspacePage({report, route}: ReportChangeWorkspacePagePro
9495
// eslint-disable-next-line @typescript-eslint/no-deprecated
9596
} else if (isExpenseReport(report) && isPolicyAdmin(policy) && report.ownerAccountID && !isPolicyMember(policy, getLoginByAccountID(report.ownerAccountID))) {
9697
const employeeList = policy?.employeeList;
97-
changeReportPolicyAndInviteSubmitter(
98+
changeReportPolicyAndInviteSubmitter({
9899
report,
100+
parentReport,
99101
policy,
100-
session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
101-
session?.email ?? '',
102-
hasViolations,
102+
currentUserAccountID: session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
103+
email: session?.email ?? '',
104+
hasViolationsParam: hasViolations,
103105
isChangePolicyTrainingModalDismissed,
104106
isASAPSubmitBetaEnabled,
105107
employeeList,
106108
formatPhoneNumber,
107109
isReportLastVisibleArchived,
108-
);
110+
});
109111
} else {
110112
changeReportPolicy(
111113
report,
114+
parentReport,
112115
policy,
113116
session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
114117
session?.email ?? '',
@@ -125,6 +128,7 @@ function ReportChangeWorkspacePage({report, route}: ReportChangeWorkspacePagePro
125128
route.params,
126129
reportID,
127130
report,
131+
parentReport,
128132
formatPhoneNumber,
129133
reportTransactions,
130134
isReportLastVisibleArchived,

tests/actions/ReportTest.ts

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,7 +2424,7 @@ describe('actions/Report', () => {
24242424
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`, newPolicy);
24252425

24262426
// When moving to another workspace
2427-
Report.changeReportPolicy(expenseReport, newPolicy, 1, '', true, false, false);
2427+
Report.changeReportPolicy(expenseReport, undefined, newPolicy, 1, '', true, false, false);
24282428
await waitForBatchedUpdates();
24292429

24302430
// Then the expense report should not be archived anymore
@@ -2462,12 +2462,16 @@ describe('actions/Report', () => {
24622462
chatReportID: '2',
24632463
parentReportID: '2',
24642464
};
2465+
const parentReport: OnyxTypes.Report = {
2466+
...createRandomReport(2, CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT),
2467+
policyID: '1',
2468+
};
24652469

24662470
const newPolicy = createRandomPolicy(2);
24672471
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`, newPolicy);
24682472

24692473
// When moving to another workspace
2470-
Report.changeReportPolicy(expenseReport, newPolicy, 1, '', false, false, false);
2474+
Report.changeReportPolicy(expenseReport, parentReport, newPolicy, 1, '', false, false, false);
24712475
await waitForBatchedUpdates();
24722476

24732477
// Then the expense report chatReportID and parentReportID should be updated to the new expense chat reportID
@@ -2524,7 +2528,7 @@ describe('actions/Report', () => {
25242528
};
25252529
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`, newPolicy);
25262530

2527-
Report.changeReportPolicy(expenseReport, newPolicy, 1, '', false, false, false);
2531+
Report.changeReportPolicy(expenseReport, undefined, newPolicy, 1, '', false, false, false);
25282532
await waitForBatchedUpdates();
25292533

25302534
const updatedReport = await new Promise<OnyxEntry<OnyxTypes.Report>>((resolve) => {
@@ -2607,7 +2611,7 @@ describe('actions/Report', () => {
26072611
};
26082612
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`, newPolicy);
26092613

2610-
Report.changeReportPolicy(expenseReport, newPolicy, 1, '', false, false, false);
2614+
Report.changeReportPolicy(expenseReport, undefined, newPolicy, 1, '', false, false, false);
26112615
await waitForBatchedUpdates();
26122616

26132617
// Then the report total should correctly include expense (-1000) and refund (+500) = -500
@@ -2678,7 +2682,7 @@ describe('actions/Report', () => {
26782682
};
26792683
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`, newPolicy);
26802684

2681-
Report.changeReportPolicy(expenseReport, newPolicy, 1, '', false, false, false);
2685+
Report.changeReportPolicy(expenseReport, undefined, newPolicy, 1, '', false, false, false);
26822686
await waitForBatchedUpdates();
26832687

26842688
// Then only AUD transaction should contribute to total (-1000), USD is excluded
@@ -2717,20 +2721,21 @@ describe('actions/Report', () => {
27172721
});
27182722

27192723
// When moving to another workspace
2720-
Report.changeReportPolicyAndInviteSubmitter(
2721-
expenseReport,
2722-
createRandomPolicy(Number(2)),
2723-
1,
2724-
'',
2725-
true,
2726-
false,
2727-
false,
2728-
{
2724+
Report.changeReportPolicyAndInviteSubmitter({
2725+
report: expenseReport,
2726+
parentReport: undefined,
2727+
policy: createRandomPolicy(Number(2)),
2728+
currentUserAccountID: 1,
2729+
email: '',
2730+
hasViolationsParam: true,
2731+
isChangePolicyTrainingModalDismissed: false,
2732+
isASAPSubmitBetaEnabled: false,
2733+
employeeList: {
27292734
[adminEmail]: {role: CONST.POLICY.ROLE.ADMIN},
27302735
},
2731-
TestHelper.formatPhoneNumber,
2732-
undefined,
2733-
);
2736+
formatPhoneNumber: TestHelper.formatPhoneNumber,
2737+
isReportLastVisibleArchived: undefined,
2738+
});
27342739
await waitForBatchedUpdates();
27352740

27362741
// Then the expense report should not be archived anymore
@@ -2804,7 +2809,19 @@ describe('actions/Report', () => {
28042809
await waitForBatchedUpdates();
28052810

28062811
// Call changeReportPolicyAndInviteSubmitter
2807-
Report.changeReportPolicyAndInviteSubmitter(expenseReport, newPolicy, 1, '', true, false, false, employeeList, TestHelper.formatPhoneNumber, false);
2812+
Report.changeReportPolicyAndInviteSubmitter({
2813+
report: expenseReport,
2814+
parentReport: undefined,
2815+
policy: newPolicy,
2816+
currentUserAccountID: 1,
2817+
email: '',
2818+
hasViolationsParam: true,
2819+
isChangePolicyTrainingModalDismissed: false,
2820+
isASAPSubmitBetaEnabled: false,
2821+
employeeList,
2822+
formatPhoneNumber: TestHelper.formatPhoneNumber,
2823+
isReportLastVisibleArchived: false,
2824+
});
28082825
await waitForBatchedUpdates();
28092826

28102827
// Simulate network failure
@@ -2991,7 +3008,7 @@ describe('actions/Report', () => {
29913008
type: CONST.REPORT.TYPE.EXPENSE,
29923009
};
29933010
const policy = createRandomPolicy(Number(1));
2994-
Report.buildOptimisticChangePolicyData(report, policy, 1, '', false, true, undefined);
3011+
Report.buildOptimisticChangePolicyData(report, undefined, policy, 1, '', false, true, undefined);
29953012
// eslint-disable-next-line @typescript-eslint/no-deprecated
29963013
expect(buildNextStepNew).toHaveBeenCalledWith({
29973014
report,
@@ -3032,7 +3049,7 @@ describe('actions/Report', () => {
30323049
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction);
30333050
await waitForBatchedUpdates();
30343051

3035-
const {optimisticData, successData, failureData} = Report.buildOptimisticChangePolicyData(report, policy, 1, '', false, true, undefined);
3052+
const {optimisticData, successData, failureData} = Report.buildOptimisticChangePolicyData(report, undefined, policy, 1, '', false, true, undefined);
30363053

30373054
// Find the transaction optimistic data
30383055
const transactionOptimisticData = optimisticData.find((data) => data.key === `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`);
@@ -3080,7 +3097,7 @@ describe('actions/Report', () => {
30803097
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction);
30813098
await waitForBatchedUpdates();
30823099

3083-
const {optimisticData} = Report.buildOptimisticChangePolicyData(report, policy, 1, '', false, true, undefined);
3100+
const {optimisticData} = Report.buildOptimisticChangePolicyData(report, undefined, policy, 1, '', false, true, undefined);
30843101

30853102
// Should NOT find transaction optimistic data when currencies are the same
30863103
const transactionOptimisticData = optimisticData.find((data) => data.key === `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`);
@@ -3115,7 +3132,7 @@ describe('actions/Report', () => {
31153132
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction);
31163133
await waitForBatchedUpdates();
31173134

3118-
const {optimisticData} = Report.buildOptimisticChangePolicyData(report, policy, 1, '', false, true, undefined);
3135+
const {optimisticData} = Report.buildOptimisticChangePolicyData(report, undefined, policy, 1, '', false, true, undefined);
31193136

31203137
// Should NOT find transaction optimistic data when transaction matches destination currency
31213138
const transactionOptimisticData = optimisticData.find((data) => data.key === `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`);
@@ -3163,7 +3180,7 @@ describe('actions/Report', () => {
31633180
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${nonMatchingTransactionID}`, nonMatchingTransaction);
31643181
await waitForBatchedUpdates();
31653182

3166-
const {optimisticData} = Report.buildOptimisticChangePolicyData(report, policy, 1, '', false, true, undefined);
3183+
const {optimisticData} = Report.buildOptimisticChangePolicyData(report, undefined, policy, 1, '', false, true, undefined);
31673184

31683185
// Should NOT find optimistic data for the matching transaction (USD matches USD destination)
31693186
const matchingOptimisticData = optimisticData.find((data) => data.key === `${ONYXKEYS.COLLECTION.TRANSACTION}${matchingTransactionID}`);

0 commit comments

Comments
 (0)