diff --git a/composer.json b/composer.json index efcfcaf5..1e1df975 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "fleetbase/core-api", - "version": "1.6.45", + "version": "1.6.46", "description": "Core Framework and Resources for Fleetbase API", "keywords": [ "fleetbase", diff --git a/src/Models/User.php b/src/Models/User.php index ee26c651..10f7a28f 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -7,6 +7,7 @@ use Fleetbase\Notifications\UserCreated; use Fleetbase\Notifications\UserInvited; use Fleetbase\Support\NotificationRegistry; +use Fleetbase\Support\Timezone; use Fleetbase\Support\Utils; use Fleetbase\Traits\ClearsHttpCache; use Fleetbase\Traits\Expandable; @@ -1220,7 +1221,14 @@ public function isNotVerified(): bool */ public static function applyUserInfoFromRequest($request, array $attributes = []): array { - $info = null; + $info = null; + $timezone = Timezone::firstValid([ + $request->input('timezone'), + $request->input('whois.timezone'), + $request->input('whois.time_zone.name'), + data_get($attributes, 'timezone'), + ]); + // Lookup user default details try { $info = \Fleetbase\Support\Http::lookupIp($request); @@ -1230,10 +1238,16 @@ public static function applyUserInfoFromRequest($request, array $attributes = [] if ($info) { $attributes['country'] = data_get($info, 'country_code'); $attributes['ip_address'] = data_get($info, 'ip', $request->ip()); - $tzInfo = data_get($info, 'time_zone.name', $request->input('timezone')); - if ($tzInfo) { - $attributes['timezone'] = $tzInfo; + $timezone = $timezone ?: Timezone::firstValid([ + data_get($info, 'time_zone.name'), + data_get($info, 'timezone'), + data_get($info, 'timezone_name'), + ]); + + if ($timezone) { + $attributes['timezone'] = $timezone; } + $attributes['meta'] = [ 'areacode' => data_get($info, 'calling_code'), 'currency' => data_get($info, 'currency.code'), @@ -1245,6 +1259,10 @@ public static function applyUserInfoFromRequest($request, array $attributes = [] ]; } + if ($timezone) { + $attributes['timezone'] = $timezone; + } + return $attributes; } diff --git a/src/Support/Timezone.php b/src/Support/Timezone.php new file mode 100644 index 00000000..25254110 --- /dev/null +++ b/src/Support/Timezone.php @@ -0,0 +1,25 @@ +toBe('America/New_York'); +}); + +test('first valid timezone accepts nested whois timezone candidates', function () { + $timezone = Timezone::firstValid([ + null, + 'Europe/London', + 'Asia/Singapore', + ]); + + expect($timezone)->toBe('Europe/London'); +}); + +test('first valid timezone ignores invalid values', function () { + $timezone = Timezone::firstValid([ + 'Not/A_Timezone', + 'Also/Invalid', + ]); + + expect($timezone)->toBeNull(); +}); + +test('first valid timezone does not default to application timezone', function () { + $timezone = Timezone::firstValid([ + null, + '', + ]); + + expect($timezone)->toBeNull(); +});