F
Filament9mo ago
nowak

How to customise completed export notification/events?

I want to change the way I receive the completed export notification, or add to it. I want to either broadcast the notification in real time, or send the completed export notification to the UI as well as to the database. I know I can control this fully in the ExportCompletion.php jobs handle method:
class ExportCompletion implements ShouldQueue
{
// Truncated

public function handle(): void
{
$this->export->touch('completed_at');

if (! $this->export->user instanceof Authenticatable) {
return;
}

$failedRowsCount = $this->export->getFailedRowsCount();

Notification::make()
->title(__('filament-actions::export.notifications.completed.title'))
->body($this->exporter::getCompletedNotificationBody($this->export))
->when(
! $failedRowsCount,
fn (Notification $notification) => $notification->success(),
)
->when(
$failedRowsCount && ($failedRowsCount < $this->export->total_rows),
fn (Notification $notification) => $notification->warning(),
)
->when(
$failedRowsCount === $this->export->total_rows,
fn (Notification $notification) => $notification->danger(),
)
->when(
$failedRowsCount < $this->export->total_rows,
fn (Notification $notification) => $notification->actions(array_map(
fn (ExportFormat $format): NotificationAction => $format->getDownloadNotificationAction($this->export),
$this->formats,
)),
)
->sendToDatabase($this->export->user);
}
}
class ExportCompletion implements ShouldQueue
{
// Truncated

public function handle(): void
{
$this->export->touch('completed_at');

if (! $this->export->user instanceof Authenticatable) {
return;
}

$failedRowsCount = $this->export->getFailedRowsCount();

Notification::make()
->title(__('filament-actions::export.notifications.completed.title'))
->body($this->exporter::getCompletedNotificationBody($this->export))
->when(
! $failedRowsCount,
fn (Notification $notification) => $notification->success(),
)
->when(
$failedRowsCount && ($failedRowsCount < $this->export->total_rows),
fn (Notification $notification) => $notification->warning(),
)
->when(
$failedRowsCount === $this->export->total_rows,
fn (Notification $notification) => $notification->danger(),
)
->when(
$failedRowsCount < $this->export->total_rows,
fn (Notification $notification) => $notification->actions(array_map(
fn (ExportFormat $format): NotificationAction => $format->getDownloadNotificationAction($this->export),
$this->formats,
)),
)
->sendToDatabase($this->export->user);
}
}
I hear that overriding files like filament/packages/actions/src/Exports/Jobs/ExportCompletion.php is not advised. So what is the best way to customise what happens when an export job completes? Any help is much appreciated!
5 Replies
nowak
nowakOP9mo ago
bump bump bump
awcodes
awcodes9mo ago
I don’t think any one understands exactly what you are trying to do, maybe that why no one has responded.
nowak
nowakOP9mo ago
An example could be to add event(new DatabaseNotificationsSent($this->export->user)); after the Notification::make() that is responsible for sending the completed export database notification. or add ->send() as well as the existing ->sendToDatabase($this->export->user) for better UX. Something like this:
public function handle(): void
{
$this->export->touch('completed_at');

if (! $this->export->user instanceof Authenticatable) {
return;
}

$failedRowsCount = $this->export->getFailedRowsCount();

Notification::make()
->title(__('filament-actions::export.notifications.completed.title'))
->body($this->exporter::getCompletedNotificationBody($this->export))
->when(
! $failedRowsCount,
fn (Notification $notification) => $notification->success(),
)
->when(
$failedRowsCount && ($failedRowsCount < $this->export->total_rows),
fn (Notification $notification) => $notification->warning(),
)
->when(
$failedRowsCount === $this->export->total_rows,
fn (Notification $notification) => $notification->danger(),
)
->when(
$failedRowsCount < $this->export->total_rows,
fn (Notification $notification) => $notification->actions(array_map(
fn (ExportFormat $format): NotificationAction => $format->getDownloadNotificationAction($this->export),
$this->formats,
)),
)
->send()
->sendToDatabase($this->export->user);

event(new DatabaseNotificationsSent($this->export->user));
}
public function handle(): void
{
$this->export->touch('completed_at');

if (! $this->export->user instanceof Authenticatable) {
return;
}

$failedRowsCount = $this->export->getFailedRowsCount();

Notification::make()
->title(__('filament-actions::export.notifications.completed.title'))
->body($this->exporter::getCompletedNotificationBody($this->export))
->when(
! $failedRowsCount,
fn (Notification $notification) => $notification->success(),
)
->when(
$failedRowsCount && ($failedRowsCount < $this->export->total_rows),
fn (Notification $notification) => $notification->warning(),
)
->when(
$failedRowsCount === $this->export->total_rows,
fn (Notification $notification) => $notification->danger(),
)
->when(
$failedRowsCount < $this->export->total_rows,
fn (Notification $notification) => $notification->actions(array_map(
fn (ExportFormat $format): NotificationAction => $format->getDownloadNotificationAction($this->export),
$this->formats,
)),
)
->send()
->sendToDatabase($this->export->user);

event(new DatabaseNotificationsSent($this->export->user));
}
But I don't want to add this directly to the ExportCompletion.php file. Does it make more sense now?
awcodes
awcodes9mo ago
Sorry, I’m still not totally following. If the you have a custom export then why wouldn’t this be the appropriate place to do it? Better question, is what you are trying to do work or not?
nowak
nowakOP9mo ago
I have a custom export job, I don't think this can be used to customize or add to the export completion event. I am just hardcoding changes directly into the filament/packages/actions/src/Exports/Jobs/ExportCompletion.php plugin file, which does work, but I want to use a better approach for doing this. Is the best approach to extend the filament/packages/actions/src/Exports/Jobs/ExportCompletion.php and override it's methods, or is this not a viable way of doing this?
Want results from more Discord servers?
Add your server