Skip to content

fix: logic for waypoint validation#26591

Merged
hayata-suenaga merged 9 commits into
Expensify:mainfrom
allroundexperts:fix-26583
Sep 4, 2023
Merged

fix: logic for waypoint validation#26591
hayata-suenaga merged 9 commits into
Expensify:mainfrom
allroundexperts:fix-26583

Conversation

@allroundexperts

Copy link
Copy Markdown
Contributor

Details

This PR fixes the logic to check the start and end way points of a distance request.

Fixed Issues

$ #26583
PROPOSAL: #26583 (comment)

Tests

  1. Click on the + icon in the LHN.
  2. Select "Request money" and then choose "Distance".
  3. Select the starting and finishing points
  4. Click on "Add stop". Don't save an address for this new waypoint/stop.
  5. Proceed by clicking the "Next" button.

Verify that the route is shown correctly.

  • Verify that no errors appear in the JS console

Offline tests

N/A

QA Steps

  1. Click on the + icon in the LHN.
  2. Select "Request money" and then choose "Distance".
  3. Select the starting and finishing points
  4. Click on "Add stop". Don't save an address for this new waypoint/stop.
  5. Proceed by clicking the "Next" button.

Verify that the route is shown correctly.

  • 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 correct English and 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 or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • 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
Screen.Recording.2023-09-03.at.4.23.29.AM.mov
Mobile Web - Chrome
Screen.Recording.2023-09-03.at.4.31.22.AM.mov
Mobile Web - Safari
Screen.Recording.2023-09-03.at.4.32.47.AM.mov
Desktop
Screen.Recording.2023-09-03.at.4.26.04.AM.mov
iOS
Screen.Recording.2023-09-03.at.4.33.27.AM.mov
Android
Screen.Recording.2023-09-03.at.4.34.30.AM.mov

@allroundexperts allroundexperts requested a review from a team as a code owner September 2, 2023 23:39
@melvin-bot melvin-bot Bot requested review from ntdiary and removed request for a team September 2, 2023 23:39
@melvin-bot

melvin-bot Bot commented Sep 2, 2023

Copy link
Copy Markdown

@ntdiary Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@situchan

situchan commented Sep 3, 2023

Copy link
Copy Markdown
Contributor

reviewing this one based on @hayata-suenaga's request

Comment thread src/libs/TransactionUtils.js Outdated
Comment thread src/libs/actions/IOU.js Outdated
@ntdiary

ntdiary commented Sep 3, 2023

Copy link
Copy Markdown
Contributor
image

If there are Start waypoint and Finish waypoint, and middle Stop waypoints are empty, the creating distance request will get error. I think this issue should also be fixed. Also, @situchan, I think you can be the primary C+ to get the bonus (I have some other things to do later, and if needed, I may have time to assist as well). 🙂

@allroundexperts

Copy link
Copy Markdown
Contributor Author

@ntdiary What error do you get?

@situchan

situchan commented Sep 3, 2023

Copy link
Copy Markdown
Contributor

If there are Start waypoint and Finish waypoint, and middle Stop waypoints are empty, the creating distance request will get error

This is the main fix from this PR.
@ntdiary Did you test on this branch?

@situchan

situchan commented Sep 3, 2023

Copy link
Copy Markdown
Contributor

@allroundexperts after validation filter, keys should be rearranged.

waypoint1 instead of waypoint3

waypoints screenshot

@ntdiary

ntdiary commented Sep 3, 2023

Copy link
Copy Markdown
Contributor
error.mp4

Did you not try clicking Request? 😂

@situchan

situchan commented Sep 3, 2023

Copy link
Copy Markdown
Contributor

error.mp4

Should be related to #26591 (comment)

@ntdiary

ntdiary commented Sep 3, 2023

Copy link
Copy Markdown
Contributor

Should be related to #26591 (comment)

Yeah, this is the error I mentioned, and it only needs to be rearranged when creating the request (not when selecting locations), otherwise it will cause the second stop waypoint and finish waypoint to become the same location.

duplicate.mp4

@allroundexperts

Copy link
Copy Markdown
Contributor Author

The issue mentioned above has been fixed @situchan!

Comment on lines +283 to +301
const validWaypoints = _.reduce(
waypointValues,
(acc, currentWaypoint, index) => {
const previousWaypoint = waypointValues[index - 1];
// Check if the waypoint has a valid address
if (!currentWaypoint || !currentWaypoint.address || typeof currentWaypoint.address !== 'string' || currentWaypoint.address.trim() === '') {
return acc;
}

// Check for adjacent waypoints with the same address
if (previousWaypoint && currentWaypoint.address === previousWaypoint.address) {
return acc;
}

const lastIndex = Math.max(..._.map(_.keys(acc), (key) => parseInt(key.replace('waypoint', ''), 10)));
return {...acc, [`waypoint${reArrangeIndexes && lastIndex > -1 ? lastIndex + 1 : index}`]: currentWaypoint};
},
{},
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we can refactor this code to be more readable.
How about changing to array first using _.filter and then back to object?
This won't require calculating key names in complex way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Doing so will not preserve the keys. We want to not preserve the keys only when creating the distance request.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Doing so will not preserve the keys. We want to not preserve the keys only when creating the distance request.

I also commented here. But did you find any issue without preserving keys on Request money page before creating request?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Doesn't work. Ref:

Screen.Recording.2023-09-03.at.4.39.48.PM.mov

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the root cause? I thought waypoints is used to display routes, not validatedWaypoints

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No. The route is calculated via the validatedWaypoints. The response from the getRoute request gets stored into onyx. Since the response has the same keys as the waypoints supplied in the request body, therefore, the above error occurs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

{_.map(waypoints, (waypoint, key) => {

@allroundexperts allroundexperts Sep 3, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. This waypoints come from the transaction object here which gets updated when a call to the API is made for getting the route here with validated waypoints.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

FYI, I have simplified the logic for calculating the new indexes!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ok, confirmed. I suspected GetRoute api would also require rearranged keys but no need

response

Comment thread src/libs/actions/IOU.js
createdIOUReportActionID,
reportPreviewReportActionID: reportPreviewAction.reportActionID,
waypoints: JSON.stringify(transaction.comment.waypoints),
waypoints: JSON.stringify(TransactionUtils.getValidWaypoints(transaction.comment.waypoints, true)),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think 2nd param is needed.
This will also be used in GetRoute api. And doesn't affect displaying routes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replied here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we need this logic to reorganize indices to be consecutive (i.e. we need to pass true)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yes, I meant to reorganize always without needing to pass 2nd param but later I realized it should be conditional

@situchan

situchan commented Sep 3, 2023

Copy link
Copy Markdown
Contributor

We should not allow Next button when there's only one unique route.

This is the case when have 2 routes and both are same address:

same route

@allroundexperts

Copy link
Copy Markdown
Contributor Author

We should not allow Next button when there's only one unique route.

This is the case when have 2 routes and both are same address:

same route

Handled!

@situchan

situchan commented Sep 3, 2023

Copy link
Copy Markdown
Contributor

Branch conflicts now

Comment thread src/libs/TransactionUtils.js Outdated

@situchan situchan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:shipit:

Comment thread src/components/DistanceRequest.js Outdated
Comment thread src/components/DistanceRequest.js Outdated
Comment thread src/libs/TransactionUtils.js
Comment thread src/libs/TransactionUtils.js Outdated
Comment thread src/libs/TransactionUtils.js
@allroundexperts

Copy link
Copy Markdown
Contributor Author

@situchan I did the NAB changes and it would be great if you could do a final sanity check!

* @param {Object} waypoint
* @returns {Boolean} Returns true if the address is valid
*/
function waypointHasValidAddress(waypoint) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

hasWaypointValidAddress is better name

@situchan

situchan commented Sep 4, 2023

Copy link
Copy Markdown
Contributor

@situchan I did the NAB changes and it would be great if you could do a final sanity check!

Looks good

@hayata-suenaga hayata-suenaga merged commit 44721d2 into Expensify:main Sep 4, 2023
@OSBotify

OSBotify commented Sep 4, 2023

Copy link
Copy Markdown
Contributor

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

@hayata-suenaga

Copy link
Copy Markdown
Contributor

@situchan @allroundexperts

I realized a problem with this PR (there is no regression yet from this one, and I doubt that there will be)

the reducer function for waypoints to filter out invalid waypoints is assuming that waypointValues are sorted when we get values from the waypoints object here.

But the ordering of values are not guaranteed. So we have to sort the waypointValues based on the index that is included in the key waypoint${index}.

If you agree, can you make a follow up PR @allroundexperts 🙇 ?

@situchan

situchan commented Sep 4, 2023

Copy link
Copy Markdown
Contributor

@hayata-suenaga you mean initial waypoints object can be like {waypoint3: xx, waypoint1: xx, waypoint2: xx}?

@situchan

situchan commented Sep 4, 2023

Copy link
Copy Markdown
Contributor

assuming that waypointValues are sorted

I haven't found any case where this doesn't happen. Checking now

@situchan

situchan commented Sep 4, 2023

Copy link
Copy Markdown
Contributor

I think addStop, removeWaypoint functions guarantee that indexes are always sorted

@ntdiary

ntdiary commented Sep 4, 2023

Copy link
Copy Markdown
Contributor

But the ordering of values are not guaranteed. So we have to sort the waypointValues based on the index that is included in the key waypoint${index}.

That's an interesting thing. In my impression, JS does not promise the order of properties in an object. I noticed this before, but after testing there did not seem to be any abnormal cases, so I did not report it. Also, I was curious before why Object was used to store waypoints rather than Array (+ a temp id) (did not have time to investigate), and from the current code we can see that we need to pay close attention to reordering frequently, which increases potential risks and complexity. 😂

@OSBotify

OSBotify commented Sep 4, 2023

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/hayata-suenaga in version: 1.3.63-0 🚀

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

@allroundexperts

Copy link
Copy Markdown
Contributor Author

PR for sorting: #26705

@hayata-suenaga

Copy link
Copy Markdown
Contributor

@ntdiary

Also, I was curious before why Object was used to store waypoints rather than Array (+ a temp id) (did not have time to investigate), and from the current code we can see that we need to pay close attention to reordering frequently, which increases potential risks and complexity.

This is a very good point that I also noticed. We should do a cleanup eventually. I first need to ask the engineer who came up with this design to see if there was a particular reason we went this way though

@OSBotify

OSBotify commented Sep 5, 2023

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/mountiny in version: 1.3.63-2 🚀

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

@OSBotify

OSBotify commented Sep 5, 2023

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/hayata-suenaga in version: 1.3.64-0 🚀

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

@OSBotify

OSBotify commented Sep 6, 2023

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/mountiny in version: 1.3.64-2 🚀

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

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.

6 participants