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

Align default IDs in ReportUtils file #54297

Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ef08327
Minor update to trigger errors
VickyStash Dec 18, 2024
9fc5a78
Remove default ids in ReportUtils file pt1
VickyStash Dec 18, 2024
9d201ac
Remove default ids in ReportUtils file pt2
VickyStash Dec 18, 2024
25f8862
Merge branch 'refs/heads/main' into VickyStash/refactor/ReportUtils-d…
VickyStash Dec 18, 2024
1d956b4
Remove default ids in ReportUtils file pt3
VickyStash Dec 18, 2024
fe2e643
Remove default ids in PolicyUtils file
VickyStash Dec 18, 2024
1c2742d
Remove default ids in ReportActionsUtils.ts file
VickyStash Dec 18, 2024
fd1d6c1
Fix TS errors in OptionsListUtils
VickyStash Dec 19, 2024
a1027fb
Minor update
VickyStash Dec 19, 2024
6bb6081
Merge branch 'refs/heads/main' into VickyStash/refactor/ReportUtils-d…
VickyStash Dec 19, 2024
42a1576
Ignore PureReportActionItem
VickyStash Dec 19, 2024
059eefc
Merge branch 'refs/heads/main' into VickyStash/refactor/ReportUtils-d…
VickyStash Dec 19, 2024
68cae12
Lint fixes
VickyStash Dec 19, 2024
33409ae
Merge branch 'refs/heads/main' into VickyStash/refactor/ReportUtils-d…
VickyStash Dec 19, 2024
5a5a020
Bug fix
VickyStash Dec 19, 2024
414f16c
Merge branch 'refs/heads/main' into VickyStash/refactor/ReportUtils-d…
VickyStash Dec 19, 2024
d99288e
Merge branch 'refs/heads/main' into VickyStash/refactor/ReportUtils-d…
VickyStash Dec 24, 2024
93834a3
Apply reviewer feedback pt1
VickyStash Dec 24, 2024
964115d
Add getReportTransactions function
VickyStash Dec 24, 2024
ddcb3ef
Minor code clean up
VickyStash Dec 24, 2024
43e79df
Merge branch 'refs/heads/main' into VickyStash/refactor/ReportUtils-d…
VickyStash Dec 30, 2024
8f52497
Remove ReportUtils.ts from ignored files
VickyStash Dec 30, 2024
3a7151d
Minor type update
VickyStash Dec 30, 2024
5869543
Remove ReportActionsUtils file from ignored list as well
VickyStash Dec 31, 2024
a7516f5
Add warn logs
VickyStash Jan 2, 2025
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
1 change: 1 addition & 0 deletions .eslintrc.changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
'src/libs/TransactionUtils/index.ts',
'src/pages/home/ReportScreen.tsx',
'src/pages/workspace/WorkspaceInitialPage.tsx',
'src/pages/home/report/PureReportActionItem.tsx',
neil-marcellini marked this conversation as resolved.
Show resolved Hide resolved
],
rules: {
'rulesdir/no-default-id-values': 'off',
Expand Down
2 changes: 1 addition & 1 deletion src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ function getValidOptions(

if (reportPreviewAction) {
const iouReportID = ReportActionUtils.getIOUReportIDFromReportActionPreview(reportPreviewAction);
const iouReportActions = allSortedReportActions[iouReportID] ?? [];
const iouReportActions = iouReportID ? allSortedReportActions[iouReportID] ?? [] : [];
const lastIOUAction = iouReportActions.find((iouAction) => iouAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU);
if (lastIOUAction) {
reportOption.lastIOUCreationDate = lastIOUAction.lastModified;
Expand Down
4 changes: 2 additions & 2 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ const isPolicyEmployee = (policyID: string | undefined, policies: OnyxCollection
/**
* Checks if the current user is an owner (creator) of the policy.
*/
const isPolicyOwner = (policy: OnyxInputOrEntry<Policy>, currentUserAccountID: number): boolean => policy?.ownerAccountID === currentUserAccountID;
const isPolicyOwner = (policy: OnyxInputOrEntry<Policy>, currentUserAccountID: number | undefined): boolean => !!currentUserAccountID && policy?.ownerAccountID === currentUserAccountID;

/**
* Create an object mapping member emails to their accountIDs. Filter for members without errors if includeMemberWithErrors is false, and get the login email from the personalDetail object using the accountID.
Expand Down Expand Up @@ -420,7 +420,7 @@ function isTaxTrackingEnabled(isPolicyExpenseChat: boolean, policy: OnyxEntry<Po
const distanceUnit = getDistanceRateCustomUnit(policy);
const customUnitID = distanceUnit?.customUnitID ?? CONST.DEFAULT_NUMBER_ID;
const isPolicyTaxTrackingEnabled = isPolicyExpenseChat && policy?.tax?.trackingEnabled;
const isTaxEnabledForDistance = isPolicyTaxTrackingEnabled && policy?.customUnits?.[customUnitID]?.attributes?.taxEnabled;
const isTaxEnabledForDistance = isPolicyTaxTrackingEnabled && !!customUnitID && policy?.customUnits?.[customUnitID]?.attributes?.taxEnabled;

return !!(isDistanceRequest ? isTaxEnabledForDistance : isPolicyTaxTrackingEnabled);
}
Expand Down
49 changes: 29 additions & 20 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ function isWhisperActionTargetedToOthers(reportAction: OnyxInputOrEntry<ReportAc
if (!isWhisperAction(reportAction)) {
return false;
}
return !getWhisperedTo(reportAction).includes(currentUserAccountID ?? -1);
return !getWhisperedTo(reportAction).includes(currentUserAccountID ?? CONST.DEFAULT_NUMBER_ID);
}

function isReimbursementQueuedAction(reportAction: OnyxInputOrEntry<ReportAction>): reportAction is ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.REIMBURSEMENT_QUEUED> {
Expand Down Expand Up @@ -347,7 +347,7 @@ function isInviteOrRemovedAction(
/**
* Returns whether the comment is a thread parent message/the first message in a thread
*/
function isThreadParentMessage(reportAction: OnyxEntry<ReportAction>, reportID: string): boolean {
function isThreadParentMessage(reportAction: OnyxEntry<ReportAction>, reportID: string | undefined): boolean {
const {childType, childVisibleActionCount = 0, childReportID} = reportAction ?? {};
return childType === CONST.REPORT.TYPE.CHAT && (childVisibleActionCount > 0 || String(childReportID) === reportID);
}
Expand Down Expand Up @@ -771,7 +771,11 @@ function replaceBaseURLInPolicyChangeLogAction(reportAction: ReportAction): Repo
return updatedReportAction;
}

function getLastVisibleAction(reportID: string, canUserPerformWriteAction?: boolean, actionsToMerge: Record<string, NullishDeep<ReportAction> | null> = {}): OnyxEntry<ReportAction> {
function getLastVisibleAction(
reportID: string | undefined,
canUserPerformWriteAction?: boolean,
actionsToMerge: Record<string, NullishDeep<ReportAction> | null> = {},
): OnyxEntry<ReportAction> {
let reportActions: Array<ReportAction | null | undefined> = [];
if (!isEmpty(actionsToMerge)) {
reportActions = Object.values(fastMerge(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? {}, actionsToMerge ?? {}, true)) as Array<
Expand Down Expand Up @@ -801,7 +805,7 @@ function formatLastMessageText(lastMessageText: string) {
}

function getLastVisibleMessage(
reportID: string,
reportID: string | undefined,
canUserPerformWriteAction?: boolean,
actionsToMerge: Record<string, NullishDeep<ReportAction> | null> = {},
reportAction: OnyxInputOrEntry<ReportAction> | undefined = undefined,
Expand Down Expand Up @@ -893,12 +897,12 @@ function getLastClosedReportAction(reportActions: OnyxEntry<ReportActions>): Ony
* 4. We will get the second last action from filtered actions because the last
* action is always the created action
*/
function getFirstVisibleReportActionID(sortedReportActions: ReportAction[] = [], isOffline = false): string {
function getFirstVisibleReportActionID(sortedReportActions: ReportAction[] = [], isOffline = false): string | undefined {
if (!Array.isArray(sortedReportActions)) {
return '';
}
const sortedFilterReportActions = sortedReportActions.filter((action) => !isDeletedAction(action) || (action?.childVisibleActionCount ?? 0) > 0 || isOffline);
return sortedFilterReportActions.length > 1 ? sortedFilterReportActions.at(sortedFilterReportActions.length - 2)?.reportActionID ?? '-1' : '';
return sortedFilterReportActions.length > 1 ? sortedFilterReportActions.at(sortedFilterReportActions.length - 2)?.reportActionID : undefined;
}

/**
Expand Down Expand Up @@ -991,8 +995,8 @@ function getReportPreviewAction(chatReportID: string | undefined, iouReportID: s
/**
* Get the iouReportID for a given report action.
*/
function getIOUReportIDFromReportActionPreview(reportAction: OnyxEntry<ReportAction>): string {
return isActionOfType(reportAction, CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW) ? getOriginalMessage(reportAction)?.linkedReportID ?? '-1' : '-1';
function getIOUReportIDFromReportActionPreview(reportAction: OnyxEntry<ReportAction>): string | undefined {
return isActionOfType(reportAction, CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW) ? getOriginalMessage(reportAction)?.linkedReportID : undefined;
}

/**
Expand Down Expand Up @@ -1057,7 +1061,11 @@ const iouRequestTypes = new Set<ValueOf<typeof CONST.IOU.REPORT_ACTION_TYPE>>([
* Gets the reportID for the transaction thread associated with a report by iterating over the reportActions and identifying the IOU report actions.
* Returns a reportID if there is exactly one transaction thread for the report, and null otherwise.
*/
function getOneTransactionThreadReportID(reportID: string, reportActions: OnyxEntry<ReportActions> | ReportAction[], isOffline: boolean | undefined = undefined): string | undefined {
function getOneTransactionThreadReportID(
reportID: string | undefined,
reportActions: OnyxEntry<ReportActions> | ReportAction[],
isOffline: boolean | undefined = undefined,
): string | undefined {
// If the report is not an IOU, Expense report, or Invoice, it shouldn't be treated as one-transaction report.
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
if (report?.type !== CONST.REPORT.TYPE.IOU && report?.type !== CONST.REPORT.TYPE.EXPENSE && report?.type !== CONST.REPORT.TYPE.INVOICE) {
Expand Down Expand Up @@ -1126,7 +1134,7 @@ function doesReportHaveVisibleActions(reportID: string, canUserPerformWriteActio
return visibleReportActionsWithoutTaskSystemMessage.length > 0;
}

function getAllReportActions(reportID: string): ReportActions {
function getAllReportActions(reportID: string | undefined): ReportActions {
return allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? {};
}

Expand Down Expand Up @@ -1500,7 +1508,7 @@ function isReportActionUnread(reportAction: OnyxEntry<ReportAction>, lastReadTim
*/
function isCurrentActionUnread(report: OnyxEntry<Report>, reportAction: ReportAction): boolean {
const lastReadTime = report?.lastReadTime ?? '';
const sortedReportActions = getSortedReportActions(Object.values(getAllReportActions(report?.reportID ?? '-1')));
const sortedReportActions = getSortedReportActions(Object.values(getAllReportActions(report?.reportID)));
const currentActionIndex = sortedReportActions.findIndex((action) => action.reportActionID === reportAction.reportActionID);
if (currentActionIndex === -1) {
return false;
Expand Down Expand Up @@ -1560,7 +1568,8 @@ function getDismissedViolationMessageText(originalMessage: ReportAction<typeof C
* Check if the linked transaction is on hold
*/
function isLinkedTransactionHeld(reportActionID: string, reportID: string): boolean {
return TransactionUtils.isOnHoldByTransactionID(getLinkedTransactionID(reportActionID, reportID) ?? '-1');
const linkedTransactionID = getLinkedTransactionID(reportActionID, reportID);
return linkedTransactionID ? TransactionUtils.isOnHoldByTransactionID(linkedTransactionID) : false;
}

function getMentionedAccountIDsFromAction(reportAction: OnyxInputOrEntry<ReportAction>) {
Expand All @@ -1577,7 +1586,7 @@ function didMessageMentionCurrentUser(reportAction: OnyxInputOrEntry<ReportActio
const accountIDsFromMessage = getMentionedAccountIDsFromAction(reportAction);
const message = getReportActionMessage(reportAction)?.html ?? '';
const emailsFromMessage = getMentionedEmailsFromMessage(message);
return accountIDsFromMessage.includes(currentUserAccountID ?? -1) || emailsFromMessage.includes(currentEmail) || message.includes('<mention-here>');
return accountIDsFromMessage.includes(currentUserAccountID ?? CONST.DEFAULT_NUMBER_ID) || emailsFromMessage.includes(currentEmail) || message.includes('<mention-here>');
}

/**
Expand All @@ -1592,9 +1601,9 @@ function wasActionTakenByCurrentUser(reportAction: OnyxInputOrEntry<ReportAction
*/
function getIOUActionForReportID(reportID: string, transactionID: string): OnyxEntry<ReportAction> {
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
const reportActions = getAllReportActions(report?.reportID ?? '');
const reportActions = getAllReportActions(report?.reportID);
const action = Object.values(reportActions ?? {})?.find((reportAction) => {
const IOUTransactionID = isMoneyRequestAction(reportAction) ? getOriginalMessage(reportAction)?.IOUTransactionID : -1;
const IOUTransactionID = isMoneyRequestAction(reportAction) ? getOriginalMessage(reportAction)?.IOUTransactionID : undefined;
return IOUTransactionID === transactionID;
});
return action;
Expand Down Expand Up @@ -1641,7 +1650,7 @@ function getExportIntegrationActionFragments(reportAction: OnyxEntry<ReportActio
const {label, markedManually} = originalMessage;
const reimbursableUrls = originalMessage.reimbursableUrls ?? [];
const nonReimbursableUrls = originalMessage.nonReimbursableUrls ?? [];
const reportID = reportAction?.reportID ?? '';
const reportID = reportAction?.reportID;
const wasExportedAfterBase62 = (reportAction?.created ?? '') > '2022-11-14';
const base62ReportID = getBase62ReportID(Number(reportID));

Expand Down Expand Up @@ -1775,7 +1784,7 @@ function getRenamedAction(reportAction: OnyxEntry<ReportAction<typeof CONST.REPO

function getRemovedFromApprovalChainMessage(reportAction: OnyxEntry<ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.REMOVED_FROM_APPROVAL_CHAIN>>) {
const originalMessage = getOriginalMessage(reportAction);
const submittersNames = PersonalDetailsUtils.getPersonalDetailsByIDs(originalMessage?.submittersAccountIDs ?? [], currentUserAccountID ?? -1).map(
const submittersNames = PersonalDetailsUtils.getPersonalDetailsByIDs(originalMessage?.submittersAccountIDs ?? [], currentUserAccountID ?? CONST.DEFAULT_NUMBER_ID).map(
({displayName, login}) => displayName ?? login ?? 'Unknown Submitter',
);
return Localize.translateLocal('workspaceActions.removedFromApprovalWorkflow', {submittersNames, count: submittersNames.length});
Expand All @@ -1802,9 +1811,9 @@ function getCardIssuedMessage(reportAction: OnyxEntry<ReportAction>, shouldRende
? getOriginalMessage(reportAction)
: undefined;

const assigneeAccountID = cardIssuedActionOriginalMessage?.assigneeAccountID ?? -1;
const cardID = cardIssuedActionOriginalMessage?.cardID ?? -1;
const assigneeDetails = PersonalDetailsUtils.getPersonalDetailsByIDs([assigneeAccountID], currentUserAccountID ?? -1).at(0);
const assigneeAccountID = cardIssuedActionOriginalMessage?.assigneeAccountID ?? CONST.DEFAULT_NUMBER_ID;
const cardID = cardIssuedActionOriginalMessage?.cardID ?? CONST.DEFAULT_NUMBER_ID;
const assigneeDetails = PersonalDetailsUtils.getPersonalDetailsByIDs([assigneeAccountID], currentUserAccountID ?? CONST.DEFAULT_NUMBER_ID).at(0);
const isPolicyAdmin = PolicyUtils.isPolicyAdmin(PolicyUtils.getPolicy(policyID));
const assignee = shouldRenderHTML ? `<mention-user accountID="${assigneeAccountID}"/>` : assigneeDetails?.firstName ?? assigneeDetails?.login ?? '';
const navigateRoute = isPolicyAdmin ? ROUTES.EXPENSIFY_CARD_DETAILS.getRoute(policyID, String(cardID)) : ROUTES.SETTINGS_DOMAINCARD_DETAIL.getRoute(String(cardID));
Expand Down
Loading
Loading