Skip to content

Commit

Permalink
Merge branch 'feat/TM-1386-bulk-approved' of https://github.com/wri/w…
Browse files Browse the repository at this point in the history
…ri-terramatch-api into feat/TM-1386-bulk-approved
  • Loading branch information
egrojMonroy committed Nov 1, 2024
2 parents 80c0e93 + 9bd1f20 commit 346b463
Show file tree
Hide file tree
Showing 3 changed files with 402 additions and 0 deletions.
65 changes: 65 additions & 0 deletions database/seeders/UpdateProjectStatusSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Database\Seeders;

use App\Models\V2\Projects\Project;
use App\Models\V2\Projects\ProjectReport;
use App\Models\V2\Sites\SiteReport;
use App\Models\V2\Tasks\Task;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\File;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;

class UpdateProjectStatusSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run()
{
$filePath = __DIR__ . '/../../resources/seeds/bulk_approve_projects.csv';

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

return;
}

$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();

foreach ($data as $row) {
$uuid = $row[0];

$project = Project::where('uuid', $uuid)->first();

if ($project) {
ProjectReport::where('project_id', $project->id)
->whereIn('status', ['awaiting-approval', 'needs-more-information'])
->update(['status' => 'approved']);

$sites = $project->sites;
foreach ($sites as $site) {
SiteReport::where('site_id', $site->id)
->whereIn('status', ['awaiting-approval', 'needs-more-information'])
->update(['status' => 'approved']);
}

Task::where('project_id', $project->id)
->whereIn('status', ['awaiting-approval', 'needs-more-information'])
->update(['status' => 'approved']);
}

$progressBar->advance();
}

$progressBar->finish();
$output->writeln("\nUpdate complete!");
}
}
Loading

0 comments on commit 346b463

Please sign in to comment.