diff --git a/app/Helpers/GeometryHelper.php b/app/Helpers/GeometryHelper.php index ba5bc6a46..c0ca8e1e1 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; @@ -125,18 +124,14 @@ 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 bb6bbac5b..179992ff3 100644 --- a/app/Models/V2/Sites/CriteriaSite.php +++ b/app/Models/V2/Sites/CriteriaSite.php @@ -4,12 +4,10 @@ use App\Models\Traits\HasUuid; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\SoftDeletes; class CriteriaSite extends Model { use HasUuid; - use SoftDeletes; /** * The table associated with the model. @@ -42,6 +40,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 000000000..255a0ebfa --- /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 eaee1393f..c6712bb77 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,13 +223,29 @@ 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; 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 000000000..6263d94e7 --- /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 000000000..6194ea5c4 --- /dev/null +++ b/database/migrations/2024_11_22_161611_create_criteria_site_historic_table.php @@ -0,0 +1,36 @@ +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->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('criteria_site_historic'); + } +} 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 000000000..8c64a8fd8 --- /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(); + }); + } +}