-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add an action method for creating a distance request #24385
Merged
Merged
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ef012c6
Add initial method and DRY up logic for IOU report
tgolen cb4f50f
Move more logic to DRY method
tgolen 11e1fe9
Move more logic into DRY method
tgolen a09ac5e
Add JS Docs and remove unused variables
tgolen 99b7d07
Simplify params, add more JS Docs
tgolen 94f7135
Merge branch 'main' into tgolen-createdistancerequest
tgolen 7ec773a
Fix the onyx data
tgolen e3d5748
Merge branch 'main' into tgolen-createdistancerequest
tgolen 8263331
Merge branch 'main' into tgolen-createdistancerequest
tgolen 6fc4a90
Merge branch 'main' into tgolen-createdistancerequest
tgolen 92012e9
Use existing transaction ID when generating optimistic transaction
tgolen b759129
Merge the transaction data instead of setting it
tgolen 0090a49
Merge branch 'main' into tgolen-createdistancerequest
tgolen 5eb3959
Always merge, don't set
tgolen 11593c1
Fix typo in comment
tgolen fd96227
Remove comment
tgolen e83cb43
Change more calls to merge
tgolen 7c03a7f
Revert "Change more calls to merge"
tgolen f5870ff
Update IOU.js
tgolen cc3d875
Manually merge transaction data from existing transaction
tgolen 145b6d3
Fix variable references
tgolen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,8 +100,7 @@ function buildOnyxDataForMoneyRequest( | |
) { | ||
const optimisticData = [ | ||
{ | ||
// Use SET for new reports because it doesn't exist yet, is faster and we need the data to be available when we navigate to the chat page | ||
onyxMethod: isNewChatReport ? Onyx.METHOD.SET : Onyx.METHOD.MERGE, | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, | ||
value: { | ||
...chatReport, | ||
|
@@ -112,7 +111,7 @@ function buildOnyxDataForMoneyRequest( | |
}, | ||
}, | ||
{ | ||
onyxMethod: isNewIOUReport ? Onyx.METHOD.SET : Onyx.METHOD.MERGE, | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, | ||
value: { | ||
...iouReport, | ||
|
@@ -122,20 +121,20 @@ function buildOnyxDataForMoneyRequest( | |
}, | ||
}, | ||
{ | ||
onyxMethod: Onyx.METHOD.SET, | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, | ||
value: transaction, | ||
}, | ||
{ | ||
onyxMethod: isNewChatReport ? Onyx.METHOD.SET : Onyx.METHOD.MERGE, | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, | ||
value: { | ||
...(isNewChatReport ? {[chatCreatedAction.reportActionID]: chatCreatedAction} : {}), | ||
[reportPreviewAction.reportActionID]: reportPreviewAction, | ||
}, | ||
}, | ||
{ | ||
onyxMethod: isNewIOUReport ? Onyx.METHOD.SET : Onyx.METHOD.MERGE, | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, | ||
value: { | ||
...(isNewIOUReport ? {[iouCreatedAction.reportActionID]: iouCreatedAction} : {}), | ||
|
@@ -302,19 +301,33 @@ function buildOnyxDataForMoneyRequest( | |
} | ||
|
||
/** | ||
* Request money from another user | ||
* 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 {Number} amount - always in the smallest unit of the currency | ||
* @param {String} currency | ||
* @param {String} payeeEmail | ||
* @param {Number} payeeAccountID | ||
* @param {Object} participant | ||
* @param {String} comment | ||
* @param {Number} amount | ||
* @param {String} currency | ||
* @param {Number} payeeAccountID | ||
* @param {String} payeeEmail | ||
* @param {Object} [receipt] | ||
* | ||
* @returns {Object} data | ||
* @returns {String} data.payerEmail | ||
* @returns {Object} data.iouReport | ||
* @returns {Object} data.chatReport | ||
* @returns {Object} data.transaction | ||
* @returns {Object} data.iouAction | ||
* @returns {Object} data.createdChatReportActionID | ||
* @returns {Object} data.createdIOUReportActionID | ||
* @returns {Object} data.reportPreviewAction | ||
* @returns {Object} data.onyxData | ||
* @returns {Object} data.onyxData.optimisticData | ||
* @returns {Object} data.onyxData.successData | ||
* @returns {Object} data.onyxData.failureData | ||
* @param {String} [existingTransactionID] | ||
*/ | ||
function requestMoney(report, amount, currency, payeeEmail, payeeAccountID, participant, comment, receipt = undefined) { | ||
function getMoneyRequestInformation(report, participant, comment, amount, currency, payeeAccountID, payeeEmail, receipt = undefined, existingTransactionID = null) { | ||
const payerEmail = OptionsListUtils.addSMSDomainIfPhoneNumber(participant.login); | ||
const payerAccountID = Number(participant.accountID); | ||
const isPolicyExpenseChat = participant.isPolicyExpenseChat; | ||
|
@@ -364,7 +377,7 @@ function requestMoney(report, amount, currency, payeeEmail, payeeAccountID, part | |
receiptObject.source = receipt.source; | ||
receiptObject.state = CONST.IOU.RECEIPT_STATE.SCANREADY; | ||
} | ||
const optimisticTransaction = TransactionUtils.buildOptimisticTransaction( | ||
const transaction = TransactionUtils.buildOptimisticTransaction( | ||
ReportUtils.isExpenseReport(iouReport) ? -amount : amount, | ||
currency, | ||
iouReport.reportID, | ||
|
@@ -373,6 +386,7 @@ function requestMoney(report, amount, currency, payeeEmail, payeeAccountID, part | |
'', | ||
undefined, | ||
receiptObject, | ||
existingTransactionID, | ||
); | ||
|
||
// STEP 4: Build optimistic reportActions. We need: | ||
|
@@ -383,18 +397,25 @@ function requestMoney(report, amount, currency, payeeEmail, payeeAccountID, part | |
// Note: The CREATED action for the IOU report must be optimistically generated before the IOU action so there's no chance that it appears after the IOU action in the chat | ||
const optimisticCreatedActionForChat = ReportUtils.buildOptimisticCreatedReportAction(payeeEmail); | ||
const optimisticCreatedActionForIOU = ReportUtils.buildOptimisticCreatedReportAction(payeeEmail); | ||
const optimisticIOUAction = ReportUtils.buildOptimisticIOUReportAction( | ||
const iouAction = ReportUtils.buildOptimisticIOUReportAction( | ||
CONST.IOU.REPORT_ACTION_TYPE.CREATE, | ||
amount, | ||
currency, | ||
comment, | ||
[participant], | ||
optimisticTransaction.transactionID, | ||
transaction.transactionID, | ||
'', | ||
iouReport.reportID, | ||
receiptObject, | ||
); | ||
|
||
let reportPreviewAction = isNewIOUReport ? null : ReportActionsUtils.getReportPreviewAction(chatReport.reportID, iouReport.reportID); | ||
if (reportPreviewAction) { | ||
reportPreviewAction = ReportUtils.updateReportPreview(iouReport, reportPreviewAction, comment); | ||
} else { | ||
reportPreviewAction = ReportUtils.buildOptimisticReportPreview(chatReport, iouReport, comment); | ||
} | ||
|
||
// Add optimistic personal details for participant | ||
const optimisticPersonalDetailListAction = isNewChatReport | ||
? { | ||
|
@@ -407,28 +428,108 @@ function requestMoney(report, amount, currency, payeeEmail, payeeAccountID, part | |
} | ||
: undefined; | ||
|
||
let reportPreviewAction = isNewIOUReport ? null : ReportActionsUtils.getReportPreviewAction(chatReport.reportID, iouReport.reportID); | ||
if (reportPreviewAction) { | ||
reportPreviewAction = ReportUtils.updateReportPreview(iouReport, reportPreviewAction, comment); | ||
} else { | ||
reportPreviewAction = ReportUtils.buildOptimisticReportPreview(chatReport, iouReport, comment); | ||
} | ||
|
||
// STEP 5: Build Onyx Data | ||
const [optimisticData, successData, failureData] = buildOnyxDataForMoneyRequest( | ||
chatReport, | ||
iouReport, | ||
optimisticTransaction, | ||
transaction, | ||
optimisticCreatedActionForChat, | ||
optimisticCreatedActionForIOU, | ||
optimisticIOUAction, | ||
iouAction, | ||
optimisticPersonalDetailListAction, | ||
reportPreviewAction, | ||
isNewChatReport, | ||
isNewIOUReport, | ||
); | ||
|
||
// STEP 6: Make the request | ||
return { | ||
payerEmail, | ||
iouReport, | ||
chatReport, | ||
transaction, | ||
iouAction, | ||
createdChatReportActionID: isNewChatReport ? optimisticCreatedActionForChat.reportActionID : 0, | ||
createdIOUReportActionID: isNewIOUReport ? optimisticCreatedActionForIOU.reportActionID : 0, | ||
reportPreviewAction, | ||
onyxData: { | ||
optimisticData, | ||
successData, | ||
failureData, | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Requests money based on a distance (eg. mileage from a map) | ||
* | ||
* @param {Object} report | ||
* @param {String} payeeEmail | ||
* @param {Number} payeeAccountID | ||
* @param {Object} participant | ||
* @param {String} comment | ||
* @param {Object[]} waypoints | ||
* @param {String} waypoints[].address required and must be non empty | ||
* @param {String} [waypoints[].lat] optional | ||
* @param {String} [waypoints[].lng] optional | ||
* @param {String} created | ||
* @param {String} [transactionID] | ||
*/ | ||
function createDistanceRequest(report, payeeEmail, payeeAccountID, participant, comment, waypoints, created, transactionID) { | ||
const {payerEmail, iouReport, chatReport, transaction, iouAction, createdChatReportActionID, createdIOUReportActionID, reportPreviewAction, onyxData} = getMoneyRequestInformation( | ||
report, | ||
participant, | ||
comment, | ||
0, | ||
'USD', | ||
payeeAccountID, | ||
payeeEmail, | ||
null, | ||
Comment on lines
+496
to
+500
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This caused #26518. We shouldn't have passed in static values like |
||
transactionID, | ||
); | ||
|
||
API.write( | ||
'CreateDistanceRequest', | ||
{ | ||
debtorEmail: payerEmail, | ||
comment, | ||
iouReportID: iouReport.reportID, | ||
chatReportID: chatReport.reportID, | ||
transactionID: transaction.transactionID, | ||
reportActionID: iouAction.reportActionID, | ||
createdChatReportActionID, | ||
createdIOUReportActionID, | ||
reportPreviewReportActionID: reportPreviewAction.reportActionID, | ||
waypoints, | ||
created, | ||
}, | ||
onyxData, | ||
); | ||
} | ||
|
||
/** | ||
* Request money from another user | ||
* | ||
* @param {Object} report | ||
* @param {Number} amount - always in the smallest unit of the currency | ||
* @param {String} currency | ||
* @param {String} payeeEmail | ||
* @param {Number} payeeAccountID | ||
* @param {Object} participant | ||
* @param {String} comment | ||
* @param {Object} [receipt] | ||
*/ | ||
function requestMoney(report, amount, currency, payeeEmail, payeeAccountID, participant, comment, receipt = undefined) { | ||
const {payerEmail, iouReport, chatReport, transaction, iouAction, createdChatReportActionID, createdIOUReportActionID, reportPreviewAction, onyxData} = getMoneyRequestInformation( | ||
report, | ||
participant, | ||
comment, | ||
amount, | ||
currency, | ||
payeeAccountID, | ||
payeeEmail, | ||
receipt, | ||
); | ||
|
||
API.write( | ||
'RequestMoney', | ||
{ | ||
|
@@ -438,14 +539,14 @@ function requestMoney(report, amount, currency, payeeEmail, payeeAccountID, part | |
comment, | ||
iouReportID: iouReport.reportID, | ||
chatReportID: chatReport.reportID, | ||
transactionID: optimisticTransaction.transactionID, | ||
reportActionID: optimisticIOUAction.reportActionID, | ||
createdChatReportActionID: isNewChatReport ? optimisticCreatedActionForChat.reportActionID : 0, | ||
createdIOUReportActionID: isNewIOUReport ? optimisticCreatedActionForIOU.reportActionID : 0, | ||
transactionID: transaction.transactionID, | ||
reportActionID: iouAction.reportActionID, | ||
createdChatReportActionID, | ||
createdIOUReportActionID, | ||
reportPreviewReportActionID: reportPreviewAction.reportActionID, | ||
receipt, | ||
}, | ||
{optimisticData, successData, failureData}, | ||
onyxData, | ||
); | ||
resetMoneyRequestInfo(); | ||
Navigation.dismissModal(chatReport.reportID); | ||
|
@@ -1622,6 +1723,7 @@ function navigateToNextPage(iou, iouType, reportID, report) { | |
} | ||
|
||
export { | ||
createDistanceRequest, | ||
editMoneyRequest, | ||
deleteMoneyRequest, | ||
splitBill, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we removing this condition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are being changed because:
set
just because "it's faster" is not a reason to use setI checked with @luacmartins who originally made the change and we agreed it was OK to use merge for everything for now.