Skip to content

Commit

Permalink
VACMS-16888: Makes a drupal command and adds to tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
omahane committed Feb 28, 2025
1 parent 5aa3e5e commit 93ce056
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 131 deletions.

This file was deleted.

85 changes: 74 additions & 11 deletions docroot/modules/custom/va_gov_migrate/src/Commands/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Drupal\va_gov_notifications\Service\NotificationsManager;
use Drupal\va_gov_workflow\Service\Flagger;
use Drush\Commands\DrushCommands;
use League\Csv\Reader;
use League\Csv\Statement;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

Expand Down Expand Up @@ -87,7 +89,7 @@ public function __construct(
Flagger $flaggerservice,
DataFetcherPluginManager $data_fetcher_plugin_manager,
LoggerInterface $migrate_channel_logger,
NotificationsManager $notifications_manager
NotificationsManager $notifications_manager,
) {
parent::__construct();
$this->database = $data_base;
Expand Down Expand Up @@ -138,6 +140,54 @@ public function cleanRevs() {
$this->logger->warning('The following revisions were not deleted: ' . implode(', ', $missed_vids));
}

/**
* Archive IntranetOnly forms in the CMS.
*
* @command va_gov_migrate:archive-intranet-only-forms
* @aliases va-gov-archive-intranet-only-forms
*/
public function archiveIntranetOnlyForms() {
$csv = Reader::createFromPath(DRUPAL_ROOT . '/sites/default/files/migrate_source/va_forms_data.csv', 'r');
$csv->setHeaderOffset(0);
$csv->setEnclosure('"');
$csv->setDelimiter(',');

// Create a Statement.
$stmt = (new Statement())
->select('rowid')
// Start from first record.
->offset(0)
->limit(-1)
->andWhere('IntranetOnly', '=', '1')
// Get all records.
->orderByAsc('rowid');

// Process the CSV with the statement and filter for IntranetOnly = 1.
$records = $stmt->process($csv);

$intranet_only = [];
foreach ($records as $record) {
// Make an array of the rowids.
$intranet_only[] = $record['rowid'];
}
// Get all the non-archived forms in the CMS that are IntranetOnly.
$select = $this->database->select('node__field_va_form_row_id', 'nfvfri');
$select->join('content_moderation_state_field_data', 'cmsfd', 'nfvfri.entity_id = cmsfd.content_entity_id');
$select->fields('nfvfri', ['entity_id'])
->condition('nfvfri.field_va_form_row_id_value', $intranet_only, 'IN')
->condition('cmsfd.moderation_state', 'archived', '<>');
$nids = $select->execute()->fetchCol();

$forms_to_archive = $this->entityTypeManager->getStorage('node')->loadMultiple(array_values($nids));
$message = 'Archived due to being set IntranetOnly in Forms CSV.';

// Archive the nodes.
foreach ($forms_to_archive as $form_to_archive) {
$this->archiveNode($form_to_archive, $message);
}

}

/**
* Flag any facilities that no longer exist in Facilty API.
*
Expand Down Expand Up @@ -210,18 +260,31 @@ public function flagMissingFacilities() {
*/
protected function archiveRemovedFacility(NodeInterface $facility) {
$this->clearStatusData($facility);
$facility->set('moderation_state', 'archived');
$facility->setRevisionLogMessage('Archived due to removal from Facility API.');
$facility->setNewRevision(TRUE);
$facility->setUnpublished();
$message = 'Archived due to removal from Facility API.';
$this->archiveNode($facility, $message);
}

/**
* Archive a node.
*
* @param \Drupal\node\NodeInterface $node
* The node to archive.
* @param string $message
* The revision message.
*/
protected function archiveNode(NodeInterface $node, string $message) {
$node->set('moderation_state', 'archived');
$node->setRevisionLogMessage($message);
$node->setNewRevision(TRUE);
$node->setUnpublished();
// Assign to CMS Migrator user.
$facility->setRevisionUserId(1317);
$node->setRevisionUserId(1317);
// Prevents some other actions.
$facility->setSyncing(TRUE);
$facility->setChangedTime(time());
$facility->isDefaultRevision(TRUE);
$facility->setRevisionCreationTime(time());
$facility->save();
$node->setSyncing(TRUE);
$node->setChangedTime(time());
$node->isDefaultRevision(TRUE);
$node->setRevisionCreationTime(time());
$node->save();
}

/**
Expand Down
1 change: 1 addition & 0 deletions tasks-periodic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ tasks:
- drush $DRUSH_ALIAS scr scripts/tasks/VACMS-10735-Forms-CSV-migration-validation.php
- drush $DRUSH_ALIAS migrate:reset-status va_node_form
- drush $DRUSH_ALIAS migrate:import va_node_form
- drush $DRUSH_ALIAS va_gov_migrate:archive-intranet-only-forms

va/background/daily/update/taxonomy_entity_index:
desc: Update the taxonomy_entity_index nightly.
Expand Down

0 comments on commit 93ce056

Please sign in to comment.