From 2427436b31de936882bc59515305875733fad958 Mon Sep 17 00:00:00 2001 From: Benjamin Nussbaum Date: Tue, 19 Aug 2025 15:36:18 +0200 Subject: [PATCH] feat(#55): auto assign device model when provided at setup --- routes/api.php | 11 +++++- tests/Feature/Api/DeviceEndpointsTest.php | 44 +++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/routes/api.php b/routes/api.php index 5f2a9f3..2881796 100644 --- a/routes/api.php +++ b/routes/api.php @@ -3,6 +3,7 @@ use App\Jobs\GenerateScreenJob; use App\Models\Device; use App\Models\DeviceLog; +use App\Models\DeviceModel; use App\Models\Plugin; use App\Models\User; use App\Services\ImageGenerationService; @@ -178,6 +179,7 @@ Route::get('/display', function (Request $request) { Route::get('/setup', function (Request $request) { $mac_address = $request->header('id'); + $model_name = $request->header('model-id'); if (! $mac_address) { return response()->json([ @@ -193,6 +195,12 @@ Route::get('/setup', function (Request $request) { $auto_assign_user = User::where('assign_new_devices', true)->first(); if ($auto_assign_user) { + // Check if device model exists by name + $device_model = null; + if ($model_name) { + $device_model = DeviceModel::where('name', $model_name)->first(); + } + // Create a new device and assign it to this user $device = Device::create([ 'mac_address' => $mac_address, @@ -202,6 +210,7 @@ Route::get('/setup', function (Request $request) { 'friendly_id' => Str::random(6), 'default_refresh_interval' => 900, 'mirror_device_id' => $auto_assign_user->assign_new_device_id, + 'device_model_id' => $device_model?->id, ]); } else { return response()->json([ @@ -282,7 +291,7 @@ Route::get('/devices', function (Request $request) { })->middleware('auth:sanctum'); Route::get('/device-models', function (Request $request) { - $deviceModels = App\Models\DeviceModel::get([ + $deviceModels = DeviceModel::get([ 'id', 'name', 'label', diff --git a/tests/Feature/Api/DeviceEndpointsTest.php b/tests/Feature/Api/DeviceEndpointsTest.php index 7e8fbdf..2ee5dcf 100644 --- a/tests/Feature/Api/DeviceEndpointsTest.php +++ b/tests/Feature/Api/DeviceEndpointsTest.php @@ -2,10 +2,12 @@ use App\Jobs\GenerateScreenJob; use App\Models\Device; +use App\Models\DeviceModel; use App\Models\Playlist; use App\Models\PlaylistItem; use App\Models\Plugin; use App\Models\User; +use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Storage; use Laravel\Sanctum\Sanctum; @@ -906,3 +908,45 @@ test('screens endpoint returns 404 for invalid device credentials', function () 'message' => 'MAC Address not registered or invalid access token', ]); }); + +test('setup endpoint assigns device model when model-id header is provided', function () { + $user = User::factory()->create(['assign_new_devices' => true]); + $deviceModel = DeviceModel::factory()->create([ + 'name' => 'test-model', + 'label' => 'Test Model', + ]); + + $response = $this->withHeaders([ + 'id' => '00:11:22:33:44:55', + 'model-id' => 'test-model', + ])->get('/api/setup'); + + $response->assertOk() + ->assertJson([ + 'status' => 200, + 'message' => 'Welcome to TRMNL BYOS', + ]); + + $device = Device::where('mac_address', '00:11:22:33:44:55')->first(); + expect($device)->not->toBeNull() + ->and($device->device_model_id)->toBe($deviceModel->id); +}); + +test('setup endpoint handles non-existent device model gracefully', function () { + $user = User::factory()->create(['assign_new_devices' => true]); + + $response = $this->withHeaders([ + 'id' => '00:11:22:33:44:55', + 'model-id' => 'non-existent-model', + ])->get('/api/setup'); + + $response->assertOk() + ->assertJson([ + 'status' => 200, + 'message' => 'Welcome to TRMNL BYOS', + ]); + + $device = Device::where('mac_address', '00:11:22:33:44:55')->first(); + expect($device)->not->toBeNull() + ->and($device->device_model_id)->toBeNull(); +});