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
27 changes: 26 additions & 1 deletion src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
import type {Comment, TransactionChanges, WaypointCollection} from '@src/types/onyx/Transaction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type IconAsset from '@src/types/utils/IconAsset';
import {createDraftTransaction, getIOUReportActionToApproveOrPay, setMoneyRequestParticipants, unholdRequest} from './actions/IOU';
import {canApproveIOU, canIOUBePaid, canSubmitReport, createDraftTransaction, getIOUReportActionToApproveOrPay, setMoneyRequestParticipants, unholdRequest} from './actions/IOU';
import {createDraftWorkspace} from './actions/Policy/Policy';
import {hasCreditBankAccount} from './actions/ReimbursementAccount/store';
import {handleReportChanged} from './actions/Report';
Expand Down Expand Up @@ -908,7 +908,7 @@
const parsedReportActionMessageCache: Record<string, string> = {};

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

Check warning on line 911 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 @@ -916,7 +916,7 @@
});

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

Check warning on line 919 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 @@ -934,7 +934,7 @@
let allPersonalDetails: OnyxEntry<PersonalDetailsList>;
let allPersonalDetailLogins: string[];
let currentUserPersonalDetails: OnyxEntry<PersonalDetails>;
Onyx.connect({

Check warning on line 937 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 @@ -946,14 +946,14 @@
});

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

Check warning on line 949 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 956 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 @@ -961,7 +961,7 @@

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

Check warning on line 964 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 @@ -1002,14 +1002,14 @@
});

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

Check warning on line 1005 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 1012 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 @@ -1035,7 +1035,7 @@
});

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

Check warning on line 1038 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 @@ -1048,7 +1048,7 @@

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

Check warning on line 1051 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 @@ -2547,6 +2547,30 @@
return isManager && isExpenseReport(report) && isProcessingReport(report);
}

function hasOutstandingChildRequest(chatReport: Report, iouReportOrID: OnyxEntry<Report> | string) {
const reportActions = getAllReportActions(chatReport.reportID);
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line deprecation/deprecation
const policy = getPolicy(chatReport.policyID);
return Object.values(reportActions).some((action) => {
const iouReportID = getIOUReportIDFromReportActionPreview(action);
if (
!iouReportID ||
action.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE ||
isDeletedAction(action) ||
(typeof iouReportOrID === 'string' && iouReportID === iouReportOrID)
) {
return false;
}

const iouReport = typeof iouReportOrID !== 'string' && iouReportOrID?.reportID === iouReportID ? iouReportOrID : getReportOrDraftReport(iouReportID);
const transactions = getReportTransactions(iouReportID);
return (
canIOUBePaid(iouReport, chatReport, policy, transactions) || canApproveIOU(iouReport, policy, transactions) || canSubmitReport(iouReport, policy, transactions, undefined, false)
);
});
}

/**
* Checks whether the card transaction support deleting based on liability type
*/
Expand Down Expand Up @@ -11766,6 +11790,7 @@
getArchiveReason,
getApprovalChain,
isIndividualInvoiceRoom,
hasOutstandingChildRequest,
isAuditor,
hasMissingInvoiceBankAccount,
reasonForReportToBeInOptionList,
Expand Down
73 changes: 28 additions & 45 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ import {
import {
getAllReportActions,
getIOUActionForReportID,
getIOUReportIDFromReportActionPreview,
getLastVisibleAction,
getLastVisibleMessage,
getOneTransactionThreadReportID,
Expand Down Expand Up @@ -156,6 +155,7 @@ import {
getTransactionDetails,
hasHeldExpenses as hasHeldExpensesReportUtils,
hasNonReimbursableTransactions as hasNonReimbursableTransactionsReportUtils,
hasOutstandingChildRequest,
hasReportBeenReopened,
hasReportBeenRetracted,
isArchivedReport,
Expand Down Expand Up @@ -8281,12 +8281,12 @@ function deleteMoneyRequest(
});
}

if (!shouldDeleteIOUReport && updatedReportPreviewAction?.childMoneyRequestCount === 0) {
if (chatReport && updatedIOUReport && !shouldDeleteIOUReport && updatedReportPreviewAction?.childMoneyRequestCount === 0) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`,
value: {
hasOutstandingChildRequest: false,
hasOutstandingChildRequest: hasOutstandingChildRequest(chatReport, updatedIOUReport),
},
});
}
Expand All @@ -8308,16 +8308,18 @@ function deleteMoneyRequest(
reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {},
)?.created;

optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`,
value: {
hasOutstandingChildRequest: false,
iouReportID: null,
lastMessageText,
lastVisibleActionCreated,
},
});
if (chatReport) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`,
value: {
hasOutstandingChildRequest: hasOutstandingChildRequest(chatReport, iouReport?.reportID),
iouReportID: null,
lastMessageText,
lastVisibleActionCreated,
},
});
}
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`,
Expand Down Expand Up @@ -9138,25 +9140,6 @@ function getReportFromHoldRequestsOnyxData(
};
}

function hasOutstandingChildRequest(chatReport: OnyxTypes.Report, excludedIOUReport: OnyxEntry<OnyxTypes.Report>, policyId?: string) {
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line deprecation/deprecation
const policy = getPolicy(policyId);
if (!policy?.achAccount?.bankAccountID) {
return false;
}
const reportActions = getAllReportActions(chatReport.reportID);
return !!Object.values(reportActions).find((action) => {
const iouReportID = getIOUReportIDFromReportActionPreview(action);
if (iouReportID === excludedIOUReport?.reportID) {
return false;
}
const iouReport = getReportOrDraftReport(iouReportID);
const transactions = getReportTransactions(iouReportID);
return canIOUBePaid(iouReport, chatReport, policy, transactions) || canIOUBePaid(iouReport, chatReport, policy, transactions, true);
});
}

function getPayMoneyRequestParams(
initialChatReport: OnyxTypes.Report,
iouReport: OnyxEntry<OnyxTypes.Report>,
Expand Down Expand Up @@ -9254,7 +9237,7 @@ function getPayMoneyRequestParams(
const optimisticChatReport = {
...chatReport,
lastReadTime: DateUtils.getDBTime(),
hasOutstandingChildRequest: hasOutstandingChildRequest(chatReport, iouReport, iouReport?.policyID),
hasOutstandingChildRequest: hasOutstandingChildRequest(chatReport, iouReport?.reportID),
iouReportID: null,
lastMessageText: getReportActionText(optimisticIOUReportAction),
lastMessageHtml: getReportActionHtml(optimisticIOUReportAction),
Expand Down Expand Up @@ -9668,10 +9651,6 @@ function getIOUReportActionToApproveOrPay(chatReport: OnyxEntry<OnyxTypes.Report
});
}

function hasIOUToApproveOrPay(chatReport: OnyxEntry<OnyxTypes.Report>, updatedIouReport: OnyxEntry<OnyxTypes.Report>): boolean {
return !!getIOUReportActionToApproveOrPay(chatReport, updatedIouReport);
}

function isLastApprover(approvalChain: string[]): boolean {
if (approvalChain.length === 0) {
return true;
Expand Down Expand Up @@ -9736,20 +9715,23 @@ function approveMoneyRequest(expenseReport: OnyxEntry<OnyxTypes.Report>, full?:
value: updatedExpenseReport,
};

const optimisticChatReportData: OnyxUpdate = {
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport.chatReportID}`,
value: {
hasOutstandingChildRequest: hasIOUToApproveOrPay(chatReport, updatedExpenseReport),
},
};
let optimisticChatReportData: OnyxUpdate | undefined;
if (chatReport) {
optimisticChatReportData = {
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport.chatReportID}`,
value: {
hasOutstandingChildRequest: hasOutstandingChildRequest(chatReport, updatedExpenseReport),
},
};
}

const optimisticNextStepData: OnyxUpdate = {
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`,
value: optimisticNextStep,
};
const optimisticData: OnyxUpdate[] = [optimisticIOUReportData, optimisticReportActionsData, optimisticNextStepData, optimisticChatReportData];
const optimisticData: OnyxUpdate[] = [optimisticIOUReportData, optimisticReportActionsData, optimisticNextStepData, ...(optimisticChatReportData ? [optimisticChatReportData] : [])];

const successData: OnyxUpdate[] = [
{
Expand Down Expand Up @@ -12678,6 +12660,7 @@ export {
assignReportToMe,
getPerDiemExpenseInformation,
getSendInvoiceInformation,
hasOutstandingChildRequest,
};
export type {
GPSPoint as GpsPoint,
Expand Down
15 changes: 10 additions & 5 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,13 @@ import {
getReportLastVisibleActionCreated,
getReportMetadata,
getReportNotificationPreference,
getReportOrDraftReport,
getReportPreviewMessage,
getReportTransactions,
getReportViolations,
getRouteFromLink,
getTitleReportField,
hasOutstandingChildRequest,
isChatThread as isChatThreadReportUtils,
isConciergeChatReport,
isExpenseReport,
Expand Down Expand Up @@ -5121,11 +5123,14 @@ function deleteAppReport(reportID: string | undefined) {
});
}

optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`,
value: {hasOutstandingChildRequest: false},
});
const chatReport = getReportOrDraftReport(report?.parentReportID);
if (chatReport) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`,
value: {hasOutstandingChildRequest: hasOutstandingChildRequest(chatReport, report?.reportID)},
});
}

failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
Expand Down
96 changes: 96 additions & 0 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import createCollection from '../utils/collections/createCollection';
import createRandomPolicy from '../utils/collections/policies';
import createRandomReportAction from '../utils/collections/reportActions';
import {createRandomReport} from '../utils/collections/reports';
import createRandomTransaction from '../utils/collections/transaction';
import getIsUsingFakeTimers from '../utils/getIsUsingFakeTimers';
import PusherHelper from '../utils/PusherHelper';
import * as TestHelper from '../utils/TestHelper';
Expand Down Expand Up @@ -1832,6 +1833,101 @@ describe('actions/Report', () => {
// The length is 3 to include the CREATED action
expect(Object.keys(selfDMReportActions ?? {}).length).toBe(3);
});

it('should not reset the chatReport hasOutstandingChildRequest if there is another outstanding report', async () => {
const currentUserAccountID = 1;
const fakePolicy: OnyxTypes.Policy = {
...createRandomPolicy(6),
role: 'admin',
ownerAccountID: currentUserAccountID,
areRulesEnabled: true,
preventSelfApproval: false,
autoReportingFrequency: 'immediate',
harvesting: {
enabled: false,
},
};
const chatReport: OnyxTypes.Report = {...createRandomReport(11), policyID: fakePolicy.id, chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, hasOutstandingChildRequest: true};

const expenseReport1: OnyxTypes.Report = {
...createRandomReport(5),
type: CONST.REPORT.TYPE.EXPENSE,
managerID: currentUserAccountID,
ownerAccountID: currentUserAccountID,
policyID: fakePolicy.id,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
chatReportID: chatReport.reportID,
parentReportID: chatReport.reportID,
};
const reportPreview1: OnyxTypes.ReportAction = {
...createRandomReportAction(1),
actionName: CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW,
originalMessage: {
linkedReportID: expenseReport1.reportID,
},
};
const expenseReport2: OnyxTypes.Report = {
...createRandomReport(6),
type: CONST.REPORT.TYPE.EXPENSE,
managerID: currentUserAccountID,
ownerAccountID: currentUserAccountID,
policyID: fakePolicy.id,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
chatReportID: chatReport.reportID,
parentReportID: chatReport.reportID,
};
const transaction: OnyxTypes.Transaction = {...createRandomTransaction(22), reportID: expenseReport2.reportID};
const reportPreview2: OnyxTypes.ReportAction = {
...createRandomReportAction(22),
actionName: CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW,
originalMessage: {
linkedReportID: expenseReport2.reportID,
},
};
const iouAction1: OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> = {
reportActionID: '1',
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
created: DateUtils.getDBTime(),
originalMessage: {
amount: 100,
currency: CONST.CURRENCY.USD,
type: CONST.IOU.REPORT_ACTION_TYPE.CREATE,
},
};

await Onyx.merge(ONYXKEYS.SESSION, {accountID: currentUserAccountID});
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport1.reportID}`, {
[iouAction1.reportActionID]: iouAction1,
});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, {
[reportPreview1.reportActionID]: reportPreview1,
[reportPreview2.reportActionID]: reportPreview2,
});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${expenseReport1.reportID}`, expenseReport1);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${expenseReport2.reportID}`, expenseReport2);
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction);

// When deleting the first expense report
Report.deleteAppReport(expenseReport1.reportID);
await waitForBatchedUpdates();

const report = await new Promise<OnyxEntry<OnyxTypes.Report>>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`,
callback: (val) => {
Onyx.disconnect(connection);
resolve(val);
},
});
});

// The hasOutstandingChildRequest should still remain true as there is a second outstanding report.
expect(report?.hasOutstandingChildRequest).toBe(true);
});
});

describe('changeReportPolicy', () => {
Expand Down
1 change: 0 additions & 1 deletion tests/utils/collections/reportActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export default function createRandomReportAction(index: number): ReportAction {
automatic: randBoolean(),
shouldShow: randBoolean(),
lastModified: getRandomDate(),
pendingAction: rand(Object.values(CONST.RED_BRICK_ROAD_PENDING_ACTION)),
delegateAccountID: index,
errors: {},
isAttachmentOnly: randBoolean(),
Expand Down
Loading