A free open source IT asset/license management system
Sync Adapters: timezone for purchase_date mapping
snipe414ea91b2a4b
diff --git a/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php b/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.phpindex 49d8acf0d4..013138311e 100644--- a/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php+++ b/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php@@ -1226,7 +1226,12 @@ class AppleBusinessManagerAdapter extends SyncAdapter}try {- return Carbon::parse($value)->toDateString();+ // Rotate to app.timezone before extracting the date part so a+ // vendor datetime at the UTC day boundary records the date+ // the user would recognize. Same reason the last_seen path+ // converts to app timezone before producing its naive string.+ // #19765.+ return Carbon::parse($value)->timezone(config('app.timezone'))->toDateString();} catch (\Throwable) {return null;}diff --git a/app/SyncAdapters/SyncsHostFromRecord.php b/app/SyncAdapters/SyncsHostFromRecord.phpindex acadf9fc3d..e2ac7310aa 100644--- a/app/SyncAdapters/SyncsHostFromRecord.php+++ b/app/SyncAdapters/SyncsHostFromRecord.php@@ -866,9 +866,16 @@ trait SyncsHostFromRecord// Watson validating rule wants date_format:Y-m-d.// Log + skip if the string isn't parseable so a bad record// doesn't donk the sync run.+ //+ // Rotate to app.timezone before extracting the date part so+ // a vendor datetime at the UTC day boundary (e.g.+ // "2024-07-23T03:00:00Z" which is 2024-07-22 20:00 Pacific)+ // records the date the user would recognize. Same reason+ // the last_seen path converts to app timezone before+ // producing its naive string. #19765.if ($value !== null && $value !== '') {try {- $asset->purchase_date = \Carbon\Carbon::parse((string) $value)->format('Y-m-d');+ $asset->purchase_date = \Carbon\Carbon::parse((string) $value)->timezone(config('app.timezone'))->format('Y-m-d');} catch (\Exception $e) {Log::channel('sync-adapters')->warning("Skipping unparseable purchase_date \"{$value}\" for asset {$asset->id}: ".$e->getMessage());}diff --git a/tests/Feature/SyncAdapters/Kandji/KandjiAdapterTest.php b/tests/Feature/SyncAdapters/Kandji/KandjiAdapterTest.phpindex 1d5013b565..139a8e7dfd 100644--- a/tests/Feature/SyncAdapters/Kandji/KandjiAdapterTest.php+++ b/tests/Feature/SyncAdapters/Kandji/KandjiAdapterTest.php@@ -109,6 +109,54 @@ class KandjiAdapterTest extends TestCaseiterator_to_array($adapter->pull());}+ public function test_purchase_date_from_vendor_datetime_rotates_to_app_timezone_before_truncation(): void+ {+ // Companion to the last_seen fix: vendor-supplied datetimes mapped+ // to the native `purchase_date` column (a Y-m-d field) also need+ // to rotate to app.timezone before the date part is extracted. A+ // vendor datetime at the UTC day boundary would otherwise record+ // the UTC-wall-clock date, which differs from the user's expected+ // local date by one calendar day.+ config(['app.timezone' => 'America/Los_Angeles']);++ // Reuse the Kandji adapter's extraFields plumbing to put a+ // purchase_date value through the mapped-extra path. Direct the+ // model's `purchased_at` extra at native:purchase_date so the+ // SyncsHostFromRecord case-branch for 'purchase_date' fires.+ $instance = SyncAdapterInstance::where('slug', 'kandji')->firstOrFail();+ SyncAdapterConfig::put($instance->id, 'url', 'https://example.api.kandji.io');+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-kandji-token'));++ // Vendor sends 03:00:00 UTC on 2026-07-23. That is 20:00:00 Pacific+ // on 2026-07-22. Pre-fix, Carbon::parse keeps UTC, and format('Y-m-d')+ // emits "2026-07-23". Post-fix, the rotate-then-format produces+ // "2026-07-22" (Pacific calendar date), matching user expectation.+ $record = new \App\SyncAdapters\HostInventoryRecord(+ sourceKey: 'kandji',+ sourceId: 'tz-pd-1',+ hostname: 'pacific-pd',+ hardwareSerial: 'TZ-PD-1',+ );++ $asset = new \App\Models\Asset;+ // Invoke the case 'purchase_date' handler directly via the public+ // facade: writeExtraValueToTarget routes through writeNative,+ // which hits the case statement. Simplest driver is to call the+ // native-field writer with a datetime payload.+ $reflection = new \ReflectionClass(\App\SyncAdapters\SyncsHostFromRecord::class);+ $writeNative = $reflection->getMethod('writeNative');+ $writeNative->setAccessible(true);+ $writeNative->invoke(null, $asset, 'purchase_date', '2026-07-23T03:00:00Z', $instance, $record);++ // Asset casts purchase_date, so read the raw stored attribute to+ // see the string the sync pipeline actually wrote.+ $this->assertSame(+ '2026-07-22',+ $asset->getAttributes()['purchase_date'] ?? null,+ 'purchase_date must store the user\'s local calendar date for a UTC datetime at the day boundary.',+ );+ }+public function test_last_seen_is_stored_in_app_timezone_not_vendor_utc(): void{// Issue #19765: Kandji returns `last_check_in` as ISO-8601 UTC with a