Skip to content

Commit

Permalink
Merge pull request Expensify#35246 from Expensify/vit-passReportID
Browse files Browse the repository at this point in the history
[Wave6] Enable requesting on specific report
  • Loading branch information
Beamanator authored Jan 31, 2024
2 parents 6bdf20f + 4241883 commit 1c1faaa
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ function getOutstandingChildRequest(policy, needsToBeManuallySubmitted) {
* @param {Array} optimisticPolicyRecentlyUsedCategories
* @param {Array} optimisticPolicyRecentlyUsedTags
* @param {boolean} isNewChatReport
* @param {boolean} isNewIOUReport
* @param {boolean} shouldCreateNewMoneyRequestReport
* @param {Object} policy - May be undefined, an empty object, or an object matching the Policy type (src/types/onyx/Policy.ts)
* @param {Array} policyTags
* @param {Array} policyCategories
Expand All @@ -368,7 +368,7 @@ function buildOnyxDataForMoneyRequest(
optimisticPolicyRecentlyUsedCategories,
optimisticPolicyRecentlyUsedTags,
isNewChatReport,
isNewIOUReport,
shouldCreateNewMoneyRequestReport,
policy,
policyTags,
policyCategories,
Expand All @@ -391,14 +391,14 @@ function buildOnyxDataForMoneyRequest(
},
},
{
onyxMethod: isNewIOUReport ? Onyx.METHOD.SET : Onyx.METHOD.MERGE,
onyxMethod: shouldCreateNewMoneyRequestReport ? Onyx.METHOD.SET : Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
value: {
...iouReport,
lastMessageText: iouAction.message[0].text,
lastMessageHtml: iouAction.message[0].html,
pendingFields: {
...(isNewIOUReport ? {createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD} : {preview: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
...(shouldCreateNewMoneyRequestReport ? {createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD} : {preview: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
},
},
},
Expand All @@ -416,10 +416,10 @@ function buildOnyxDataForMoneyRequest(
},
},
{
onyxMethod: isNewIOUReport ? Onyx.METHOD.SET : Onyx.METHOD.MERGE,
onyxMethod: shouldCreateNewMoneyRequestReport ? Onyx.METHOD.SET : Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`,
value: {
...(isNewIOUReport ? {[iouCreatedAction.reportActionID]: iouCreatedAction} : {}),
...(shouldCreateNewMoneyRequestReport ? {[iouCreatedAction.reportActionID]: iouCreatedAction} : {}),
[iouAction.reportActionID]: iouAction,
},
},
Expand Down Expand Up @@ -507,7 +507,7 @@ function buildOnyxDataForMoneyRequest(
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`,
value: {
...(isNewIOUReport
...(shouldCreateNewMoneyRequestReport
? {
[iouCreatedAction.reportActionID]: {
pendingAction: null,
Expand Down Expand Up @@ -547,7 +547,7 @@ function buildOnyxDataForMoneyRequest(
value: {
pendingFields: null,
errorFields: {
...(isNewIOUReport ? {createChat: ErrorUtils.getMicroSecondOnyxError('report.genericCreateReportFailureMessage')} : {}),
...(shouldCreateNewMoneyRequestReport ? {createChat: ErrorUtils.getMicroSecondOnyxError('report.genericCreateReportFailureMessage')} : {}),
},
},
},
Expand Down Expand Up @@ -593,7 +593,7 @@ function buildOnyxDataForMoneyRequest(
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`,
value: {
...(isNewIOUReport
...(shouldCreateNewMoneyRequestReport
? {
[iouCreatedAction.reportActionID]: {
errors: getReceiptError(transaction.receipt, transaction.filename || transaction.receipt.filename, isScanRequest),
Expand Down Expand Up @@ -634,7 +634,7 @@ function buildOnyxDataForMoneyRequest(
* Gathers all the data needed to make a money request. It attempts to find existing reports, iouReports, and receipts. If it doesn't find them, then
* it creates optimistic versions of them and uses those instead
*
* @param {Object} report
* @param {Object} parentChatReport
* @param {Object} participant
* @param {String} comment
* @param {Number} amount
Expand All @@ -651,6 +651,7 @@ function buildOnyxDataForMoneyRequest(
* @param {Object} [policy]
* @param {Object} [policyTags]
* @param {Object} [policyCategories]
* @param {Number} [moneyRequestReportID] - If user requests money via the report composer on some money request report, we always add a request to that specific report.
* @returns {Object} data
* @returns {String} data.payerEmail
* @returns {Object} data.iouReport
Expand All @@ -666,7 +667,7 @@ function buildOnyxDataForMoneyRequest(
* @returns {Object} data.onyxData.failureData
*/
function getMoneyRequestInformation(
report,
parentChatReport,
participant,
comment,
amount,
Expand All @@ -683,14 +684,15 @@ function getMoneyRequestInformation(
policy = undefined,
policyTags = undefined,
policyCategories = undefined,
moneyRequestReportID = 0,
) {
const payerEmail = OptionsListUtils.addSMSDomainIfPhoneNumber(participant.login);
const payerAccountID = Number(participant.accountID);
const isPolicyExpenseChat = participant.isPolicyExpenseChat;

// STEP 1: Get existing chat report OR build a new optimistic one
let isNewChatReport = false;
let chatReport = lodashGet(report, 'reportID', null) ? report : null;
let chatReport = lodashGet(parentChatReport, 'reportID', null) ? parentChatReport : null;

// If this is a policyExpenseChat, the chatReport must exist and we can get it from Onyx.
// report is null if the flow is initiated from the global create menu. However, participant always stores the reportID if it exists, which is the case for policyExpenseChats
Expand All @@ -708,9 +710,15 @@ function getMoneyRequestInformation(
chatReport = ReportUtils.buildOptimisticChatReport([payerAccountID]);
}

// STEP 2: Get existing IOU report and update its total OR build a new optimistic one
const isNewIOUReport = !chatReport.iouReportID || ReportUtils.hasIOUWaitingOnCurrentUserBankAccount(chatReport);
let iouReport = isNewIOUReport ? null : allReports[`${ONYXKEYS.COLLECTION.REPORT}${chatReport.iouReportID}`];
// STEP 2: Get the money request report. If the moneyRequestReportID has been provided, we want to add the transaction to this specific report.
// If no such reportID has been provided, let's use the chatReport.iouReportID property. In case that is not present, build a new optimistic money request report.
let iouReport = null;
const shouldCreateNewMoneyRequestReport = !moneyRequestReportID && (!chatReport.iouReportID || ReportUtils.hasIOUWaitingOnCurrentUserBankAccount(chatReport));
if (moneyRequestReportID > 0) {
iouReport = allReports[`${ONYXKEYS.COLLECTION.REPORT}${moneyRequestReportID}`];
} else if (!shouldCreateNewMoneyRequestReport) {
iouReport = allReports[`${ONYXKEYS.COLLECTION.REPORT}${chatReport.iouReportID}`];
}

// Check if the Scheduled Submit is enabled in case of expense report
let needsToBeManuallySubmitted = true;
Expand Down Expand Up @@ -807,7 +815,7 @@ function getMoneyRequestInformation(
currentTime,
);

let reportPreviewAction = isNewIOUReport ? null : ReportActionsUtils.getReportPreviewAction(chatReport.reportID, iouReport.reportID);
let reportPreviewAction = shouldCreateNewMoneyRequestReport ? null : ReportActionsUtils.getReportPreviewAction(chatReport.reportID, iouReport.reportID);
if (reportPreviewAction) {
reportPreviewAction = ReportUtils.updateReportPreview(iouReport, reportPreviewAction, false, comment, optimisticTransaction);
} else {
Expand Down Expand Up @@ -845,7 +853,7 @@ function getMoneyRequestInformation(
optimisticPolicyRecentlyUsedCategories,
optimisticPolicyRecentlyUsedTags,
isNewChatReport,
isNewIOUReport,
shouldCreateNewMoneyRequestReport,
policy,
policyTags,
policyCategories,
Expand All @@ -860,7 +868,7 @@ function getMoneyRequestInformation(
transaction: optimisticTransaction,
iouAction,
createdChatReportActionID: isNewChatReport ? optimisticCreatedActionForChat.reportActionID : 0,
createdIOUReportActionID: isNewIOUReport ? optimisticCreatedActionForIOU.reportActionID : 0,
createdIOUReportActionID: shouldCreateNewMoneyRequestReport ? optimisticCreatedActionForIOU.reportActionID : 0,
reportPreviewAction,
onyxData: {
optimisticData,
Expand Down Expand Up @@ -892,6 +900,7 @@ function createDistanceRequest(report, participant, comment, created, category,
// If the report is an iou or expense report, we should get the linked chat report to be passed to the getMoneyRequestInformation function
const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report);
const currentChatReport = isMoneyRequestReport ? ReportUtils.getReport(report.chatReportID) : report;
const moneyRequestReportID = isMoneyRequestReport ? report.reportID : 0;
const currentCreated = DateUtils.enrichMoneyRequestTimestamp(created);

const optimisticReceipt = {
Expand All @@ -916,6 +925,7 @@ function createDistanceRequest(report, participant, comment, created, category,
policy,
policyTags,
policyCategories,
moneyRequestReportID,
);
API.write(
'CreateDistanceRequest',
Expand Down Expand Up @@ -1313,6 +1323,7 @@ function requestMoney(
// If the report is iou or expense report, we should get the linked chat report to be passed to the getMoneyRequestInformation function
const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report);
const currentChatReport = isMoneyRequestReport ? ReportUtils.getReport(report.chatReportID) : report;
const moneyRequestReportID = isMoneyRequestReport ? report.reportID : 0;
const currentCreated = DateUtils.enrichMoneyRequestTimestamp(created);
const {payerAccountID, payerEmail, iouReport, chatReport, transaction, iouAction, createdChatReportActionID, createdIOUReportActionID, reportPreviewAction, onyxData} =
getMoneyRequestInformation(
Expand All @@ -1333,6 +1344,7 @@ function requestMoney(
policy,
policyTags,
policyCategories,
moneyRequestReportID,
);
const activeReportID = isMoneyRequestReport ? report.reportID : chatReport.reportID;

Expand Down

0 comments on commit 1c1faaa

Please sign in to comment.