feat(storage): implement transaction handling in storage settings submission

- Wrapped the storage settings submission process in a database transaction to ensure data integrity.
- Added connection testing within the transaction to verify settings before finalizing the save.
- Enhanced error handling by refreshing the model state after a rollback, ensuring the UI reflects the latest database values.
- Dispatch success event upon successful update and verification of storage settings.
This commit is contained in:
Andras Bacsai 2025-10-07 15:08:22 +02:00
parent 2c64136503
commit fbbaab55f5

View file

@ -5,6 +5,7 @@
use App\Models\S3Storage;
use App\Support\ValidationPatterns;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
class Form extends Component
@ -91,9 +92,24 @@ public function submit()
try {
$this->authorize('update', $this->storage);
$this->validate();
$this->testConnection();
DB::transaction(function () {
$this->validate();
$this->storage->save();
// Test connection with new values - if this fails, transaction will rollback
$this->storage->testConnection(shouldSave: false);
// If we get here, the connection test succeeded
$this->storage->is_usable = true;
$this->storage->unusable_email_sent = false;
$this->storage->save();
});
$this->dispatch('success', 'Storage settings updated and connection verified.');
} catch (\Throwable $e) {
// Refresh the model to revert UI to database values after rollback
$this->storage->refresh();
return handleError($e, $this);
}
}