Skip to content

Commit

Permalink
[TM-1471] command to approve polygons (#567)
Browse files Browse the repository at this point in the history
* [TM-1471] command to approve polygons

* [TM-1471] add lint
  • Loading branch information
egrojMonroy authored Nov 13, 2024
1 parent 9e63a7f commit 537c436
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions app/Console/Commands/BulkApproveProjectPolygonsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Console\Commands;

use App\Models\Traits\SaveAuditStatusTrait;
use App\Models\V2\Projects\Project;
use App\Models\V2\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;

class BulkApproveProjectPolygonsCommand extends Command
{
use SaveAuditStatusTrait;

protected $signature = 'bulk-approve-project-polygons {file}';

protected $description = 'Bulk approve site polygons for projects listed in a CSV file';

public function handle(): void
{
$filePath = $this->argument('file');

if (! File::exists($filePath)) {
$this->error("CSV file not found at {$filePath}");

return;
}
$userEmail = '[email protected]';
$user = User::where('email_address', $userEmail)->first();
Auth::login($user);
$data = array_map('str_getcsv', file($filePath));
$header = array_shift($data);
$output = new ConsoleOutput();
$progressBar = new ProgressBar($output, count($data));
$progressBar->setFormat('Processing: %current% [%bar%] %percent:3s%%');

$progressBar->start();

$polygonsChanged = [];

foreach ($data as $row) {
$uuid = $row[0];
$project = Project::isUuid($uuid)->first();
$this->info("\nProcessing project " . $uuid);
if ($project) {
foreach ($project->sitePolygons as $sitePolygon) {
$sitePolygon->status = 'approved';
$sitePolygon->save();

$this->saveAuditStatus(get_class($sitePolygon), $sitePolygon->id, $sitePolygon->status, 'Approved via bulk command', 'status');

$polygonsChanged[] = $sitePolygon;
}
}

$progressBar->advance();
}

$progressBar->finish();

// Print summary of the number of polygons approved
$this->info("\n" . count($polygonsChanged) . ' polygons were updated.');
}
}

0 comments on commit 537c436

Please sign in to comment.