Skip to content

Commit

Permalink
Transmogrify Terms and Conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Feb 3, 2026
1 parent 71ae2b0 commit b46d66a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
23 changes: 23 additions & 0 deletions app/plugins/Transmogrify/config/schema/tables.json
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,19 @@
},
"addChangelog": true
},
"terms_and_conditions": {
"source": "cm_co_terms_and_conditions",
"displayField": "description",
"booleans": [
"agree_to_updates"
],
"cache": ["co_id", "cou_id"],
"fieldMap": {
"mostly_static_page_id": "&mapToStaticPage",
"co_terms_and_conditions_id": "terms_and_conditions_id",
"body": null
}
},
"__NOTES__": "DATA MIGRATIONS",
"authentication_events": {
"source": "cm_authentication_events",
Expand All @@ -404,6 +417,16 @@
"status": "&mapPersonStatus"
}
},
"t_and_c_agreements": {
"source": "cm_co_t_and_c_agreements",
"displayField": "id",
"cache": ["co_person_id"],
"fieldMap": {
"co_terms_and_conditions_id": "terms_and_conditions_id",
"co_person_id": "person_id"
},
"addChangelog": true
},
"authenticator_statuses": {
"source": "cm_authenticator_statuses",
"displayField": "id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public function execute(Arguments $args, ConsoleIo $io): int
if ($replyBool === true) {
$this->cmdPrinter->info(sprintf('Skipping transmogrification for table %s as requested.', $t));

// We need to skip the petiton metadata migration if the petitions table is not present
// We need to skip the petition metadata migration if the petitions table is not present
if ($t === 'petitions') {
unset($this->tables['historic_petition_metadata_records']);
unset($this->tables['historic_petition_attributes']);
Expand Down
87 changes: 87 additions & 0 deletions app/plugins/Transmogrify/src/Lib/Traits/ManageDefaultsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

namespace Transmogrify\Lib\Traits;

use App\Lib\Enum\PageContextEnum;
use App\Lib\Enum\PermittedTelephoneNumberFieldsEnum;
use App\Lib\Enum\SuspendableStatusEnum;
use App\Lib\Util\PaginatedSqlIterator;
use Cake\ORM\TableRegistry;

Expand Down Expand Up @@ -187,6 +189,91 @@ protected function mapToDefaultTelephoneNumberTypeId(array $row): ?int
);
}

/**
* Create a Mostly Static Page for Terms & Conditions content.
*
* If the incoming row has a non-empty `body`, create a MostlyStaticPage
* using the row's `co_id`, `body`, and `description`, and return the new
* page's ID. If `body` is empty or not set, return null.
*
* @param array $row
* @return int|null
*/
protected function mapToStaticPage(array $row): ?int
{
if (empty($row['body']) || trim((string)$row['body']) === '') {
return null;
}

if (empty($row['co_id'])) {
// Without a CO, we can't create a Mostly Static Page
return null;
}

$MostlyStaticPages = TableRegistry::getTableLocator()->get('MostlyStaticPages');

// Use a stable identifier for the T&C across changelog rows
$tcId = isset($row['co_terms_and_conditions_id'])
? (int)$row['co_terms_and_conditions_id']
: (int)$row['id'];

// Slug: only lowercase alnum + dashes, unique per CO + logical T&C
$name = sprintf(
'terms-and-conditions-%d-%d',
(int)$row['co_id'],
$tcId
);

// If we've already created this page for this T&C, reuse it
$existing = $MostlyStaticPages->find()
->where([
'co_id' => (int)$row['co_id'],
'name' => $name,
])
->first();

$isCurrentVersion = empty($row['co_terms_and_conditions_id']);

if ($existing) {
if ($isCurrentVersion) {
// Current row: update the existing static page with the latest data
$existing = $MostlyStaticPages->patchEntity($existing, [
'title' => $row['description'] ?? 'Terms and Conditions',
'description' => $row['description'] ?? null,
'status' => SuspendableStatusEnum::Active,
'context' => PageContextEnum::TermsAndConditions,
'body' => $row['body']
]);
$saved = $MostlyStaticPages->saveOrFail($existing, ['archive' => false]);

return $saved->id ?? $existing->id;
}

// Historical row: just reuse the current page
return $existing->id;
}

// No page yet: create one from this row (could be historical or current;
// current will later patch it when its row is processed)
$data = [
'co_id' => (int)$row['co_id'],
'name' => $name,
'title' => $row['description'] ?? 'Terms and Conditions',
'description' => $row['description'] ?? null,
'status' => SuspendableStatusEnum::Active,
'context' => PageContextEnum::TermsAndConditions,
'body' => $row['body'],
];
$this->populateChangelogDefaults('mostly_static_pages', $data, true);

$entity = $MostlyStaticPages->newEntity($data);

$saved = $MostlyStaticPages->saveOrFail($entity, ['archive' => false]);

return $saved->id ?? null;
}


/**
* Insert default CO Settings for COs that don't have settings.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ private function performFunctionMapping(array &$row, string $oldname, string $fu
'mapAffiliationType',
'mapHistoricPetitionViewerId',
'mapCoIdFromApiUserId',
'mapToStaticPage',
];

// The pipelines table allows the identifier and email types to be null
Expand Down

0 comments on commit b46d66a

Please sign in to comment.