Eileen has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/328120 )

Change subject: Create & use location update function in the scenarios where we 
are updating.
......................................................................

Create & use location update function in the scenarios where we are updating.

The update function will create or update (it uses the replace api) and is 
slightly more expensive
as it does a look up. I have only switched to it in the one place where we have 
identified that we
should be updating rather than creating duplicate addresses due to this small 
penalty.

Bug: T152475
Change-Id: Ia21d771b94228267c54bed4fd0de73ef4fc99dcc
---
M sites/all/modules/queue2civicrm/recurring/RecurringQueueConsumer.php
M sites/all/modules/queue2civicrm/tests/phpunit/ProcessMessageTest.php
M sites/all/modules/wmf_civicrm/wmf_civicrm.module
3 files changed, 116 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/20/328120/1

diff --git 
a/sites/all/modules/queue2civicrm/recurring/RecurringQueueConsumer.php 
b/sites/all/modules/queue2civicrm/recurring/RecurringQueueConsumer.php
index 0d303a1..288ab7a 100644
--- a/sites/all/modules/queue2civicrm/recurring/RecurringQueueConsumer.php
+++ b/sites/all/modules/queue2civicrm/recurring/RecurringQueueConsumer.php
@@ -318,7 +318,7 @@
                        $contact = wmf_civicrm_message_contact_update( $msg, 
$recur_record->contact_id );
 
                        // Insert the location record
-                       wmf_civicrm_message_location_insert( $msg, $contact );
+                       wmf_civicrm_message_location_update( $msg, $contact );
 
                        // Tag contact for review
                        wmf_civicrm_tag_contact_for_review( $contact );
diff --git 
a/sites/all/modules/queue2civicrm/tests/phpunit/ProcessMessageTest.php 
b/sites/all/modules/queue2civicrm/tests/phpunit/ProcessMessageTest.php
index a0e48ae..5c23e3e 100644
--- a/sites/all/modules/queue2civicrm/tests/phpunit/ProcessMessageTest.php
+++ b/sites/all/modules/queue2civicrm/tests/phpunit/ProcessMessageTest.php
@@ -268,7 +268,15 @@
         $this->assertEquals( $contributions[0]['contact_id'], 
$contributions2[0]['contact_id'] );
         $addresses = $this->callAPISuccess('Address', 'get', 
array('contact_id' => $contributions2[0]['contact_id']));
         $this->assertEquals(1, $addresses['count']);
-        $this->assertEquals('5109 Lockwood Rd', 
$addresses['values'][$addresses['id']]['street_address']);
+        // The address comes from the recurring_payment.json not the 
recurring_signup.json as it
+        // has been overwritten. This is perhaps not a valid scenario in 
production but it is
+        // the scenario the code works to. In production they would probably 
always be the same.
+        $this->assertEquals('1211122 132 st', 
$addresses['values'][$addresses['id']]['street_address']);
+
+        $emails = $this->callAPISuccess('Email', 'get', array('contact_id' => 
$contributions2[0]['contact_id']));
+        $this->assertEquals(1, $addresses['count']);
+        $this->assertEquals('test...@wikimedia.org', 
$emails['values'][$emails['id']]['email']);
+
         db_delete('contribution_tracking')
         ->condition('id', $msg['custom'])
         ->execute();
diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
index 824e783..d86dde4 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
@@ -1366,6 +1366,63 @@
 }
 
 /**
+ * Update the location record
+ *
+ * Serves as a standard way for message processors to handle location
+ * updates.
+ *
+ * @param array $msg
+ * @param array $contact
+ */
+function wmf_civicrm_message_location_update($msg, $contact) {
+    wmf_civicrm_message_email_update($msg, $contact['id']);
+    wmf_civicrm_message_address_update($msg, $contact['id']);
+}
+
+/**
+ * Updates the email for a contact.
+ *
+ * If updating or unsure use the marginally slower update function.
+ *
+ * @param string $msg
+ * @param int $contact_id
+ * @throws \WmfException
+ */
+function wmf_civicrm_message_email_update($msg, $contact_id ) {
+  if (!wmf_civicrm_is_email_valid(CRM_Utils_Array::value('email', $msg))) {
+    return;
+  }
+
+  try {
+    civicrm_api3( "Email", "Replace", array(
+      'location_type_id' => $loc_type_id = 
wmf_civicrm_get_default_location_type_id(),
+      'contact_id' => $contact_id,
+      'values' => array(array(
+        'email' => $msg[ 'email' ],
+        'is_primary' => 1,
+        'is_billing' => 1,
+      ),
+    )));
+  }
+  catch (CiviCRM_API3_Exception $e) {
+    throw new WmfException( 'IMPORT_CONTACT', "Couldn't store email for the 
contact.", array('result' => $e->getMessage()) );
+  }
+}
+
+/**
+ * We do not store empty emails or placeholder emails.
+ *
+ * @param string $email
+ * @return bool
+ */
+function wmf_civicrm_is_email_valid($email) {
+  if(empty($email) || $email === 'nob...@wikimedia.org' ){
+    return FALSE;
+  }
+  return TRUE;
+}
+
+/**
  * Updates the email for a contact.
  *
  * If updating or unsure use the marginally slower update function.
@@ -1375,9 +1432,7 @@
  * @throws \WmfException
  */
 function wmf_civicrm_message_email_insert($msg, $contact_id ) {
-    // unset the email address if the default is used
-    // this enables us to properly dedupe contacts later on
-    if( empty( $msg['email'] ) or $msg[ 'email' ] === 'nob...@wikimedia.org' ){
+    if (!wmf_civicrm_is_email_valid(CRM_Utils_Array::value('email', $msg))) {
         return;
     }
 
@@ -1396,6 +1451,50 @@
     if ( array_key_exists( 'is_error', $result ) && $result['is_error'] != 0 ) 
{
         throw new WmfException( 'IMPORT_CONTACT', "Couldn't store email for 
the contact.", array('result' => $result) );
     }
+}
+
+/**
+ * Insert a new address for a contact.
+ *
+ * If updating or unsure use the marginally slower update function.
+ *
+ * @param array $msg
+ * @param int $contact_id
+ *
+ * @throws \WmfException
+ */
+function wmf_civicrm_message_address_update($msg, $contact_id ) {
+
+  // CiviCRM does a DB lookup instead of checking the pseudoconstant.
+  // @todo fix Civi to use the pseudoconstant.
+  $country_id = wmf_civicrm_get_country_id( $msg[ 'country' ] );
+
+  $address_params = array(
+    'contact_id' => $contact_id,
+    'location_type_id' => wmf_civicrm_get_default_location_type_id(),
+    'values' => array(array(
+      'is_primary' => 1,
+      'street_address' => $msg['street_address'],
+      'supplemental_address_1' => $msg['supplemental_address_1'],
+      'city' => $msg['city'],
+      'postal_code' => $msg['postal_code'],
+      'country_id' => $country_id,
+      'country' => $msg['country' ],
+      'is_billing' => 1,
+    )),
+  );
+
+  if (!empty($msg['state_province'])) {
+    $address_params['values'][0]['state_province'] = $msg['state_province'];
+    $address_params['values'][0]['state_province_id'] = 
wmf_civicrm_get_state_id($country_id, $msg['state_province']);
+  }
+
+  try {
+    civicrm_api3('Address', 'replace', $address_params);
+  }
+  catch (CiviCRM_API3_Exception $e) {
+    throw new WmfException( 'IMPORT_CONTACT', "Couldn't store address for the 
contact." . $e->getMessage());
+  }
 }
 
 /**
@@ -1434,6 +1533,10 @@
     }
 
     // FIXME: api does not offer control over fixAddress flag
+    // UPDATE - the fixAddress function mostly backs out early based on params
+    // checks so as long as things like 'country_id' are set it doesn't seem 
very
+    // expensive (& those checks would be an easy fix). Bypassing the api
+    // probably does not gain us much.
     //$result = civicrm_api( "Address", "Create", $address_params );
     //if ( array_key_exists( 'is_error', $result ) && $result['is_error'] != 0 
) {
 

-- 
To view, visit https://gerrit.wikimedia.org/r/328120
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia21d771b94228267c54bed4fd0de73ef4fc99dcc
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Eileen <emcnaugh...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to