Merge pull request #14486 from marcusmoore/bug/sc-25139

Fixes `last_audit_date` not being stored via API correctly
This commit is contained in:
snipe 2024-03-25 19:49:54 +00:00 committed by GitHub
commit 83ef7c6fe8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 2 deletions

View file

@ -4,6 +4,7 @@ namespace App\Http\Requests;
use App\Models\Asset; use App\Models\Asset;
use App\Models\Company; use App\Models\Company;
use Carbon\Carbon;
use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Gate;
class StoreAssetRequest extends ImageUploadRequest class StoreAssetRequest extends ImageUploadRequest
@ -27,6 +28,12 @@ class StoreAssetRequest extends ImageUploadRequest
? Company::getIdForCurrentUser($this->company_id) ? Company::getIdForCurrentUser($this->company_id)
: $this->company_id; : $this->company_id;
if ($this->input('last_audit_date')) {
$this->merge([
'last_audit_date' => Carbon::parse($this->input('last_audit_date'))->startOfDay()->format('Y-m-d H:i:s'),
]);
}
$this->merge([ $this->merge([
'asset_tag' => $this->asset_tag ?? Asset::autoincrement_asset(), 'asset_tag' => $this->asset_tag ?? Asset::autoincrement_asset(),
'company_id' => $idForCurrentUser, 'company_id' => $idForCurrentUser,

View file

@ -65,8 +65,7 @@ class AssetStoreTest extends TestCase
$this->assertEquals('random_string', $asset->asset_tag); $this->assertEquals('random_string', $asset->asset_tag);
$this->assertEquals($userAssigned->id, $asset->assigned_to); $this->assertEquals($userAssigned->id, $asset->assigned_to);
$this->assertTrue($asset->company->is($company)); $this->assertTrue($asset->company->is($company));
// I don't see this on the GUI side either, but it's in the docs so I'm guessing that's a mistake? It wasn't in the controller. $this->assertEquals('2023-09-03 00:00:00', $asset->last_audit_date->format('Y-m-d H:i:s'));
// $this->assertEquals('2023-09-03', $asset->last_audit_date);
$this->assertTrue($asset->location->is($location)); $this->assertTrue($asset->location->is($location));
$this->assertTrue($asset->model->is($model)); $this->assertTrue($asset->model->is($model));
$this->assertEquals('A New Asset', $asset->name); $this->assertEquals('A New Asset', $asset->name);
@ -82,6 +81,38 @@ class AssetStoreTest extends TestCase
$this->assertEquals(10, $asset->warranty_months); $this->assertEquals(10, $asset->warranty_months);
} }
public function testSetsLastAuditDateToMidnightOfProvidedDate()
{
$response = $this->actingAsForApi(User::factory()->superuser()->create())
->postJson(route('api.assets.store'), [
'last_audit_date' => '2023-09-03 12:23:45',
'asset_tag' => '1234',
'model_id' => AssetModel::factory()->create()->id,
'status_id' => Statuslabel::factory()->create()->id,
])
->assertOk()
->assertStatusMessageIs('success');
$asset = Asset::find($response['payload']['id']);
$this->assertEquals('00:00:00', $asset->last_audit_date->format('H:i:s'));
}
public function testLastAuditDateCanBeNull()
{
$response = $this->actingAsForApi(User::factory()->superuser()->create())
->postJson(route('api.assets.store'), [
// 'last_audit_date' => '2023-09-03 12:23:45',
'asset_tag' => '1234',
'model_id' => AssetModel::factory()->create()->id,
'status_id' => Statuslabel::factory()->create()->id,
])
->assertOk()
->assertStatusMessageIs('success');
$asset = Asset::find($response['payload']['id']);
$this->assertNull($asset->last_audit_date);
}
public function testArchivedDepreciateAndPhysicalCanBeNull() public function testArchivedDepreciateAndPhysicalCanBeNull()
{ {
$model = AssetModel::factory()->ipadModel()->create(); $model = AssetModel::factory()->ipadModel()->create();