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-1503] criteria historic #587

Merged
merged 5 commits into from
Nov 25, 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
21 changes: 8 additions & 13 deletions app/Helpers/GeometryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions app/Models/V2/Sites/CriteriaSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
45 changes: 45 additions & 0 deletions app/Models/V2/Sites/CriteriaSiteHistoric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Models\V2\Sites;

use App\Models\Traits\HasUuid;
use Illuminate\Database\Eloquent\Model;

class CriteriaSiteHistoric extends Model
{
use HasUuid;

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'criteria_site_historic';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'criteria_id',
'polygon_id',
'valid',
'extra_info',
];

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'deleted_at',
'date_created',
];

public function scopeForCriteria($query, $criteriaId)
{
return $query->where('criteria_id', $criteriaId)->latest();
}
}
29 changes: 23 additions & 6 deletions app/Services/PolygonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class RemoveSoftDeletesFromCriteriaSite extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('criteria_site', function (Blueprint $table) {
$table->dropSoftDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('criteria_site', function (Blueprint $table) {
$table->softDeletes();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCriteriaSiteHistoricTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('criteria_site_historic', function (Blueprint $table) {
$table->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');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class RemoveDateCreatedFromCriteriaSite extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('criteria_site', function (Blueprint $table) {
$table->dropColumn('date_created');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('criteria_site', function (Blueprint $table) {
$table->timestamp('date_created')->nullable();
});
}
}
Loading