From d8c45887eaf906cbda32464adc1678a1e3eaca9b Mon Sep 17 00:00:00 2001 From: Daniel Mohns Date: Tue, 23 Jul 2024 17:28:34 +0200 Subject: [PATCH] Update geoInformation when not exists on SteamaCo site sync (#201) --- .../src/Services/SteamaSiteService.php | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Website/htdocs/mpmanager/packages/inensus/steama-meter/src/Services/SteamaSiteService.php b/Website/htdocs/mpmanager/packages/inensus/steama-meter/src/Services/SteamaSiteService.php index a18ef5143..aa219acdd 100644 --- a/Website/htdocs/mpmanager/packages/inensus/steama-meter/src/Services/SteamaSiteService.php +++ b/Website/htdocs/mpmanager/packages/inensus/steama-meter/src/Services/SteamaSiteService.php @@ -75,7 +75,7 @@ public function sync() 'mpm_mini_grid_id' => $miniGrid->id, 'hash' => $site['hash'], ]); - $this->updateGeographicalInformation($miniGrid->id, $site); + $this->createOrUpdateGeographicalInformation($miniGrid->id, $site); }); $syncCheck['data']->filter(function ($value) { @@ -83,7 +83,7 @@ public function sync() })->each(function ($site) { $miniGrid = is_null($site['relatedMiniGrid']) ? $this->creteRelatedMiniGrid($site) : $this->updateRelatedMiniGrid($site, $site['relatedMiniGrid']); - $this->updateGeographicalInformation($miniGrid->id, $site); + $this->createOrUpdateGeographicalInformation($miniGrid->id, $site); $site['registeredStmSite']->update([ 'site_id' => $site['id'], 'mpm_mini_grid_id' => $miniGrid->id, @@ -176,7 +176,7 @@ public function updateRelatedMiniGrid($site, $miniGrid) return $miniGrid->fresh(); } - public function updateGeographicalInformation($miniGridId, $site) + public function createOrUpdateGeographicalInformation($miniGridId, $site) { $geographicalInformation = $this->geographicalInformation->newQuery()->whereHasMorph( 'owner', @@ -187,9 +187,18 @@ static function ($q) use ($miniGridId) { )->first(); $points = $site['latitude'] === null ? config('steama.geoLocation') : $site['latitude'].','.$site['longitude']; - $geographicalInformation->update([ - 'points' => $points, - ]); + + if ($geographicalInformation) { + $geographicalInformation->update([ + 'points' => $points, + ]); + } else { + $this->geographicalInformation->create([ + 'owner_type' => 'mini-grid', + 'owner_id' => $miniGridId, + 'points' => $points, + ]); + } } public function checkLocationAvailability()