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

Allow to disable an invalid relationship (eg. contact subtype was changed so no longer valid) #25647

Merged
merged 4 commits into from
Apr 4, 2023

Conversation

mattwire
Copy link
Contributor

Overview

If you use (and change) contact sub-types it is easy to end up with invalid relationships that you can't do anything with.
It should be possible to disable these relationships without having to modify them (eg. a contact who had "Staff" subtype is changed to "Ex-Staff" subtype).
The relationship is now invalid according to CiviCRM but it is still correct from a historical data perspective. This also affects the "update expired relationships" scheduled job which can get stuck if it finds and invalid relationship.

Before

Invalid relationships can't be disabled..

After

Invalid relationships can be disabled.

Technical Details

Explained above. It would be nice to have a separate API call for disable so we don't have to have so much logic in ::create. But that would require changes to various parts of the API/UI etc. so out of scope for now.

Comments

@civibot
Copy link

civibot bot commented Feb 22, 2023

(Standard links)

@civibot civibot bot added the master label Feb 22, 2023
@mattwire mattwire added has-test and removed master labels Feb 22, 2023
@mattwire mattwire force-pushed the disableinvalidrelationship branch from 8ad0b2f to db855bd Compare February 22, 2023 21:37
@demeritcowboy
Copy link
Contributor

Fail seems related:

CRM_Contact_BAO_RelationshipTest::testDisableInvalidRelationship
Exception: CRM_Core_Exception: "'2' is not a valid option for field relationship_type_id"
#0 /home/jenkins/bknix-dfl/build/build-2/web/sites/all/modules/civicrm/tests/phpunit/CRM/Contact/BAO/RelationshipTest.php(369): civicrm_api3("Relationship", "create", (Array:3))

@mattwire
Copy link
Contributor Author

Hmm, passes locally!

@mattwire
Copy link
Contributor Author

test this please

@demeritcowboy
Copy link
Contributor

Hmm...

@mattwire mattwire force-pushed the disableinvalidrelationship branch from db855bd to 4c29834 Compare March 20, 2023 16:02
@mattwire mattwire force-pushed the disableinvalidrelationship branch from 4c29834 to 00c49d2 Compare March 20, 2023 16:08
@demeritcowboy
Copy link
Contributor

It's the same test fail.
I don't think it's a v3 vs v4 - when you run the test locally just the single test, it passes but by fluke, because there is a relationship_type_id 2 that happens to exist (Spouse), but when run as part of the whole file, the teardown() removes the stock relationships and they are never readded, so 2 no longer exists when it gets to this test.

Something is checking the relationship_type even when you're just trying to update is_active. Now, why it is checking "2" I haven't looked. Possibly this surfaces a different bug?

@mattwire
Copy link
Contributor Author

Possibly this surfaces a different bug?

Yep, it's looking for related memberships for an unrelated relationship that doesn't exist in the test environment...
I've added a check so we don't call that code if not required - which should help a little bit with performance and future refactoring of that giant ball of code.

@mattwire mattwire force-pushed the disableinvalidrelationship branch from 1759e72 to 798be50 Compare March 20, 2023 19:07
@mattwire mattwire force-pushed the disableinvalidrelationship branch from 798be50 to bafcad8 Compare March 20, 2023 19:20
@mattwire
Copy link
Contributor Author

Possibly this surfaces a different bug?

Or two...

if (!empty($params['id']) && array_key_exists('is_active', $params) && empty($params['is_active'])) {
$disableRelationship = TRUE;
}
if (empty($disableRelationship) && !CRM_Contact_BAO_Relationship::checkRelationshipType($params['contact_id_a'], $params['contact_id_b'], $params['relationship_type_id'])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Taking another look at this now but first noticing a non-blocking r-code comment:
The variable $disableRelationship can be undefined at line 63 and while php doesn't complain it just seems better to explicitly set it, and some fancy IDE's might nag about it.

-    if (!empty($params['id']) && array_key_exists('is_active', $params) && empty($params['is_active'])) {
-      $disableRelationship = TRUE;
-    }
+    $disableRelationship = !empty($params['id']) && array_key_exists('is_active', $params) && empty($params['is_active']);

Copy link
Member

Choose a reason for hiding this comment

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

@mattwire can you make this change?
@demeritcowboy if you use the suggestion feature then it can be merged in with a click :)

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll just merge and do a followup in a bit.

@demeritcowboy demeritcowboy added the merge ready PR will be merged after a few days if there are no objections label Mar 29, 2023
@demeritcowboy demeritcowboy merged commit 2f74e53 into civicrm:master Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has-test merge ready PR will be merged after a few days if there are no objections
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants