some cool progress, but something with unique not working

This commit is contained in:
spencerrlongg 2024-03-12 18:33:59 -05:00
parent eb8f1dd553
commit 6732b6601e
3 changed files with 184 additions and 23 deletions

View file

@ -18,6 +18,7 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;
use Watson\Validating\ValidatingTrait; use Watson\Validating\ValidatingTrait;
/** /**
@ -88,30 +89,58 @@ class Asset extends Depreciable
'deleted_at' => 'datetime', 'deleted_at' => 'datetime',
]; ];
protected $rules = [ //protected $rules = [
'model_id' => 'required|integer|exists:models,id,deleted_at,NULL|not_array', // 'model_id' => 'required|integer|exists:models,id,deleted_at,NULL|not_array',
'status_id' => 'required|integer|exists:status_labels,id', // 'status_id' => 'required|integer|exists:status_labels,id',
'asset_tag' => 'required|min:1|max:255|unique_undeleted:assets,asset_tag|not_array', // 'asset_tag' => ['required', 'min:1', 'max:255', 'unique_undeleted:assets,asset_tag', 'not_array', Rule::unique('assets')->ignore($this->id)->withoutTrashed()],
'name' => 'nullable|max:255', // 'name' => 'nullable|max:255',
'company_id' => 'nullable|integer|exists:companies,id', // 'company_id' => 'nullable|integer|exists:companies,id',
'warranty_months' => 'nullable|numeric|digits_between:0,240', // 'warranty_months' => 'nullable|numeric|digits_between:0,240',
'last_checkout' => 'nullable|date_format:Y-m-d H:i:s', // 'last_checkout' => 'nullable|date_format:Y-m-d H:i:s',
'expected_checkin' => 'nullable|date', // 'expected_checkin' => 'nullable|date',
'location_id' => 'nullable|exists:locations,id', // 'location_id' => 'nullable|exists:locations,id',
'rtd_location_id' => 'nullable|exists:locations,id', // 'rtd_location_id' => 'nullable|exists:locations,id',
'purchase_date' => 'nullable|date|date_format:Y-m-d', // 'purchase_date' => 'nullable|date|date_format:Y-m-d',
'serial' => 'nullable|unique_undeleted:assets,serial', // 'serial' => 'nullable|unique_undeleted:assets,serial',
'purchase_cost' => 'nullable|numeric|gte:0', // 'purchase_cost' => 'nullable|numeric|gte:0',
'supplier_id' => 'nullable|exists:suppliers,id', // 'supplier_id' => 'nullable|exists:suppliers,id',
'asset_eol_date' => 'nullable|date', // 'asset_eol_date' => 'nullable|date',
'eol_explicit' => 'nullable|boolean', // 'eol_explicit' => 'nullable|boolean',
'byod' => 'nullable|boolean', // 'byod' => 'nullable|boolean',
'order_number' => 'nullable|string|max:191', // 'order_number' => 'nullable|string|max:191',
'notes' => 'nullable|string|max:65535', // 'notes' => 'nullable|string|max:65535',
'assigned_to' => 'nullable|integer', // 'assigned_to' => 'nullable|integer',
'requestable' => 'nullable|boolean', // 'requestable' => 'nullable|boolean',
]; //];
public function getRulesAttribute()
{
$rule = Rule::unique('assets')->ignore($this->id)->withoutTrashed();
$asset = $this->id;
return [
'model_id' => 'required|integer|exists:models,id,deleted_at,NULL|not_array',
'status_id' => 'required|integer|exists:status_labels,id',
'asset_tag' => ['required', 'min:1', 'max:255', Rule::unique('assets', 'asset_tag')->ignore($this->id, 'id')->withoutTrashed()],
'name' => 'nullable|max:255',
'company_id' => 'nullable|integer|exists:companies,id',
'warranty_months' => 'nullable|numeric|digits_between:0,240',
'last_checkout' => 'nullable|date_format:Y-m-d H:i:s',
'expected_checkin' => 'nullable|date',
'location_id' => 'nullable|exists:locations,id',
'rtd_location_id' => 'nullable|exists:locations,id',
'purchase_date' => 'nullable|date|date_format:Y-m-d',
'serial' => 'nullable|unique_undeleted:assets,serial',
'purchase_cost' => 'nullable|numeric|gte:0',
'supplier_id' => 'nullable|exists:suppliers,id',
'asset_eol_date' => 'nullable|date',
'eol_explicit' => 'nullable|boolean',
'byod' => 'nullable|boolean',
'order_number' => 'nullable|string|max:191',
'notes' => 'nullable|string|max:65535',
'assigned_to' => 'nullable|integer',
'requestable' => 'nullable|boolean',
];
}
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.

View file

@ -79,6 +79,21 @@ class ValidationServiceProvider extends ServiceProvider
return $count < 1; return $count < 1;
} }
//else {
// if (($parameters[0] == 'assets') && ($attribute == 'serial') && (Setting::getSettings()->unique_serial != '1')) {
// return true;
// }
//
// $count = DB::table($parameters[0])
// ->select('id')
// ->where($attribute, '=', $value)
// ->whereNull('deleted_at')
// ->where('id', '!=', $parameters[1])
// ->where('id', '!=', $parameters[2])
// ->count();
//
// return $count < 1;
//}
}); });
/** /**

View file

@ -0,0 +1,117 @@
<?php
namespace Tests\Feature\Api\Assets;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Company;
use App\Models\Location;
use App\Models\Statuslabel;
use App\Models\Supplier;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class AssetUpdateTest extends TestCase
{
use InteractsWithSettings;
public function testRequiresPermissionToUpdateAsset()
{
$asset = Asset::factory()->create();
$this->actingAsForApi(User::factory()->create())
->patchJson(route('api.assets.update', $asset->id))
->assertForbidden();
}
public function testAllAssetAttributesAreStored()
{
$asset = Asset::factory()->create();
$user = User::factory()->editAssets()->create();
$userAssigned = User::factory()->create();
$company = Company::factory()->create();
$location = Location::factory()->create();
$model = AssetModel::factory()->create();
$rtdLocation = Location::factory()->create();
$status = Statuslabel::factory()->create();
$supplier = Supplier::factory()->create();
$response = $this->actingAsForApi($user)
->patchJson(route('api.assets.update', $asset->id), [
'asset_eol_date' => '2024-06-02',
'asset_tag' => 'random_string',
'assigned_user' => $userAssigned->id,
'company_id' => $company->id,
'last_audit_date' => '2023-09-03',
'location_id' => $location->id,
'model_id' => $model->id,
'name' => 'A New Asset',
'notes' => 'Some notes',
'order_number' => '5678',
'purchase_cost' => '123.45',
'purchase_date' => '2023-09-02',
'requestable' => true,
'rtd_location_id' => $rtdLocation->id,
'serial' => '1234567890',
'status_id' => $status->id,
'supplier_id' => $supplier->id,
'warranty_months' => 10,
])
->assertOk()
->assertStatusMessageIs('success')
->json();
$updatedAsset = Asset::find($response['payload']['id']);
// TODO: this isn't working, i assume `adminuser` is the user that created asset
//$this->assertTrue($updatedAsset->adminuser->is($user));
$this->assertEquals('2024-06-02', $updatedAsset->asset_eol_date);
$this->assertEquals('random_string', $updatedAsset->asset_tag);
$this->assertEquals($userAssigned->id, $updatedAsset->assigned_to);
$this->assertTrue($updatedAsset->company->is($company));
// TODO: this doesn't work
//$this->assertTrue($updatedAsset->location->is($location));
$this->assertTrue($updatedAsset->model->is($model));
$this->assertEquals('A New Asset', $updatedAsset->name);
$this->assertEquals('Some notes', $updatedAsset->notes);
$this->assertEquals('5678', $updatedAsset->order_number);
$this->assertEquals('123.45', $updatedAsset->purchase_cost);
$this->assertTrue($updatedAsset->purchase_date->is('2023-09-02'));
$this->assertEquals('1', $updatedAsset->requestable);
$this->assertTrue($updatedAsset->defaultLoc->is($rtdLocation));
$this->assertEquals('1234567890', $updatedAsset->serial);
$this->assertTrue($updatedAsset->assetstatus->is($status));
$this->assertTrue($updatedAsset->supplier->is($supplier));
$this->assertEquals(10, $updatedAsset->warranty_months);
}
public function testArchivedDepreciateAndPhysicalCanBeNull()
{
$model = AssetModel::factory()->ipadModel()->create();
$status = Statuslabel::factory()->create();
$asset = Asset::factory()->create();
$this->settings->enableAutoIncrement();
$response = $this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.assets.update', $asset->id), [
'archive' => null,
'depreciate' => null,
'physical' => null
])
->dd()
->assertOk()
->assertStatusMessageIs('success')
->json();
$asset = Asset::find($response['payload']['id']);
$this->assertEquals(0, $asset->archived);
$this->assertEquals(1, $asset->physical);
$this->assertEquals(0, $asset->depreciate);
}
}