Skip to content
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

Optimistically generate transactions in existing IOU flows #17718

Merged
merged 92 commits into from
May 5, 2023

Conversation

roryabraham
Copy link
Contributor

@roryabraham roryabraham commented Apr 20, 2023

Details

Optimistically generate transactions for IOU flows and maintain their state under the new transaction Onyx collection.

Fixed Issues

$ https://github.com/Expensify/Expensify/issues/277227

Tests

Request Money

  1. Go offline.
  2. Use the global create menu to request money from someone you've never chatted with before. Make sure you include a comment.
  3. Verify that the chat report and IOU request is created correctly, including the comment. Look in Onyx, and verify that a transaction was created for the IOU under the transaction collection key. The comments and transaction should be pending. Verify that the transaction has the pendingAction field in Onyx.
  4. In the chat that was created in the previous step, request more money. Verify that the IOU actions are created, the total is updated, and that a second transaction appears in Onyx under the transaction collection key.
  5. Come online, verify everything finalizes correctly.
  6. Request money a third time, and cancel that money request. Verify that the amounts update correctly.
  7. Repeat the previous step offline, verify it works as expected. Then go back online.
  8. As the other person, settle up the money request. Verify that for both users the IOU shows as having been settled up.
  9. Edit the RequestMoney api endpoint to just immediately throw a 400 error.
  10. Go offline.
  11. Use the global create menu to request money from another user you've never chatted with.
  12. Verify that the optimistic report, reportActions, and transactions appear as expected.
  13. Come back online.
  14. Verify that you see RBR errors for the report + all the reportActions. Look in Onyx and verify that the transaction has an errors field.
  15. Clear all the RBR errors, verify that everything disappears, including the optimistic transaction in Onyx.

Split Bill

For the following steps, let's assume we have 3 fresh accounts, A, B, and C.

  1. As user A, create a 1:1 chat with user B. Request $50 from that user.
  2. As user A, create a 1:1 chat with user C. Send them some messages, but do not request money from them.
  3. As user A, go offline.
  4. As user A, split a bill for $100 with A, B, C, and D, where D is an email not currently associated with an Expensify account.
  5. Verify that a group chat is optimistically created with the four users, and the bill split IOU action appears for the $100 bill.
  6. Verify that the chat with user B now shows a $75 debt (the $50 from before plus their $25 portion of the bill split.
  7. Verify that the chat with C has been updated to show the $25 debt
  8. Verify that a 1:1 chat with user D has been optimistically created and updated with the $25 IOU debt.
  9. Look in Onyx, and there should be four transactions – a $100 for the group, and three $25 (1:1) transactions. They should all be pending.
  10. As user A, come back online.
  11. Verify that the group chat and 1:1 chat with D has been finalized. Reverify all the IOU amounts. Look at the transactions in Onyx and verify that they are no longer pending.
  12. As user C, settle up the IOU. Verify that the 1:1 IOU is reconciled.
  13. As user A, go back offline.
  14. Edit the SplitBill API endpoint to throw an error.
  15. Split another $100 with A, B, C, and D.
  16. Verify everything is created optimistically again.
  17. Now come back online and verify that everything from the previous step now shows an error.
  18. Clear out the errors, and verify that the optimistic entities disappear from Onyx.
  • Verify that no errors appear in the JS console

Offline tests

Included above.

QA Steps

Maybe controversial, but everything we've changed here should be covered by regression tests already, and we aren't expecting any new or changed behaviors from a user perspective here. So this is No QA, just regressions only.

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
web.mov
Mobile Web - Chrome
AndroidmWeb.mov
Mobile Web - Safari
iOSWeb.mov
Desktop
DesktopOffline.mov
DesktopComesBackOnline.mov
iOS
iOS.mov
Android
Android.mov

@roryabraham roryabraham self-assigned this Apr 20, 2023
@luacmartins luacmartins self-requested a review April 20, 2023 15:10
authEndpoint: `${CONFIG.EXPENSIFY.DEFAULT_API_ROOT}api?command=AuthenticatePusher`,
});

PusherHelper.setup();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't end up needing this PusherHelper change, but I do think it's a potentially useful abstraction, so I left it here. I can remove it if you prefer

* @returns {Object}
*/
function buildOptimisticIOUReportAction(type, amount, currency, comment, participants, paymentType = '', iouTransactionID = '', iouReportID = '', isSettlingUp = false) {
const IOUTransactionID = iouTransactionID || NumberUtils.rand64();
function buildOptimisticIOUReportAction(type, amount, currency, comment, participants, iouTransactionID, paymentType = '', iouReportID = '', isSettlingUp = false) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review note: iouTransactionID became a required param because it needs to be associated with an actual transaction in Onyx and not an ID generated on the fly here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This diff looks worse than it is. Tests were failing, and all I did was:

  1. Wrap all tests in a single describe, which is the most common pattern we have for tests, and makes it easier to run or debug all the tests in a file in VSCode or JetBrains IDEs. (this is why the diff looks crazy)
  2. Changed the method signature of createIOUReportAction and cancelMoneyRequests to account for the fact that IOUTransactionID became a required param in ReportUtils.buildOptimisticIOUReportAction
  3. Changed the method signature of cancelMoneyRequest to account for 2

@s77rt
Copy link
Contributor

s77rt commented May 4, 2023

Cool. We just have some RORY_DEBUG logs 😅

@s77rt
Copy link
Contributor

s77rt commented May 4, 2023

Another bug, seems to be native only
Screenshot from 2023-05-04 22-38-29

@roryabraham
Copy link
Contributor Author

roryabraham commented May 4, 2023

Not able to reproduce on iOS:

image image

Edit: also tried with Euros, seems to work fine

src/libs/actions/Report.js Show resolved Hide resolved
@mountiny
Copy link
Contributor

mountiny commented May 4, 2023

@roryabraham the github actions job2 is still failing, have you been able to look into that?

@roryabraham roryabraham force-pushed the Rory-OptimisticTransactions branch from 10a072d to 74bef77 Compare May 4, 2023 23:50
@roryabraham
Copy link
Contributor Author

@mountiny @s77rt I reverted the last two commits (force-pushed so they're just gone), but yeah they were causing tests to fail and were just for an unbenchmarked performance optimization suggested here. I was kind of unsure about the change anyways since it didn't really follow our patterns. I think this is fine

@s77rt
Copy link
Contributor

s77rt commented May 5, 2023

I still think allReportActions is a little too much. Basically the whole onyx is now on memory 😅. We should probably find a better way to get transactions given a report id. I won't block on that though given the high priority.

s77rt
s77rt previously approved these changes May 5, 2023
Copy link
Contributor

@s77rt s77rt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀

@melvin-bot
Copy link

melvin-bot bot commented May 5, 2023

🎯 @s77rt, thanks for reviewing and testing this PR! 🎉

An E/App issue has been created to issue payment here: #18461.

@roryabraham
Copy link
Contributor Author

roryabraham commented May 5, 2023

Basically the whole onyx is now on memory 😅.

Agree. FWIW, the same code already exists in ReportActionsUtils. Plus Onyx itself has an in-memory cache later, so now all the reportActions are in memory 3x 🙃. These are the existing patterns so I think we're ok for now given the priority, but I agree it seems like we should come up with a better way to handle this, such as just implementing Onyx.get (which would first pull from cache and only go into storage if it's a cache miss)

Copy link
Contributor

@luacmartins luacmartins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments

src/libs/CurrencyUtils.js Outdated Show resolved Hide resolved
src/libs/CurrencyUtils.js Outdated Show resolved Hide resolved
src/libs/actions/IOU.js Show resolved Hide resolved
src/libs/actions/IOU.js Show resolved Hide resolved
src/libs/actions/IOU.js Show resolved Hide resolved
src/libs/actions/IOU.js Show resolved Hide resolved
src/libs/actions/IOU.js Show resolved Hide resolved
src/libs/actions/IOU.js Show resolved Hide resolved
src/libs/actions/IOU.js Show resolved Hide resolved
src/libs/actions/IOU.js Show resolved Hide resolved
@luacmartins luacmartins dismissed Julesssss’s stale review May 5, 2023 04:39

comments addressed

@luacmartins luacmartins merged commit 230b7f7 into main May 5, 2023
@luacmartins luacmartins deleted the Rory-OptimisticTransactions branch May 5, 2023 04:39
@OSBotify
Copy link
Contributor

OSBotify commented May 5, 2023

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify
Copy link
Contributor

OSBotify commented May 5, 2023

🚀 Deployed to staging by https://github.com/luacmartins in version: 1.3.11-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

OSBotify commented May 9, 2023

🚀 Deployed to production by https://github.com/roryabraham in version: 1.3.12-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

Comment on lines +916 to +926
failureData.push({
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: optimisticChatReportData.key,
value: {
errorFields: {
createChat: {
[DateUtils.getMicroseconds()]: Localize.translateLocal('report.genericCreateReportFailureMessage'),
},
},
},
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caused a regression #18839. We already set an error for the report action (in line 883) and we are setting another error for the report itself. This resulted in redundant error messages.

Comment on lines +76 to +90
* Takes an amount as a floating point number and converts it to an integer amount.
* For example, given [25, USD], return 2500.
* Given [25.50, USD] return 2550.
* Given [2500, JPY], return 2500.
*
* @note we do not currently support any currencies with more than two decimal places. Sorry Tunisia :(
*
* @param {String} currency
* @param {Number} amountAsFloat
* @returns {Number}
*/
function convertToSmallestUnit(currency, amountAsFloat) {
const currencyUnit = getCurrencyUnit(currency);
return Math.trunc(amountAsFloat * currencyUnit);
}
Copy link
Contributor

@aldo-expensify aldo-expensify Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be used for the amount of a transaction we send to the backend? If that's the case, this is wrong. If in the front end we input 25 JPY, the backend expects 2500. The backend ALWAYS works in "cents", even with currencies that don't have cents.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Investigating this bug: #23235

I'm not sure if this is the cause, but it may be it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing here: #24175

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants