From db25e268bba13cf6f530f6eddb7da24191a60ff5 Mon Sep 17 00:00:00 2001 From: Wildan Muhlis Date: Thu, 5 Sep 2024 13:27:21 +0700 Subject: [PATCH 1/5] Fix getPolicyName when report / policies not ready --- src/libs/ReportUtils.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 7b226b2e5c8e..e307082f7eeb 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -739,13 +739,17 @@ const unavailableTranslation = Localize.translateLocal('workspace.common.unavail */ function getPolicyName(report: OnyxInputOrEntry, returnEmptyIfNotFound = false, policy?: OnyxInputOrEntry): 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) { return unavailableTranslation; } + const finalPolicy = policy ?? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]; const parentReport = getRootParentReport(report); From 3134d83b6d10e8a07e5f2fd95fbaef24e43e47c5 Mon Sep 17 00:00:00 2001 From: Wildan Muhlis Date: Thu, 5 Sep 2024 14:12:20 +0700 Subject: [PATCH 2/5] lint --- src/libs/ReportUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index e307082f7eeb..8bedeae5dbcb 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -739,13 +739,13 @@ const unavailableTranslation = Localize.translateLocal('workspace.common.unavail */ function getPolicyName(report: OnyxInputOrEntry, returnEmptyIfNotFound = false, policy?: OnyxInputOrEntry): string { const noPolicyFound = returnEmptyIfNotFound ? '' : unavailableTranslation; - + // 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 (isEmptyObject(allPolicies) && !report?.policyName) { return unavailableTranslation; } From 93dbf1a2ad6978f7051cc1d2264ea3025abd88e3 Mon Sep 17 00:00:00 2001 From: Wildan Muhlis Date: Thu, 5 Sep 2024 14:12:40 +0700 Subject: [PATCH 3/5] Add dummy reportID instead of empty --- tests/unit/ReportUtilsTest.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index 6161fa57f75c..aeec0b92ff41 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -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, @@ -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, @@ -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, From d0a5e8b1d4324eece8aab5cd5679fed9ea511513 Mon Sep 17 00:00:00 2001 From: Wildan Muhlis Date: Thu, 5 Sep 2024 16:07:38 +0700 Subject: [PATCH 4/5] Revert unnecessary changes --- tests/unit/ReportUtilsTest.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index aeec0b92ff41..6161fa57f75c 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -208,7 +208,7 @@ describe('ReportUtils', () => { test('as member', () => { expect( ReportUtils.getReportName({ - reportID: '123', + reportID: '', chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, policyID: policy.id, isOwnPolicyExpenseChat: true, @@ -220,7 +220,7 @@ describe('ReportUtils', () => { test('as admin', () => { expect( ReportUtils.getReportName({ - reportID: '123', + reportID: '', chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, policyID: policy.id, isOwnPolicyExpenseChat: false, @@ -232,7 +232,7 @@ describe('ReportUtils', () => { describe('Archived', () => { const baseArchivedPolicyExpenseChat = { - reportID: '123', + reportID: '', chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, ownerAccountID: 1, policyID: policy.id, From d49601aa625bfe6e0c0199797079c8aabc102f17 Mon Sep 17 00:00:00 2001 From: Wildan Muhlis Date: Mon, 9 Sep 2024 13:15:31 +0700 Subject: [PATCH 5/5] Change solution that pass all test cases --- src/libs/ReportUtils.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 5d892c73f75a..e65f88032cfd 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -735,17 +735,10 @@ const unavailableTranslation = Localize.translateLocal('workspace.common.unavail */ function getPolicyName(report: OnyxInputOrEntry, returnEmptyIfNotFound = false, policy?: OnyxInputOrEntry): string { const noPolicyFound = returnEmptyIfNotFound ? '' : unavailableTranslation; - - // 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)) { + if (isEmptyObject(report) || (isEmptyObject(allPolicies) && !report?.policyName)) { return noPolicyFound; } - if (isEmptyObject(allPolicies) && !report?.policyName) { - return unavailableTranslation; - } - const finalPolicy = policy ?? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]; const parentReport = getRootParentReport(report);