Skip to content

Commit

Permalink
[TM-1503] delete and create site and historic
Browse files Browse the repository at this point in the history
  • Loading branch information
egrojMonroy committed Nov 25, 2024
1 parent 9067f0c commit fb41632
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 22 deletions.
21 changes: 9 additions & 12 deletions app/Helpers/GeometryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 1 addition & 2 deletions app/Models/V2/Sites/CriteriaSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
class CriteriaSite extends Model
{
use HasUuid;
use SoftDeletes;

/**
* The table associated with the model.
Expand Down Expand Up @@ -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);
}
}
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();
}
}
32 changes: 25 additions & 7 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,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down

0 comments on commit fb41632

Please sign in to comment.