Add data export retry and cancel controls
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Filament\Resources\DataExportResource\Tables;
|
||||
|
||||
use App\Jobs\GenerateDataExport;
|
||||
use App\Models\DataExport;
|
||||
use App\Services\Audit\SuperAdminAuditLogger;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
@@ -38,6 +40,7 @@ class DataExportTable
|
||||
DataExport::STATUS_READY => 'success',
|
||||
DataExport::STATUS_FAILED => 'danger',
|
||||
DataExport::STATUS_PROCESSING => 'warning',
|
||||
DataExport::STATUS_CANCELED => 'gray',
|
||||
default => 'gray',
|
||||
}),
|
||||
IconColumn::make('include_media')
|
||||
@@ -71,6 +74,7 @@ class DataExportTable
|
||||
DataExport::STATUS_PROCESSING => __('admin.data_exports.status.processing'),
|
||||
DataExport::STATUS_READY => __('admin.data_exports.status.ready'),
|
||||
DataExport::STATUS_FAILED => __('admin.data_exports.status.failed'),
|
||||
DataExport::STATUS_CANCELED => __('admin.data_exports.status.canceled'),
|
||||
]),
|
||||
])
|
||||
->actions([
|
||||
@@ -80,6 +84,45 @@ class DataExportTable
|
||||
->url(fn (DataExport $record) => route('superadmin.data-exports.download', $record))
|
||||
->openUrlInNewTab()
|
||||
->visible(fn (DataExport $record): bool => $record->isReady() && ! $record->hasExpired()),
|
||||
Action::make('retry')
|
||||
->label(__('admin.data_exports.actions.retry'))
|
||||
->icon('heroicon-o-arrow-path')
|
||||
->color('warning')
|
||||
->requiresConfirmation()
|
||||
->visible(fn (DataExport $record): bool => $record->canRetry())
|
||||
->action(function (DataExport $record): void {
|
||||
if (! $record->canRetry()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$record->resetForRetry();
|
||||
GenerateDataExport::dispatch($record->id);
|
||||
|
||||
app(SuperAdminAuditLogger::class)->recordModelMutation(
|
||||
'updated',
|
||||
$record,
|
||||
source: self::class
|
||||
);
|
||||
}),
|
||||
Action::make('cancel')
|
||||
->label(__('admin.data_exports.actions.cancel'))
|
||||
->icon('heroicon-o-x-circle')
|
||||
->color('danger')
|
||||
->requiresConfirmation()
|
||||
->visible(fn (DataExport $record): bool => $record->canCancel())
|
||||
->action(function (DataExport $record): void {
|
||||
if (! $record->canCancel()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$record->markCanceled();
|
||||
|
||||
app(SuperAdminAuditLogger::class)->recordModelMutation(
|
||||
'updated',
|
||||
$record,
|
||||
source: self::class
|
||||
);
|
||||
}),
|
||||
])
|
||||
->bulkActions([]);
|
||||
}
|
||||
|
||||
@@ -39,6 +39,10 @@ class GenerateDataExport implements ShouldQueue
|
||||
return;
|
||||
}
|
||||
|
||||
if ($export->status !== DataExport::STATUS_PENDING) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $export->user) {
|
||||
$export->update([
|
||||
'status' => DataExport::STATUS_FAILED,
|
||||
|
||||
@@ -19,6 +19,8 @@ class DataExport extends Model
|
||||
|
||||
public const STATUS_FAILED = 'failed';
|
||||
|
||||
public const STATUS_CANCELED = 'canceled';
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'tenant_id',
|
||||
@@ -58,6 +60,38 @@ class DataExport extends Model
|
||||
return $this->status === self::STATUS_READY;
|
||||
}
|
||||
|
||||
public function canRetry(): bool
|
||||
{
|
||||
return in_array($this->status, [self::STATUS_FAILED, self::STATUS_CANCELED], true);
|
||||
}
|
||||
|
||||
public function canCancel(): bool
|
||||
{
|
||||
return in_array($this->status, [self::STATUS_PENDING, self::STATUS_PROCESSING], true);
|
||||
}
|
||||
|
||||
public function resetForRetry(): void
|
||||
{
|
||||
$this->update([
|
||||
'status' => self::STATUS_PENDING,
|
||||
'error_message' => null,
|
||||
'path' => null,
|
||||
'size_bytes' => null,
|
||||
'expires_at' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function markCanceled(?string $reason = null): void
|
||||
{
|
||||
$this->update([
|
||||
'status' => self::STATUS_CANCELED,
|
||||
'error_message' => $reason ?: 'Canceled by superadmin.',
|
||||
'path' => null,
|
||||
'size_bytes' => null,
|
||||
'expires_at' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function hasExpired(): bool
|
||||
{
|
||||
return $this->expires_at !== null && $this->expires_at->isPast();
|
||||
|
||||
Reference in New Issue
Block a user