Skip to content

Commit

Permalink
Merge pull request #458 from City-of-Helsinki/develop
Browse files Browse the repository at this point in the history
Release 21.9.
  • Loading branch information
tvalimaa authored Sep 21, 2023
2 parents e24fbb9 + cfbe49a commit cd9bfa3
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 13 deletions.
52 changes: 52 additions & 0 deletions public/modules/custom/asu_api/asu_api.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Contains asu_api.module.
*/

use Drupal\asu_api\Api\BackendApi\Request\GetApartmentStatusRequest;
use Symfony\Component\HttpFoundation\Response;
use Drupal\asu_api\Api\BackendApi\Request\GetApartmentRevaluationsRequest;
use Drupal\node\Entity\Node;

Expand All @@ -15,6 +17,11 @@ use Drupal\node\Entity\Node;
* alteration_work_total values.
*/
function asu_api_cron() {
getApartmentRevaluationCron();
getApartmentStatusRequestCron();
}

function getApartmentRevaluationCron() {
$apartment_revaluation = NULL;

try {
Expand Down Expand Up @@ -55,5 +62,50 @@ function asu_api_cron() {
catch (\Exception $e) {
Drupal::logger('asu_cron')->critical('Unable to fetch revaluations');
}
}

/**
* Implements hook_cron().
*
* Update apartments state of sale sold status.
*/
function getApartmentStatusRequestCron() {
try {
/** @var \Drupal\asu_api\Api\BackendApi\BackendApi $api */
$api = \Drupal::service('asu_api.backendapi');
$request = new GetApartmentStatusRequest();
/** @var \Drupal\asu_api\Api\BackendApi\Response\GetApartmentStatusResponse $response */
$response = $api->send($request);
$apartment_states = $response->getContent();

if (empty($apartment_states)) {
return;
}
}
catch (\Exception $e) {
return new Response('problem with request.', 400);
}

// Get apartment uuids.
$apartment_uuids = array_keys($apartment_states);

// Get all aparments which state is not sold.
$query = \Drupal::entityTypeManager()->getStorage('node')->getQuery();
$query->condition('status', 1)
->condition('type', 'apartment')
->condition('uuid', $apartment_uuids, 'IN');
$apartments = $query->execute();

foreach ($apartments as $apartment) {
// Load node object by nid.
$node = Node::load($apartment);
// Node get uuid.
$uuid = $node->uuid();
// Get apartment status from $apartment_states.
$state = strtolower($apartment_states[$uuid]);
// Update node apartment state of sale to sold.
$node->field_apartment_state_of_sale->target_id = $state;
// Save node.
$node->save();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use Drupal\node\Entity\Node;
* Implements hook_cron().
*/
function asu_clear_cache_cron() {
$time_ago = strtotime('-5 minutes');
$time_ago = strtotime('-10 minutes');
$database = \Drupal::database();
$query = $database->select('node_field_data', 'n');
$query->condition('n.type', 'project');
Expand Down
60 changes: 48 additions & 12 deletions public/modules/custom/asu_migration/src/ProjectMigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public function __construct(
}

/**
* Run migrations.
* Get migration files from server tmp.
*/
public function migrate(): array {
private function getFilesFromTmp() {
if (!file_exists($this->projectFilePath)) {
return ['Project file is missing!'];
}
Expand All @@ -55,8 +55,22 @@ public function migrate(): array {
}

$this->file = fopen($this->projectFilePath, 'r');

$this->file2 = fopen($this->apartmentFilePath, 'r');
}

/**
* Get migration files from local.
*/
private function getFilesFromLocal() {
$this->file = fopen('/app/migrations/projects.csv', 'r');
$this->file2 = fopen('/app/migrations/apartments.csv', 'r');
}

/**
* Run migrations.
*/
public function migrate(): array {
getFilesFromTmp();

$errors = $this->migrateProjects();

Expand Down Expand Up @@ -152,10 +166,23 @@ private function migrateProjects(): array {
'author' => 1,
]);

$project->field_holding_type->entity = $holdingType;
$project->field_ownership_type->entity = $ownershipType;
$project->field_district->entity = $district;
$project->field_site_owner->entity = $siteOwner;
if (!$holdingType || !$ownershipType || !$siteOwner) {
$errors[] = "Error with the project $currentProjectId. Error in create term.";
break;
}
else {
$project->field_holding_type->entity = $holdingType;
$project->field_ownership_type->entity = $ownershipType;
$project->field_site_owner->entity = $siteOwner;
}

if (!$district) {
$errors[] = "Error with the project $currentProjectId. Error in create district term.";
continue;
}
else {
$project->field_district->entity = $district;
}

if ($project) {
if (feof($this->file2)) {
Expand Down Expand Up @@ -340,11 +367,20 @@ private function getTerm(array $terms, string $term, string $vid) {
return reset($terms);
}

$new_term = Term::create([
'name' => $term,
'vid' => $vid,
]);
$new_term->save();
if (preg_match('~[0-9]+~', $term)) {
return FALSE;
}

try {
$new_term = Term::create([
'name' => $term,
'vid' => $vid,
]);
$new_term->save();
}
catch (\Exception $exception) {
return FALSE;
}

return $new_term;
}
Expand Down

0 comments on commit cd9bfa3

Please sign in to comment.