Merge pull request #14488 from marcusmoore/bug/pass-last-audit-date-through-for-validation
Handle badly formatted `last_audit_date` in `StoreAssetRequest`
This commit is contained in:
commit
67b5e9093e
2 changed files with 33 additions and 5 deletions
|
@ -5,6 +5,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 Carbon\Carbon;
|
||||||
|
use Carbon\Exceptions\InvalidFormatException;
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
|
|
||||||
class StoreAssetRequest extends ImageUploadRequest
|
class StoreAssetRequest extends ImageUploadRequest
|
||||||
|
@ -28,11 +29,7 @@ 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->parseLastAuditDate();
|
||||||
$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(),
|
||||||
|
@ -55,4 +52,21 @@ class StoreAssetRequest extends ImageUploadRequest
|
||||||
|
|
||||||
return $rules;
|
return $rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function parseLastAuditDate(): void
|
||||||
|
{
|
||||||
|
if ($this->input('last_audit_date')) {
|
||||||
|
try {
|
||||||
|
$lastAuditDate = Carbon::parse($this->input('last_audit_date'));
|
||||||
|
|
||||||
|
$this->merge([
|
||||||
|
'last_audit_date' => $lastAuditDate->startOfDay()->format('Y-m-d H:i:s'),
|
||||||
|
]);
|
||||||
|
} catch (InvalidFormatException $e) {
|
||||||
|
// we don't need to do anything here...
|
||||||
|
// we'll keep the provided date in an
|
||||||
|
// invalid format so validation picks it up later
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,6 +113,20 @@ class AssetStoreTest extends TestCase
|
||||||
$this->assertNull($asset->last_audit_date);
|
$this->assertNull($asset->last_audit_date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testNonDateUsedForLastAuditDateReturnsValidationError()
|
||||||
|
{
|
||||||
|
$response = $this->actingAsForApi(User::factory()->superuser()->create())
|
||||||
|
->postJson(route('api.assets.store'), [
|
||||||
|
'last_audit_date' => 'this-is-not-valid',
|
||||||
|
'asset_tag' => '1234',
|
||||||
|
'model_id' => AssetModel::factory()->create()->id,
|
||||||
|
'status_id' => Statuslabel::factory()->create()->id,
|
||||||
|
])
|
||||||
|
->assertStatusMessageIs('error');
|
||||||
|
|
||||||
|
$this->assertNotNull($response->json('messages.last_audit_date'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testArchivedDepreciateAndPhysicalCanBeNull()
|
public function testArchivedDepreciateAndPhysicalCanBeNull()
|
||||||
{
|
{
|
||||||
$model = AssetModel::factory()->ipadModel()->create();
|
$model = AssetModel::factory()->ipadModel()->create();
|
||||||
|
|
Loading…
Add table
Reference in a new issue