Skip to content

Commit

Permalink
Merge pull request #531 from wri/release/rare-redwood
Browse files Browse the repository at this point in the history
[RELEASE] Rare Redwood
  • Loading branch information
roguenet authored Nov 1, 2024
2 parents 7a4795d + d122125 commit d6b0d21
Show file tree
Hide file tree
Showing 42 changed files with 2,078 additions and 248 deletions.
61 changes: 61 additions & 0 deletions app/Console/Commands/DeleteDummyBufferedPolygons.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Console\Commands;

use App\Models\V2\PolygonGeometry;
use App\Models\V2\Sites\SitePolygon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class DeleteDummyBufferedPolygons extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'delete:dummy-buffered-polygons {siteId : The UUID of the site to delete polygons for}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete dummy buffered polygons for a specified site';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$siteId = $this->argument('siteId');

DB::beginTransaction();

try {

$sitePolygons = SitePolygon::where('site_id', $siteId)->get();

foreach ($sitePolygons as $sitePolygon) {
$polygonGeometry = PolygonGeometry::where('uuid', $sitePolygon->poly_id)->first();
if ($polygonGeometry) {
$polygonGeometry->deleteWithRelated();
}
}

DB::commit();

$this->info("Dummy buffered polygons for site ID $siteId have been successfully deleted.");

} catch (\Exception $e) {
DB::rollBack();
$this->error('Failed to delete dummy buffered polygons: ' . $e->getMessage());

return Command::FAILURE;
}

return Command::SUCCESS;
}
}
4 changes: 2 additions & 2 deletions app/Console/Commands/OneOff/FixDuplicateDemographics.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function handle()

foreach ($workdays as $workdayId => $stats) {
foreach ($stats as $stat) {
$workday = Workday::find($workdayId);
$workday = Workday::withTrashed()->find($workdayId);

$rows = collect($stat['rows']);
$max = $rows->max('amount');
Expand All @@ -79,7 +79,7 @@ public function handle()
'workday_id' => $workdayId,
'workdayable_type' => $workday->workdayable_type,
'workdayable_id' => $workday->workdayable_id,
'workdayable_uuid' => $workday->workdayable->uuid,
'workdayable_uuid' => $workday->workdayable()->withTrashed()->first()->uuid,
'collection' => $workday->collection,
'type' => $stat['type'],
'subtype' => $stat['subtype'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace App\Console\Commands\OneOff;

use App\Models\V2\Action;
use App\Models\V2\Projects\Project;
use App\Models\V2\Projects\ProjectReport;
use App\Models\V2\Tasks\Task;
use App\StateMachines\ReportStatusStateMachine;
use App\StateMachines\TaskStatusStateMachine;
use Carbon\Carbon;
use Illuminate\Console\Command;

class GenerateBackDatedReportsEnterprisesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'one-off:generate-backdated-reports-enterprises-command';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generates backdated reports for enterprises framework';

/**
* Execute the console command.
*/
public function handle()
{
$due_at = Carbon::createFromFormat('m', 7)->startOfMonth()->setDay(30)->setHours(5);
$period_key = $due_at->year . '-' . $due_at->month;
$framework_key = 'enterprises';
Project::where('framework_key', $framework_key)
->chunkById(100, function ($projects) use ($framework_key, $period_key, $due_at) {
foreach ($projects as $project) {
$this->createTask($project, $framework_key, $period_key, $due_at);
}
});
}

public function createTask($project, $framework_key, $period_key, $due_at)
{
$task = Task::create([
'organisation_id' => $project->organisation_id,
'project_id' => $project->id,
'status' => TaskStatusStateMachine::DUE,
'period_key' => $period_key,
'due_at' => $due_at,
]);

$projectReport = $task->projectReport()->create([
'framework_key' => $framework_key,
'project_id' => $project->id,
'status' => ReportStatusStateMachine::DUE,
'due_at' => $due_at,
]);

$hasSite = false;
foreach ($project->sites as $site) {
$hasSite = true;
$task->siteReports()->create([
'framework_key' => $framework_key,
'site_id' => $site->id,
'status' => ReportStatusStateMachine::DUE,
'due_at' => $due_at,
]);
}

$hasNursery = false;
foreach ($project->nurseries as $nursery) {
$hasNursery = true;
$task->nurseryReports()->create([
'framework_key' => $framework_key,
'nursery_id' => $nursery->id,
'status' => ReportStatusStateMachine::DUE,
'due_at' => $due_at,
]);
}

$labels = ['Project'];
if ($hasSite) {
$labels[] = 'site';
}
if ($hasNursery) {
$labels[] = 'nursery';
}
$message = printf(
'%s %s available',
implode(', ', $labels),
count($labels) > 1 ? 'reports' : 'report'
);

Action::create([
'status' => Action::STATUS_PENDING,
'targetable_type' => ProjectReport::class,
'targetable_id' => $projectReport->id,
'type' => Action::TYPE_NOTIFICATION,
'title' => 'Project report',
'sub_title' => '',
'text' => $message,
'project_id' => $project->id,
'organisation_id' => $project->organisation_id,
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Console\Commands\OneOff;

use App\Models\V2\ScheduledJobs\TaskDueJob;
use Carbon\Carbon;
use Illuminate\Console\Command;

class SeedScheduledJobsHbfEnterprisesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'one-off:seed-scheduled-jobs-hbf-enterprises';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Creates the initial definition of scheduled jobs for hbf and enterprises frameworks';

/**
* Execute the console command.
*/
public function handle()
{
// HBF reports
// May-October, November 1, December 1
TaskDueJob::createTaskDue(
Carbon::createFromFormat('m', 11)->startOfMonth()->setDay(1),
'hbf',
Carbon::createFromFormat('m', 12)->startOfMonth()->setDay(1)->setHours(5),
);
// November-April, May 1, June 1
TaskDueJob::createTaskDue(
Carbon::createFromFormat('m', 5)->startOfMonth()->setDay(1),
'hbf',
Carbon::createFromFormat('m', 6)->startOfMonth()->setDay(3)->setHours(5),
);

// Enterprises reports
// January-June, July 1, July 30
TaskDueJob::createTaskDue(
Carbon::createFromFormat('m', 7)->startOfMonth()->setDay(1),
'enterprises',
Carbon::createFromFormat('m', 7)->startOfMonth()->setDay(30)->setHours(5),
);
// July-December, January 1, January 30
TaskDueJob::createTaskDue(
Carbon::createFromFormat('m', 1)->startOfMonth()->setDay(1),
'enterprises',
Carbon::createFromFormat('m', 1)->startOfMonth()->setDay(30)->setHours(5),
);

}
}
63 changes: 27 additions & 36 deletions app/Helpers/CreateVersionPolygonGeometryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,34 @@ class CreateVersionPolygonGeometryHelper
*/
public static function createVersionPolygonGeometry(string $uuid, $geometry)
{
try {
Log::info("Creating geometry version for polygon with UUID: $uuid");
Log::info("Creating geometry version for polygon with UUID: $uuid");

if ($geometry instanceof Request) {
$geometry = $geometry->input('geometry');
}

$polygonGeometry = PolygonGeometry::isUuid($uuid)->first();

if (! $polygonGeometry) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
}

$geometry = json_decode($geometry);
$geom = DB::raw("ST_GeomFromGeoJSON('" . json_encode($geometry) . "')");

$sitePolygon = SitePolygon::where('poly_id', $polygonGeometry->uuid)->first();

$user = Auth::user();

$newGeometryVersion = PolygonGeometry::create([
'geom' => $geom,
'created_by' => $user->id,
]);
$newPolygonVersion = $sitePolygon->createCopy($user, $newGeometryVersion->uuid, false);

if ($newPolygonVersion) {
PolyHelper::updateEstAreainSitePolygon($newGeometryVersion, $geometry);
PolyHelper::updateProjectCentroidFromPolygon($newGeometryVersion);
$newPolygonVersion->changeStatusOnEdit();

return response()->json(['message' => 'Site polygon version created successfully.', 'geometry' => $geometry, 'uuid' => $newPolygonVersion->poly_id], 201);
}

return response()->json(['message' => 'Failed to create site polygon version.'], 500);
} catch (\Exception $e) {
return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500);
if ($geometry instanceof Request) {
$geometry = $geometry->input('geometry');
}
$polygonGeometry = PolygonGeometry::isUuid($uuid)->first();
if (! $polygonGeometry) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
}
$geometry = json_decode($geometry);
$geom = DB::raw("ST_GeomFromGeoJSON('" . json_encode($geometry) . "')");

$sitePolygon = SitePolygon::where('poly_id', $polygonGeometry->uuid)->first();

$user = Auth::user();
$newGeometryVersion = PolygonGeometry::create([
'geom' => $geom,
'created_by' => $user->id,
]);
$newPolygonVersion = $sitePolygon->createCopy($user, $newGeometryVersion->uuid, false);
if ($newPolygonVersion) {
PolyHelper::updateEstAreainSitePolygon($newGeometryVersion, $geometry);
PolyHelper::updateProjectCentroidFromPolygon($newGeometryVersion);
$newPolygonVersion->changeStatusOnEdit();

return response()->json(['message' => 'Site polygon version created successfully.', 'geometry' => $geometry, 'uuid' => $newPolygonVersion->poly_id], 201);
}

return response()->json(['message' => 'Failed to create site polygon version.'], 500);
}
}
8 changes: 8 additions & 0 deletions app/Helpers/GeometryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ public static function getPolygonsGeojson(array $polygonUuids): array
];
}

public static function getProjectPolygonsUuids($projectId)
{
$project = Project::where('id', $projectId)->firstOrFail();
$projectPolygonUuids = $project->sitePolygons()->pluck('poly_id')->toArray();

return $projectPolygonUuids;
}

public static function getSitePolygonsUuids($uuid)
{
return SitePolygon::where('site_id', $uuid)->where('is_active', true)->get()->pluck('poly_id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function getAllProjects($request, $perPage, $page)
'number_of_trees_goal' => $project->trees_grown_goal,
'date_added' => $project->created_at,
'hectares_under_restoration' => round($project->sitePolygons->sum('calc_area')),
'programme' => $project->framework_key,
];
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __invoke(Request $request, Project $project)
'restorationStrategy' => $project->restoration_strategy,
'targetLandUse' => $project->land_use_types,
'landTenure' => $project->land_tenure_project_area,
'framework' => $project->framework_key,
];

return response()->json($response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function getTopProjects($projects)
$totalSpeciesAmountForSiteReport = $project->trees_planted_count;

$topProjects[] = [
'organization' => $project->organisation->name,
'project' => $project->name,
'uuid' => $project->uuid,
'trees_planted' => $totalSpeciesAmountForSiteReport,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Controllers\V2\Exports;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class GeneratePreSignedURLDownloadReportController extends Controller
{
public function __invoke(Request $request, string $entity, string $framework)
{
$fileKey = 'exports/all-entity-records/'.$entity.'-'.$framework.'.csv';

$expiration = now()->addMinutes(60);

$presignedUrl = Storage::disk('s3')->temporaryUrl($fileKey, $expiration);

return response()->json(['url' => $presignedUrl]);
}
}
Loading

0 comments on commit d6b0d21

Please sign in to comment.