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

dev/core#2047 Fix merge code so that deleted contacts are not left without a primary address #18555

Merged
merged 1 commit into from
Oct 12, 2020

Conversation

eileenmcnaughton
Copy link
Contributor

Overview

Fixes dedupe code so the deleted contact does not wind up with 1 or more address/ emails etc where none are marked primary.

This was picked up through adding test cover for https://lab.civicrm.org/dev/core/-/issues/2039

I considered altering the test to exclude deleted contacts but we run the risk that if a contact
is undeleted they will have no primary address so I figured the integrity makes sense.

Before

When a contact with 2 addresses is merged and only the primary is moved over the deleted contact has an address still, but it is not marked is_primary. If it is subsequently undeleted then there is an integrity issue

After

Ensure the deleted contact's remaining location entities have at least one primary

Technical Details

Note there are a couple of queries in this code that can go (retrieving stuff
we already have) - depending how I go on getting review on this & related tidy up I'll remove them
in a later PR

Comments

If #18552 is merged first I'll need to rebase. #18500 is also a related cleanup

@civibot
Copy link

civibot bot commented Sep 22, 2020

(Standard links)

@civibot civibot bot added the master label Sep 22, 2020
@eileenmcnaughton eileenmcnaughton changed the title dev/core#2039 Fix merge code so that deleted contacts are not left without a primary address dev/core#2047 Fix merge code so that deleted contacts are not left without a primary address Sep 22, 2020
@eileenmcnaughton eileenmcnaughton force-pushed the del_primary branch 4 times, most recently from e5fd7bf to 98facda Compare September 22, 2020 23:56
@eileenmcnaughton eileenmcnaughton force-pushed the del_primary branch 2 times, most recently from 1e9382e to 9654a75 Compare September 23, 2020 00:50
@eileenmcnaughton
Copy link
Contributor Author

@JKingsnorth @pfigel any chance one of you could review this?

@pfigel
Copy link
Contributor

pfigel commented Sep 23, 2020

@eileenmcnaughton a bit low on bandwidth for the next 2 weeks or so, can review after if no one's gotten to it

@eileenmcnaughton
Copy link
Contributor Author

thanks @pfigel

@eileenmcnaughton
Copy link
Contributor Author

@jitendrapurohit (cc @petednz) could I get you to review this & the other dedupe PR?

@eileenmcnaughton
Copy link
Contributor Author

@pfigel no one else jumped in :-( Do you have a chance to look ?

@jitendrapurohit
Copy link
Contributor

can look at this today.

@jitendrapurohit
Copy link
Contributor

@eileenmcnaughton Have tested the following usecase

Contact A: (Duplicate)
Fname = Jitendra
Lname = Purohit
Email = abc@example.com
Home Address (Primary), Billing Address

Contact B:
Fname = Jitendra
Lname = Purohit
Email = abc@example.com

Used the email only rule to merge the above contacts A => B and selected the home address to move to contact B.

Before the patch:

After merge, Contact A is deleted (correct), Contact A is only left with no primary address. Billing has is_primary = 0.

After the patch

Same behaviour. Billing address is on contact A but is_primary is not updated on this address. Contact A is left with no primary address.

Also get the below e-notice after the merge.

Notice: Undefined offset: 0 in CRM_Dedupe_MergeHandler->getBlocksToUpdateForDeletedContact() (line 239 of /Users/jitendra/src/civicrm/CRM/Dedupe/MergeHandler.php).

public function getBlocksToUpdateForDeletedContact($entity) {
$movedBlocks = $this->getLocationBlocksToMerge()[$entity];
$deletedContactsBlocks = $this->getLocationBlocksForContactToRemove()[$entity];
$unMovedBlocks = array_diff_key($deletedContactsBlocks, $movedBlocks);
Copy link
Contributor

Choose a reason for hiding this comment

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

If this line is updated to

$unMovedBlocks = array_values(array_diff_key($deletedContactsBlocks, $movedBlocks));

it passes the above testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @jitendrapurohit - I've just made that change - if it still passes tests then the flow I tested will still be fine

…thout a primary address

This was picked up in tests to ensure that removing a line of code creating excessing queries
would not cause regressions.

https://lab.civicrm.org/dev/core/-/issues/2039

I considered altering the test to exclude deleted contacts but we run the risk that if a contact
is undeleted they will have no primary address so I figured the integrity makes sense.

Note there are a couple of queries in this code that can go (retrieving stuff
we already have) - depending how I go on getting review on this & related tidy up I'll remove them
in a later PR
Copy link
Contributor

@jitendrapurohit jitendrapurohit left a comment

Choose a reason for hiding this comment

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

think it is good to merge now. Confirmed that the above issue is fixed. Thanks @eileenmcnaughton

@eileenmcnaughton
Copy link
Contributor Author

thanks @jitendrapurohit !

@monishdeb
Copy link
Member

I have tested the use-case again mentioned by @jitendrapurohit and on multiple duplicate contacts. Looks good to me. Merging now.

@monishdeb monishdeb merged commit 19cd186 into civicrm:master Oct 12, 2020
@eileenmcnaughton eileenmcnaughton deleted the del_primary branch October 12, 2020 22:08
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.

4 participants