Skip to content

Commit 83a7b8e

Browse files
authored
Merge pull request Expensify#84017 from dukenv0307/fix/66415-part-1
refactor getSortedSections getWelcomeMessage and getAlternateText to use policy from useOnyx
2 parents 55c834e + d9532da commit 83a7b8e

7 files changed

Lines changed: 157 additions & 7 deletions

File tree

src/components/Search/SearchFiltersChatsSelector.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ function SearchFiltersChatsSelector({initialReportIDs, onFiltersUpdate, isScreen
7373
createOptionFromReport({...reportData, reportID: id}, personalDetails, currentUserAccountID, chatReport, privateIsArchived, reportAttributesDerived),
7474
);
7575
const isReportArchived = !!privateIsArchived;
76-
const alternateText = getAlternateText(report, {}, isReportArchived, currentUserEmail, {}, undefined, undefined, reportAttributesDerived);
76+
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${reportData?.policyID}`];
77+
const alternateText = getAlternateText(report, {}, isReportArchived, currentUserEmail, policy, {}, undefined, undefined, reportAttributesDerived);
7778
return {...report, alternateText};
7879
});
7980

src/libs/OptionsListUtils/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ function getAlternateText(
425425
{showChatPreviewLine = false, forcePolicyNamePreview = false}: PreviewConfig,
426426
isReportArchived: boolean | undefined,
427427
currentUserLogin: string,
428+
// We'll make it required in the next PR. Ref: https://github.com/Expensify/App/issues/66415
429+
policy?: OnyxEntry<Policy>,
428430
lastActorDetails: Partial<PersonalDetails> | null = {},
429431
visibleReportActionsData: VisibleReportActionsDerivedValue = {},
430432
translate?: LocalizedTranslate,
@@ -445,6 +447,7 @@ function getAlternateText(
445447
translate: translateFn,
446448
report,
447449
lastActorDetails,
450+
policy,
448451
isReportArchived,
449452
chatReport,
450453
visibleReportActionsDataParam: visibleReportActionsData,
@@ -1052,6 +1055,8 @@ function createOption(
10521055
{showChatPreviewLine, forcePolicyNamePreview},
10531056
!!result.private_isArchived,
10541057
currentUserLogin,
1058+
// TODO: Remove this in the next PR that will refactor prepareReportOptionsForDisplay. Ref: https://github.com/Expensify/App/issues/66415
1059+
undefined,
10551060
lastActorDetails,
10561061
visibleReportActionsData,
10571062
translateFn,
@@ -2251,6 +2256,8 @@ function prepareReportOptionsForDisplay(
22512256
{showChatPreviewLine, forcePolicyNamePreview},
22522257
!!option.private_isArchived,
22532258
currentUserLogin,
2259+
// TODO: Remove this in the next PR that will refactor prepareReportOptionsForDisplay. Ref: https://github.com/Expensify/App/issues/66415
2260+
undefined,
22542261
null,
22552262
visibleReportActionsData,
22562263
undefined,

src/libs/ReportUtils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,14 +1066,12 @@ Onyx.connect({
10661066
});
10671067

10681068
let allPolicies: OnyxCollection<Policy>;
1069-
let hasPolicies: boolean;
10701069
let policiesArray: Policy[] = [];
10711070
Onyx.connect({
10721071
key: ONYXKEYS.COLLECTION.POLICY,
10731072
waitForCollectionCallback: true,
10741073
callback: (value) => {
10751074
allPolicies = value;
1076-
hasPolicies = !isEmptyObject(value);
10771075
policiesArray = Object.values(value ?? {}).filter((policy): policy is Policy => !!policy);
10781076
},
10791077
});
@@ -1400,7 +1398,7 @@ function getPolicyName({report, returnEmptyIfNotFound = false, policy, policies,
14001398
const noPolicyFound = returnEmptyIfNotFound ? '' : unavailableTranslation;
14011399
const parentReport = report ? getRootParentReport({report, reports}) : undefined;
14021400

1403-
if ((!report?.policyName && !parentReport?.policyName && !hasPolicies && isEmptyObject(policies)) || isEmptyObject(report)) {
1401+
if ((!report?.policyName && !parentReport?.policyName && isEmptyObject(policies) && isEmptyObject(allPolicies)) || isEmptyObject(report)) {
14041402
return noPolicyFound;
14051403
}
14061404
const finalPolicy = (() => {

src/libs/SearchUIUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,8 +3147,8 @@ function getSortedTransactionData(
31473147
const aIsUnreported = a.report?.type !== CONST.REPORT.TYPE.EXPENSE && a.report?.type !== CONST.REPORT.TYPE.INVOICE;
31483148
const bIsUnreported = b.report?.type !== CONST.REPORT.TYPE.EXPENSE && b.report?.type !== CONST.REPORT.TYPE.INVOICE;
31493149

3150-
const aValue = !aIsUnreported ? getPolicyName({report: a.report}) : '';
3151-
const bValue = !bIsUnreported ? getPolicyName({report: b.report}) : '';
3150+
const aValue = !aIsUnreported ? getPolicyName({report: a.report, policy: a.policy}) : '';
3151+
const bValue = !bIsUnreported ? getPolicyName({report: b.report, policy: b.policy}) : '';
31523152
return compareValues(aValue, bValue, sortOrder, sortBy, localeCompare);
31533153
});
31543154
}

src/libs/SidebarUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ function getWelcomeMessage(
12301230
} else {
12311231
welcomeMessage.messageHtml = translate(
12321232
'reportActionsView.beginningOfChatHistoryPolicyExpenseChat',
1233-
getPolicyName({report}),
1233+
getPolicyName({report, policy}),
12341234
getDisplayNameForParticipant({accountID: report?.ownerAccountID, formatPhoneNumber: formatPhoneNumberPhoneUtils}),
12351235
);
12361236
welcomeMessage.messageText = Parser.htmlToText(welcomeMessage.messageHtml);

tests/unit/OptionsListUtilsTest.tsx

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4324,6 +4324,7 @@ describe('OptionsListUtils', () => {
43244324
translate: translateLocal,
43254325
report,
43264326
lastActorDetails: null,
4327+
policy: undefined,
43274328
isReportArchived: false,
43284329
chatReport: undefined,
43294330
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4349,6 +4350,7 @@ describe('OptionsListUtils', () => {
43494350
translate: translateLocal,
43504351
report,
43514352
lastActorDetails: null,
4353+
policy: undefined,
43524354
isReportArchived: false,
43534355
chatReport: undefined,
43544356
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4375,6 +4377,7 @@ describe('OptionsListUtils', () => {
43754377
translate: translateLocal,
43764378
report,
43774379
lastActorDetails: null,
4380+
policy: undefined,
43784381
isReportArchived: false,
43794382
chatReport: undefined,
43804383
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4401,6 +4404,7 @@ describe('OptionsListUtils', () => {
44014404
translate: translateLocal,
44024405
report,
44034406
lastActorDetails: null,
4407+
policy: undefined,
44044408
isReportArchived: false,
44054409
chatReport: undefined,
44064410
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4424,6 +4428,7 @@ describe('OptionsListUtils', () => {
44244428
translate: translateLocal,
44254429
report,
44264430
lastActorDetails: null,
4431+
policy: undefined,
44274432
isReportArchived: false,
44284433
chatReport: undefined,
44294434
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4446,6 +4451,7 @@ describe('OptionsListUtils', () => {
44464451
translate: translateLocal,
44474452
report,
44484453
lastActorDetails: null,
4454+
policy: undefined,
44494455
isReportArchived: false,
44504456
chatReport: undefined,
44514457
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4467,6 +4473,7 @@ describe('OptionsListUtils', () => {
44674473
translate: translateLocal,
44684474
report,
44694475
lastActorDetails: null,
4476+
policy: undefined,
44704477
isReportArchived: false,
44714478
chatReport: undefined,
44724479
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4488,6 +4495,7 @@ describe('OptionsListUtils', () => {
44884495
translate: translateLocal,
44894496
report,
44904497
lastActorDetails: null,
4498+
policy: undefined,
44914499
isReportArchived: false,
44924500
chatReport: undefined,
44934501
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4509,6 +4517,7 @@ describe('OptionsListUtils', () => {
45094517
translate: translateLocal,
45104518
report,
45114519
lastActorDetails: null,
4520+
policy: undefined,
45124521
isReportArchived: false,
45134522
chatReport: undefined,
45144523
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4530,6 +4539,7 @@ describe('OptionsListUtils', () => {
45304539
translate: translateLocal,
45314540
report,
45324541
lastActorDetails: null,
4542+
policy: undefined,
45334543
isReportArchived: false,
45344544
chatReport: undefined,
45354545
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4551,6 +4561,7 @@ describe('OptionsListUtils', () => {
45514561
translate: translateLocal,
45524562
report,
45534563
lastActorDetails: null,
4564+
policy: undefined,
45544565
isReportArchived: false,
45554566
chatReport: undefined,
45564567
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4576,6 +4587,7 @@ describe('OptionsListUtils', () => {
45764587
translate: translateLocal,
45774588
report,
45784589
lastActorDetails: null,
4590+
policy: undefined,
45794591
isReportArchived: false,
45804592
chatReport: undefined,
45814593
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4603,6 +4615,7 @@ describe('OptionsListUtils', () => {
46034615
translate: translateLocal,
46044616
report,
46054617
lastActorDetails: null,
4618+
policy: undefined,
46064619
isReportArchived: false,
46074620
chatReport: undefined,
46084621
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4621,6 +4634,7 @@ describe('OptionsListUtils', () => {
46214634
translate: translateLocal,
46224635
report,
46234636
lastActorDetails: null,
4637+
policy: undefined,
46244638
isReportArchived: false,
46254639
chatReport: undefined,
46264640
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4651,6 +4665,7 @@ describe('OptionsListUtils', () => {
46514665
translate: translateLocal,
46524666
report,
46534667
lastActorDetails: null,
4668+
policy: undefined,
46544669
isReportArchived: false,
46554670
chatReport: undefined,
46564671
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4690,6 +4705,7 @@ describe('OptionsListUtils', () => {
46904705
report,
46914706
translate: translateLocal,
46924707
lastActorDetails: null,
4708+
policy: undefined,
46934709
isReportArchived: false,
46944710
chatReport: undefined,
46954711
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4775,6 +4791,7 @@ describe('OptionsListUtils', () => {
47754791
translate: translateLocal,
47764792
report,
47774793
lastActorDetails: null,
4794+
policy: undefined,
47784795
isReportArchived: false,
47794796
chatReport: undefined,
47804797
currentUserLogin: CURRENT_USER_EMAIL,
@@ -4805,13 +4822,99 @@ describe('OptionsListUtils', () => {
48054822
translate: translateLocal,
48064823
report,
48074824
lastActorDetails: null,
4825+
policy: undefined,
48084826
isReportArchived: false,
48094827
chatReport: undefined,
48104828
currentUserLogin: CURRENT_USER_EMAIL,
48114829
});
48124830
expect(lastMessage).toBe(translate(CONST.LOCALES.EN, 'iou.error.genericCreateFailureMessage'));
48134831
});
48144832
});
4833+
4834+
describe('archived report with policy', () => {
4835+
it('should use the passed policy name for POLICY_DELETED archive reason', async () => {
4836+
const testPolicyID = 'archivePolicyTest';
4837+
const policy: Policy = {
4838+
id: testPolicyID,
4839+
name: 'Test Workspace',
4840+
type: CONST.POLICY.TYPE.TEAM,
4841+
} as Policy;
4842+
const report: Report = {
4843+
...createRandomReport(0, undefined),
4844+
policyID: testPolicyID,
4845+
type: CONST.REPORT.TYPE.CHAT,
4846+
};
4847+
const closedAction = {
4848+
...createRandomReportAction(1),
4849+
actionName: CONST.REPORT.ACTIONS.TYPE.CLOSED,
4850+
originalMessage: {
4851+
policyName: policy.name,
4852+
reason: CONST.REPORT.ARCHIVE_REASON.POLICY_DELETED,
4853+
},
4854+
} as ReportAction;
4855+
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, {
4856+
[closedAction.reportActionID]: closedAction,
4857+
});
4858+
4859+
const lastMessage = getLastMessageTextForReport({
4860+
translate: translateLocal,
4861+
report,
4862+
lastActorDetails: null,
4863+
policy,
4864+
isReportArchived: true,
4865+
chatReport: undefined,
4866+
currentUserLogin: '',
4867+
});
4868+
4869+
expect(lastMessage).toBe(
4870+
translateLocal('reportArchiveReasons.policyDeleted', {
4871+
policyName: policy.name,
4872+
}),
4873+
);
4874+
});
4875+
4876+
it('should use the passed policy name for REMOVED_FROM_POLICY archive reason', async () => {
4877+
const testPolicyID = 'archivePolicyTest2';
4878+
const policy: Policy = {
4879+
id: testPolicyID,
4880+
name: 'My Workspace',
4881+
type: CONST.POLICY.TYPE.TEAM,
4882+
} as Policy;
4883+
const report: Report = {
4884+
...createRandomReport(0, undefined),
4885+
policyID: testPolicyID,
4886+
type: CONST.REPORT.TYPE.CHAT,
4887+
};
4888+
const closedAction = {
4889+
...createRandomReportAction(1),
4890+
actionName: CONST.REPORT.ACTIONS.TYPE.CLOSED,
4891+
originalMessage: {
4892+
policyName: policy.name,
4893+
reason: CONST.REPORT.ARCHIVE_REASON.REMOVED_FROM_POLICY,
4894+
},
4895+
} as ReportAction;
4896+
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, {
4897+
[closedAction.reportActionID]: closedAction,
4898+
});
4899+
4900+
const lastMessage = getLastMessageTextForReport({
4901+
translate: translateLocal,
4902+
report,
4903+
lastActorDetails: null,
4904+
policy,
4905+
isReportArchived: true,
4906+
chatReport: undefined,
4907+
currentUserLogin: '',
4908+
});
4909+
4910+
expect(lastMessage).toBe(
4911+
translateLocal('reportArchiveReasons.removedFromPolicy', {
4912+
displayName: 'Hidden',
4913+
policyName: policy.name,
4914+
}),
4915+
);
4916+
});
4917+
});
48154918
});
48164919

48174920
describe('getPersonalDetailSearchTerms', () => {
@@ -6536,6 +6639,7 @@ describe('OptionsListUtils', () => {
65366639
translate: jest.fn().mockReturnValue(''),
65376640
report,
65386641
lastActorDetails: null,
6642+
policy: undefined,
65396643
isReportArchived: false,
65406644
chatReport,
65416645
currentUserLogin: CURRENT_USER_EMAIL,
@@ -6555,6 +6659,7 @@ describe('OptionsListUtils', () => {
65556659
translate: jest.fn().mockReturnValue(''),
65566660
report,
65576661
lastActorDetails: null,
6662+
policy: undefined,
65586663
isReportArchived: false,
65596664
chatReport: undefined,
65606665
currentUserLogin: CURRENT_USER_EMAIL,

tests/unit/ReportUtilsTest.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13428,6 +13428,45 @@ describe('ReportUtils', () => {
1342813428
});
1342913429
expect(result).toBe('Report Fallback Name');
1343013430
});
13431+
13432+
it('should return noPolicyFound when policy, policies, and allPolicies are all empty and report has no policyName', async () => {
13433+
// Clear all policies from Onyx so allPolicies is empty
13434+
await Onyx.clear();
13435+
await waitForBatchedUpdates();
13436+
13437+
const report: Report = {
13438+
...createRandomReport(1, undefined),
13439+
policyID: 'nonexistent',
13440+
policyName: undefined,
13441+
oldPolicyName: undefined,
13442+
};
13443+
13444+
const result = getPolicyName({report, returnEmptyIfNotFound: true});
13445+
expect(result).toBe('');
13446+
});
13447+
13448+
it('should not trigger early return when allPolicies has data even if policy and policies params are empty', async () => {
13449+
const onyxPolicy: Policy = {
13450+
...createRandomPolicy(1),
13451+
id: 'guardTestPolicy',
13452+
name: 'Guard Test Policy',
13453+
};
13454+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${onyxPolicy.id}`, onyxPolicy);
13455+
await waitForBatchedUpdates();
13456+
13457+
const report: Report = {
13458+
...createRandomReport(1, undefined),
13459+
policyID: 'guardTestPolicy',
13460+
policyName: undefined,
13461+
};
13462+
13463+
// No policy or policies passed, but allPolicies has the policy via Onyx
13464+
const result = getPolicyName({report});
13465+
expect(result).toBe('Guard Test Policy');
13466+
13467+
// Cleanup
13468+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${onyxPolicy.id}`, null);
13469+
});
1343113470
});
1343213471

1343313472
describe('getBillableAndTaxTotal', () => {

0 commit comments

Comments
 (0)