Skip to content

Commit

Permalink
stop dup location record creation 👀
Browse files Browse the repository at this point in the history
  • Loading branch information
yordadev committed Oct 8, 2024
1 parent 0c2d4d9 commit 51693d2
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Repositories/LocationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public static function findOrCreateLocationRecord(array $clickLocation): ShortUr
->where('regionCode', $clickLocation['regionCode'])
->firstOrFail();
} catch (Exception $exception) {
return ShortUrlLocation::query()->firstOrCreate($clickLocation);
$ipAddress = $clickLocation['ip'];

unset($clickLocation['ip']);

return ShortUrlLocation::query()->firstOrCreate([
'ip' => $ipAddress,
], $clickLocation);
}
} catch (Exception $exception) {
throw new UrlRepositoryException($exception->getMessage());
Expand All @@ -39,7 +45,7 @@ public static function findOrCreateLocationRecord(array $clickLocation): ShortUr
public static function getLocationFrom(string $ip): array
{
// only look up location when not existing yet
if (! $clickLocation = ShortUrlLocation::query()
if (!$clickLocation = ShortUrlLocation::query()
->where('ip', $ip)
->whereNotNull('countryCode')
->whereNotNull('longitude')
Expand All @@ -48,12 +54,12 @@ public static function getLocationFrom(string $ip): array
->first()) {
$clickLocation = Location::get($ip);

if (! $clickLocation) {
if (!$clickLocation) {
return LocationRepository::locationUnknown($ip);
}

$clickLocation->longitude = (float) $clickLocation->longitude;
$clickLocation->latitude = (float) $clickLocation->latitude;
$clickLocation->longitude = (float)$clickLocation->longitude;
$clickLocation->latitude = (float)$clickLocation->latitude;

unset($clickLocation->driver);
}
Expand Down

0 comments on commit 51693d2

Please sign in to comment.