Skip to content

Commit

Permalink
VACMS-15559 Add all migrated fields to migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
swirtSJW committed Jan 31, 2024
1 parent 5009f9a commit 55941c3
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 62 deletions.
163 changes: 106 additions & 57 deletions docroot/modules/custom/va_gov_vamc/src/ServiceLocationMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace Drupal\va_gov_vamc;

use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
use Psr\Log\LogLevel;

/**
Expand All @@ -19,16 +16,16 @@ class ServiceLocationMigration {
/**
* The current facility service node being processed.
*
* @var \Drupal\paragraphs\ParagraphInterface
* @var \Drupal\node\NodeInterface
*/
public $facilityService;
protected $facilityService;

/**
* The current service_location paragraph being processed.
*
* @var \Drupal\node\NodeInterface
* @var \Drupal\paragraphs\ParagraphInterface
*/
public $serviceLocation;
protected $serviceLocation;

/**
* Constructor for ServiceLocationMigration.
Expand All @@ -43,7 +40,6 @@ public function __construct(array &$sandbox) {
$node_storage = get_node_storage();
$nids = array_slice($sandbox['items_to_process'], 0, $batch_size, TRUE);
$facility_services = (empty($nids)) ? [] : $node_storage->loadMultiple(array_values($nids));

foreach ($facility_services as $nid => $facility_service_node) {
/** @var \Drupal\node\NodeInterface $facility_service_node */
$this->facilityService = $facility_service_node;
Expand All @@ -55,13 +51,18 @@ public function __construct(array &$sandbox) {
$new_service_location = Paragraph::create(['type' => 'field_service_location']);
$facility_service_node->field_service_location->appendItem($new_service_location);
$service_locations = [$new_service_location];
$this->facilityService->field_service_location[] = $new_service_location;
$sandbox['service_locations_created_count'] = (isset($sandbox['service_locations_created_count'])) ? ++$sandbox['service_locations_created_count'] : 1;
}
else {
$sandbox['service_locations_updated_count'] = (isset($sandbox['service_locations_updated_count'])) ? ++$sandbox['service_locations_updated_count'] : 1;
}

$this->migrateServicesLocationsFromFacility($service_locations);
$message = 'Automated move of Facility Health Service data into Service Locations.';
// Must grab this before save, because after save it will always be true.
$is_latest_revision = $this->facilityService->isLatestRevision();
save_node_revision($facility_service_node, $message, TRUE);
save_node_revision($this->facilityService, $message, TRUE);

if (!$is_latest_revision) {
// The Facility service we have is the default revision, but if it is
Expand All @@ -74,99 +75,147 @@ public function __construct(array &$sandbox) {
}

unset($sandbox['items_to_process'][_va_gov_stringifynid($nid)]);

$processed_nids .= $nid . ', ';
$sandbox['current']++;
}
// Log the processed nodes.
Drupal::logger('va_gov_db')
\Drupal::logger('va_gov_vamc')
->log(LogLevel::INFO, 'Facility Health Service nodes %current nodes saved to migrate some data into paragraphs. %forward_revisions were also updated. Nodes processed: %nids', [
'%current' => $sandbox['current'],
'%nids' => $processed_nids,
'%forward_revisions' => $sandbox['forward_revisions_count'],
]);
}

protected function migrateServicesLocationsFromFacility(array $service_locations) {
/**
* Migrate into each of the service location fields.
*
* @param array $service_locations
* An array of service locations to migrate into.
*/
protected function migrateServicesLocationsFromFacility(array $service_locations): void {
foreach ($service_locations as $service_location) {
/** @var \Drupal\paragraphs\ParagraphInterface $service_location */
$this->serviceLocation = $service_location;
$this->migrateAddress();
$this->migrateAppointmentIntoText();
$this->migrateAppointmentPhoneNumber();
$this->migrateContactInfo();
$this->migrateHours();
$this->migrateAppointmentIntroType();
$this->migrateAppointmentIntroText();
$this->migrateAppointmentUseFacilityPhone();
$this->migrateAppointmentPhoneNumbers();
$this->migrateScheduleOnline();
$this->migrateWalkinsAccepted();
// Save the paragraph as a new revision.
$service_location->setNewRevision(TRUE);
// @todo I think I need to grab a new revision id for the node save.
// put this into the node field'target_revision_id' => $service_location->getRevisionId(),
// $service_location->set('field_fieldname1', 'some value');
//PREVENT THIS FROM SAVING TEMP
exit('BOOGA');
$service_location->save();
$this->serviceLocation->setNewRevision(TRUE);
}
}

protected function migrateAddress() {
// Moving from:
// Moving to: service_location -> field_service_location_address.

// @todo Double check this, I think this does not need to migrate.

/**
* Move appointment type from the service node to the service location.
*/
protected function migrateAppointmentIntroType(): void {
// Moving from: field_hservice_appt_intro_select.
// Moving to: paragraph.service_location.field_appt_intro_text_type.
$intro_type = $this->facilityService->get('field_hservice_appt_intro_select')->value;
$type_map = [
'default_intro_text' => 'use_default_text',
'custom_intro_text' => 'customize_text',
'no_intro_text' => 'remove_text',
'default' => 'use_default_text',
];
$new_value = script_libary_map_to_value($intro_type, $type_map);
$this->serviceLocation->set('field_appt_intro_text_type', $new_value);
}

protected function migrateAppointmentIntoText() {
// Moving from:
/**
* Move the appointment text from the service node to the service location.
*/
protected function migrateAppointmentIntroText(): void {
// Moving from: field_hservice_appt_leadin
// Moving to: paragraph.service_location.field_appointment_intro_text
// Needs a bifurcation for VAMC vs VBA.
$intro_text = $this->facilityService->get('field_hservice_appt_leadin')->value;
$this->serviceLocation->set('field_appt_intro_text_custom', $intro_text);
}

protected function migrateAppointmentPhoneNumber() {
// Moving from: field_phone_numbers_paragraph ??
// Moving to: field_phone (paragraphs)
// and: field_use_main_facility_phone (boolean)
}

protected function migrateContactInfo() {
// Moving from:
// Moving to: service_location ->field_email_contacts.
/**
* Populates the service location based on the presence of apt phone.
*/
protected function migrateAppointmentUseFacilityPhone(): void {
// Based on the presence of data in field_phone_numbers_paragraph.
// To field_use_facility_phone_number.
$has_no_apt_phone = $this->facilityService->get('field_phone_numbers_paragraph')->isEmpty();
// The assumption is that if there is no existing phone, then it should
// be the main number by default. There is a risk in that they wanted
// the main number and to provide a second, but there is nothing to
// base that decision on to make it cleaner.
$use_main_facility_phone = ($has_no_apt_phone) ? 1 : 0;
$this->serviceLocation->set('field_use_facility_phone_number', $use_main_facility_phone);
}

protected function migrateHours() {
// Moving from:
// Moving to: service_location ->field_office_hours (Office hours field).
// and: service_location ->field_hours (list).
//$this->serviceLocation->set('field_office_hours') = $this->facilityService->get('field_office_hours');
/**
* Move the apt phone numbers properties.
*/
protected function migrateAppointmentPhoneNumbers(): void {
// Moving from: field_phone_numbers_paragraph.
// Moving to: field_other_phone_numbers (paragraphs).
$phone_paragraphs = $this->facilityService->get('field_phone_numbers_paragraph')->referencedEntities();

foreach ($phone_paragraphs as $phone) {
// Add each one to the service location.
// Need to move Type, Phone number, Extension number, and Label.
$data = [
'type' => 'phone_number',
'field_phone_number_type' => $phone->get('field_phone_number_type')->value,
'field_phone_number' => $phone->get('field_phone_number')->value,
'field_phone_extension' => $phone->get('field_phone_extension')->value,
'field_phone_label' => $phone->get('field_phone_label')->value,
];
$new_phone = Paragraph::create($data);
$this->serviceLocation->field_other_phone_numbers[] = $new_phone;
}
}

protected function migrateScheduleOnline() {
/**
* Moves the online scheduling from Service to Service location.
*/
protected function migrateScheduleOnline(): void {
// Moving from: field_online_scheduling_availabl.
// Moving to:
// Moving to: field_online_scheduling_avail.
$schedule_online_map = [
// Schedule online => service location schedule online.
'yes' => 'yes',
'no' => 'no',
// 'Yes' => 'Yes'.
'1' => 'yes',
// 'No' => 'No'.
'0' => 'no',
// This is the do no harm, option defaulting to most restrictive.
// 'unspecified' => '??? @todo is this needed',
// 'unspecified' => 'No'.
'not_applicable' => 'no',
'default' => 'no',
];
$lookup = $this->facilityService->get('field_online_scheduling_availabl')->value;
$new_value = script_libary_map_to_value($lookup, $schedule_online_map);
$this->serviceLocation->set('field_online_scheduling_avail', $new_value);
}

protected function migrateWalkinsAccepted() {
/**
* Move and map the walkins accepted to office visits.
*/
protected function migrateWalkinsAccepted(): void {
// Moving from: node field_walk_ins_accepted.
// Moving to:
// Moving to: field_office_visits.
$walkins_accepted_map = [
// Walkins accepted => office visits.
'yes' => 'yes with or without an appointment',
'no' => 'yes by appointment only',
// 'No' => 'yes by appointment only'.
'0' => 'no',
// 'Yes' => 'Yes, with or without an appointment'.
'1' => 'yes_with_or_without_appointment',
'' => '',
// This is the do no harm, option defaulting to most restrictive.
'unspecified' => 'yes by appointment only',
// 'unspecified' => 'yes by appointment only'.
'not_applicable' => 'yes_appointment_only',
'default' => 'yes_appointment_only',
];
$lookup = $this->facilityService->get('field_walk_ins_accepted')->value;
$new_value = script_libary_map_to_value($lookup, $walkins_accepted_map);
$this->serviceLocation->set('field_office_visits', $new_value);
}

}
14 changes: 9 additions & 5 deletions docroot/modules/custom/va_gov_vamc/va_gov_vamc.deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

use Drupal\va_gov_vamc\ServiceLocationMigration;

require_once __DIR__ . '/../../../../scripts/content/script-library.php';

/**
* Build top VA Police page and menu items for each system.
*/
Expand All @@ -22,11 +24,13 @@ function va_gov_vamc_deploy_build_va_police_9001(&$sandbox) {
* Migrate some facility service data to service location paragraphs.
*/
function va_gov_vamc_deploy_migrate_service_data_to_service_location_9002(&$sandbox) {
\Drupal::moduleHandler()->loadInclude('va_gov_vamc', 'install');

$source_bundle = 'health_care_local_health_service';
script_library_sandbox_init($sandbox, 'get_nids_of_type', [$source_bundle, FALSE]);
$migration = new ServiceLocationMigration($sandbox);
$new_service_locations = $migration->getCreatedServiceLocations();
$updated_service_locations = $migration->getUpdatedServiceLocations();
return script_library_sandbox_complete($sandbox, "migrated @total {$source_bundle} nodes into {$new_service_locations} new service_location paragraphs, and {$updated_service_locations} updated .");
new ServiceLocationMigration($sandbox);
$new_service_locations = $sandbox['service_locations_created_count'] ?? 0;
$updated_service_locations = $sandbox['service_locations_updated_count'] ?? 0;
$forward_revisions = $sandbox['forward_revisions_count'] ?? 0;

return script_library_sandbox_complete($sandbox, "migrated @total {$source_bundle} nodes into {$new_service_locations} new service_location paragraphs, and {$updated_service_locations} updated. Also updated {$forward_revisions} forward revisions.");
}

0 comments on commit 55941c3

Please sign in to comment.