From 593eef094e55f544cd5a44a32c72c79d3a27b891 Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Thu, 13 Feb 2025 17:14:30 +0200 Subject: [PATCH] Fix how we handle the primary name save.Bubble up save attribute exception to the controller. --- .../Model/Table/AttributeCollectorsTable.php | 2 +- app/src/Controller/PetitionsController.php | 14 +++++++- app/src/Model/Table/NamesTable.php | 34 ++----------------- app/src/Model/Table/PetitionsTable.php | 7 ++-- 4 files changed, 21 insertions(+), 36 deletions(-) diff --git a/app/plugins/CoreEnroller/src/Model/Table/AttributeCollectorsTable.php b/app/plugins/CoreEnroller/src/Model/Table/AttributeCollectorsTable.php index 29a954d80..942080db9 100644 --- a/app/plugins/CoreEnroller/src/Model/Table/AttributeCollectorsTable.php +++ b/app/plugins/CoreEnroller/src/Model/Table/AttributeCollectorsTable.php @@ -33,7 +33,6 @@ use App\Lib\Enum\StatusEnum; use App\Model\Entity\Person; use App\Model\Entity\PersonRole; -use App\Model\Entity\Petition; use Cake\Collection\Collection; use Cake\Database\Connection; use Cake\Database\Expression\QueryExpression; @@ -47,6 +46,7 @@ class AttributeCollectorsTable extends Table { use \App\Lib\Traits\AutoViewVarsTrait; use \App\Lib\Traits\CoLinkTrait; + use \App\Lib\Traits\LabeledLogTrait; use \App\Lib\Traits\LayoutTrait; use \App\Lib\Traits\PermissionsTrait; use \App\Lib\Traits\PrimaryLinkTrait; diff --git a/app/src/Controller/PetitionsController.php b/app/src/Controller/PetitionsController.php index 9ebb4c4f7..c0fe233a7 100644 --- a/app/src/Controller/PetitionsController.php +++ b/app/src/Controller/PetitionsController.php @@ -152,7 +152,19 @@ public function finalize(string $id) { try { if($op == 'finalize') { // Step 1 - $this->Petitions->finalizePlugins((int)$id); + try { + $this->Petitions->finalizePlugins((int)$id); + } catch (\Exception $e) { + $this->Flash->error(__d('error', 'Petitions.attributes.save.failed')); + // Get me back to the resume page + $resumeUrl = [ + 'plugin' => null, + 'controller' => 'petitions', + 'action' => 'resume', + (int)$id + ]; + return $this->redirect($resumeUrl); + } // Next operation is assign $baseUrl['?']['op'] = 'assign'; diff --git a/app/src/Model/Table/NamesTable.php b/app/src/Model/Table/NamesTable.php index 326598398..7ea1418f1 100644 --- a/app/src/Model/Table/NamesTable.php +++ b/app/src/Model/Table/NamesTable.php @@ -253,33 +253,6 @@ public function primaryName(int $id, string $recordType='person') { } } - /** - * Check if the Person has a primary name - * - * @param int $id Record ID - * @param string $recordType Type of record to find primary name for, 'person' or 'external_identity' - * @return bool Name Entity - * @since COmanage Registry v5.1.0 - */ - - public function hasPrimaryName(int $id, string $recordType='person'): bool - { - if($recordType == 'person') { - // Return the Primary Name - - return $this->find() - ->where(['person_id' => $id, - 'primary_name' => true]) - ->count() > 0; - } else { - // Return the first name, whatever it is - - return $this->find() - ->where(['external_identity_id' => $id]) - ->count() > 0; - } - } - /** * Application Rule to determine if there is at least one Name associated * with the Person. @@ -478,13 +451,10 @@ public function saveAttributes(int $personId, ?int $roleId, string $parentModel, { $name = [ 'person_id' => $personId, + // Set this to true and delegate the confirmation to the localAfterSave callback + 'primary_name' => true ]; - // Save the primary name - if(!$this->hasPrimaryName($personId)) { - $name['primary_name'] = true; - } - foreach($fields as $fld) { $name[$fld->column_name] = $fld->value; $name['type_id'] = $fld->enrollment_attribute->attribute_type; diff --git a/app/src/Model/Table/PetitionsTable.php b/app/src/Model/Table/PetitionsTable.php index 9540f2df7..f452b938d 100644 --- a/app/src/Model/Table/PetitionsTable.php +++ b/app/src/Model/Table/PetitionsTable.php @@ -298,9 +298,10 @@ public function finalize(int $id) { /** * Perform Plugin finalization for a Petition. - * + * + * @param int $id Petition ID + * @throws \Exception * @since COmanage Registry v5.1.0 - * @param int $id Petition ID */ public function finalizePlugins(int $id) { @@ -377,6 +378,8 @@ public function finalizePlugins(int $id) { } catch(\Exception $e) { $this->llog('error', "Plugin " . $step->plugin . " error during finalization of petition " . $petition->id . ": " . $e->getMessage()); + // Rethrow to the controller + throw new \Exception($e->getMessage()); } } }