Skip to content

Commit

Permalink
Merge pull request #49325 from Expensify/srikar-reportUtilsLint
Browse files Browse the repository at this point in the history
Fix ReportUtils Lint
  • Loading branch information
robertjchen authored Sep 20, 2024
2 parents b0ce206 + d790940 commit 7af7a3f
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,11 @@ type ParsingDetails = {
policyID?: string;
};

type Thread = {
parentReportID: string;
parentReportActionID: string;
} & Report;

let currentUserEmail: string | undefined;
let currentUserPrivateDomain: string | undefined;
let currentUserAccountID: number | undefined;
Expand Down Expand Up @@ -1112,14 +1117,14 @@ function isWorkspaceTaskReport(report: OnyxEntry<Report>): boolean {
/**
* Returns true if report has a parent
*/
function isThread(report: OnyxInputOrEntry<Report>): boolean {
function isThread(report: OnyxInputOrEntry<Report>): report is Thread {
return !!(report?.parentReportID && report?.parentReportActionID);
}

/**
* Returns true if report is of type chat and has a parent and is therefore a Thread.
*/
function isChatThread(report: OnyxInputOrEntry<Report>): boolean {
function isChatThread(report: OnyxInputOrEntry<Report>): report is Thread {
return isThread(report) && report?.type === CONST.REPORT.TYPE.CHAT;
}

Expand Down Expand Up @@ -1535,9 +1540,9 @@ function isChildReport(report: OnyxEntry<Report>): boolean {
* An Expense Request is a thread where the parent report is an Expense Report and
* the parentReportAction is a transaction.
*/
function isExpenseRequest(report: OnyxInputOrEntry<Report>): boolean {
function isExpenseRequest(report: OnyxInputOrEntry<Report>): report is Thread {
if (isThread(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReportAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`]?.[report.parentReportActionID];
const parentReport = ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`];
return isExpenseReport(parentReport) && !isEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction);
}
Expand All @@ -1550,7 +1555,7 @@ function isExpenseRequest(report: OnyxInputOrEntry<Report>): boolean {
*/
function isIOURequest(report: OnyxInputOrEntry<Report>): boolean {
if (isThread(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReportAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`]?.[report.parentReportActionID];
const parentReport = ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`];
return isIOUReport(parentReport) && !isEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction);
}
Expand All @@ -1563,7 +1568,7 @@ function isIOURequest(report: OnyxInputOrEntry<Report>): boolean {
*/
function isTrackExpenseReport(report: OnyxInputOrEntry<Report>): boolean {
if (isThread(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReportAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`]?.[report.parentReportActionID];
return !isEmptyObject(parentReportAction) && ReportActionsUtils.isTrackExpenseAction(parentReportAction);
}
return false;
Expand Down Expand Up @@ -2212,7 +2217,7 @@ function getIcons(
return [fallbackIcon];
}
if (isExpenseRequest(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReportAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`]?.[report.parentReportActionID];
const workspaceIcon = getWorkspaceIcon(report, policy);
const memberIcon = {
source: personalDetails?.[parentReportAction?.actorAccountID ?? -1]?.avatar ?? FallbackAvatar,
Expand All @@ -2225,7 +2230,7 @@ function getIcons(
return [memberIcon, workspaceIcon];
}
if (isChatThread(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReportAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`]?.[report.parentReportActionID];

const actorAccountID = getReportActionActorAccountID(parentReportAction, report);
const actorDisplayName = PersonalDetailsUtils.getDisplayNameOrDefault(allPersonalDetails?.[actorAccountID ?? -1], '', false);
Expand Down Expand Up @@ -3106,7 +3111,9 @@ function canHoldUnholdReportAction(reportAction: OnyxInputOrEntry<ReportAction>)
const transactionID = moneyRequestReport ? ReportActionsUtils.getOriginalMessage(reportAction)?.IOUTransactionID : 0;
const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? ({} as Transaction);

const parentReportAction = ReportActionsUtils.getParentReportAction(moneyRequestReport);
const parentReportAction = isThread(moneyRequestReport)
? allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${moneyRequestReport.parentReportID}`]?.[moneyRequestReport.parentReportActionID]
: undefined;

const isRequestIOU = isIOUReport(moneyRequestReport);
const isHoldActionCreator = isHoldCreator(transaction, reportAction.childReportID ?? '-1');
Expand Down Expand Up @@ -3713,7 +3720,12 @@ function getReportName(
}

let formattedName: string | undefined;
const parentReportAction = parentReportActionParam ?? ReportActionsUtils.getParentReportAction(report);
let parentReportAction: OnyxEntry<ReportAction>;
if (parentReportActionParam) {
parentReportAction = parentReportActionParam;
} else {
parentReportAction = isThread(report) ? allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`]?.[report.parentReportActionID] : undefined;
}
const parentReportActionMessage = ReportActionsUtils.getReportActionMessage(parentReportAction);

if (parentReportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.SUBMITTED) {
Expand Down Expand Up @@ -4249,6 +4261,7 @@ function buildOptimisticTaskCommentReportAction(

// These parameters are not saved on the reportAction, but are used to display the task in the UI
// Added when we fetch the reportActions on a report
// eslint-disable-next-line
reportAction.reportAction.originalMessage = {
html: ReportActionsUtils.getReportActionHtml(reportAction.reportAction),
taskReportID: ReportActionsUtils.getReportActionMessage(reportAction.reportAction)?.taskReportID,
Expand Down Expand Up @@ -5995,7 +6008,7 @@ function shouldReportBeInOptionList({
// This can also happen for anyone accessing a public room or archived room for which they don't have access to the underlying policy.
// Optionally exclude reports that do not belong to currently active workspace

const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReportAction = isThread(report) ? allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`]?.[report.parentReportActionID] : undefined;

if (
!report?.reportID ||
Expand Down

0 comments on commit 7af7a3f

Please sign in to comment.