diff --git a/app/Helpers/TerrafundDashboardQueryHelper.php b/app/Helpers/TerrafundDashboardQueryHelper.php index c607623eb..2fb56b1c2 100644 --- a/app/Helpers/TerrafundDashboardQueryHelper.php +++ b/app/Helpers/TerrafundDashboardQueryHelper.php @@ -36,11 +36,14 @@ 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')); + $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) { @@ -98,36 +101,42 @@ public static function getPolygonsByStatus() } } - public static function retrievePolygonUuidsByStatusForProject($projectUuid) + public static function retrievePolygonUuidsByStatusForProjects($projectUuids, $requestedStatuses = null) { - $project = Project::where('uuid', $projectUuid)->first(); - $sitePolygons = $project->sitePolygons; - $statuses = ['needs-more-information', 'submitted', 'approved','draft']; + $statuses = $requestedStatuses ?? ['needs-more-information', 'submitted', 'approved', 'draft']; $polygons = []; - foreach ($statuses as $status) { - $polygonsOfProject = $sitePolygons - ->where('status', $status) - ->pluck('poly_id'); + 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'); - $polygons[$status] = $polygonsOfProject; + 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) + public static function getPolygonsByStatusOfProjects($request) { - $projectUuid = TerrafundDashboardQueryHelper::buildQueryFromRequest($request) - ->pluck('v2_projects.uuid')->first(); + $projectUuids = TerrafundDashboardQueryHelper::buildQueryFromRequest($request) + ->pluck('v2_projects.uuid'); - return self::retrievePolygonUuidsByStatusForProject($projectUuid); - } - - public static function getPolygonsUuidsByStatusForProject($request) - { - $projectUuid = $request->input('uuid'); + $requestedStatuses = $request->input('statuses'); - return self::retrievePolygonUuidsByStatusForProject($projectUuid); + return self::retrievePolygonUuidsByStatusForProjects($projectUuids, $requestedStatuses); } } diff --git a/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php b/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php index 7c2e9db37..2d31c8584 100644 --- a/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php +++ b/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php @@ -22,18 +22,9 @@ public function getPolygonsOfProject(Request $request): GetPolygonsResource ]); } - public function getPolygonsByStatusOfProject(Request $request): GetPolygonsResource + public function getPolygonsDataByStatusOfProject(Request $request): GetPolygonsResource { - $polygonsIds = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProject($request); - - return new GetPolygonsResource([ - 'data' => $polygonsIds, - ]); - } - - public function getPolygonsUuidsByStatusForProject(Request $request): GetPolygonsResource - { - $polygonsIds = TerrafundDashboardQueryHelper::getPolygonsUuidsByStatusForProject($request); + $polygonsIds = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProjects($request); return new GetPolygonsResource([ 'data' => $polygonsIds, diff --git a/app/Http/Controllers/V2/Dashboard/GetProjectsController.php b/app/Http/Controllers/V2/Dashboard/GetProjectsController.php index adb67d569..79f24c4a4 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..86ca5fdea 100644 --- a/app/Http/Controllers/V2/Dashboard/TotalTerrafundHeaderDashboardController.php +++ b/app/Http/Controllers/V2/Dashboard/TotalTerrafundHeaderDashboardController.php @@ -2,12 +2,16 @@ 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\App; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Redis; @@ -50,4 +54,23 @@ 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..168c83449 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 */ @@ -68,7 +68,7 @@ public function getAllProjectsAllowedToUser() } 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() } } 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() } 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,38 +98,51 @@ public function getAllProjectsAllowedToUser() $projectUuids = null; } - Log::info('Returning this value: ' . json_encode($projectUuids)); $polygonsData = [ 'needs-more-information' => [], 'submitted' => [], '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', ''); + $filterWithProjects = [ + 'filter' => [ + 'country' => $country, + 'programmes' => $frameworks, + 'landscapes' => $landscapes, + 'organisationType' => $organisations, + 'projectUuid' => $projectUuids, + ], + 'statuses' => ['approved'], + ]; - foreach ($projectUuids as $uuid) { - Log::info('Fetching polygons for project UUID ' . $uuid); - $request = new Request(['uuid' => $uuid]); - try { - $polygonsResource = TerrafundDashboardQueryHelper::getPolygonsByStatusOfProject($request); + $request = new Request($filterWithProjects); + + 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 ' . $uuid . ': ' . $e->getMessage()); } + } catch (\Exception $e) { + 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); } } 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']);