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

Order form is dropping second last names unexpectedly #3113

Merged
merged 21 commits into from
Nov 27, 2024

Conversation

AnirudhBhat
Copy link
Contributor

@AnirudhBhat AnirudhBhat commented Nov 25, 2024

closes #11236

This PR fixes the logic to retrieve first name and last name given customer full name.

Before this PR, we were considering first word to be the first name and last word to be the last name. This caused problem when there were multiple words in customers name.
Full Name : Firstname and a very long last name
firstname : Firstname
lastname : name

After this PR, we are considering first word to be the first name and all other words as last names
Full Name : Firstname and a very long last name
firstname : Firstname
lastname : and a very long last name

customer_last_name.mp4

@AnirudhBhat AnirudhBhat requested a review from kidinov November 26, 2024 03:40
@AnirudhBhat AnirudhBhat marked this pull request as ready for review November 26, 2024 03:40
@@ -273,4 +273,124 @@ class WCCustomerMapperTest {
// THEN
assertThat(result.isPayingCustomer).isEqualTo(false)
}

@Test
fun `given customer name, then first name is properly assigned`() {
Copy link
Contributor

Choose a reason for hiding this comment

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

np: in all tests, maybe assert both first and last names, because they kinda related and deducted from one string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, my reasoning for splitting the test cases is mainly to understand why the test fails in future. There must be only one reason for the failure of tests IMHO. If we combine both, we need to see if the test failed because of logical error in the extraction of first name or last name which could be avoided if the test asserts just one thing.

Copy link
Contributor

Choose a reason for hiding this comment

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

In my opinion, the one reason here - "if correct logic used in splitting the string". The current implementation, which consists of two private extensions, should not affect the test's logic. Currently, the test does not validate whether the splitter is functioning correctly; instead, it only checks that the first word is designated as the first name.


@Test
fun `given customer name, then first name is properly assigned`() {
// given
Copy link
Contributor

Choose a reason for hiding this comment

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

np: the comments are upper case in the other tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done: 8f338d2

private fun String?.lastNameFromName() = this?.split(" ")?.lastOrNull() ?: ""
private fun String?.firstNameFromName() = this?.substringBefore(" ") ?: ""

private fun String?.lastNameFromName() = this?.substringAfter(" ", "") ?: ""
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this handle extra spaces correctly? I wrote a test and it doesn't pass

    @Test
    fun `given customer name has multiple spaces, then last name returns proper string`() {
        // GIVEN
        val siteId = 23
        val site = SiteModel().apply { id = siteId }

        val customerDTO = CustomerFromAnalyticsDTO(name = "firstname   and a very long last name")

        // WHEN
        val result = mapper.mapToModel(site, customerDTO)

        // THEN
        assertEquals("and a very long last name", result.lastName)
        assertEquals("firstname", result.firstName)
    }

Copy link
Contributor Author

@AnirudhBhat AnirudhBhat Nov 26, 2024

Choose a reason for hiding this comment

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

Good catch! I've fixed this

  1. Multiple in-between spaces
  2. Leading spaces
  3. Trailing spaces

3b5294e

Copy link
Contributor

@kidinov kidinov left a comment

Choose a reason for hiding this comment

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

@AnirudhBhat, thanks for the improvement! I left a couple of nitpicks and one more potential bug. Please take a look!

@AnirudhBhat AnirudhBhat requested a review from kidinov November 26, 2024 08:12
@@ -117,7 +117,11 @@ class WCCustomerMapper @Inject constructor() {
}
}

private fun String?.firstNameFromName() = this?.substringBefore(" ") ?: ""
// Please refer WCCustomerMapperTest file which serves as documentation of how this function behaves.
private fun String?.firstNameFromName(): String =
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it do the trick in both cases?

.substringBefore(" ").trim() ?: ""

You implementation works, but both (maybe) slow and not easy to read

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 wouldn't work as it fails for names that has leading spaces, multiple spaces in between.

For example: First Name:
.substringBefore(" "): Returns "" because the first substring before a space is empty due to leading spaces.
.trim(): Has no effect since the result is already an empty string.
Result: ""

Copy link
Contributor

Choose a reason for hiding this comment

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

Considering that we run this code 6 times per 1 customer mapped I'd propose something simple like that:

    private fun String?.firstNameFromName(): String =
        this?.trim()?.substringBefore(' ')?.trim().orEmpty()

    private fun String?.lastNameFromName(): String =
        this?.trim()?.substringAfter(' ', "")?.trim().orEmpty()

It won't remove spaces inside of the first and last name, but IMO, its not what we even want and we should leave it up to the users and the way they want to write their names

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done: 2aca001

@AnirudhBhat AnirudhBhat requested a review from kidinov November 27, 2024 03:16
Copy link
Contributor

@kidinov kidinov left a comment

Choose a reason for hiding this comment

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

LGTM!

@kidinov kidinov merged commit d01056e into trunk Nov 27, 2024
13 checks passed
@kidinov kidinov deleted the issue/11236-customer-last-name branch November 27, 2024 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Order form is dropping second last names unexpectedly
2 participants