Skip to content

Commit

Permalink
Merge pull request #1457 from younginnovations/1407-check-use-of-upda…
Browse files Browse the repository at this point in the history
…ted-iati-codelists

Review:1407-check-use-of-updated-iati-codelists
  • Loading branch information
Sanilblank authored Jun 12, 2024
2 parents 1e120d7 + f523407 commit a2a0c89
Show file tree
Hide file tree
Showing 268 changed files with 44,434 additions and 33,264 deletions.
3 changes: 3 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

php artisan optimize:clear && \
php artisan command:UpdateJsonFiles && \
php artisan command:FetchOrganisationRegistrationAgency && \
php artisan command:SetAppDataJsonCache && \
php artisan test && \
npx stylelint "resources/**/*.scss" --fix && \
Expand Down
3 changes: 1 addition & 2 deletions app/Console/Commands/FetchOrganisationRegistrationAgency.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public function handle()
if ($response->successful()) {
$jsonValue = $response->json()['lists'];
$iatiJsonValues = collect($jsonValue)
->filter(fn ($rawJson) => !isset($rawJson['deprecated']) || !$rawJson['deprecated'])
->map(fn ($rawJson) => $this->parseToIatiOrganisationRegistrationAgencyJson($rawJson))
->all();

Expand Down Expand Up @@ -96,7 +95,7 @@ public function handle()
public function parseToIatiOrganisationRegistrationAgencyJson($data): array
{
$category = $data['coverage'][0] ?? '';
$status = isset($data['deprecated']) && $data['deprecated'] === false ? 'active' : null;
$status = isset($data['deprecated']) && $data['deprecated'] === true ? 'withdrawn' : 'active';

return [
'code' => data_get($data, 'code', ''),
Expand Down
126 changes: 126 additions & 0 deletions app/Console/Commands/Issue1407CheckUseOfUpdatedIatiCodelists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\IATI\Models\Activity\Activity;
use App\IATI\Models\Activity\Indicator;
use App\IATI\Models\Activity\Period;
use App\IATI\Models\Activity\Result;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

class Issue1407CheckUseOfUpdatedIatiCodelists extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:Issue1407CheckUseOfUpdatedIatiCodelists';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Refresh deprecation list for data prior to this change.';

/**
* Execute the console command.
*
* @return void
*/
public function handle(): void
{
$dataModelMappedToHelperMidfix = [
'activity' => Activity::class,
'result' => Result::class,
'indicator' => Indicator::class,
'period' => Period::class,
];

$AttributesMappedToHelperMidfix = [
'activity' => [
'id',
'iati_identifier',
'other_identifier',
'title',
'description',
'activity_status',
'status',
'activity_date',
'contact_info',
'activity_scope',
'participating_org',
'recipient_country',
'recipient_region',
'location',
'sector',
'country_budget_items',
'humanitarian_scope',
'policy_marker',
'collaboration_type',
'default_flow_type',
'default_finance_type',
'default_aid_type',
'default_tied_status',
'budget',
'planned_disbursement',
'capital_spend',
'document_link',
'related_activity',
'legacy_data',
'conditions',
'org_id',
'default_field_values',
'tag',
'reporting_org',
],
'result' => ['id', 'result'],
'indicator' => ['id', 'indicator'],
'period' => ['id', 'period'],
'transaction' => ['id', 'transaction'],
];

try {
DB::beginTransaction();

foreach ($dataModelMappedToHelperMidfix as $key => $model) {
$columns = $AttributesMappedToHelperMidfix[$key];

$model::select($columns)->chunkById(10, function ($chunkedRecords) use ($model, $key) {
foreach ($chunkedRecords as $record) {
$this->info("Processing {$model} ID: {$record->id}");

$record->timestamps = false;

if ($key === 'activity') {
$element = $record->toArray();
} else {
$element = Arr::get($record->toArray(), $key, []);
}

/** Call helper here */
$helperFunctionName = 'refresh' . ucfirst($key) . 'DeprecationStatusMap';

if (is_callable($helperFunctionName)) {
$record->updateQuietly(
['deprecation_status_map' => call_user_func($helperFunctionName, $element)],
['touch' => false]
);
}
}
});
}

DB::commit();
} catch (\Exception $e) {
DB::rollBack();

logger()->error($e);
}
}
}
Loading

0 comments on commit a2a0c89

Please sign in to comment.