From d9b622c9b19a7c21dcccb83fc721c5c2cffed3a9 Mon Sep 17 00:00:00 2001 From: JORGE Date: Tue, 5 Nov 2024 12:13:33 -0400 Subject: [PATCH 1/7] [TM-1343] restore functions to data for popups --- .../V2/Dashboard/GetProjectsController.php | 15 +++++++++++ ...otalTerrafundHeaderDashboardController.php | 24 ++++++++++++++++++ .../V2/Dashboard/ViewProjectController.php | 25 +++++++++++++++---- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/V2/Dashboard/GetProjectsController.php b/app/Http/Controllers/V2/Dashboard/GetProjectsController.php index adb67d569..7d13d5dfe 100644 --- a/app/Http/Controllers/V2/Dashboard/GetProjectsController.php +++ b/app/Http/Controllers/V2/Dashboard/GetProjectsController.php @@ -10,6 +10,21 @@ class GetProjectsController extends Controller { public function __invoke(Request $request) { + $frameworks = data_get($request, 'filter.programmes', []); + $landscapes = data_get($request, 'filter.landscapes', []); + $organisations = data_get($request, 'filter.organisationType', []); + $country = data_get($request, 'filter.country', ''); + $uuid = data_get($request, 'filter.projectUuid', ''); + $request = new Request([ + 'filter' => [ + 'country' => $country, + 'programmes' => $frameworks, + 'landscapes' => $landscapes, + 'organisationType' => $organisations, + 'projectUuid' => $uuid, + ], + ]); + $projects = TerrafundDashboardQueryHelper::buildQueryFromRequest($request) ->whereNotNull('long') ->whereNotNull('lat') diff --git a/app/Http/Controllers/V2/Dashboard/TotalTerrafundHeaderDashboardController.php b/app/Http/Controllers/V2/Dashboard/TotalTerrafundHeaderDashboardController.php index 7a53a1e2f..f2cfaa706 100644 --- a/app/Http/Controllers/V2/Dashboard/TotalTerrafundHeaderDashboardController.php +++ b/app/Http/Controllers/V2/Dashboard/TotalTerrafundHeaderDashboardController.php @@ -2,14 +2,18 @@ namespace App\Http\Controllers\V2\Dashboard; +use App\Helpers\TerrafundDashboardQueryHelper; use App\Http\Controllers\Controller; use App\Http\Resources\DelayedJobResource; use App\Jobs\Dashboard\RunTotalHeaderJob; use App\Models\DelayedJob; use App\Models\Traits\HasCacheParameter; +use App\Models\V2\WorldCountryGeneralized; +use App\Services\Dashboard\RunTotalHeaderService; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Redis; +use Illuminate\Support\Facades\App; class TotalTerrafundHeaderDashboardController extends Controller { @@ -50,4 +54,24 @@ public function __invoke(Request $request) return response()->json(['error' => 'An error occurred during total-header'], 500); } } + + public function getTotalDataForCountry(Request $request) + { + $projects = TerrafundDashboardQueryHelper::buildQueryFromRequest($request)->get(); + $countryName = ''; + if ($country = data_get($request, 'filter.country')) { + $countryName = WorldCountryGeneralized::where('iso', $country)->first()->country; + } + $response = (object)[ + 'total_non_profit_count' => App::make(RunTotalHeaderService::class)->getTotalNonProfitCount($projects), + 'total_enterprise_count' => App::make(RunTotalHeaderService::class)->getTotalEnterpriseCount($projects), + 'total_entries' => App::make(RunTotalHeaderService::class)->getTotalJobsCreatedSum($projects), + 'total_hectares_restored' => round(App::make(RunTotalHeaderService::class)->getTotalHectaresSum($projects)), + 'total_trees_restored' => App::make(RunTotalHeaderService::class)->getTotalTreesRestoredSum($projects), + 'country_name' => $countryName, + ]; + + return response()->json($response); + } + } diff --git a/app/Http/Controllers/V2/Dashboard/ViewProjectController.php b/app/Http/Controllers/V2/Dashboard/ViewProjectController.php index fd3dc9a0c..2988ae55d 100644 --- a/app/Http/Controllers/V2/Dashboard/ViewProjectController.php +++ b/app/Http/Controllers/V2/Dashboard/ViewProjectController.php @@ -54,7 +54,7 @@ public function getIfUserIsAllowedToProject(String $uuid) return response()->json($response); } - public function getAllProjectsAllowedToUser() + public function getAllProjectsAllowedToUser(Request $request) { try { /** @var User $user */ @@ -105,11 +105,26 @@ public function getAllProjectsAllowedToUser() 'approved' => [], 'draft' => [], ]; - + $frameworks = data_get($request, 'filter.programmes', []); + $landscapes = data_get($request, 'filter.landscapes', []); + $organisations = data_get($request, 'filter.organisationType', []); + $country = data_get($request, 'filter.country', ''); + $baseFilter = [ + 'filter' => [ + 'country' => $country, + 'programmes' => $frameworks, + 'landscapes' => $landscapes, + 'organisationType' => $organisations, + ], + ]; + foreach ($projectUuids as $uuid) { - Log::info('Fetching polygons for project UUID ' . $uuid); - $request = new Request(['uuid' => $uuid]); - + $filterWithProject = array_merge_recursive($baseFilter, [ + 'filter' => [ + 'projectUuid' => $uuid + ] + ]); + $request = new Request($filterWithProject); try { $polygonsResource = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProject($request); foreach ($polygonsResource as $status => $polygons) { From 418c9dce1f08d0d19675fe0ea1f87d5aad3bba03 Mon Sep 17 00:00:00 2001 From: JORGE Date: Tue, 5 Nov 2024 12:16:15 -0400 Subject: [PATCH 2/7] [TM-1343] lint fix --- .../V2/Dashboard/GetProjectsController.php | 2 +- .../TotalTerrafundHeaderDashboardController.php | 3 +-- .../V2/Dashboard/ViewProjectController.php | 13 +++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/V2/Dashboard/GetProjectsController.php b/app/Http/Controllers/V2/Dashboard/GetProjectsController.php index 7d13d5dfe..79f24c4a4 100644 --- a/app/Http/Controllers/V2/Dashboard/GetProjectsController.php +++ b/app/Http/Controllers/V2/Dashboard/GetProjectsController.php @@ -24,7 +24,7 @@ public function __invoke(Request $request) 'projectUuid' => $uuid, ], ]); - + $projects = TerrafundDashboardQueryHelper::buildQueryFromRequest($request) ->whereNotNull('long') ->whereNotNull('lat') diff --git a/app/Http/Controllers/V2/Dashboard/TotalTerrafundHeaderDashboardController.php b/app/Http/Controllers/V2/Dashboard/TotalTerrafundHeaderDashboardController.php index f2cfaa706..86ca5fdea 100644 --- a/app/Http/Controllers/V2/Dashboard/TotalTerrafundHeaderDashboardController.php +++ b/app/Http/Controllers/V2/Dashboard/TotalTerrafundHeaderDashboardController.php @@ -11,9 +11,9 @@ use App\Models\V2\WorldCountryGeneralized; use App\Services\Dashboard\RunTotalHeaderService; use Illuminate\Http\Request; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Redis; -use Illuminate\Support\Facades\App; class TotalTerrafundHeaderDashboardController extends Controller { @@ -73,5 +73,4 @@ public function getTotalDataForCountry(Request $request) return response()->json($response); } - } diff --git a/app/Http/Controllers/V2/Dashboard/ViewProjectController.php b/app/Http/Controllers/V2/Dashboard/ViewProjectController.php index 2988ae55d..35cfb2828 100644 --- a/app/Http/Controllers/V2/Dashboard/ViewProjectController.php +++ b/app/Http/Controllers/V2/Dashboard/ViewProjectController.php @@ -117,14 +117,15 @@ public function getAllProjectsAllowedToUser(Request $request) 'organisationType' => $organisations, ], ]; - + foreach ($projectUuids as $uuid) { - $filterWithProject = array_merge_recursive($baseFilter, [ - 'filter' => [ - 'projectUuid' => $uuid - ] + $filterWithProject = array_merge_recursive($baseFilter, [ + 'filter' => [ + 'projectUuid' => $uuid, + ], ]); - $request = new Request($filterWithProject); + $request = new Request($filterWithProject); + try { $polygonsResource = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProject($request); foreach ($polygonsResource as $status => $polygons) { From 0fb1958c0cd2a560b0242fe0986afa47fac78030 Mon Sep 17 00:00:00 2001 From: JORGE Date: Tue, 5 Nov 2024 14:40:24 -0400 Subject: [PATCH 3/7] [TM-1343] rename function for clarity --- app/Http/Controllers/V2/Dashboard/GetPolygonsController.php | 2 +- routes/api_v2.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php b/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php index 7c2e9db37..6a2f980f6 100644 --- a/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php +++ b/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php @@ -22,7 +22,7 @@ public function getPolygonsOfProject(Request $request): GetPolygonsResource ]); } - public function getPolygonsByStatusOfProject(Request $request): GetPolygonsResource + public function getPolygonsDataByStatusOfProject(Request $request): GetPolygonsResource { $polygonsIds = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProject($request); diff --git a/routes/api_v2.php b/routes/api_v2.php index 2ed86db90..8c668b1af 100644 --- a/routes/api_v2.php +++ b/routes/api_v2.php @@ -729,7 +729,7 @@ function () { Route::get('/tree-restoration-goal', ViewTreeRestorationGoalController::class); Route::get('/project-list-export', ProjectListExportController::class); Route::get('/get-polygons', [GetPolygonsController::class, 'getPolygonsOfProject']); - Route::get('/get-polygons/statuses', [GetPolygonsController::class, 'getPolygonsByStatusOfProject']); + Route::get('/get-polygons/statuses', [GetPolygonsController::class, 'getPolygonsDataByStatusOfProject']); Route::get('/get-bbox-project', [GetPolygonsController::class, 'getBboxOfCompleteProject']); Route::get('/bbox/project', [GetPolygonsController::class, 'getProjectBbox']); Route::get('/country/{country}', [CountryDataController::class, 'getCountryBbox']); From e84b222daa512ab0fe7f91a32227143c00ca9151 Mon Sep 17 00:00:00 2001 From: cesarLima1 Date: Tue, 5 Nov 2024 14:56:32 -0400 Subject: [PATCH 4/7] [TM-1343] add condition for array of project uuids --- app/Helpers/TerrafundDashboardQueryHelper.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/Helpers/TerrafundDashboardQueryHelper.php b/app/Helpers/TerrafundDashboardQueryHelper.php index c607623eb..c5280898f 100644 --- a/app/Helpers/TerrafundDashboardQueryHelper.php +++ b/app/Helpers/TerrafundDashboardQueryHelper.php @@ -41,6 +41,14 @@ public static function buildQueryFromRequest(Request $request) $query->where('v2_projects.uuid', data_get($filters, 'filter.projectUuid')); } + if ($projectUuids = data_get($filters, 'filter.projectUuid')) { + if (is_array($projectUuids)) { + $query->whereIn('v2_projects.uuid', $projectUuids); + } else { + $query->where('v2_projects.uuid', $projectUuids); + } + } + if ($request->has('search')) { $searchTerm = $request->query('search'); $query->where(function ($query) use ($searchTerm) { From b6bf899c6d76553f446a2b97f4d8f71eea1508f9 Mon Sep 17 00:00:00 2001 From: JORGE Date: Tue, 5 Nov 2024 16:09:58 -0400 Subject: [PATCH 5/7] [TM-1343] improve query to bulk instead of single --- app/Helpers/TerrafundDashboardQueryHelper.php | 79 ++++++++++--------- .../V2/Dashboard/GetPolygonsController.php | 10 +-- .../V2/Dashboard/ViewProjectController.php | 34 ++++---- .../TerrafundEditGeometryController.php | 15 ++-- 4 files changed, 62 insertions(+), 76 deletions(-) diff --git a/app/Helpers/TerrafundDashboardQueryHelper.php b/app/Helpers/TerrafundDashboardQueryHelper.php index c5280898f..dfc9977a4 100644 --- a/app/Helpers/TerrafundDashboardQueryHelper.php +++ b/app/Helpers/TerrafundDashboardQueryHelper.php @@ -36,26 +36,20 @@ public static function buildQueryFromRequest(Request $request) } else { $query->whereIn('organisations.type', ['non-profit-organization', 'for-profit-organization']); } - - if (data_get($filters, 'filter.projectUuid')) { - $query->where('v2_projects.uuid', data_get($filters, 'filter.projectUuid')); - } - - if ($projectUuids = data_get($filters, 'filter.projectUuid')) { - if (is_array($projectUuids)) { - $query->whereIn('v2_projects.uuid', $projectUuids); - } else { - $query->where('v2_projects.uuid', $projectUuids); - } - } - + if (data_get($filters, 'filter.projectUuid')) { + $projectUuids = data_get($filters, 'filter.projectUuid'); + if (is_array($projectUuids)) { + $query->whereIn('v2_projects.uuid', $projectUuids); + } else { + $query->where('v2_projects.uuid', $projectUuids); + } + } if ($request->has('search')) { $searchTerm = $request->query('search'); $query->where(function ($query) use ($searchTerm) { $query->where('v2_projects.name', 'like', "%$searchTerm%"); }); } - return $query; } @@ -105,37 +99,44 @@ public static function getPolygonsByStatus() return []; } } - - public static function retrievePolygonUuidsByStatusForProject($projectUuid) + public static function retrievePolygonUuidsByStatusForProjects($projectUuids) { - $project = Project::where('uuid', $projectUuid)->first(); - $sitePolygons = $project->sitePolygons; - $statuses = ['needs-more-information', 'submitted', 'approved','draft']; - $polygons = []; - - foreach ($statuses as $status) { - $polygonsOfProject = $sitePolygons - ->where('status', $status) - ->pluck('poly_id'); - - $polygons[$status] = $polygonsOfProject; - } - + $statuses = ['needs-more-information', 'submitted', 'approved', 'draft']; + $polygons = []; + + foreach ($projectUuids as $projectUuid) { + $project = Project::where('uuid', $projectUuid)->first(); + if ($project) { + $sitePolygons = $project->sitePolygons; + + foreach ($statuses as $status) { + $polygonsOfProject = $sitePolygons + ->where('status', $status) + ->pluck('poly_id'); + + if (!isset($polygons[$status])) { + $polygons[$status] = []; + } + + $polygons[$status] = array_merge($polygons[$status], $polygonsOfProject->toArray()); + } + + + } else { + Log::warning("Project with UUID $projectUuid not found."); + } + } return $polygons; } + - public static function getPolygonsByStatusOfProject($request) - { - $projectUuid = TerrafundDashboardQueryHelper::buildQueryFromRequest($request) - ->pluck('v2_projects.uuid')->first(); - return self::retrievePolygonUuidsByStatusForProject($projectUuid); - } - public static function getPolygonsUuidsByStatusForProject($request) + public static function getPolygonsByStatusOfProjects($request) { - $projectUuid = $request->input('uuid'); - - return self::retrievePolygonUuidsByStatusForProject($projectUuid); + $projectUuids = TerrafundDashboardQueryHelper::buildQueryFromRequest($request) + ->pluck('v2_projects.uuid'); + return self::retrievePolygonUuidsByStatusForProjects($projectUuids); } + } diff --git a/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php b/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php index 6a2f980f6..80fff6f16 100644 --- a/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php +++ b/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php @@ -24,21 +24,13 @@ public function getPolygonsOfProject(Request $request): GetPolygonsResource public function getPolygonsDataByStatusOfProject(Request $request): GetPolygonsResource { - $polygonsIds = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProject($request); + $polygonsIds = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProjects($request); return new GetPolygonsResource([ 'data' => $polygonsIds, ]); } - public function getPolygonsUuidsByStatusForProject(Request $request): GetPolygonsResource - { - $polygonsIds = TerrafundDashboardQueryHelper::getPolygonsUuidsByStatusForProject($request); - - return new GetPolygonsResource([ - 'data' => $polygonsIds, - ]); - } public function getBboxOfCompleteProject(Request $request) { diff --git a/app/Http/Controllers/V2/Dashboard/ViewProjectController.php b/app/Http/Controllers/V2/Dashboard/ViewProjectController.php index 35cfb2828..85297acad 100644 --- a/app/Http/Controllers/V2/Dashboard/ViewProjectController.php +++ b/app/Http/Controllers/V2/Dashboard/ViewProjectController.php @@ -68,7 +68,7 @@ public function getAllProjectsAllowedToUser(Request $request) } else { if ($user->hasRole('government')) { try { - $projectUuids = Project::where('framework_key', 'terrafund')->where('country', $user->country)->pluck('uuid'); + $projectUuids = Project::where('framework_key', 'terrafund')->where('country', $user->country)->pluck('uuid')->toArray(); } catch (\Exception $e) { $errorMessage = $e->getMessage(); Log::error('Error fetching projects for government: ' . $errorMessage); @@ -77,7 +77,7 @@ public function getAllProjectsAllowedToUser(Request $request) } } elseif ($user->hasRole('funder')) { try { - $projectUuids = Project::where('framework_key', $user->program)->pluck('uuid'); + $projectUuids = Project::where('framework_key', $user->program)->pluck('uuid')->toArray(); } catch (\Exception $e) { $errorMessage = $e->getMessage(); Log::error('Error fetching projects for funder: ' . $errorMessage); @@ -87,7 +87,7 @@ public function getAllProjectsAllowedToUser(Request $request) } elseif ($user->hasRole('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'); + $projectUuids = Project::whereIn('id', $projectIds)->where('framework_key', 'terrafund')->pluck('uuid')->toArray(); } catch (\Exception $e) { $errorMessage = $e->getMessage(); Log::error('Error fetching projects for project developer: ' . $errorMessage); @@ -98,7 +98,6 @@ public function getAllProjectsAllowedToUser(Request $request) $projectUuids = null; } - Log::info('Returning this value: ' . json_encode($projectUuids)); $polygonsData = [ 'needs-more-information' => [], 'submitted' => [], @@ -109,43 +108,40 @@ public function getAllProjectsAllowedToUser(Request $request) $landscapes = data_get($request, 'filter.landscapes', []); $organisations = data_get($request, 'filter.organisationType', []); $country = data_get($request, 'filter.country', ''); - $baseFilter = [ + $filterWithProjects = [ 'filter' => [ 'country' => $country, 'programmes' => $frameworks, 'landscapes' => $landscapes, 'organisationType' => $organisations, + 'projectUuid' => $projectUuids ], ]; - foreach ($projectUuids as $uuid) { - $filterWithProject = array_merge_recursive($baseFilter, [ - 'filter' => [ - 'projectUuid' => $uuid, - ], - ]); - $request = new Request($filterWithProject); + + $request = new Request($filterWithProjects); try { - $polygonsResource = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProject($request); - foreach ($polygonsResource as $status => $polygons) { + $polygonsResource = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProjects($request); + if ($polygonsResource !== null) { + 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()); + Log::error('Error fetching polygons for project UUID ' . json_encode(['projectslist' => $projectUuids]) . ': ' . $e->getMessage()); } - } return response()->json([ - 'projectsUuids' => $projectUuids->toArray(), + 'projectsUuids' => $projectUuids, 'polygonsUuids' => $polygonsData, ]); } } catch (\Exception $e) { $errorMessage = $e->getMessage(); - Log::error('An error occurred: ' . $errorMessage); + Log::error('An error occurred at get projects allowed to user: ' . $errorMessage); return response()->json(['error' => 'An error occurred while fetching the data', 'message' => $errorMessage], 500); } diff --git a/app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php b/app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php index b686c1123..26ff7ac17 100644 --- a/app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php +++ b/app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php @@ -107,7 +107,7 @@ public function deletePolygonAndSitePolygon(string $uuid) return response()->json(['message' => 'All related polygons and site polygons deleted successfully.', 'uuid' => $primaryUuid]); } catch (\Exception $e) { - Log::error('An error occurred: ' . $e->getMessage()); + Log::error('An error occurred at delete function: ' . $e->getMessage()); return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500); } @@ -143,7 +143,7 @@ public function deleteMultiplePolygonsAndSitePolygons(Request $request) return response()->json($response); } catch (\Exception $e) { - Log::error('An error occurred: ' . $e->getMessage()); + Log::error('An error occurred at delete multiple polygons and sites: ' . $e->getMessage()); return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500); } @@ -192,7 +192,7 @@ public function updateGeometry(string $uuid, Request $request) return response()->json(['message' => 'Geometry updated successfully.', 'geometry' => $geometry, 'uuid' => $uuid]); } catch (\Exception $e) { - return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500); + return response()->json(['error' => 'An error occurred at update geometry: ' . $e->getMessage()], 500); } } @@ -231,8 +231,7 @@ public function updateSitePolygon(string $uuid, Request $request) return response()->json(['message' => 'Site polygon updated successfully']); } catch (\Exception $e) { - // Handle other exceptions - return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500); + return response()->json(['error' => 'An error occurred at update site polygons: ' . $e->getMessage()], 500); } } @@ -263,8 +262,7 @@ public function createSitePolygonNewVersion(string $uuid, Request $request) return response()->json(['message' => 'Site polygon version created successfully'], 201); } catch (\Exception $e) { - // Handle other exceptions - return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500); + return response()->json(['error' => 'An error occurred at creating version: ' . $e->getMessage()], 500); } } @@ -363,8 +361,7 @@ public function createSitePolygon(string $uuid, string $siteUuid, Request $reque return response()->json(['message' => 'Site polygon created successfully', 'uuid' => $sitePolygon, 'area' => $areaHectares], 201); } catch (\Exception $e) { - // Handle other exceptions - return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500); + return response()->json(['error' => 'An error occurred at create site polygon: ' . $e->getMessage()], 500); } } From 0b9c9f5c372092ae781a4d7699d19a9471fb64a5 Mon Sep 17 00:00:00 2001 From: JORGE Date: Tue, 5 Nov 2024 16:42:22 -0400 Subject: [PATCH 6/7] [TM-1343] add filter by statuses of polygons --- app/Helpers/TerrafundDashboardQueryHelper.php | 56 +++++++++---------- .../V2/Dashboard/ViewProjectController.php | 1 + 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/app/Helpers/TerrafundDashboardQueryHelper.php b/app/Helpers/TerrafundDashboardQueryHelper.php index dfc9977a4..4c36856fc 100644 --- a/app/Helpers/TerrafundDashboardQueryHelper.php +++ b/app/Helpers/TerrafundDashboardQueryHelper.php @@ -99,36 +99,34 @@ public static function getPolygonsByStatus() return []; } } - public static function retrievePolygonUuidsByStatusForProjects($projectUuids) + public static function retrievePolygonUuidsByStatusForProjects($projectUuids, $requestedStatuses = null) { - $statuses = ['needs-more-information', 'submitted', 'approved', 'draft']; - $polygons = []; - - foreach ($projectUuids as $projectUuid) { - $project = Project::where('uuid', $projectUuid)->first(); - if ($project) { - $sitePolygons = $project->sitePolygons; - - foreach ($statuses as $status) { - $polygonsOfProject = $sitePolygons - ->where('status', $status) - ->pluck('poly_id'); - - if (!isset($polygons[$status])) { - $polygons[$status] = []; - } - - $polygons[$status] = array_merge($polygons[$status], $polygonsOfProject->toArray()); - } - - - } else { - Log::warning("Project with UUID $projectUuid not found."); - } - } + $statuses = $requestedStatuses ?? ['needs-more-information', 'submitted', 'approved', 'draft']; + $polygons = []; + + foreach ($projectUuids as $projectUuid) { + $project = Project::where('uuid', $projectUuid)->first(); + if ($project) { + $sitePolygons = $project->sitePolygons; + + foreach ($statuses as $status) { + $polygonsOfProject = $sitePolygons + ->where('status', $status) + ->pluck('poly_id'); + + if (!isset($polygons[$status])) { + $polygons[$status] = []; + } + + $polygons[$status] = array_merge($polygons[$status], $polygonsOfProject->toArray()); + } + } else { + Log::warning("Project with UUID $projectUuid not found."); + } + } + return $polygons; } - @@ -136,7 +134,9 @@ public static function getPolygonsByStatusOfProjects($request) { $projectUuids = TerrafundDashboardQueryHelper::buildQueryFromRequest($request) ->pluck('v2_projects.uuid'); - return self::retrievePolygonUuidsByStatusForProjects($projectUuids); + + $requestedStatuses = $request->input('statuses'); + return self::retrievePolygonUuidsByStatusForProjects($projectUuids, $requestedStatuses); } } diff --git a/app/Http/Controllers/V2/Dashboard/ViewProjectController.php b/app/Http/Controllers/V2/Dashboard/ViewProjectController.php index 85297acad..9de6682ba 100644 --- a/app/Http/Controllers/V2/Dashboard/ViewProjectController.php +++ b/app/Http/Controllers/V2/Dashboard/ViewProjectController.php @@ -116,6 +116,7 @@ public function getAllProjectsAllowedToUser(Request $request) 'organisationType' => $organisations, 'projectUuid' => $projectUuids ], + 'statuses' => ['approved'] ]; From 34fd619e689e66ca729fe5d92f333ec03daf97f1 Mon Sep 17 00:00:00 2001 From: JORGE Date: Tue, 5 Nov 2024 16:43:04 -0400 Subject: [PATCH 7/7] [TM-1343] make lint fix --- app/Helpers/TerrafundDashboardQueryHelper.php | 36 +++++++++---------- .../V2/Dashboard/GetPolygonsController.php | 1 - .../V2/Dashboard/ViewProjectController.php | 22 ++++++------ 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/app/Helpers/TerrafundDashboardQueryHelper.php b/app/Helpers/TerrafundDashboardQueryHelper.php index 4c36856fc..2fb56b1c2 100644 --- a/app/Helpers/TerrafundDashboardQueryHelper.php +++ b/app/Helpers/TerrafundDashboardQueryHelper.php @@ -36,20 +36,21 @@ public static function buildQueryFromRequest(Request $request) } else { $query->whereIn('organisations.type', ['non-profit-organization', 'for-profit-organization']); } - if (data_get($filters, 'filter.projectUuid')) { - $projectUuids = data_get($filters, 'filter.projectUuid'); - if (is_array($projectUuids)) { - $query->whereIn('v2_projects.uuid', $projectUuids); - } else { - $query->where('v2_projects.uuid', $projectUuids); - } - } + if (data_get($filters, 'filter.projectUuid')) { + $projectUuids = data_get($filters, 'filter.projectUuid'); + if (is_array($projectUuids)) { + $query->whereIn('v2_projects.uuid', $projectUuids); + } else { + $query->where('v2_projects.uuid', $projectUuids); + } + } if ($request->has('search')) { $searchTerm = $request->query('search'); $query->where(function ($query) use ($searchTerm) { $query->where('v2_projects.name', 'like', "%$searchTerm%"); }); } + return $query; } @@ -99,44 +100,43 @@ public static function getPolygonsByStatus() return []; } } + public static function retrievePolygonUuidsByStatusForProjects($projectUuids, $requestedStatuses = null) { $statuses = $requestedStatuses ?? ['needs-more-information', 'submitted', 'approved', 'draft']; $polygons = []; - + foreach ($projectUuids as $projectUuid) { $project = Project::where('uuid', $projectUuid)->first(); if ($project) { $sitePolygons = $project->sitePolygons; - + foreach ($statuses as $status) { $polygonsOfProject = $sitePolygons ->where('status', $status) ->pluck('poly_id'); - - if (!isset($polygons[$status])) { + + if (! isset($polygons[$status])) { $polygons[$status] = []; } - + $polygons[$status] = array_merge($polygons[$status], $polygonsOfProject->toArray()); } } else { Log::warning("Project with UUID $projectUuid not found."); } } - + return $polygons; } - - public static function getPolygonsByStatusOfProjects($request) { $projectUuids = TerrafundDashboardQueryHelper::buildQueryFromRequest($request) ->pluck('v2_projects.uuid'); - + $requestedStatuses = $request->input('statuses'); + return self::retrievePolygonUuidsByStatusForProjects($projectUuids, $requestedStatuses); } - } diff --git a/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php b/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php index 80fff6f16..2d31c8584 100644 --- a/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php +++ b/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php @@ -31,7 +31,6 @@ public function getPolygonsDataByStatusOfProject(Request $request): GetPolygonsR ]); } - public function getBboxOfCompleteProject(Request $request) { try { diff --git a/app/Http/Controllers/V2/Dashboard/ViewProjectController.php b/app/Http/Controllers/V2/Dashboard/ViewProjectController.php index 9de6682ba..168c83449 100644 --- a/app/Http/Controllers/V2/Dashboard/ViewProjectController.php +++ b/app/Http/Controllers/V2/Dashboard/ViewProjectController.php @@ -114,25 +114,25 @@ public function getAllProjectsAllowedToUser(Request $request) 'programmes' => $frameworks, 'landscapes' => $landscapes, 'organisationType' => $organisations, - 'projectUuid' => $projectUuids + 'projectUuid' => $projectUuids, ], - 'statuses' => ['approved'] + 'statuses' => ['approved'], ]; - $request = new Request($filterWithProjects); + $request = new Request($filterWithProjects); - try { - $polygonsResource = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProjects($request); - if ($polygonsResource !== null) { - foreach ($polygonsResource as $status => $polygons) { + try { + $polygonsResource = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProjects($request); + if ($polygonsResource !== null) { + 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 ' . json_encode(['projectslist' => $projectUuids]) . ': ' . $e->getMessage()); + } } + } catch (\Exception $e) { + Log::error('Error fetching polygons for project UUID ' . json_encode(['projectslist' => $projectUuids]) . ': ' . $e->getMessage()); + } return response()->json([ 'projectsUuids' => $projectUuids,