From b420d849e8fb6cb2fc719af378c8fe3a746719f7 Mon Sep 17 00:00:00 2001 From: Jose Carlos Laura Ramirez Date: Mon, 10 Jun 2024 12:06:14 -0400 Subject: [PATCH 1/8] add endpoint for updating status --- .../Entities/UpdateEntityStatusController.php | 70 +++++++++++++++++++ .../AuditStatus/AuditStatusUpdateRequest.php | 24 +++++++ .../definitions/AuditStatusUpdateRequest.yml | 13 ++++ openapi-src/V2/definitions/_index.yml | 2 + .../Entity/put-v2-entity-uuid-status.yml | 19 +++++ openapi-src/V2/paths/_index.yml | 3 + resources/docs/swagger-v2.yml | 34 +++++++++ routes/api_v2.php | 2 + 8 files changed, 167 insertions(+) create mode 100644 app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php create mode 100644 app/Http/Requests/V2/AuditStatus/AuditStatusUpdateRequest.php create mode 100644 openapi-src/V2/definitions/AuditStatusUpdateRequest.yml create mode 100644 openapi-src/V2/paths/Entity/put-v2-entity-uuid-status.yml diff --git a/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php b/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php new file mode 100644 index 000000000..3596eaece --- /dev/null +++ b/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php @@ -0,0 +1,70 @@ +authorize('update', $entity); + + + if (! $this->canChangeStatus($entity, $request->status)) { + return response()->json(['message' => 'Cannot change status'], 400); + } + + $body = $request->all(); + $status = $body['status']; + + $entity->status = $status; + $entity->save(); + + if (isset($body['status'])) { + $this->saveAuditStatus(get_class($entity), $entity->id, $status, $body['comment'], $body['type']); + } elseif (isset($body['is_active'])) { + AuditStatus::where('auditable_id', $entity->id) + ->where('type', $body['type']) + ->where('is_active', true) + ->update(['is_active' => false]); + $this->saveAuditStatus(get_class($entity), $entity->id, $status, $body['comment'], $body['type'], $body['is_active'], $body['request_removed']); + } + + return $entity->createResource(); + } + + private function canChangeStatus($entity, $status): bool + { + switch(get_class($entity)) { + case 'App\Models\V2\Sites\Site': + return $this->canChangeSiteStatusTo($entity, $status); + case 'App\Models\V2\Sites\SitePolygon': + return $this->canChangeSitePolygonStatusTo($entity, $status); + default: + return true; + } + } + + private function canChangeSiteStatusTo($entity, $status) + { + if ($status === 'approved') { + return ! SitePolygon::where('site_id', $entity->id)->where('status', 'approved')->exists(); + } + + return true; + } + + private function canChangeSitePolygonStatusTo($entity, $status) + { + //TODO ask Cesar how to handle this one. + return true; + } +} diff --git a/app/Http/Requests/V2/AuditStatus/AuditStatusUpdateRequest.php b/app/Http/Requests/V2/AuditStatus/AuditStatusUpdateRequest.php new file mode 100644 index 000000000..3f49cf9ad --- /dev/null +++ b/app/Http/Requests/V2/AuditStatus/AuditStatusUpdateRequest.php @@ -0,0 +1,24 @@ + 'sometimes|nullable|string', + 'comment' => 'sometimes|nullable|string', + 'status' => 'sometimes|nullable|string', + 'is_active' => 'sometimes|nullable|boolean', + 'request_removed' => 'sometimes|nullable|boolean', + ]; + } +} diff --git a/openapi-src/V2/definitions/AuditStatusUpdateRequest.yml b/openapi-src/V2/definitions/AuditStatusUpdateRequest.yml new file mode 100644 index 000000000..718063017 --- /dev/null +++ b/openapi-src/V2/definitions/AuditStatusUpdateRequest.yml @@ -0,0 +1,13 @@ +title: AuditStatusUpdateRequest +type: object +properties: + type: + type: string + comment: + type: string + status: + type: string + is_active: + type: boolean + request_removed: + type: boolean diff --git a/openapi-src/V2/definitions/_index.yml b/openapi-src/V2/definitions/_index.yml index 94046a463..778bd0605 100644 --- a/openapi-src/V2/definitions/_index.yml +++ b/openapi-src/V2/definitions/_index.yml @@ -324,3 +324,5 @@ GeojsonData: $ref: './GeojsonData.yml' EntityTypeResponse: $ref: './EntityTypeResponse.yml' +AuditStatusUpdateRequest: + $ref: './AuditStatusUpdateRequest.yml' diff --git a/openapi-src/V2/paths/Entity/put-v2-entity-uuid-status.yml b/openapi-src/V2/paths/Entity/put-v2-entity-uuid-status.yml new file mode 100644 index 000000000..1e9fc5481 --- /dev/null +++ b/openapi-src/V2/paths/Entity/put-v2-entity-uuid-status.yml @@ -0,0 +1,19 @@ + +summary: Update the status of a specific entity +tags: + - V2 Projects + - V2 Sites + - V2 SitePolygons +parameters: + - type: string + name: ENTITY + in: path + required: true + description: allowed values project/site/site-polygons + - type: string + name: UUID + in: path + required: true +responses: + '200': + description: OK diff --git a/openapi-src/V2/paths/_index.yml b/openapi-src/V2/paths/_index.yml index b957bf256..daca65838 100644 --- a/openapi-src/V2/paths/_index.yml +++ b/openapi-src/V2/paths/_index.yml @@ -2604,3 +2604,6 @@ /v2/type-entity: get: $ref: './Entity/get-v2-type-entity.yml' +/v2/{ENTITY}/{UUID}/status: + put: + $ref: './Entity/put-v2-entity-uuid-status.yml' diff --git a/resources/docs/swagger-v2.yml b/resources/docs/swagger-v2.yml index d9ef22534..1e960c31d 100644 --- a/resources/docs/swagger-v2.yml +++ b/resources/docs/swagger-v2.yml @@ -44717,6 +44717,20 @@ definitions: items: type: number description: Bounding box of the entity + AuditStatusUpdateRequest: + title: AuditStatusUpdateRequest + type: object + properties: + type: + type: string + comment: + type: string + status: + type: string + is_active: + type: boolean + request_removed: + type: boolean paths: '/v2/tree-species/{entity}/{UUID}': get: @@ -95911,3 +95925,23 @@ paths: error: type: string description: Error message + '/v2/{ENTITY}/{UUID}/status': + put: + summary: Update the status of a specific entity + tags: + - V2 Projects + - V2 Sites + - V2 SitePolygons + parameters: + - type: string + name: ENTITY + in: path + required: true + description: allowed values project/site/site-polygons + - type: string + name: UUID + in: path + required: true + responses: + '200': + description: OK diff --git a/routes/api_v2.php b/routes/api_v2.php index 4af90f7fc..73240e2f9 100644 --- a/routes/api_v2.php +++ b/routes/api_v2.php @@ -31,6 +31,7 @@ use App\Http\Controllers\V2\Entities\AdminStatusEntityController; use App\Http\Controllers\V2\Entities\EntityTypeController; use App\Http\Controllers\V2\Entities\SubmitEntityWithFormController; +use App\Http\Controllers\V2\Entities\UpdateEntityStatusController; use App\Http\Controllers\V2\Entities\UpdateEntityWithFormController; use App\Http\Controllers\V2\Entities\ViewEntityController; use App\Http\Controllers\V2\Entities\ViewEntityWithFormController; @@ -549,6 +550,7 @@ ModelInterfaceBindingMiddleware::with(EntityModel::class, function () { Route::get('/{entity}', ViewEntityController::class); + Route::put('/{entity}/status', UpdateEntityStatusController::class); }); Route::prefix('project-reports')->group(function () { From 49a7d73cf1b98e901db556b17780578f34c4679b Mon Sep 17 00:00:00 2001 From: Jose Carlos Laura Ramirez Date: Mon, 10 Jun 2024 15:41:52 -0400 Subject: [PATCH 2/8] change EntityModel -> AuditableModel and add missing policy --- .../Entities/UpdateEntityStatusController.php | 35 +++++++++---------- app/Policies/V2/Sites/SitePolygonPolicy.php | 15 ++++++++ routes/api_v2.php | 3 ++ 3 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 app/Policies/V2/Sites/SitePolygonPolicy.php diff --git a/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php b/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php index 3596eaece..60c914558 100644 --- a/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php +++ b/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php @@ -5,64 +5,63 @@ use App\Http\Controllers\Controller; use App\Http\Requests\V2\AuditStatus\AuditStatusUpdateRequest; use App\Models\Traits\SaveAuditStatusTrait; +use App\Models\V2\AuditableModel; use App\Models\V2\AuditStatus\AuditStatus; -use App\Models\V2\EntityModel; use App\Models\V2\Sites\SitePolygon; class UpdateEntityStatusController extends Controller { use SaveAuditStatusTrait; - public function __invoke(AuditStatusUpdateRequest $request, EntityModel $entity) + public function __invoke(AuditStatusUpdateRequest $request, AuditableModel $auditable) { - $this->authorize('update', $entity); + $this->authorize('update', $auditable); - - if (! $this->canChangeStatus($entity, $request->status)) { + if (! $this->canChangeStatus($auditable, $request->status)) { return response()->json(['message' => 'Cannot change status'], 400); } $body = $request->all(); $status = $body['status']; - $entity->status = $status; - $entity->save(); + $auditable->status = $status; + $auditable->save(); if (isset($body['status'])) { - $this->saveAuditStatus(get_class($entity), $entity->id, $status, $body['comment'], $body['type']); + $this->saveAuditStatus(get_class($auditable), $auditable->id, $status, $body['comment'], $body['type']); } elseif (isset($body['is_active'])) { - AuditStatus::where('auditable_id', $entity->id) + AuditStatus::where('auditable_id', $auditable->id) ->where('type', $body['type']) ->where('is_active', true) ->update(['is_active' => false]); - $this->saveAuditStatus(get_class($entity), $entity->id, $status, $body['comment'], $body['type'], $body['is_active'], $body['request_removed']); + $this->saveAuditStatus(get_class($auditable), $auditable->id, $status, $body['comment'], $body['type'], $body['is_active'], $body['request_removed']); } - return $entity->createResource(); + return $auditable; } - private function canChangeStatus($entity, $status): bool + private function canChangeStatus($auditable, $status): bool { - switch(get_class($entity)) { + switch(get_class($auditable)) { case 'App\Models\V2\Sites\Site': - return $this->canChangeSiteStatusTo($entity, $status); + return $this->canChangeSiteStatusTo($auditable, $status); case 'App\Models\V2\Sites\SitePolygon': - return $this->canChangeSitePolygonStatusTo($entity, $status); + return $this->canChangeSitePolygonStatusTo($auditable, $status); default: return true; } } - private function canChangeSiteStatusTo($entity, $status) + private function canChangeSiteStatusTo($auditable, $status) { if ($status === 'approved') { - return ! SitePolygon::where('site_id', $entity->id)->where('status', 'approved')->exists(); + return ! SitePolygon::where('site_id', $auditable->id)->where('status', 'approved')->exists(); } return true; } - private function canChangeSitePolygonStatusTo($entity, $status) + private function canChangeSitePolygonStatusTo($auditable, $status) { //TODO ask Cesar how to handle this one. return true; diff --git a/app/Policies/V2/Sites/SitePolygonPolicy.php b/app/Policies/V2/Sites/SitePolygonPolicy.php new file mode 100644 index 000000000..47cf253c1 --- /dev/null +++ b/app/Policies/V2/Sites/SitePolygonPolicy.php @@ -0,0 +1,15 @@ + Date: Tue, 11 Jun 2024 14:44:45 -0400 Subject: [PATCH 3/8] move criteria site to helper for reuse --- app/Helpers/GeometryHelper.php | 27 ++++++++++++++ .../Entities/UpdateEntityStatusController.php | 37 ++++++++++++++++++- .../TerrafundCreateGeometryController.php | 24 ++---------- 3 files changed, 65 insertions(+), 23 deletions(-) diff --git a/app/Helpers/GeometryHelper.php b/app/Helpers/GeometryHelper.php index 96c918bcc..c30a3c4db 100644 --- a/app/Helpers/GeometryHelper.php +++ b/app/Helpers/GeometryHelper.php @@ -4,6 +4,8 @@ use App\Models\V2\PolygonGeometry; use App\Models\V2\Projects\Project; +use App\Models\V2\Sites\CriteriaSite; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; class GeometryHelper @@ -117,4 +119,29 @@ public static function getPolygonsBbox($polygonsIds) return [$minX, $minY, $maxX, $maxY]; } + + public static function getCriteriaDataForPolygonGeometry($polygonGeometry) + { + // Fetch data from criteria_site with distinct criteria_id based on the latest created_at + $criteriaDataQuery = 'SELECT criteria_id, MAX(created_at) AS latest_created_at + FROM criteria_site + WHERE polygon_id = ? + GROUP BY criteria_id'; + + $criteriaData = DB::select($criteriaDataQuery, [$polygonGeometry->uuid]); + + // Determine the validity of each criteria + $criteriaList = []; + foreach ($criteriaData as $criteria) { + $criteriaId = $criteria->criteria_id; + $valid = CriteriaSite::where(['polygon_id' => $polygonGeometry->uuid, 'criteria_id' => $criteriaId])->orderBy('created_at', 'desc')->select('valid')->first()?->valid; + $criteriaList[] = [ + 'criteria_id' => $criteriaId, + 'latest_created_at' => $criteria->latest_created_at, + 'valid' => $valid, + ]; + } + + return $criteriaList; + } } diff --git a/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php b/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php index 60c914558..3f21307c0 100644 --- a/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php +++ b/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php @@ -2,12 +2,14 @@ namespace App\Http\Controllers\V2\Entities; +use App\Helpers\GeometryHelper; use App\Http\Controllers\Controller; use App\Http\Requests\V2\AuditStatus\AuditStatusUpdateRequest; use App\Models\Traits\SaveAuditStatusTrait; use App\Models\V2\AuditableModel; use App\Models\V2\AuditStatus\AuditStatus; use App\Models\V2\Sites\SitePolygon; +use App\Services\PolygonService; class UpdateEntityStatusController extends Controller { @@ -61,9 +63,40 @@ private function canChangeSiteStatusTo($auditable, $status) return true; } - private function canChangeSitePolygonStatusTo($auditable, $status) + private function canChangeSitePolygonStatusTo($sitePolygon, $status) { - //TODO ask Cesar how to handle this one. + if ($status === 'approved') { + $geometry = $sitePolygon->polygonGeometry()->get(); + + if ($geometry === null) { + return false; + } + + $criteriaList = GeometryHelper::getCriteriaDataForPolygonGeometry($geometry); + + if (empty($criteriaList)) { + return false; + } + + $criteriaList = array_filter($criteriaList, function ($criteria) { + return $criteria->criteria_id == PolygonService::ESTIMATED_AREA_CRITERIA_ID; + }); + + $canApprove = true; + foreach ($criteriaList as $criteria) { + Log::info('criteria->valid'); + Log::info($criteria->valid); + if (! $criteria->valid) { + $canApprove = false; + + break; + } + } + + return $canApprove; + + } + return true; } } diff --git a/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php b/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php index b550bb0df..7562f95cd 100644 --- a/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php +++ b/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php @@ -2,9 +2,9 @@ namespace App\Http\Controllers\V2\Terrafund; +use App\Helpers\GeometryHelper; use App\Http\Controllers\Controller; use App\Models\V2\PolygonGeometry; -use App\Models\V2\Sites\CriteriaSite; use App\Models\V2\Sites\SitePolygon; use App\Models\V2\WorldCountryGeneralized; use App\Services\PolygonService; @@ -313,30 +313,12 @@ public function getCriteriaData(Request $request) return response()->json(['error' => 'Polygon not found for the given UUID'], 404); } - // Fetch data from criteria_site with distinct criteria_id based on the latest created_at - $criteriaDataQuery = 'SELECT criteria_id, MAX(created_at) AS latest_created_at - FROM criteria_site - WHERE polygon_id = ? - GROUP BY criteria_id'; + $criteriaList = GeometryHelper::getCriteriaDataForPolygonGeometry($geometry); - $criteriaData = DB::select($criteriaDataQuery, [$uuid]); - - if (empty($criteriaData)) { + if (empty($criteriaList)) { return response()->json(['error' => 'Criteria data not found for the given polygon ID'], 404); } - // Determine the validity of each criteria - $criteriaList = []; - foreach ($criteriaData as $criteria) { - $criteriaId = $criteria->criteria_id; - $valid = CriteriaSite::where(['polygon_id' => $uuid, 'criteria_id' => $criteriaId])->orderBy('created_at', 'desc')->select('valid')->first()?->valid; - $criteriaList[] = [ - 'criteria_id' => $criteriaId, - 'latest_created_at' => $criteria->latest_created_at, - 'valid' => $valid, - ]; - } - return response()->json(['polygon_id' => $uuid, 'criteria_list' => $criteriaList]); } From 9172ff87bac575c054d00a1d36b80893546a6ff2 Mon Sep 17 00:00:00 2001 From: Jose Carlos Laura Ramirez Date: Tue, 11 Jun 2024 15:18:14 -0400 Subject: [PATCH 4/8] add criteria check for site polygon approval --- .../V2/Entities/UpdateEntityStatusController.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php b/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php index 3f21307c0..805be8db9 100644 --- a/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php +++ b/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php @@ -66,7 +66,7 @@ private function canChangeSiteStatusTo($auditable, $status) private function canChangeSitePolygonStatusTo($sitePolygon, $status) { if ($status === 'approved') { - $geometry = $sitePolygon->polygonGeometry()->get(); + $geometry = $sitePolygon->polygonGeometry()->first(); if ($geometry === null) { return false; @@ -79,14 +79,12 @@ private function canChangeSitePolygonStatusTo($sitePolygon, $status) } $criteriaList = array_filter($criteriaList, function ($criteria) { - return $criteria->criteria_id == PolygonService::ESTIMATED_AREA_CRITERIA_ID; + return $criteria['criteria_id'] !== PolygonService::ESTIMATED_AREA_CRITERIA_ID; }); $canApprove = true; foreach ($criteriaList as $criteria) { - Log::info('criteria->valid'); - Log::info($criteria->valid); - if (! $criteria->valid) { + if (! $criteria['valid']) { $canApprove = false; break; @@ -94,7 +92,6 @@ private function canChangeSitePolygonStatusTo($sitePolygon, $status) } return $canApprove; - } return true; From 9212137e5f9e8a95926f170b0a5defbcbda97c9b Mon Sep 17 00:00:00 2001 From: Jose Carlos Laura Ramirez Date: Tue, 11 Jun 2024 15:25:16 -0400 Subject: [PATCH 5/8] move endpoint to auditable path --- .../UpdateAuditableStatusController.php} | 4 ++-- routes/api_v2.php | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) rename app/Http/Controllers/V2/{Entities/UpdateEntityStatusController.php => Auditable/UpdateAuditableStatusController.php} (96%) diff --git a/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php b/app/Http/Controllers/V2/Auditable/UpdateAuditableStatusController.php similarity index 96% rename from app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php rename to app/Http/Controllers/V2/Auditable/UpdateAuditableStatusController.php index 805be8db9..96e1c0d4d 100644 --- a/app/Http/Controllers/V2/Entities/UpdateEntityStatusController.php +++ b/app/Http/Controllers/V2/Auditable/UpdateAuditableStatusController.php @@ -1,6 +1,6 @@ group(function () { Route::get('/{projectReport}/files', ViewProjectReportGalleryController::class); Route::get('/{projectReport}/image/locations', ProjectReportImageLocationsController::class); @@ -687,6 +683,10 @@ function () { Route::get('/{auditable}', GetAuditStatusController::class); }, prefix: 'audit-status'); +ModelInterfaceBindingMiddleware::with(AuditableModel::class, function () { + Route::put('/{auditable}/status', UpdateAuditableStatusController::class); +}); + Route::prefix('dashboard')->group(function () { Route::get('/restoration-strategy', ViewRestorationStrategyController::class); Route::get('/jobs-created', GetJobsCreatedController::class); From 6362a497e1497592b53784beff6647024d7c0d58 Mon Sep 17 00:00:00 2001 From: Jose Carlos Laura Ramirez Date: Tue, 11 Jun 2024 15:40:35 -0400 Subject: [PATCH 6/8] add a policy for updating Site Polygons --- app/Policies/V2/Sites/SitePolygonPolicy.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/Policies/V2/Sites/SitePolygonPolicy.php b/app/Policies/V2/Sites/SitePolygonPolicy.php index 47cf253c1..8781fe005 100644 --- a/app/Policies/V2/Sites/SitePolygonPolicy.php +++ b/app/Policies/V2/Sites/SitePolygonPolicy.php @@ -3,6 +3,7 @@ namespace App\Policies\V2\Sites; use App\Models\User; +use App\Models\V2\Sites\Site; use App\Models\V2\Sites\SitePolygon; use App\Policies\Policy; @@ -10,6 +11,17 @@ class SitePolygonPolicy extends Policy { public function update(?User $user, ?SitePolygon $sitePolygon = null): bool { - return true; + $site = $sitePolygon->site()->first(); + + if ($user->can('framework-' . $site->framework_key)) { + return true; + } + + return $user->can('manage-own') && $this->isTheirs($user, $site); + } + + protected function isTheirs(?User $user, ?Site $site = null): bool + { + return $user->organisation_id == $site->project->organisation_id || $user->projects->contains($site->project_id); } } From 80008552af3bea7ac652d126d4162a8e6a24be35 Mon Sep 17 00:00:00 2001 From: Jose Carlos Laura Ramirez Date: Tue, 11 Jun 2024 16:26:15 -0400 Subject: [PATCH 7/8] fix problem with parameters missing in definitio --- .../Projects/get-v2-projects-uuid-site-polygons-all.yml | 5 +++++ .../V2/paths/Sites/get-v2-sites-uuid-check-approve.yml | 2 +- resources/docs/swagger-v2.yml | 7 ++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/openapi-src/V2/paths/Projects/get-v2-projects-uuid-site-polygons-all.yml b/openapi-src/V2/paths/Projects/get-v2-projects-uuid-site-polygons-all.yml index 741f37956..7af2ea421 100644 --- a/openapi-src/V2/paths/Projects/get-v2-projects-uuid-site-polygons-all.yml +++ b/openapi-src/V2/paths/Projects/get-v2-projects-uuid-site-polygons-all.yml @@ -4,6 +4,11 @@ tags: - V2 Projects - V2 Sites - V2 SitePolygons +parameters: + - type: string + name: UUID + in: path + required: true produces: - application/json responses: diff --git a/openapi-src/V2/paths/Sites/get-v2-sites-uuid-check-approve.yml b/openapi-src/V2/paths/Sites/get-v2-sites-uuid-check-approve.yml index d9e35e634..1ba6f053d 100644 --- a/openapi-src/V2/paths/Sites/get-v2-sites-uuid-check-approve.yml +++ b/openapi-src/V2/paths/Sites/get-v2-sites-uuid-check-approve.yml @@ -4,7 +4,7 @@ tags: - V2 Sites parameters: - type: string - name: UUID + name: site in: path required: true responses: diff --git a/resources/docs/swagger-v2.yml b/resources/docs/swagger-v2.yml index 859ff802c..e450ab440 100644 --- a/resources/docs/swagger-v2.yml +++ b/resources/docs/swagger-v2.yml @@ -96212,6 +96212,11 @@ paths: - V2 Projects - V2 Sites - V2 SitePolygons + parameters: + - type: string + name: UUID + in: path + required: true produces: - application/json responses: @@ -96244,7 +96249,7 @@ paths: - V2 Sites parameters: - type: string - name: UUID + name: site in: path required: true responses: From 1f3efa084c9d7b02ffe6916991c76f3c56470101 Mon Sep 17 00:00:00 2001 From: Jose Carlos Laura Ramirez Date: Tue, 11 Jun 2024 22:25:24 -0400 Subject: [PATCH 8/8] improve query for criteria site --- app/Helpers/GeometryHelper.php | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/app/Helpers/GeometryHelper.php b/app/Helpers/GeometryHelper.php index c30a3c4db..b0a4099b7 100644 --- a/app/Helpers/GeometryHelper.php +++ b/app/Helpers/GeometryHelper.php @@ -5,7 +5,6 @@ use App\Models\V2\PolygonGeometry; use App\Models\V2\Projects\Project; use App\Models\V2\Sites\CriteriaSite; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; class GeometryHelper @@ -122,26 +121,16 @@ public static function getPolygonsBbox($polygonsIds) public static function getCriteriaDataForPolygonGeometry($polygonGeometry) { - // Fetch data from criteria_site with distinct criteria_id based on the latest created_at - $criteriaDataQuery = 'SELECT criteria_id, MAX(created_at) AS latest_created_at - FROM criteria_site - WHERE polygon_id = ? - GROUP BY criteria_id'; - - $criteriaData = DB::select($criteriaDataQuery, [$polygonGeometry->uuid]); - - // Determine the validity of each criteria - $criteriaList = []; - foreach ($criteriaData as $criteria) { - $criteriaId = $criteria->criteria_id; - $valid = CriteriaSite::where(['polygon_id' => $polygonGeometry->uuid, 'criteria_id' => $criteriaId])->orderBy('created_at', 'desc')->select('valid')->first()?->valid; - $criteriaList[] = [ - 'criteria_id' => $criteriaId, - 'latest_created_at' => $criteria->latest_created_at, - 'valid' => $valid, - ]; - } - - return $criteriaList; + return CriteriaSite::whereIn( + 'id', + $polygonGeometry + ->criteriaSite() + ->groupBy('criteria_id') + ->selectRaw('max(id) as latest_id') + )->get([ + 'criteria_id', + 'valid', + 'created_at as latest_created_at', + ]); } }