User data can be overwritten during import if names match
Problem
When a user imports people from a CSV file, deduplication is performed. If an existing user with the same name as an imported user is found, but the existing user doesn't have an email address while the imported one does - instead of creating a new user, we should add an email address to the existing user. This was part of the initial specification for deduplication.
The code for this deduplication (among other possible types of deduplication during importing) has been added here: #736 . Unfortunately, the team object available in the import route at this moment doesn't contain email addresses. This means that the code doesn't function correctly and instead always assumes that existing users don't have emails.
In some scenarios this could cause problems:
- If an admin user imports a CSV file with the name of an existing person but an incorrect email address
- if an admin user imports a CSV file with the name of an existing person but a different email intending to add a second person with another email address.
Both scenarios seem relatively unlikely, so I'm leaving this functionality as is and the problem should be solved once we've got proper state management (and the "currently selected team" object is fully-featured and correct throughout the app). Alternatively, we can always just fetch the full team object, hydrate members, etc., if we need to solve this problem.
Screenshot

Instructions for reproducing
- Go to the import page
- Download the CSV template
- Import the template (without changes, it contains two example users already!)
- Reset / refresh the page and import the template again
Added (via Zenhub) a dependency on #573 as refactoring state management should fix this bug - though we should probably double check when we get there that it indeed has fixed the problem.
Some useful debugging code is available here: https://github.com/Maptio/maptio/compare/bug/use-full-team-object-in-import-deduplication?expand=1
I'm increasing the priority of this bug after discovering a consequence that should have been obvious, but somehow I missed it!
If a user account object is overwritten due to incorrect deduplication (which itself will happen more frequently because of this bug), information about the teams they belong to, etc., will also be erased!
We've just seen this happen (https://app.intercom.com/a/inbox/q3x5lnhp/inbox/shared/all/conversation/106323200008448?view=List) when an organisation admin imported people and they were themselves on the list of people to be imported. The deduplication code matched the name of the person and, because the data in the teamMembers array was incomplete (i.e. there was no email address) the incomplete user object was used to replace the user object in the database, leading to a lot of missing information, not great!
We've got three options that come to mind just now:
- Fix this part of state management
- Before overwriting data, first fetch the current version of the data from the database (actually in our data model this may be necessary even if we fix state management as the user object could change between when we first fetch it and save it as state and when we choose to overwrite it...)
- Stop adding emails to people who don't have them on import