From adca269748238c9f0ca878c20eb3f27a32a0fed3 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 27 Jul 2022 12:17:30 +0100 Subject: [PATCH] dev/core#3749 Fix process pledges to throw exception when civiPledge disabled Per the issue - it's fine for the job to not work if disabled, but the error should be logical. updatePledgeStatus is only called from one place so made the return handling flatter for success, and exception on error --- CRM/Pledge/BAO/Pledge.php | 12 +++++------- api/v3/Job.php | 17 ++++++----------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/CRM/Pledge/BAO/Pledge.php b/CRM/Pledge/BAO/Pledge.php index 112a3eefe5..ead406b302 100644 --- a/CRM/Pledge/BAO/Pledge.php +++ b/CRM/Pledge/BAO/Pledge.php @@ -772,11 +772,10 @@ GROUP BY currency * @param array $params * * @return array - * @throws \API_Exception + * * @throws \CRM_Core_Exception - * @throws \CiviCRM_API3_Exception */ - public static function updatePledgeStatus($params): array { + public static function updatePledgeStatus(array $params): array { $returnMessages = []; @@ -875,7 +874,7 @@ SELECT pledge.contact_id as contact_id, ); if ($newStatus != $pledgeStatus[$pledgeId]) { $returnMessages[] = "- status updated to: {$allPledgeStatus[$newStatus]}"; - $updateCnt += 1; + ++$updateCnt; } } @@ -992,8 +991,7 @@ SELECT pledge.contact_id as contact_id, civicrm_api3('activity', 'create', $activityParams); } catch (CiviCRM_API3_Exception $e) { - $returnMessages[] = "Failed creating Activity for Pledge Reminder: " . $e->getMessage(); - return ['is_error' => 1, 'message' => $returnMessages]; + throw new CRM_Core_Exception('Failed creating Activity for Pledge Reminder: ' . $e->getMessage()); } $returnMessages[] = "Payment reminder sent to: {$pledgerName} - {$toEmail}"; } @@ -1005,7 +1003,7 @@ SELECT pledge.contact_id as contact_id, // end if ( $sendReminders ) $returnMessages[] = "{$updateCnt} records updated."; - return ['is_error' => 0, 'messages' => implode("\n\r", $returnMessages)]; + return $returnMessages; } /** diff --git a/api/v3/Job.php b/api/v3/Job.php index 4210e43a69..f4057444a4 100644 --- a/api/v3/Job.php +++ b/api/v3/Job.php @@ -306,19 +306,14 @@ function _civicrm_api3_job_update_greeting_spec(&$params) { * @param array $params * * @return array + * + * @throws \CRM_Core_Exception */ -function civicrm_api3_job_process_pledge($params) { - // *** Uncomment the next line if you want automated reminders to be sent - // $params['send_reminders'] = true; - $result = CRM_Pledge_BAO_Pledge::updatePledgeStatus($params); - - if ($result['is_error'] == 0) { - // experiment: detailed execution log is a result here - return civicrm_api3_create_success($result['messages']); - } - else { - return civicrm_api3_create_error($result['error_message']); +function civicrm_api3_job_process_pledge(array $params): array { + if (!CRM_Core_Component::isEnabled('CiviPledge')) { + throw new CRM_Core_Exception(ts('%1 is not enabled'), [1 => ['CiviPledge']]); } + return civicrm_api3_create_success(implode("\n\r", CRM_Pledge_BAO_Pledge::updatePledgeStatus($params))); } /** -- 2.25.1