OBPIH-6825 Prioritize active persons when fetching by name or email#4971
Conversation
…e we have duplicates
|
Marking this as do not merge for now since it's not essential to the release as it's not a new bug. If we feel confident about it though, I'm happy to try fitting it in. |
| * format can be provided during data imports. | ||
| * @return A Person who has the provided name and email. Will be a new Person if they didn't previously exist. | ||
| */ | ||
| Person getOrCreatePersonByRecipient(String recipient) { |
There was a problem hiding this comment.
for the most part this method is copied from ShipmentService. I did refactor it though (and created tests for it)
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #4971 +/- ##
============================================
+ Coverage 7.61% 7.76% +0.14%
- Complexity 817 845 +28
============================================
Files 601 601
Lines 42300 42315 +15
Branches 10277 10281 +4
============================================
+ Hits 3222 3284 +62
+ Misses 38610 38545 -65
- Partials 468 486 +18 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| /** | ||
| * @return The first person we can find who has the given email, giving preference to active people. | ||
| */ | ||
| private Person getPersonByEmail(String email) { |
There was a problem hiding this comment.
I am not feeling fully comfortable with this function name, because, at the last line, we are returning a list of active people, so I would like to add this information in the naming because at this moment the name suggests we are just returning all matching people by email, but I don't have any better idea right now
There was a problem hiding this comment.
the find only returns a single active person. To me, if we were going to return a list of person I'd name the method getAllPersonByEmail or getPersonsByEmail or getPeopleByEmail
grails-app/services/org/pih/warehouse/data/PersonService.groovy
Outdated
Show resolved
Hide resolved
grails-app/services/org/pih/warehouse/data/PersonService.groovy
Outdated
Show resolved
Hide resolved
| return null | ||
| } | ||
|
|
||
| InternetAddress internetAddress |
There was a problem hiding this comment.
I know you are not the original author of this, but maybe you can explain how and why we use InternetAdress?
There was a problem hiding this comment.
Internet address is an email of the format: "Justin Miranda <[email protected]>". So this code parses that in and outputs a person with:
- email: [email protected]
- firstName: Justin
- lastName: Miranda
I don't know why that format was the original requirement. Maybe @jmiranda has the answer. This format is only used during the import packing list flow.
There was a problem hiding this comment.
Yeah, i think i was originally trying to impose a data standard here https://www.ietf.org/rfc/rfc822.txt but was quickly overruled (within 10 days) when met with reality.
| } | ||
| String[] names = extractNames(internetAddress.personal) | ||
| person = new Person(firstName: names[0], lastName: names[1], email: internetAddress.address) | ||
| if (!person.save(flush: true)) { |
There was a problem hiding this comment.
probably not but it was in the original code so I didn't want to change it without knowing why it was added
There was a problem hiding this comment.
Theoretically it shouldn't be necessary. But practically, it depends on where this code is being invoked. For adding a single person to the system, probably not. But in the previous version of Grails, things were less predictable when we were persisting lots of data all at once. Like say in a data import. It might be possible to remove the flush now (and I would be happy with that refactoring) but I appreciate Evan's instinct to leave well enough alone for now. And if/when we do make the change we need to ensure we have some integration tests around it to make sure it works for all use cases:
- add a single person in an admin page
- add a single person per line item and assign them as recipient on the shipment item
Again it should work without the flush, but we should perform a discovery before making changes. This might be a good scenario to add to the Cascading Behavior on Associations tech debt that we've discussed in the past.
| throw new ValidationException("Cannot save recipient ${combinedNames} due to errors", person.errors) | ||
| } | ||
| person = new Person(firstName: names[0], lastName: names[1]) | ||
| if (!person.save(flush: true)) { |
There was a problem hiding this comment.
Same answer. It was in the original code so I didn't want to change it without knowing why it was added
| recipient = Person.findByFirstNameAndLastName(firstName, lastName) | ||
| } else if (names.length == 1) { | ||
| recipient = Person.findByEmail(names[0]) | ||
| if (line.recipient.contains(" ")) { |
There was a problem hiding this comment.
I don't get it, why do we check if line.recipient contains " "?
There was a problem hiding this comment.
- You can just use the ternary operator for such an assignment
There was a problem hiding this comment.
It's checking how many words are in the recipient string. If there are no spaces, it's an email, if there's one or more spaces, it's a name. It's kind of clunky, but we don't really have a better method of distinguishing between the formats
| '' || null | ||
| 'a b' || '2' | ||
| '<[email protected]>' || '2' | ||
| 'e f <[email protected]>' || '3' // email takes priority, even if name doesn't match and user is inactive. |
There was a problem hiding this comment.
This is an interesting quirk of the system. This is how the logic was working originally so I didn't change it, but it could be worth validating that both the name and email match the user if we're given both
awalkowiak
left a comment
There was a problem hiding this comment.
Approved on the tech huddle

✨ Description of Change
Link to GitHub issue or Jira ticket: https://pihemr.atlassian.net/browse/OBPIH-6825
Description: Because we don't enforce any uniqueness on name or email in our system for entries in the Person table, when fetching people by name and email, there's a chance we'll find more than one. When this happens, we simply pick the first one we get. This can cause inconsistent behaviour when we have two identical users, one active and one inactive.
This was uncovered in PO import but is relevant to all flows that fetch a single Person.
This change is to prioritize returning active Person entries in the case where we have duplicates.