diff --git a/app/Http/Requests/StoreAssetRequest.php b/app/Http/Requests/StoreAssetRequest.php index 0730a19a37..719dfbb4b3 100644 --- a/app/Http/Requests/StoreAssetRequest.php +++ b/app/Http/Requests/StoreAssetRequest.php @@ -20,9 +20,18 @@ class StoreAssetRequest extends ImageUploadRequest public function prepareForValidation(): void { + if ($this->has('assigned_user')) { + $assigned_to = $this->assigned_user; + } elseif ($this->has('assigned_location')) { + $assigned_to = $this->assigned_location; + } elseif ($this->has('assigned_asset')) { + $assigned_to = $this->assigned_asset; + } + $this->merge([ 'asset_tag' => $this->asset_tag ?? Asset::autoincrement_asset(), 'company_id' => Company::getIdForCurrentUser($this->company_id), + 'assigned_to' => $assigned_to ?? null, ]); } diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 406f4deb19..60fd6dbb58 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -109,7 +109,7 @@ class Asset extends Depreciable 'byod' => 'nullable|boolean', 'order_number' => 'nullable|string|max:191', 'notes' => 'nullable|string|max:65535', - 'assigned_to' => 'nullable|integer|exists:users,id', + 'assigned_to' => 'nullable|integer', 'requestable' => 'nullable|boolean', ]; diff --git a/tests/Feature/Api/Assets/AssetStoreTest.php b/tests/Feature/Api/Assets/AssetStoreTest.php index cfbed7404f..746bea9d8f 100644 --- a/tests/Feature/Api/Assets/AssetStoreTest.php +++ b/tests/Feature/Api/Assets/AssetStoreTest.php @@ -253,7 +253,6 @@ class AssetStoreTest extends TestCase $response = $this->actingAsForApi($user) ->postJson(route('api.assets.store'), [ 'assigned_user' => $userAssigned->id, - 'assigned_to' => $userAssigned->id, // why are both of these needed? documentation says only assigned_user 'model_id' => $model->id, 'status_id' => $status->id, ]) @@ -266,4 +265,54 @@ class AssetStoreTest extends TestCase $this->assertTrue($asset->adminuser->is($user)); $this->assertTrue($asset->assignedTo->is($userAssigned)); } + + public function testAnAssetCanBeCheckedOutToLocationOnStore() + { + $model = AssetModel::factory()->create(); + $status = Statuslabel::factory()->create(); + $location = Location::factory()->create(); + $user = User::factory()->createAssets()->create(); + + $this->settings->enableAutoIncrement(); + + $response = $this->actingAsForApi($user) + ->postJson(route('api.assets.store'), [ + 'assigned_location' => $location->id, + 'model_id' => $model->id, + 'status_id' => $status->id, + ]) + ->assertOk() + ->assertStatusMessageIs('success') + ->json(); + + $asset = Asset::find($response['payload']['id']); + + $this->assertTrue($asset->adminuser->is($user)); + $this->assertTrue($asset->location->is($location)); + } + + public function testAnAssetCanBeCheckedOutToAssetOnStore() + { + $model = AssetModel::factory()->create(); + $status = Statuslabel::factory()->create(); + $asset = Asset::factory()->create(); + $user = User::factory()->createAssets()->create(); + + $this->settings->enableAutoIncrement(); + + $response = $this->actingAsForApi($user) + ->postJson(route('api.assets.store'), [ + 'assigned_asset' => $asset->id, + 'model_id' => $model->id, + 'status_id' => $status->id, + ]) + ->assertOk() + ->assertStatusMessageIs('success') + ->json(); + + $apiAsset = Asset::find($response['payload']['id']); + + $this->assertTrue($apiAsset->adminuser->is($user)); + $this->assertTrue($apiAsset->assignedAssets()->is($asset)); //todo: figure this out + } }