Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TM-866] project profile display all polygons related to project #255

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/Helpers/GeometryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public function updateProjectCentroid(string $projectUuid)

public static function getPolygonsBbox($polygonsIds)
{
if (count($polygonsIds) === 0) {
return null;
}
$envelopes = PolygonGeometry::whereIn('uuid', $polygonsIds)
->selectRaw('ST_ASGEOJSON(ST_Envelope(geom)) as envelope')
->get();
Expand Down
79 changes: 63 additions & 16 deletions app/Helpers/TerrafundDashboardQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\V2\Projects\Project;
use App\Models\V2\Sites\SitePolygon;
use Illuminate\Support\Facades\Log;

class TerrafundDashboardQueryHelper
{
Expand All @@ -24,37 +25,83 @@ public static function buildQueryFromRequest($request)
return $query;
}

public static function getPolygonIdsOfProject($request)
public static function retrievePolygonUuidsForProject($projectUuId)
{
$projectIds = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)
->pluck('uuid');
$polygonsIds = SitePolygon::whereHas('site.project', function ($query) use ($projectIds) {
$query->whereIn('uuid', $projectIds);
})->pluck('poly_id');
$project = Project::where('uuid', $projectUuId)->first();
$sitePolygons = $project->sitePolygons;

$polygonsIds = $sitePolygons->pluck('poly_id');

return $polygonsIds;
}

public static function getPolygonsByStatusOfProject($request)
public static function getPolygonIdsOfProject($request)
{
$projectIds = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)
->pluck('uuid');
$projectUuId = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)
->pluck('uuid')->first();

return self::retrievePolygonUuidsForProject($projectUuId);
}

public static function getPolygonUuidsOfProject($request)
{
$projectUuId = $request->input('uuid');

return self::retrievePolygonUuidsForProject($projectUuId);
}

public static function getPolygonsByStatus()
{
try {
$statuses = ['needs-more-information', 'submitted', 'approved'];
$polygons = [];
foreach ($statuses as $status) {
$polygonsOfProject = SitePolygon::where('status', $status)
->whereNotNull('site_id')
->where('site_id', '!=', 'NULL')
->pluck('poly_id');

$polygons[$status] = $polygonsOfProject;
}

$statuses = ['Needs More Info', 'Submitted', 'Approved'];
return $polygons;
} catch (\Exception $e) {
Log::error('Error fetching polygons by status of project: ' . $e->getMessage());

return [];
}
}

public static function retrievePolygonUuidsByStatusForProject($projectUuid)
{
$project = Project::where('uuid', $projectUuid)->first();
$sitePolygons = $project->sitePolygons;
$statuses = ['needs-more-information', 'submitted', 'approved'];
$polygons = [];

foreach ($statuses as $status) {
// Get polygons of the project filtered by status
$polygonsOfProject = SitePolygon::whereHas('site.project', function ($query) use ($projectIds) {
$query->whereIn('uuid', $projectIds);
})
->where('status', $status)
->pluck('poly_id');
$polygonsOfProject = $sitePolygons
->where('status', $status)
->pluck('poly_id');

$polygons[$status] = $polygonsOfProject;
}

return $polygons;
}

public static function getPolygonsByStatusOfProject($request)
{
$projectUuid = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)
->pluck('uuid')->first();

return self::retrievePolygonUuidsByStatusForProject($projectUuid);
}

public static function getPolygonsUuidsByStatusForProject($request)
{
$projectUuid = $request->input('uuid');

return self::retrievePolygonUuidsByStatusForProject($projectUuid);
}
}
23 changes: 23 additions & 0 deletions app/Http/Controllers/V2/Dashboard/GetPolygonsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public function getPolygonsByStatusOfProject(Request $request): GetPolygonsResou
]);
}

public function getPolygonsUuidsByStatusForProject(Request $request): GetPolygonsResource
{
$polygonsIds = TerrafundDashboardQueryHelper::getPolygonsUuidsByStatusForProject($request);

return new GetPolygonsResource([
'data' => $polygonsIds,
]);
}

public function getBboxOfCompleteProject(Request $request)
{
try {
Expand All @@ -44,4 +53,18 @@ public function getBboxOfCompleteProject(Request $request)
return response()->json(['error' => 'An error occurred while fetching the bounding box coordinates'], 404);
}
}

public function getProjectBbox(Request $request)
{
try {
$polygonsIds = TerrafundDashboardQueryHelper::getPolygonUuidsOfProject($request);
$bboxCoordinates = GeometryHelper::getPolygonsBbox($polygonsIds);

return response()->json(['bbox' => $bboxCoordinates]);
} catch (\Exception $e) {
Log::error($e->getMessage());

return response()->json(['error' => 'An error occurred while fetching the bounding box coordinates'], 404);
}
}
};
137 changes: 137 additions & 0 deletions app/Http/Controllers/V2/Dashboard/ViewProjectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace App\Http\Controllers\V2\Dashboard;

use App\Helpers\TerrafundDashboardQueryHelper;
use App\Http\Controllers\Controller;
use App\Models\V2\Projects\Project;
use App\Models\V2\Projects\ProjectInvite;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class ViewProjectController extends Controller
{
public function getIfUserIsAllowedToProject(String $uuid)
{
$user = Auth::user();
$role = $user->role;
if ($role === 'government') {
$isAllowed = Project::where('uuid', $uuid)
->where('country', $user->country)
->first();
$response = (object)[
'allowed' => $isAllowed ? true : false,
];
} elseif ($role === 'funder') {
$isAllowed = Project::where('uuid', $uuid)
->where('framework_key', $user->program)
->first();
$response = (object)[
'allowed' => $isAllowed ? true : false,
];
} elseif ($role === 'project_developer') {
$projectId = Project::where('uuid', $uuid)
->value('id');
$isInvite = ProjectInvite::where('email_address', $user->email_address)
->where('project_id', $projectId)
->first();
$response = (object)[
'allowed' => $isInvite ? true : false,
];
} elseif ($role === 'admin' || $role === 'terrafund_admin') {
$response = (object)[
'allowed' => true,
];
} else {
$response = (object)[
'allowed' => false,
];
}

return response()->json($response);
}

public function getAllProjectsAllowedToUser()
{
try {
$user = Auth::user();
$role = $user->role;
Log::info($role);
if ($role === 'admin' || $role === 'terrafund_admin' || $role === 'terrafund-admin') {
$response = TerrafundDashboardQueryHelper::getPolygonsByStatus();

return response()->json([
'polygonsUuids' => $response,
]);
} else {
if ($role === 'government') {
try {
$projectUuids = Project::where('framework_key', 'terrafund')->where('country', $user->country)->pluck('uuid');
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
Log::error('Error fetching projects for government: ' . $errorMessage);

return response()->json(['error' => 'An error occurred while fetching government projects', 'message' => $errorMessage], 500);
}
} elseif ($role === 'funder') {
try {
$projectUuids = Project::where('framework_key', $user->program)->pluck('uuid');
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
Log::error('Error fetching projects for funder: ' . $errorMessage);

return response()->json(['error' => 'An error occurred while fetching funder projects', 'message' => $errorMessage], 500);
}
} elseif ($role === 'project_developer') {
try {
$projectIds = ProjectInvite::where('email_address', $user->email_address)->pluck('project_id');
$projectUuids = Project::whereIn('id', $projectIds)->where('framework_key', 'terrafund')->pluck('uuid');
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
Log::error('Error fetching projects for project developer: ' . $errorMessage);

return response()->json(['error' => 'An error occurred while fetching project developer projects', 'message' => $errorMessage], 500);
}
} elseif ($role === 'admin' || $role === 'terrafund_admin' || $role === 'terrafund-admin') {

} else {
$projectUuids = null;
}

Log::info('Returning this value: ' . json_encode($projectUuids));
$polygonsData = [
'needs-more-information' => [],
'submitted' => [],
'approved' => [],
];

foreach ($projectUuids as $uuid) {
Log::info('Fetching polygons for project UUID ' . $uuid);
$request = new Request(['uuid' => $uuid]);

try {
$polygonsResource = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProject($request);
foreach ($polygonsResource as $status => $polygons) {
$polygons = $polygons instanceof \Illuminate\Support\Collection ? $polygons->toArray() : $polygons;
$polygonsData[$status] = array_merge($polygonsData[$status], $polygons);
}
} catch (\Exception $e) {
Log::error('Error fetching polygons for project UUID ' . $uuid . ': ' . $e->getMessage());
}
}

return response()->json([
'projectsUuids' => $projectUuids->toArray(),
'polygonsUuids' => $polygonsData,
]);
}

} catch (\Exception $e) {
$errorMessage = $e->getMessage();
Log::error('An error occurred: ' . $errorMessage);

return response()->json(['error' => 'An error occurred while fetching the data', 'message' => $errorMessage], 500);
}
}
};
Loading
Loading