Skip to content

Commit

Permalink
Fix how we handle the primary name save.Bubble up save attribute exce…
Browse files Browse the repository at this point in the history
…ption to the controller.
  • Loading branch information
Ioannis committed Feb 13, 2025
1 parent 70d2f2d commit d37f7ba
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
14 changes: 13 additions & 1 deletion app/src/Controller/PetitionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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($e->getMessage());
// 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';
Expand Down
34 changes: 2 additions & 32 deletions app/src/Model/Table/NamesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions app/src/Model/Table/PetitionsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -376,7 +377,9 @@ public function finalizePlugins(int $id) {
}
}
catch(\Exception $e) {
$this->llog('error', "Plugin " . $step->plugin . " error during finalization of petition " . $petition->id . ": " . $e->getMessage());
$msg = "Plugin " . $step->plugin . " error during finalization of petition " . $petition->id ;
$this->llog('error', $msg . ": " . $e->getMessage());
throw new \RuntimeException($msg);
}
}
}
Expand Down

0 comments on commit d37f7ba

Please sign in to comment.