From 9067f0cb4d96a2ba4503142e4f6602afe456a14d Mon Sep 17 00:00:00 2001 From: JORGE Date: Fri, 22 Nov 2024 16:32:52 -0400 Subject: [PATCH 1/5] [TM-1503] add migrations to create tables historic criteria --- ..._remove_softdeletes_from_criteria_site.php | 32 ++++++++++++++++ ...11_create_criteria_site_historic_table.php | 37 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 database/migrations/2024_11_22_161557_remove_softdeletes_from_criteria_site.php create mode 100644 database/migrations/2024_11_22_161611_create_criteria_site_historic_table.php diff --git a/database/migrations/2024_11_22_161557_remove_softdeletes_from_criteria_site.php b/database/migrations/2024_11_22_161557_remove_softdeletes_from_criteria_site.php new file mode 100644 index 00000000..6263d94e --- /dev/null +++ b/database/migrations/2024_11_22_161557_remove_softdeletes_from_criteria_site.php @@ -0,0 +1,32 @@ +dropSoftDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('criteria_site', function (Blueprint $table) { + $table->softDeletes(); + }); + } +} diff --git a/database/migrations/2024_11_22_161611_create_criteria_site_historic_table.php b/database/migrations/2024_11_22_161611_create_criteria_site_historic_table.php new file mode 100644 index 00000000..e7f84e08 --- /dev/null +++ b/database/migrations/2024_11_22_161611_create_criteria_site_historic_table.php @@ -0,0 +1,37 @@ +id(); + $table->uuid('uuid')->unique(); + $table->integer('criteria_id')->nullable(); + $table->string('polygon_id')->nullable(); + $table->integer('valid')->nullable(); + $table->json('extra_info')->nullable(); + $table->date('date_created')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('criteria_site_historic'); + } +} From fb4163206310451d616d09302c49b401ca314e7b Mon Sep 17 00:00:00 2001 From: JORGE Date: Mon, 25 Nov 2024 10:08:07 -0400 Subject: [PATCH 2/5] [TM-1503] delete and create site and historic --- app/Helpers/GeometryHelper.php | 21 ++++----- app/Models/V2/Sites/CriteriaSite.php | 3 +- app/Models/V2/Sites/CriteriaSiteHistoric.php | 45 +++++++++++++++++++ app/Services/PolygonService.php | 32 ++++++++++--- ...11_create_criteria_site_historic_table.php | 1 - 5 files changed, 80 insertions(+), 22 deletions(-) create mode 100644 app/Models/V2/Sites/CriteriaSiteHistoric.php diff --git a/app/Helpers/GeometryHelper.php b/app/Helpers/GeometryHelper.php index ba5bc6a4..97ea5bfc 100755 --- a/app/Helpers/GeometryHelper.php +++ b/app/Helpers/GeometryHelper.php @@ -125,19 +125,16 @@ public static function getPolygonsBbox($polygonsIds) public static function getCriteriaDataForPolygonGeometry($polygonGeometry) { - 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', - 'extra_info', - ]); + return $polygonGeometry + ->criteriaSite() + ->get([ + 'criteria_id', + 'valid', + 'created_at as latest_created_at', + 'extra_info', + ]); } + public static function groupFeaturesBySiteId($geojson) { diff --git a/app/Models/V2/Sites/CriteriaSite.php b/app/Models/V2/Sites/CriteriaSite.php index bb6bbac5..62597e30 100644 --- a/app/Models/V2/Sites/CriteriaSite.php +++ b/app/Models/V2/Sites/CriteriaSite.php @@ -9,7 +9,6 @@ class CriteriaSite extends Model { use HasUuid; - use SoftDeletes; /** * The table associated with the model. @@ -42,6 +41,6 @@ class CriteriaSite extends Model public function scopeForCriteria($query, $criteriaId) { - return $query->where('criteria_id', $criteriaId)->latest(); + return $query->where('criteria_id', $criteriaId); } } diff --git a/app/Models/V2/Sites/CriteriaSiteHistoric.php b/app/Models/V2/Sites/CriteriaSiteHistoric.php new file mode 100644 index 00000000..77e095cc --- /dev/null +++ b/app/Models/V2/Sites/CriteriaSiteHistoric.php @@ -0,0 +1,45 @@ +where('criteria_id', $criteriaId)->latest(); + } +} diff --git a/app/Services/PolygonService.php b/app/Services/PolygonService.php index eaee1393..f314e670 100755 --- a/app/Services/PolygonService.php +++ b/app/Services/PolygonService.php @@ -11,6 +11,7 @@ use App\Models\V2\Projects\Project; use App\Models\V2\Projects\ProjectPolygon; use App\Models\V2\Sites\CriteriaSite; +use App\Models\V2\Sites\CriteriaSiteHistoric; use App\Models\V2\Sites\Site; use App\Models\V2\Sites\SitePolygon; use App\Models\V2\User; @@ -222,20 +223,37 @@ private function insertPolygon($uuid, $sitePolygonProperties, $featureProperties public function createCriteriaSite($polygonId, $criteriaId, $valid, $extraInfo = null): bool|string { - $criteriaSite = new CriteriaSite(); - $criteriaSite->polygon_id = $polygonId; - $criteriaSite->criteria_id = $criteriaId; - $criteriaSite->valid = $valid; - $criteriaSite->extra_info = $extraInfo ? json_encode($extraInfo) : null; - try { + $existingCriteriaSite = CriteriaSite::where('polygon_id', $polygonId) + ->where('criteria_id', $criteriaId) + ->first(); + + if ($existingCriteriaSite) { + CriteriaSiteHistoric::create([ + 'polygon_id' => $existingCriteriaSite->polygon_id, + 'criteria_id' => $existingCriteriaSite->criteria_id, + 'valid' => $existingCriteriaSite->valid, + 'extra_info' => $existingCriteriaSite->extra_info, + 'created_at' => $existingCriteriaSite->created_at, + 'updated_at' => $existingCriteriaSite->updated_at, + ]); + + $existingCriteriaSite->delete(); + } + + $criteriaSite = new CriteriaSite(); + $criteriaSite->polygon_id = $polygonId; + $criteriaSite->criteria_id = $criteriaId; + $criteriaSite->valid = $valid; + $criteriaSite->extra_info = $extraInfo ? json_encode($extraInfo) : null; $criteriaSite->save(); - + return true; } catch (\Exception $e) { return $e->getMessage(); } } + /** * Note: At this time, this method assumes that the geometry is a single polygon. diff --git a/database/migrations/2024_11_22_161611_create_criteria_site_historic_table.php b/database/migrations/2024_11_22_161611_create_criteria_site_historic_table.php index e7f84e08..6194ea5c 100644 --- a/database/migrations/2024_11_22_161611_create_criteria_site_historic_table.php +++ b/database/migrations/2024_11_22_161611_create_criteria_site_historic_table.php @@ -20,7 +20,6 @@ public function up() $table->string('polygon_id')->nullable(); $table->integer('valid')->nullable(); $table->json('extra_info')->nullable(); - $table->date('date_created')->nullable(); $table->timestamps(); }); } From f13cbad6001dde5e0de01d45c46697860973b596 Mon Sep 17 00:00:00 2001 From: JORGE Date: Mon, 25 Nov 2024 10:08:32 -0400 Subject: [PATCH 3/5] [TM-1503] lint --- app/Helpers/GeometryHelper.php | 2 -- app/Models/V2/Sites/CriteriaSite.php | 1 - app/Models/V2/Sites/CriteriaSiteHistoric.php | 2 +- app/Services/PolygonService.php | 9 ++++----- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/app/Helpers/GeometryHelper.php b/app/Helpers/GeometryHelper.php index 97ea5bfc..c0ca8e1e 100755 --- 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\Projects\ProjectPolygon; -use App\Models\V2\Sites\CriteriaSite; use App\Models\V2\Sites\Site; use App\Models\V2\Sites\SitePolygon; use Exception; @@ -134,7 +133,6 @@ public static function getCriteriaDataForPolygonGeometry($polygonGeometry) 'extra_info', ]); } - public static function groupFeaturesBySiteId($geojson) { diff --git a/app/Models/V2/Sites/CriteriaSite.php b/app/Models/V2/Sites/CriteriaSite.php index 62597e30..179992ff 100644 --- a/app/Models/V2/Sites/CriteriaSite.php +++ b/app/Models/V2/Sites/CriteriaSite.php @@ -4,7 +4,6 @@ use App\Models\Traits\HasUuid; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\SoftDeletes; class CriteriaSite extends Model { diff --git a/app/Models/V2/Sites/CriteriaSiteHistoric.php b/app/Models/V2/Sites/CriteriaSiteHistoric.php index 77e095cc..255a0ebf 100644 --- a/app/Models/V2/Sites/CriteriaSiteHistoric.php +++ b/app/Models/V2/Sites/CriteriaSiteHistoric.php @@ -25,7 +25,7 @@ class CriteriaSiteHistoric extends Model 'criteria_id', 'polygon_id', 'valid', - 'extra_info' + 'extra_info', ]; /** diff --git a/app/Services/PolygonService.php b/app/Services/PolygonService.php index f314e670..c6712bb7 100755 --- a/app/Services/PolygonService.php +++ b/app/Services/PolygonService.php @@ -227,7 +227,7 @@ public function createCriteriaSite($polygonId, $criteriaId, $valid, $extraInfo = $existingCriteriaSite = CriteriaSite::where('polygon_id', $polygonId) ->where('criteria_id', $criteriaId) ->first(); - + if ($existingCriteriaSite) { CriteriaSiteHistoric::create([ 'polygon_id' => $existingCriteriaSite->polygon_id, @@ -237,23 +237,22 @@ public function createCriteriaSite($polygonId, $criteriaId, $valid, $extraInfo = 'created_at' => $existingCriteriaSite->created_at, 'updated_at' => $existingCriteriaSite->updated_at, ]); - + $existingCriteriaSite->delete(); } - + $criteriaSite = new CriteriaSite(); $criteriaSite->polygon_id = $polygonId; $criteriaSite->criteria_id = $criteriaId; $criteriaSite->valid = $valid; $criteriaSite->extra_info = $extraInfo ? json_encode($extraInfo) : null; $criteriaSite->save(); - + return true; } catch (\Exception $e) { return $e->getMessage(); } } - /** * Note: At this time, this method assumes that the geometry is a single polygon. From 3e25ad0ccc23ad8ebdfd9362289b92e80de46aba Mon Sep 17 00:00:00 2001 From: JORGE Date: Mon, 25 Nov 2024 14:11:28 -0400 Subject: [PATCH 4/5] [TM-1503] remove useless attribut --- ...remove_date_created_from_criteria_site.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 database/migrations/2024_11_25_181031_remove_date_created_from_criteria_site.php diff --git a/database/migrations/2024_11_25_181031_remove_date_created_from_criteria_site.php b/database/migrations/2024_11_25_181031_remove_date_created_from_criteria_site.php new file mode 100644 index 00000000..eddaabe1 --- /dev/null +++ b/database/migrations/2024_11_25_181031_remove_date_created_from_criteria_site.php @@ -0,0 +1,32 @@ +dropColumn('date_created'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('criteria_site', function (Blueprint $table) { + $table->timestamp('date_created')->nullable(); // Adjust as necessary + }); + } +} From 5b1a6de1356663e0ba1cd6b0416f653b50d23195 Mon Sep 17 00:00:00 2001 From: JORGE Date: Mon, 25 Nov 2024 14:15:28 -0400 Subject: [PATCH 5/5] [TM-1503] remove comment --- ...2024_11_25_181031_remove_date_created_from_criteria_site.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/2024_11_25_181031_remove_date_created_from_criteria_site.php b/database/migrations/2024_11_25_181031_remove_date_created_from_criteria_site.php index eddaabe1..8c64a8fd 100644 --- a/database/migrations/2024_11_25_181031_remove_date_created_from_criteria_site.php +++ b/database/migrations/2024_11_25_181031_remove_date_created_from_criteria_site.php @@ -26,7 +26,7 @@ public function up() public function down() { Schema::table('criteria_site', function (Blueprint $table) { - $table->timestamp('date_created')->nullable(); // Adjust as necessary + $table->timestamp('date_created')->nullable(); }); } }