mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 15:07:49 +00:00
feat(#55): auto assign device model when provided at setup
Some checks are pending
tests / ci (push) Waiting to run
Some checks are pending
tests / ci (push) Waiting to run
This commit is contained in:
parent
51af95da2c
commit
2427436b31
2 changed files with 54 additions and 1 deletions
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue