Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
82 changes: 29 additions & 53 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
import type {Message, OldDotReportAction, ReportActions} from '@src/types/onyx/ReportAction';
import type {PendingChatMember} from '@src/types/onyx/ReportMetadata';
import type {OnyxData} from '@src/types/onyx/Request';
import type {SearchReport, SearchTransaction} from '@src/types/onyx/SearchResults';
import type {SearchTransaction} from '@src/types/onyx/SearchResults';
import type {Comment, TransactionChanges, WaypointCollection} from '@src/types/onyx/Transaction';
import type {FileObject} from '@src/types/utils/Attachment';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
Expand Down Expand Up @@ -908,8 +908,7 @@
returnEmptyIfNotFound?: boolean;
policy?: OnyxInputOrEntry<Policy>;
policies?: Policy[];
// eslint-disable-next-line @typescript-eslint/no-deprecated
reports?: SearchReport[];
reports?: Report[];
Comment thread
neil-marcellini marked this conversation as resolved.
};

type GetReportNameParams = {
Expand All @@ -919,8 +918,7 @@
personalDetails?: Partial<PersonalDetailsList>;
invoiceReceiverPolicy?: OnyxEntry<Policy>;
transactions?: SearchTransaction[];
// eslint-disable-next-line @typescript-eslint/no-deprecated
reports?: SearchReport[];
reports?: Report[];
policies?: Policy[];
isReportArchived?: boolean;
};
Expand All @@ -945,7 +943,7 @@
const parsedReportActionMessageCache: Record<string, string> = {};

let conciergeReportID: OnyxEntry<string>;
Onyx.connect({

Check warning on line 946 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.CONCIERGE_REPORT_ID,
callback: (value) => {
conciergeReportID = value;
Expand All @@ -953,7 +951,7 @@
});

const defaultAvatarBuildingIconTestID = 'SvgDefaultAvatarBuilding Icon';
Onyx.connect({

Check warning on line 954 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
// When signed out, val is undefined
Expand All @@ -971,7 +969,7 @@
let allPersonalDetails: OnyxEntry<PersonalDetailsList>;
let allPersonalDetailLogins: string[];
let currentUserPersonalDetails: OnyxEntry<PersonalDetails>;
Onyx.connect({

Check warning on line 972 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
if (currentUserAccountID) {
Expand All @@ -983,14 +981,14 @@
});

let allReportsDraft: OnyxCollection<Report>;
Onyx.connect({

Check warning on line 984 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_DRAFT,
waitForCollectionCallback: true,
callback: (value) => (allReportsDraft = value),
});

let allPolicies: OnyxCollection<Policy>;
Onyx.connect({

Check warning on line 991 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => (allPolicies = value),
Expand All @@ -998,7 +996,7 @@

let allReports: OnyxCollection<Report>;
let reportsByPolicyID: ReportByPolicyMap;
Onyx.connect({

Check warning on line 999 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand Down Expand Up @@ -1039,14 +1037,14 @@
});

let allBetas: OnyxEntry<Beta[]>;
Onyx.connect({

Check warning on line 1040 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.BETAS,
callback: (value) => (allBetas = value),
});

let allTransactions: OnyxCollection<Transaction> = {};
let reportsTransactions: Record<string, Transaction[]> = {};
Onyx.connect({

Check warning on line 1047 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -1072,7 +1070,7 @@
});

let allReportActions: OnyxCollection<ReportActions>;
Onyx.connect({

Check warning on line 1073 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Expand All @@ -1085,7 +1083,7 @@

let allReportMetadata: OnyxCollection<ReportMetadata>;
const allReportMetadataKeyValue: Record<string, ReportMetadata> = {};
Onyx.connect({

Check warning on line 1086 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_METADATA,
waitForCollectionCallback: true,
callback: (value) => {
Expand Down Expand Up @@ -1203,8 +1201,7 @@
/**
* Get the report or draft report given a reportID
*/
// eslint-disable-next-line @typescript-eslint/no-deprecated
function getReportOrDraftReport(reportID: string | undefined, searchReports?: SearchReport[], fallbackReport?: Report): OnyxEntry<Report> | SearchReport {
function getReportOrDraftReport(reportID: string | undefined, searchReports?: Report[], fallbackReport?: Report): OnyxEntry<Report> {
const searchReport = searchReports?.find((report) => report.reportID === reportID);
const onyxReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
return searchReport ?? onyxReport ?? allReportsDraft?.[`${ONYXKEYS.COLLECTION.REPORT_DRAFT}${reportID}`] ?? fallbackReport;
Expand Down Expand Up @@ -1238,8 +1235,7 @@
/**
* @private
*/
// eslint-disable-next-line @typescript-eslint/no-deprecated
function isSearchReportArray(object: SearchReport[] | OnyxCollection<Report>): object is SearchReport[] {
function isSearchReportArray(object: Report[] | OnyxCollection<Report>): object is Report[] {
if (!Array.isArray(object)) {
return false;
}
Expand All @@ -1251,8 +1247,7 @@
* @private
* Returns the report
*/
// eslint-disable-next-line @typescript-eslint/no-deprecated
function getReport(reportID: string, reports: SearchReport[] | OnyxCollection<Report>): OnyxEntry<Report> | SearchReport {
function getReport(reportID: string, reports: Report[] | OnyxCollection<Report>): OnyxEntry<Report> | Report {
if (isSearchReportArray(reports)) {
reports?.find((report) => report.reportID === reportID);
} else {
Expand Down Expand Up @@ -1281,8 +1276,8 @@
visitedReportIDs = new Set<string>(),
}: {
report: OnyxEntry<Report>;
// eslint-disable-next-line @typescript-eslint/no-deprecated
reports?: SearchReport[];

reports?: Report[];
visitedReportIDs?: Set<string>;
}): OnyxEntry<Report> {
if (!report) {
Expand Down Expand Up @@ -1370,8 +1365,7 @@
return report?.type === CONST.REPORT.TYPE.CHAT;
}

// eslint-disable-next-line @typescript-eslint/no-deprecated
function isInvoiceReport(reportOrID: OnyxInputOrEntry<Report> | SearchReport | string): boolean {
function isInvoiceReport(reportOrID: OnyxInputOrEntry<Report> | string): boolean {
const report = typeof reportOrID === 'string' ? (getReport(reportOrID, allReports) ?? null) : reportOrID;
return report?.type === CONST.REPORT.TYPE.INVOICE;
}
Expand Down Expand Up @@ -1405,17 +1399,15 @@
/**
* Checks if a report is an Expense report.
*/
// eslint-disable-next-line @typescript-eslint/no-deprecated
function isExpenseReport(reportOrID: OnyxInputOrEntry<Report> | SearchReport | string): boolean {
function isExpenseReport(reportOrID: OnyxInputOrEntry<Report> | string): boolean {
const report = typeof reportOrID === 'string' ? (getReport(reportOrID, allReports) ?? null) : reportOrID;
return report?.type === CONST.REPORT.TYPE.EXPENSE;
}

/**
* Checks if a report is an IOU report using report or reportID
*/
// eslint-disable-next-line @typescript-eslint/no-deprecated
function isIOUReport(reportOrID: OnyxInputOrEntry<Report> | SearchReport | string): boolean {
function isIOUReport(reportOrID: OnyxInputOrEntry<Report> | string): boolean {
const report = typeof reportOrID === 'string' ? (getReport(reportOrID, allReports) ?? null) : reportOrID;
return report?.type === CONST.REPORT.TYPE.IOU;
}
Expand Down Expand Up @@ -1524,8 +1516,7 @@
/**
* Whether the Money Request report is settled
*/
// eslint-disable-next-line @typescript-eslint/no-deprecated
function isSettled(reportOrID: OnyxInputOrEntry<Report> | SearchReport | string | undefined, reports?: SearchReport[] | OnyxCollection<Report>): boolean {
function isSettled(reportOrID: OnyxInputOrEntry<Report> | string | undefined, reports?: Report[] | OnyxCollection<Report>): boolean {
if (!reportOrID) {
return false;
}
Expand Down Expand Up @@ -2171,8 +2162,7 @@
/**
* Whether the provided report is an archived room
*/
// eslint-disable-next-line @typescript-eslint/no-deprecated
function isArchivedNonExpenseReport(report: OnyxInputOrEntry<Report> | SearchReport, isReportArchived = false): boolean {
function isArchivedNonExpenseReport(report: OnyxInputOrEntry<Report>, isReportArchived = false): boolean {
return isReportArchived && !(isExpenseReport(report) || isExpenseRequest(report));
}

Expand All @@ -2197,8 +2187,7 @@
/**
* Whether the provided report is a closed report
*/
// eslint-disable-next-line @typescript-eslint/no-deprecated
function isClosedReport(report: OnyxInputOrEntry<Report> | SearchReport): boolean {
function isClosedReport(report: OnyxInputOrEntry<Report>): boolean {
return report?.statusNum === CONST.REPORT.STATUS_NUM.CLOSED;
}

Expand Down Expand Up @@ -2383,8 +2372,7 @@
/**
* Checks if a report is an IOU or expense report.
*/
// eslint-disable-next-line @typescript-eslint/no-deprecated
function isMoneyRequestReport(reportOrID: OnyxInputOrEntry<Report> | SearchReport | string, reports?: SearchReport[] | OnyxCollection<Report>): boolean {
function isMoneyRequestReport(reportOrID: OnyxInputOrEntry<Report> | string, reports?: Report[] | OnyxCollection<Report>): boolean {
const report = typeof reportOrID === 'string' ? (getReport(reportOrID, reports ?? allReports) ?? null) : reportOrID;
return isIOUReport(report) || isExpenseReport(report);
}
Expand Down Expand Up @@ -3742,11 +3730,9 @@
personalDetails,
}: {
reportAction: OnyxEntry<ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.REIMBURSEMENT_QUEUED>>;
// eslint-disable-next-line @typescript-eslint/no-deprecated
reportOrID: OnyxEntry<Report> | string | SearchReport;
reportOrID: OnyxEntry<Report> | string;
shouldUseShortDisplayName?: boolean;
// eslint-disable-next-line @typescript-eslint/no-deprecated
reports?: SearchReport[];
reports?: Report[];
personalDetails?: Partial<PersonalDetailsList>;
}): string {
const report = typeof reportOrID === 'string' ? getReport(reportOrID, reports ?? allReports) : reportOrID;
Expand All @@ -3767,8 +3753,7 @@
*/
function getReimbursementDeQueuedOrCanceledActionMessage(
reportAction: OnyxEntry<ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.REIMBURSEMENT_DEQUEUED | typeof CONST.REPORT.ACTIONS.TYPE.REIMBURSEMENT_ACH_CANCELED>>,
// eslint-disable-next-line @typescript-eslint/no-deprecated
reportOrID: OnyxEntry<Report> | string | SearchReport,
reportOrID: OnyxEntry<Report> | string,
): string {
const report = typeof reportOrID === 'string' ? getReport(reportOrID, allReports) : reportOrID;
const originalMessage = getOriginalMessage(reportAction);
Expand Down Expand Up @@ -4065,8 +4050,7 @@
return transactions.filter((transaction) => transaction.reimbursable === false).length > 0;
}

// eslint-disable-next-line @typescript-eslint/no-deprecated
function getMoneyRequestSpendBreakdown(report: OnyxInputOrEntry<Report>, searchReports?: SearchReport[]): SpendBreakdown {
function getMoneyRequestSpendBreakdown(report: OnyxInputOrEntry<Report>, searchReports?: Report[]): SpendBreakdown {
const reports = searchReports ?? allReports;
let moneyRequestReport: OnyxEntry<Report>;
if (report && (isMoneyRequestReport(report, searchReports) || isInvoiceReport(report))) {
Expand Down Expand Up @@ -4366,8 +4350,7 @@
function canEditMoneyRequest(
reportAction: OnyxInputOrEntry<ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU>>,
isChatReportArchived = false,
// eslint-disable-next-line @typescript-eslint/no-deprecated
report?: OnyxInputOrEntry<Report> | SearchReport,
report?: OnyxInputOrEntry<Report>,
policy?: OnyxEntry<Policy>,
linkedTransaction?: OnyxEntry<Transaction> | SearchTransaction,
): boolean {
Expand Down Expand Up @@ -4512,8 +4495,7 @@
isChatReportArchived = false,
outstandingReportsByPolicyID?: OutstandingReportsByPolicyIDDerivedValue,
linkedTransaction?: OnyxEntry<Transaction> | SearchTransaction,
// eslint-disable-next-line @typescript-eslint/no-deprecated
report?: OnyxInputOrEntry<Report> | SearchReport,
report?: OnyxInputOrEntry<Report>,
policy?: OnyxEntry<Policy>,
): boolean {
// A list of fields that cannot be edited by anyone, once an expense has been settled
Expand Down Expand Up @@ -4569,7 +4551,7 @@
}

if ((fieldToEdit === CONST.EDIT_REQUEST_FIELD.AMOUNT || fieldToEdit === CONST.EDIT_REQUEST_FIELD.CURRENCY) && isDistanceRequest(transaction)) {
return isAdmin || isManager;
return isAdmin || isManager || isRequestor;
}

if (
Expand Down Expand Up @@ -4882,8 +4864,7 @@
}: {
reportAction: OnyxEntry<ReportAction | OptimisticIOUReportAction>;
transactions?: SearchTransaction[];
// eslint-disable-next-line @typescript-eslint/no-deprecated
reports?: SearchReport[];
reports?: Report[];
}): string {
if (isReversedTransaction(reportAction)) {
// eslint-disable-next-line @typescript-eslint/no-deprecated
Expand Down Expand Up @@ -5387,8 +5368,7 @@
reportAction: OnyxEntry<ReportAction>;
reportID?: string;
childReportID?: string;
// eslint-disable-next-line @typescript-eslint/no-deprecated
reports?: SearchReport[];
reports?: Report[];
personalDetails?: Partial<PersonalDetailsList>;
}) {
if (isEmptyObject(reportAction)) {
Expand Down Expand Up @@ -5518,8 +5498,7 @@
reportAttributes?: ReportAttributesDerivedValue['reports'],
transactions?: SearchTransaction[],
isReportArchived?: boolean,
// eslint-disable-next-line @typescript-eslint/no-deprecated
reports?: SearchReport[],
reports?: Report[],
policies?: Policy[],
): string {
// Check if we can use report name in derived values - only when we have report but no other params
Expand Down Expand Up @@ -9238,14 +9217,12 @@
/**
* Returns true if Chronos is one of the chat participants (1:1)
*/
// eslint-disable-next-line @typescript-eslint/no-deprecated
function chatIncludesChronos(report: OnyxInputOrEntry<Report> | SearchReport): boolean {
function chatIncludesChronos(report: OnyxInputOrEntry<Report>): boolean {
const participantAccountIDs = Object.keys(report?.participants ?? {}).map(Number);
return participantAccountIDs.includes(CONST.ACCOUNT_ID.CHRONOS);
}

// eslint-disable-next-line @typescript-eslint/no-deprecated
function chatIncludesChronosWithID(reportOrID?: string | SearchReport): boolean {
function chatIncludesChronosWithID(reportOrID?: string | Report): boolean {
if (!reportOrID) {
return false;
}
Expand Down Expand Up @@ -10577,7 +10554,7 @@
}

function getTripIDFromTransactionParentReportID(transactionParentReportID: string | undefined): string | undefined {
return (getReportOrDraftReport(transactionParentReportID) as OnyxEntry<Report>)?.tripData?.tripID;
return getReportOrDraftReport(transactionParentReportID)?.tripData?.tripID;
}

/**
Expand Down Expand Up @@ -12065,8 +12042,7 @@
return newGroupChatDraft;
}

// eslint-disable-next-line @typescript-eslint/no-deprecated
function getChatListItemReportName(action: ReportAction & {reportName?: string}, report: SearchReport | undefined): string {
function getChatListItemReportName(action: ReportAction & {reportName?: string}, report: Report | undefined): string {
if (report && isInvoiceReport(report)) {
const properInvoiceReport = report;
properInvoiceReport.chatReportID = report.parentReportID;
Expand Down
3 changes: 1 addition & 2 deletions src/pages/iou/SplitExpensePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import {getChildTransactions, isManagedCardTransaction, isPerDiemRequest} from '
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
import type {Report} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

type SplitExpensePageProps = PlatformStackScreenProps<SplitExpenseParamList, typeof SCREENS.MONEY_REQUEST.SPLIT_EXPENSE>;
Expand Down Expand Up @@ -239,7 +238,7 @@ function SplitExpensePage({route}: SplitExpensePageProps) {
const items: SplitListItemType[] = (draftTransaction?.comment?.splitExpenses ?? []).map((item): SplitListItemType => {
const previewHeaderText: TranslationPathOrText[] = [showCashOrCard];
const currentTransaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${item?.transactionID}`];
const currentReport = getReportOrDraftReport(currentTransaction?.reportID) as Report;
const currentReport = getReportOrDraftReport(currentTransaction?.reportID);
const isApproved = isReportApproved({report: currentReport});
const isSettled = isSettledReportUtils(currentReport?.reportID);
const isCancelled = currentReport && currentReport?.isCancelledIOU;
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/canEditFieldOfMoneyRequestTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ describe('canEditFieldOfMoneyRequest', () => {
},
};

const moneyRequestTransaction = {...createRandomTransaction(Number(IOUTransactionID)), reportID: IOUReportID, transactionID: IOUTransactionID, amount: EXPENSE_AMOUNT};
const moneyRequestTransaction = {
...createRandomTransaction(Number(IOUTransactionID)),
reportID: IOUReportID,
managedCard: false,
transactionID: IOUTransactionID,
amount: EXPENSE_AMOUNT,
};

const expenseReport = {
...createExpenseReport(Number(IOUReportID)),
Expand All @@ -196,6 +202,20 @@ describe('canEditFieldOfMoneyRequest', () => {
return waitForBatchedUpdates();
});

it('should return true for submitter of a distance request for amount and currency fields', async () => {
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${IOUReportID}`, expenseReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${moneyRequestTransaction.transactionID}`, {iouRequestType: CONST.IOU.REQUEST_TYPE.DISTANCE});
await waitForBatchedUpdates();

// If it is the submitter of a distance request
const canEditReportFieldAmount = canEditFieldOfMoneyRequest(reportAction, CONST.EDIT_REQUEST_FIELD.AMOUNT, undefined, undefined);
const canEditReportFieldCurrency = canEditFieldOfMoneyRequest(reportAction, CONST.EDIT_REQUEST_FIELD.CURRENCY, undefined, undefined);

// Then we should allow editing amount and currency fields.
expect(canEditReportFieldAmount).toBe(true);
expect(canEditReportFieldCurrency).toBe(true);
});

it('should return true for submitter when there are multiple outstanding reports', async () => {
// Given that there are multiple outstanding expense reports in the same policy
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${IOUReportID}`, expenseReport);
Expand Down
Loading