Skip to content

Commit

Permalink
- [x] Moved duplicate command
Browse files Browse the repository at this point in the history
- [x] Moved duplicate command
  • Loading branch information
PG-Momik committed Sep 25, 2024
1 parent 6042c75 commit e09da2a
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 1 deletion.
141 changes: 141 additions & 0 deletions app/Console/Commands/DuplicateActiityData.php.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace App\Console\Commands;

use App\IATI\Models\Activity\Activity;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

/**
* Class DuplicateActivityData.
*/
class DuplicateActivityData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'duplicate:activity-data';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Duplicates activity data';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
try {
$activity_id = $this->ask('Insert activity id to duplicate');
$no_of_iterations = $this->ask('Insert number of iterations');

DB::beginTransaction();
$activity = Activity::where('id', $activity_id)->with(['transactions', 'results.indicators.periods'])->first();

for ($i = 1; $i <= $no_of_iterations; $i++) {
$this->info('--------------------------------------');
$this->info('Duplicating activity no ' . $i);
$newActivity = $activity->replicate();
$newActivity->title = $this->getTitleData($activity->title, $i);
$newActivity->iati_identifier = $this->getIatiIdentifier($activity->iati_identifier, $i);
$newActivity->push();

$this->duplicateTransactions($newActivity, $activity->transactions);
$this->duplicateResults($newActivity, $activity->results);
$this->info('Duplicated activity no ' . $i);
}

DB::commit();
} catch (\Exception $e) {
DB::rollBack();
logger()->error($e);

$this->error($e->getMessage());
}
}

public function getIatiIdentifier($oldIdentifier, $amount)
{
$newIdentifier = $oldIdentifier;
$newIdentifier['activity_identifier'] = $this->getIncrementedData($oldIdentifier['activity_identifier'], $amount);
$newIdentifier['iati_identifier_text'] = $this->getIncrementedData($oldIdentifier['iati_identifier_text'], $amount);

return $newIdentifier;
}

public function getTitleData($oldTitle, $amount)
{
$newTitle = $oldTitle;
$newTitle[0]['narrative'] = $this->getIncrementedData($oldTitle[0]['narrative'], $amount);

return $newTitle;
}

public function getIncrementedData($data, $amount)
{
$array = explode('-', $data);
$array[array_key_last($array)] = (int) $array[array_key_last($array)] + $amount;

return implode('-', $array);
}

public function duplicateTransactions($newActivity, $oldTransactions)
{
foreach ($oldTransactions as $transaction) {
$this->info('Duplicating transaction ' . $transaction->id);
$newTransaction = $transaction->replicate();
$newTransaction->activity_id = $newActivity->id;
$newTransaction->save();
$this->info('Duplicated transaction ' . $transaction->id);
}
}

public function duplicateResults($newActivity, $oldResults)
{
// $count = 0;

foreach ($oldResults as $result) {
// if ($count <= 5) {
$this->info('Duplicating result ' . $result->id);
$newResult = $result->replicate();
$newResult->activity_id = $newActivity->id;
$newResult->save();

$this->duplicateIndicators($newResult, $result->indicators);
$this->info('Duplicated result ' . $result->id);
// $count++;
// }
}
}

public function duplicateIndicators($newResult, $oldIndicators)
{
foreach ($oldIndicators as $indicator) {
$this->info('Duplicating indicator ' . $indicator->id);
$newIndicator = $indicator->replicate();
$newIndicator->result_id = $newResult->id;
$newIndicator->save();

$this->duplicatePeriods($newIndicator, $indicator->periods);
$this->info('Duplicated indicator ' . $indicator->id);
}
}

public function duplicatePeriods($newIndicator, $oldPeriods)
{
foreach ($oldPeriods as $period) {
$this->info('Duplicating period ' . $period->id);
$newPeriod = $period->replicate();
$newPeriod->indicator_id = $newIndicator->id;
$newPeriod->save();
$this->info('Duplicated period ' . $period->id);
}
}
}
2 changes: 1 addition & 1 deletion app/Console/Commands/SetAllPublisherPublishedFileSizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function handle(): void

try {
foreach ($activityPublishedChunk as $activityPublished) {
$this->info("Started for org_id: $activityPublished->organization_id");
$filename = $activityPublished->filename;
$xmlString = awsGetFile("xml/mergedActivityXml/$filename");
$fileSize = $xmlString ? calculateStringSizeInMb($xmlString) : 0;
Expand All @@ -50,6 +51,5 @@ public function handle(): void
$this->error('Error updating activity published file sizes: ' . $e->getMessage());
}
});

}
}
Empty file removed app/Console/Commands/Test.php
Empty file.

0 comments on commit e09da2a

Please sign in to comment.