Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the getPolicyName function for when the report or policies are not ready #48621

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,13 +739,17 @@ const unavailableTranslation = Localize.translateLocal('workspace.common.unavail
*/
function getPolicyName(report: OnyxInputOrEntry<Report>, returnEmptyIfNotFound = false, policy?: OnyxInputOrEntry<Policy>): string {
const noPolicyFound = returnEmptyIfNotFound ? '' : unavailableTranslation;
if (isEmptyObject(report)) {

// Ensure policies and report are fully loaded before proceeding.
// On initial login, they may be partially loaded, so additional checks are necessary to prevent unexpected behavior.
if (isEmptyObject(report) || !report.reportID || isEmptyObject(allPolicies)) {
return noPolicyFound;
}

if ((!allPolicies || Object.keys(allPolicies).length === 0) && !report?.policyName) {
if (isEmptyObject(allPolicies) && !report?.policyName) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@allgandalf upon further consideration, I believe the second check is unreachable. My suggestion would be to eliminate it. Your thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i too checked, it is unreachable , lets remove this

return unavailableTranslation;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wildan-m This whole expression is unreachable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm aware of that. This is my latest suggestion.

@allgandalf what is your opinion?


const finalPolicy = policy ?? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`];

const parentReport = getRootParentReport(report);
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe('ReportUtils', () => {
test('as member', () => {
expect(
ReportUtils.getReportName({
reportID: '',
reportID: '123',
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
policyID: policy.id,
isOwnPolicyExpenseChat: true,
Expand All @@ -220,7 +220,7 @@ describe('ReportUtils', () => {
test('as admin', () => {
expect(
ReportUtils.getReportName({
reportID: '',
reportID: '123',
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
policyID: policy.id,
isOwnPolicyExpenseChat: false,
Expand All @@ -232,7 +232,7 @@ describe('ReportUtils', () => {

describe('Archived', () => {
const baseArchivedPolicyExpenseChat = {
reportID: '',
reportID: '123',
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
ownerAccountID: 1,
policyID: policy.id,
Expand Down
Loading