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-1343] restore functions to map #551

Merged
merged 7 commits into from
Nov 5, 2024
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
53 changes: 31 additions & 22 deletions app/Helpers/TerrafundDashboardQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
13 changes: 2 additions & 11 deletions app/Http/Controllers/V2/Dashboard/GetPolygonsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions app/Http/Controllers/V2/Dashboard/GetProjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
41 changes: 27 additions & 14 deletions app/Http/Controllers/V2/Dashboard/ViewProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion routes/api_v2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
Loading