Skip to content

CFM-324-PrimaryName-save-improvement #296

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
15 changes: 14 additions & 1 deletion app/src/Controller/PetitionsController.php
Expand Up @@ -149,10 +149,23 @@ public function finalize(string $id) {
$op = 'finalize';
}

$resumeUrl = [
'plugin' => null,
'controller' => 'petitions',
'action' => 'resume',
(int)$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
return $this->redirect($resumeUrl);
}

// Next operation is assign
$baseUrl['?']['op'] = 'assign';
Expand Down
36 changes: 3 additions & 33 deletions app/src/Model/Table/NamesTable.php
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 All @@ -494,4 +464,4 @@ public function saveAttributes(int $personId, ?int $roleId, string $parentModel,
$this->saveOrFail($this->newEntity($name));
return true;
}
}
}
9 changes: 6 additions & 3 deletions app/src/Model/Table/PetitionsTable.php
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