diff --git a/app/Http/Controllers/V2/Sites/AdminSitesPolygonController.php b/app/Http/Controllers/V2/Sites/AdminSitesPolygonController.php new file mode 100644 index 000000000..ff01c95d1 --- /dev/null +++ b/app/Http/Controllers/V2/Sites/AdminSitesPolygonController.php @@ -0,0 +1,45 @@ +input('uuid'); + $type = $request->input('type'); + $offset = $request->input('offset', 0); + $limit = $request->input('limit', 10); + $request = request(); + + + if ($type === 'projects') { + $project = Project::where('uuid', $uuid)->firstOrFail(); + $finalEntityQuery = App::make(PolygonService::class)->getSitePolygonsWithFiltersAndSorts($project->sitePolygons(), $request); + } elseif ($type === 'sites') { + $sitePolygonsQuery = SitePolygon::active()->where('site_id', $uuid); + $finalEntityQuery = App::make(PolygonService::class)->getSitePolygonsWithFiltersAndSorts($sitePolygonsQuery, $request); + } + $sitePolygons = $finalEntityQuery + ->offset($offset) + ->limit($limit) + ->get(); + + return response()->json($sitePolygons); + } catch (\Exception $e) { + Log::error($e->getMessage()); + + return response()->json(['error' => 'An error occurred while fetching site polygons'], 500); + } + } +} diff --git a/app/Http/Controllers/V2/Sites/AdminSitesPolygonCountController.php b/app/Http/Controllers/V2/Sites/AdminSitesPolygonCountController.php new file mode 100644 index 000000000..cb1f990b9 --- /dev/null +++ b/app/Http/Controllers/V2/Sites/AdminSitesPolygonCountController.php @@ -0,0 +1,41 @@ +input('uuid'); + $type = $request->input('type'); + $request = request(); + if ($type === 'projects') { + $project = Project::where('uuid', $uuid)->firstOrFail(); + $countSitePolygons = App::make(PolygonService::class)->getSitePolygonsWithFiltersAndSorts($project->sitePolygons(), $request); + } elseif ($type === 'sites') { + $sitePolygonsQuery = SitePolygon::active()->where('site_id', $uuid); + $countSitePolygons = App::make(PolygonService::class)->getSitePolygonsWithFiltersAndSorts($sitePolygonsQuery, $request); + } + + $totalCount = $countSitePolygons->count(); + + return response()->json([ + 'count' => $totalCount, + ]); + } catch (\Exception $e) { + Log::error($e->getMessage()); + + return response()->json(['error' => 'An error occurred while fetching site polygons'], 500); + } + } +} diff --git a/app/Services/PolygonService.php b/app/Services/PolygonService.php index ac80dd04d..eaee1393f 100755 --- a/app/Services/PolygonService.php +++ b/app/Services/PolygonService.php @@ -17,6 +17,7 @@ use App\Validators\SitePolygonValidator; use DateTime; use Exception; +use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\App; @@ -582,4 +583,25 @@ public function processClippedPolygons(array $polygonUuids) return $updatedPolygons; } + + public function getSitePolygonsWithFiltersAndSorts($sitePolygonsQuery, Request $request) + { + if ($request->has('status') && $request->input('status')) { + $statusValues = explode(',', $request->input('status')); + $sitePolygonsQuery->whereIn('site_polygon.status', $statusValues); + } + + $sortFields = $request->input('sort', []); + foreach ($sortFields as $field => $direction) { + if ($field === 'status') { + $sitePolygonsQuery->orderByRaw('FIELD(site_polygon.status, "draft", "submitted", "needs-more-information", "approved") ' . $direction); + } elseif ($field === 'poly_name') { + $sitePolygonsQuery->orderByRaw('site_polygon.poly_name IS NULL, site_polygon.poly_name ' . $direction); + } else { + $sitePolygonsQuery->orderBy($field, $direction); + } + } + + return $sitePolygonsQuery; + } } diff --git a/openapi-src/V2/definitions/EntityPolygonResponse.yml b/openapi-src/V2/definitions/EntityPolygonResponse.yml new file mode 100644 index 000000000..6aaff2fcd --- /dev/null +++ b/openapi-src/V2/definitions/EntityPolygonResponse.yml @@ -0,0 +1,13 @@ +type: object +properties: + type: + type: string + description: Type of the entity ('project', 'site', 'unknown') + uuid: + type: string + format: uuid + description: UUID of the entity + polygonsData: + type: array + items: + $ref: './_index.yml#/SitePolygon' \ No newline at end of file diff --git a/openapi-src/V2/definitions/_index.yml b/openapi-src/V2/definitions/_index.yml index 371955c1c..fb2f8e31c 100644 --- a/openapi-src/V2/definitions/_index.yml +++ b/openapi-src/V2/definitions/_index.yml @@ -352,6 +352,8 @@ GeojsonData: $ref: './GeojsonData.yml' EntityTypeResponse: $ref: './EntityTypeResponse.yml' +EntityPolygonResponse: + $ref: './EntityPolygonResponse.yml' AuditStatusUpdateRequest: $ref: './AuditStatusUpdateRequest.yml' SitePolygonResource: diff --git a/openapi-src/V2/paths/Entity/get-v2-entity-count.yml b/openapi-src/V2/paths/Entity/get-v2-entity-count.yml new file mode 100644 index 000000000..677f9f33c --- /dev/null +++ b/openapi-src/V2/paths/Entity/get-v2-entity-count.yml @@ -0,0 +1,34 @@ +summary: Get Entity Type +description: | + Determine the type of entity based on UUID. +parameters: + - in: query + name: uuid + required: true + description: UUID of the entity + type: string + - in: query + name: type + required: true + description: type of the entity + type: string + - in: query + name: status + required: false + description: Comma-separated list of status values to filter by + type: string + - in: query + name: sort + required: false + description: Sort criteria in the format `sort[poly_name]=asc or sort[status]=desc` + type: string +responses: + '200': + description: Successful response + schema: + type: object + properties: + count: + type: number + '500': + description: Internal server error diff --git a/openapi-src/V2/paths/Entity/get-v2-entity-polygon.yml b/openapi-src/V2/paths/Entity/get-v2-entity-polygon.yml new file mode 100644 index 000000000..50149e48d --- /dev/null +++ b/openapi-src/V2/paths/Entity/get-v2-entity-polygon.yml @@ -0,0 +1,39 @@ +summary: Get Entity Type +description: | + Determine the type of entity based on UUID. +parameters: + - in: query + name: uuid + required: true + description: UUID of the entity + type: string + - in: query + name: type + required: true + description: type of the entity + type: string + - in: query + name: status + required: false + description: Comma-separated list of status values to filter by + type: string + - in: query + name: sort + required: false + description: Sort criteria in the format `sort[poly_name]=asc or sort[status]=desc` + type: string +responses: + '200': + description: Successful response + schema: + $ref: '../../definitions/_index.yml#/EntityPolygonResponse' + '400': + description: Bad request, UUID parameter is missing + '500': + description: Internal server error + schema: + type: object + properties: + error: + type: string + description: Error message \ No newline at end of file diff --git a/openapi-src/V2/paths/Sites/get-v2-admin-sites-uuid-polygons-count.yml b/openapi-src/V2/paths/Sites/get-v2-admin-sites-uuid-polygons-count.yml new file mode 100644 index 000000000..112eb0801 --- /dev/null +++ b/openapi-src/V2/paths/Sites/get-v2-admin-sites-uuid-polygons-count.yml @@ -0,0 +1,17 @@ +summary: Get count polygons for a specific site +parameters: + - in: path + name: UUID + required: true + type: string + description: The UUID of the site +responses: + '200': + description: Successful response + schema: + type: object + properties: + count: + type: number + '500': + description: Internal server error diff --git a/openapi-src/V2/paths/Sites/get-v2-admin-sites-uuid-polygons.yml b/openapi-src/V2/paths/Sites/get-v2-admin-sites-uuid-polygons.yml new file mode 100644 index 000000000..ce5346716 --- /dev/null +++ b/openapi-src/V2/paths/Sites/get-v2-admin-sites-uuid-polygons.yml @@ -0,0 +1,26 @@ +summary: Get polygons for a specific site +parameters: + - in: path + name: UUID + required: true + type: string + description: The UUID of the site + - in: query + name: limit + required: true + description: The maximum number of polygons to return + type: number + - in: query + name: offset + required: true + description: The number of polygons to skip + type: string +responses: + '200': + description: Successful response + schema: + type: array + items: + $ref: '../../definitions/_index.yml#/SitePolygon' + '500': + description: Internal server error diff --git a/openapi-src/V2/paths/_index.yml b/openapi-src/V2/paths/_index.yml index 9ee3560b2..8f9fa32b8 100644 --- a/openapi-src/V2/paths/_index.yml +++ b/openapi-src/V2/paths/_index.yml @@ -2497,6 +2497,12 @@ $ref: './Sites/delete-v2-sites-uuid.yml' get: $ref: './Sites/get-v2-sites-uuid.yml' +/v2/sites/{UUID}/polygons: + get: + $ref: './Sites/get-v2-admin-sites-uuid-polygons.yml' +/v2/sites/{UUID}/polygons/count: + get: + $ref: './Sites/get-v2-admin-sites-uuid-polygons-count.yml' /v2/site-monitorings/{UUID}: get: $ref: './Sites/Monitoring/get-v2-site-monitorings-uuid.yml' @@ -2741,9 +2747,15 @@ /v2/type-entity: get: $ref: './Entity/get-v2-type-entity.yml' +/v2/entity/polygons/count: + get: + $ref: './Entity/get-v2-entity-count.yml' +/v2/entity/polygons: + get: + $ref: './Entity/get-v2-entity-polygon.yml' /v2/{ENTITY}/{UUID}/status: - put: - $ref: './Entity/put-v2-entity-uuid-status.yml' + put: + $ref: './Entity/put-v2-entity-uuid-status.yml' /v2/{ENTITY}/{UUID}/{ID}/delete: delete: $ref: './AuditStatus/delete-v2-audit-status.yml' diff --git a/resources/docs/swagger-v2.yml b/resources/docs/swagger-v2.yml index 0022ab99b..2884a7c92 100644 --- a/resources/docs/swagger-v2.yml +++ b/resources/docs/swagger-v2.yml @@ -43686,6 +43686,82 @@ definitions: items: type: number description: Bounding box of the entity + EntityPolygonResponse: + type: object + properties: + type: + type: string + description: 'Type of the entity (''project'', ''site'', ''unknown'')' + uuid: + type: string + format: uuid + description: UUID of the entity + polygonsData: + type: array + items: + title: SitePolygon + type: object + properties: + id: + type: integer + uuid: + type: string + primary_uuid: + type: string + project_id: + type: string + proj_name: + type: string + org_name: + type: string + poly_id: + type: string + poly_name: + type: string + site_id: + type: string + site_name: + type: string + plantstart: + type: string + format: date + plantend: + type: string + format: date + practice: + type: string + target_sys: + type: string + distr: + type: string + num_trees: + type: integer + calc_area: + type: number + format: float + created_by: + type: string + last_modified_by: + type: string + deleted_at: + type: string + format: date-time + created_at: + type: string + format: date-time + updated_at: + type: string + format: date-time + status: + type: string + source: + type: string + country: + type: string + is_active: + type: boolean + version_name: + type: string AuditStatusUpdateRequest: title: AuditStatusUpdateRequest type: object @@ -94075,6 +94151,115 @@ paths: description: 'this is a list of key value pairs eg slug: name ' items: type: string + '/v2/sites/{UUID}/polygons': + get: + summary: Get polygons for a specific site + parameters: + - in: path + name: UUID + required: true + type: string + description: The UUID of the site + - in: query + name: limit + required: true + description: The maximum number of polygons to return + type: number + - in: query + name: offset + required: true + description: The number of polygons to skip + type: string + responses: + '200': + description: Successful response + schema: + type: array + items: + title: SitePolygon + type: object + properties: + id: + type: integer + uuid: + type: string + primary_uuid: + type: string + project_id: + type: string + proj_name: + type: string + org_name: + type: string + poly_id: + type: string + poly_name: + type: string + site_id: + type: string + site_name: + type: string + plantstart: + type: string + format: date + plantend: + type: string + format: date + practice: + type: string + target_sys: + type: string + distr: + type: string + num_trees: + type: integer + calc_area: + type: number + format: float + created_by: + type: string + last_modified_by: + type: string + deleted_at: + type: string + format: date-time + created_at: + type: string + format: date-time + updated_at: + type: string + format: date-time + status: + type: string + source: + type: string + country: + type: string + is_active: + type: boolean + version_name: + type: string + '500': + description: Internal server error + '/v2/sites/{UUID}/polygons/count': + get: + summary: Get count polygons for a specific site + parameters: + - in: path + name: UUID + required: true + type: string + description: The UUID of the site + responses: + '200': + description: Successful response + schema: + type: object + properties: + count: + type: number + '500': + description: Internal server error '/v2/site-monitorings/{UUID}': get: summary: View a specific site monitoring @@ -97996,6 +98181,157 @@ paths: error: type: string description: Error message + /v2/entity/polygons/count: + get: + summary: Get Entity Type + description: | + Determine the type of entity based on UUID. + parameters: + - in: query + name: uuid + required: true + description: UUID of the entity + type: string + - in: query + name: type + required: true + description: type of the entity + type: string + - in: query + name: status + required: false + description: Comma-separated list of status values to filter by + type: string + - in: query + name: sort + required: false + description: 'Sort criteria in the format `sort[poly_name]=asc or sort[status]=desc`' + type: string + responses: + '200': + description: Successful response + schema: + type: object + properties: + count: + type: number + '500': + description: Internal server error + /v2/entity/polygons: + get: + summary: Get Entity Type + description: | + Determine the type of entity based on UUID. + parameters: + - in: query + name: uuid + required: true + description: UUID of the entity + type: string + - in: query + name: type + required: true + description: type of the entity + type: string + - in: query + name: status + required: false + description: Comma-separated list of status values to filter by + type: string + - in: query + name: sort + required: false + description: 'Sort criteria in the format `sort[poly_name]=asc or sort[status]=desc`' + type: string + responses: + '200': + description: Successful response + schema: + type: object + properties: + type: + type: string + description: 'Type of the entity (''project'', ''site'', ''unknown'')' + uuid: + type: string + format: uuid + description: UUID of the entity + polygonsData: + type: array + items: + title: SitePolygon + type: object + properties: + id: + type: integer + uuid: + type: string + primary_uuid: + type: string + project_id: + type: string + proj_name: + type: string + org_name: + type: string + poly_id: + type: string + poly_name: + type: string + site_id: + type: string + site_name: + type: string + plantstart: + type: string + format: date + plantend: + type: string + format: date + practice: + type: string + target_sys: + type: string + distr: + type: string + num_trees: + type: integer + calc_area: + type: number + format: float + created_by: + type: string + last_modified_by: + type: string + deleted_at: + type: string + format: date-time + created_at: + type: string + format: date-time + updated_at: + type: string + format: date-time + status: + type: string + source: + type: string + country: + type: string + is_active: + type: boolean + version_name: + type: string + '400': + description: 'Bad request, UUID parameter is missing' + '500': + description: Internal server error + schema: + type: object + properties: + error: + type: string + description: Error message '/v2/{ENTITY}/{UUID}/status': put: summary: Update the status of a specific entity diff --git a/routes/api_v2.php b/routes/api_v2.php index e46cf7dbd..e1d77685e 100644 --- a/routes/api_v2.php +++ b/routes/api_v2.php @@ -179,6 +179,8 @@ use App\Http\Controllers\V2\SiteReports\SiteReportsViaSiteController; use App\Http\Controllers\V2\Sites\AdminIndexSitesController; use App\Http\Controllers\V2\Sites\AdminSitesMultiController; +use App\Http\Controllers\V2\Sites\AdminSitesPolygonController; +use App\Http\Controllers\V2\Sites\AdminSitesPolygonCountController; use App\Http\Controllers\V2\Sites\CreateSiteWithFormController; use App\Http\Controllers\V2\Sites\IndexSitePolygonVersionsController; use App\Http\Controllers\V2\Sites\Monitoring\AdminCreateSiteMonitoringController; @@ -582,6 +584,10 @@ Route::get('/bbox', [SitePolygonDataController::class, 'getBboxOfCompleteSite']); Route::get('/check-approve', SiteCheckApproveController::class); }); +Route::prefix('entity')->group(function () { + Route::get('/polygons/count', AdminSitesPolygonCountController::class); + Route::get('/polygons', AdminSitesPolygonController::class); +}); Route::prefix('geometry')->group(function () { Route::post('', [GeometryController::class, 'storeGeometry']);