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 {NotificationPreference, Participants, Participant as ReportParticip
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 @@ type GetPolicyNameParams = {
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.
Resolved
};

type GetReportNameParams = {
Expand All @@ -919,8 +918,7 @@ type GetReportNameParams = {
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 Down Expand Up @@ -1203,8 +1201,7 @@ function getChatType(report: OnyxInputOrEntry<Report> | Participant): ValueOf<ty
/**
* 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 @@ function isDraftReport(reportID: string | undefined): boolean {
/**
* @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 @@ function isSearchReportArray(object: SearchReport[] | OnyxCollection<Report>): o
* @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 @@ function getRootParentReport({
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 @@ function isChatReport(report: OnyxEntry<Report>): boolean {
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 @@ function isReportIDApproved(reportID: string | undefined) {
/**
* 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 @@ function hasParticipantInArray(report: OnyxEntry<Report>, memberAccountIDs: numb
/**
* 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 @@ function isClosedExpenseReportWithNoExpenses(report: OnyxEntry<Report>, transact
/**
* 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 @@ function isArchivedNonExpenseReportWithID(report?: OnyxInputOrEntry<Report>, isR
/**
* 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 @@ function isMoneyRequest(reportOrID: OnyxEntry<Report> | string): boolean {
/**
* 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 @@ function getReimbursementQueuedActionMessage({
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 getReimbursementQueuedActionMessage({
*/
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 @@ function hasNonReimbursableTransactions(iouReportID: string | undefined, reports
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 getTransactionCommentObject(transaction: OnyxEntry<Transaction>): Comme
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 @@ function canEditFieldOfMoneyRequest(
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 @@ function canEditFieldOfMoneyRequest(
}

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 @@ function getTransactionReportName({
}: {
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 @@ function getReportActionMessage({
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 @@ function getReportName(
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 @@ function getAllPolicyReports(policyID: string): Array<OnyxEntry<Report>> {
/**
* 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 shouldCreateNewMoneyRequestReport(
}

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 @@ function getGroupChatDraft() {
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