Skip to content

DRY inbound date of birth handler #348

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,13 @@ protected function resultsToEntityData(
'valid_through' => $result->valid_through
];

if(!empty($result->date_of_birth) && empty($eidata['date_of_birth'])) {
if(empty($eidata['date_of_birth'])) {
// We take the first DoB we see. Multiple rows should have the same
// DoB, if they don't that's a problem in the data that needs to be fixed.

// We have to convert the DateTime back to a string
$eidata['date_of_birth'] = $result->date_of_birth->format('Y-m-d');
$dob = StringUtilities::handleInboundDateOfBirth($result->date_of_birth);
if (!empty($dob)) {
$eidata['date_of_birth'] = $dob;
}
}

// MVEAs that have a foreign key to EIR get attached to the EIR
Expand Down
34 changes: 34 additions & 0 deletions app/src/Lib/Util/StringUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,40 @@ public static function foreignKeyToController(string $s): string {
return Inflector::underscore(Inflector::pluralize(substr($s, 0, strlen($s)-3)));
}

/**
* Handles date of birth conversion from various input formats.
*
* @param string|DateTimeImmutable $dateOfBirth Date of birth as string or DateTimeImmutable
* @return \string|null Date of birth as string or null if invalid/empty
* @since COmanage Registry v5.2.0
*/
public static function handleInboundDateOfBirth(mixed $dateOfBirth): ?string
{
if (empty($dateOfBirth)) {
return null;
}

if ($dateOfBirth instanceof \DateTimeImmutable) {
return $dateOfBirth->format('Y-m-d');
}

if (is_string($dateOfBirth)) {
$dob = \DateTimeImmutable::createFromFormat('Y-m-d', $dateOfBirth);
if ($dob instanceof \DateTimeImmutable) {
return $dob->format('Y-m-d');
}

$dob = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $dateOfBirth);
if ($dob instanceof \DateTimeImmutable) {
return $dob->format('Y-m-d');
}

return null;
}

return null;
}

/**
* Localize a controller name, accounting for plugins.
*
Expand Down
11 changes: 3 additions & 8 deletions app/src/Model/Table/PeopleTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace App\Model\Table;

use App\Lib\Util\StringUtilities;
use App\Model\Entity\Person;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
Expand Down Expand Up @@ -777,14 +778,8 @@ public function saveAttributeCollectorPetitionAttributes(int $personId, array $f
foreach ($fields as $field) {
$attribute = $field->enrollment_attribute->attribute;
$value = $field->value;
if($attribute === 'date_of_birth' && is_string($value)) {
// While we're here, make sure it's in YYYY-MM-DD format. This should fail
// if the inbound attribute is invalid.
$dob = \DateTimeImmutable::createFromFormat('Y-m-d', $value);

if($dob) {
$value = $dob->format('Y-m-d');
}
if($attribute === 'date_of_birth') {
$value = StringUtilities::handleInboundDateOfBirth($value);
}
$person->$attribute = $value;
}
Expand Down
11 changes: 3 additions & 8 deletions app/src/Model/Table/PipelinesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,15 +898,10 @@ protected function mapAttributesToCO(
// Make sure source_key is a string
'source_key' => (string)$eisAttributes['source_key']
];

if(!empty($eisAttributes['date_of_birth'])) {
// While we're here, make sure it's in YYYY-MM-DD format. This should fail
// if the inbound attribute is invalid.
$dob = \DateTimeImmutable::createFromFormat('Y-m-d', $eisAttributes['date_of_birth']);

if($dob) {
$ret['date_of_birth'] = $dob->format('Y-m-d');
}
$dob = StringUtilities::handleInboundDateOfBirth($eisAttributes['date_of_birth']);
if($dob) {
$ret['date_of_birth'] = $dob;
}

foreach([
Expand Down