Skip to content

Commit

Permalink
Merge pull request #1416 from younginnovations/1393-missing-document-…
Browse files Browse the repository at this point in the history
…link-organization-xml

Review: 1393-missing-document-link-organization-xml
  • Loading branch information
A4family authored Apr 3, 2024
2 parents 485dcad + e414cdd commit cc4e743
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 56 deletions.
108 changes: 108 additions & 0 deletions app/Console/Commands/FixPublishedOrganizationXmlFile1393.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\IATI\Elements\Xml\OrganizationXmlGenerator;
use App\IATI\Models\Organization\Organization;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Arr;

/**
* Class FixPublishedOrganizationXmlFile1393.
*/
class FixPublishedOrganizationXmlFile1393 extends Command
{
public function __construct(protected OrganizationXmlGenerator $xmlGenerator)
{
parent::__construct();
}

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'fix:published-organization-xml-document-link-recipient-country';

/**
* The console command description.
*
* @var string
*/
protected $description = '
One time run command
This command will fetch published organization and its published xml file
check if recipient-country present inside the document-link node
if not then it will append recipient-country inside the document-link node and publishes it to registry
without modifying the last updated date.
';

/**
* @return void
*/
public function handle(): void
{
echo 'Do you want to continue? (yes/no)';

if (strtolower(trim(fgets(STDIN))) === 'yes') {
$publishedOrganizations = $this->getPublishedOrganizations();

foreach ($publishedOrganizations as $organization) {
if (array_key_exists('recipient_country', Arr::collapse($publishedOrganizations[0]->document_link)) && !is_array_value_empty(Arr::collapse($publishedOrganizations[0]->document_link)['recipient_country'])) {
$organizationXmlFile = awsGetFile("organizationXmlFiles/$organization->publisher_id-organisation.xml");

if ($organizationXmlFile) {
try {
$xmlFile = simplexml_load_string($organizationXmlFile);
$organization = Organization::find($organization->id);
$settings = $organization->settings;

if (!count($xmlFile->xpath('//iati-organisation/document-link/recipient-country'))) {
$this->takeOrganizationXmlFileBackup($organization);
$this->xmlGenerator->generateOrganizationXml($settings, $organization, false);
$this->info("organizationXmlFiles/$organization->publisher_id-organisation.xml");
}
} catch (\Exception | \Throwable $exception) {
$message = 'Organization Xml File generation failed - ' . $organization->publisher_name;
logger($message);
logger()->error($exception);
$this->info($message);
}
}
}
}
}
}

/**
* Takes backup from affected organizations.
*
* @param $organization
*
* @return void
*/
public function takeOrganizationXmlFileBackup($organization): void
{
$getFile = awsGetFile("organizationXmlFiles/$organization->publisher_id-organisation.xml");

if (awsUploadFile("organizationXmlBackupFiles/$organization->publisher_id-organisation.xml", $getFile)) {
$this->info('Successfully backed up organization xml file - ' . $organization->publisher_name);
}
}

/**
* Get organization whose status is either published or draft.
*
* @return Collection|array
*/
public function getPublishedOrganizations(): Collection|array
{
return Organization::query()
->whereNotNull('document_link')
->whereIn('status', ['published', 'draft'])
->get();
}
}
33 changes: 33 additions & 0 deletions app/Helpers/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -991,3 +991,36 @@ function getJsonFromSource(string $completePath): string
return file_get_contents(public_path($completePath));
}
}

if (!function_exists('getTimestampFromOrganizationXml')) {
/**
* Returns timestamp from xml file or last updated at if xml not exists.
*
* @param string $publisherId
*
* @return string
*/
function getTimestampFromOrganizationXml(string $publisherId, App\IATI\Models\Organization\Organization $organization): string
{
$xmlName = "$publisherId-organisation.xml";
$xmlPath = "organizationXmlFiles/$xmlName";

if (awsHasFile($xmlPath)) {
$xmlString = awsGetFile($xmlPath);

if ($xmlString) {
$xmlContent = new SimpleXMLElement($xmlString);

$lastUpdatedDatetime = (string) $xmlContent->xpath('//iati-organisation/@last-updated-datetime')[0];

if ($lastUpdatedDatetime) {
$carbonDate = Carbon::parse($lastUpdatedDatetime);

return $carbonDate->toIso8601String();
}
}
}

return $organization->updated_at->toIso8601String();
}
}
12 changes: 6 additions & 6 deletions app/IATI/Elements/Xml/OrganizationXmlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,18 @@ public function __construct(
*
* @param $settings
* @param $organization
* @param bool $refreshTimestamp
*
* @return void
*/
public function generateOrganizationXml($settings, $organization)
public function generateOrganizationXml($settings, $organization, bool $refreshTimestamp = true): void
{
$publishingInfo = $settings->publishing_info;

$publisherId = Arr::get($publishingInfo, 'publisher_id', 'Not Available');
$filename = sprintf('%s-%s.xml', $publisherId, 'organisation');
$publishedOrganization = sprintf('%s-%s.xml', $publisherId, $organization->id);
$xml = $this->getXml($settings, $organization);

$xml = $this->getXml($settings, $organization, $refreshTimestamp);
$result = Storage::disk('s3')->put(
sprintf('%s/%s', 'organizationXmlFiles', $filename),
$xml->saveXML(),
Expand Down Expand Up @@ -155,20 +155,20 @@ public function savePublishedFiles($filename, $organizationId, $publishedActivit
*
* @return \DomDocument
*/
public function getXml($settings, $organization): ?\DomDocument
public function getXml($settings, $organization, bool $refreshTimestamp = true): ?\DomDocument
{
$this->setServices();
$xmlData = [];
$xmlData['@attributes'] = [
'version' => '2.03',
'generated-datetime' => gmdate('c'),
'generated-datetime' => $refreshTimestamp ? gmdate('c') : getTimestampFromOrganizationXml($organization->publisher_id, $organization),
];
$default_values = $settings->default_values;
$activity_default_values = $settings->activity_default_values;

$xmlData['iati-organisation'] = $this->getXmlData($organization);
$xmlData['iati-organisation']['@attributes'] = [
'last-updated-datetime' => gmdate('c', time()),
'last-updated-datetime' => $refreshTimestamp ? gmdate('c', time()) : getTimestampFromOrganizationXml($organization->publisher_id, $organization),
'xml:lang' => Arr::get($default_values, 'default_language', null),
'default-currency' => Arr::get($default_values, 'default_currency', null),
];
Expand Down
1 change: 1 addition & 0 deletions app/IATI/Elements/Xml/XmlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public function generateActivityXml($activity, $transaction, $result, $settings,
$publisherId = Arr::get($publishingInfo, 'publisher_id', 'Not Available');
$publishedActivity = sprintf('%s-%s.xml', $publisherId, $activity->id);
$xml = $this->getXml($activity, $transaction, $result, $settings, $organization);

$result = awsUploadFile(
sprintf('%s/%s/%s', 'xml', 'activityXmlFiles', $publishedActivity),
$xml->saveXML()
Expand Down
13 changes: 13 additions & 0 deletions app/IATI/Services/Organization/DocumentLinkService.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ public function getXmlData(Organization $organization): array
];
}

$recipientCountries = [];

foreach (Arr::get($documentLink, 'recipient_country', []) as $recipient_country) {
$recipientCountries[] = [
'@attributes' => [
'code' => Arr::get($recipient_country, 'code', null),
'percentage' => Arr::get($recipient_country, 'percentage', null),
],
'narrative' => $this->buildNarrative(Arr::get($recipient_country, 'narrative', null)),
];
}

$organizationData[] = [
'@attributes' => [
'url' => Arr::get($documentLink, 'url', null),
Expand All @@ -152,6 +164,7 @@ public function getXmlData(Organization $organization): array
'iso-date' => Arr::get($documentLink, 'document_date.0.date', null),
],
],
'recipient-country' => $recipientCountries,
];
}
}
Expand Down
50 changes: 0 additions & 50 deletions storage/test.xml

This file was deleted.

0 comments on commit cc4e743

Please sign in to comment.