Closed vjrj closed 1 month ago
I think we can refactor this code a bit like this for better results:
First the EMLimportService
gets a list a current eml contacts in a dr and tries to find the contacts in the db.
1) If the contact already exist (looking for the email or the full name), update them if we have more info (a name or an email, etc)
2) If a contact is not in the contact
table, add it
3) take into consideration if a contact is primary or not and maintain an additional list of primary contacts
return the current updated contact list and the primary contact list
In the IptService
we receive that new contact lists and:
4) maintain a list of old contacts in a variable
5) remove all contact_for
for that dr
6) relink the new in sync contacts list created in EMLImportService
to that dr in contacts_for
(as primary or not as we have a list created in 3)
7) loop the old contacts list of step 4) and remove orphan contacts that does not have an entry in contact_for
.
With this I think we'll have the contacts exactly with the same info as the EML and the IPT, and contacts not longer linked to a dr will be removed. I think I'm not missing anything, but...
I added a first part that update existing contacts too taking into consideration if the contact is primary or not.
I can continue with the rest or do it in a new PR in the near future.
This PR solves a lot of issues (the most severe is that currently the first contact without email get associated all drs of contacts without emails, that is a big list for one contact).
This PR fix #116. Basically:
Contact.findByEmail(empty)
was picked in this case.