Skip to content

Commit 6f03f7a

Browse files
add try catch to export action
1 parent 88a837b commit 6f03f7a

File tree

2 files changed

+154
-134
lines changed

2 files changed

+154
-134
lines changed

app/Filament/Resources/DonationResource/Actions/ExportAction.php

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Filament\Exports\ExcelExportWithNotificationInDB;
88
use App\Filament\Resources\DonationResource;
99
use App\Models\Donation;
10+
use Filament\Notifications\Notification;
1011
use Illuminate\Support\Str;
1112
use pxlrbt\FilamentExcel\Actions\Tables\ExportAction as BaseAction;
1213
use pxlrbt\FilamentExcel\Columns\Column;
@@ -21,48 +22,57 @@ protected function setUp(): void
2122

2223
$this->color('secondary');
2324

24-
$this->exports([
25-
ExcelExportWithNotificationInDB::make()
26-
->withFilename(fn () => sprintf(
27-
'%s-%s',
28-
now()->format('Y_m_d-H_i_s'),
29-
Str::slug(DonationResource::getPluralModelLabel()),
30-
))
31-
->fromTable()
32-
33-
->withColumns([
34-
Column::make('id')
35-
->heading('ID'),
36-
37-
Column::make('organization.name')
38-
->formatStateUsing(fn ($state) => Str::upper($state))
39-
->heading(__('organization.label.singular')),
40-
41-
Column::make('project.name')
42-
->formatStateUsing(fn ($state) => Str::upper($state))
43-
->heading(__('project.label.singular')),
44-
45-
Column::make('full_name')
46-
->formatStateUsing(fn ($state) => Str::upper($state))
47-
->heading(__('donation.labels.full_name')),
48-
49-
Column::make('amount')
50-
->heading(__('donation.labels.amount')),
51-
52-
Column::make('created_at')
53-
->formatStateUsing(fn (Donation $record) => $record->created_at->toFormattedDateTime())
54-
->heading(__('donation.labels.created_at')),
55-
56-
Column::make('updated_at')
57-
->formatStateUsing(fn (Donation $record) => $record->updated_at?->toFormattedDateTime())
58-
->heading(__('donation.labels.status_updated_at')),
59-
60-
Column::make('status')
61-
->formatStateUsing(fn (Donation $record) => $record->status->label())
62-
->heading(__('donation.labels.status')),
63-
64-
])
65-
->queue(),
66-
]);
25+
try {
26+
$this->exports([
27+
ExcelExportWithNotificationInDB::make()
28+
->withFilename(fn () => sprintf(
29+
'%s-%s',
30+
now()->format('Y_m_d-H_i_s'),
31+
Str::slug(DonationResource::getPluralModelLabel()),
32+
))
33+
->fromTable()
34+
35+
->withColumns([
36+
Column::make('id')
37+
->heading('ID'),
38+
39+
Column::make('organization.name')
40+
->formatStateUsing(fn ($state) => Str::upper($state))
41+
->heading(__('organization.label.singular')),
42+
43+
Column::make('project.name')
44+
->formatStateUsing(fn ($state) => Str::upper($state))
45+
->heading(__('project.label.singular')),
46+
47+
Column::make('full_name')
48+
->formatStateUsing(fn ($state) => Str::upper($state))
49+
->heading(__('donation.labels.full_name')),
50+
51+
Column::make('amount')
52+
->heading(__('donation.labels.amount')),
53+
54+
Column::make('created_at')
55+
->formatStateUsing(fn (Donation $record) => $record->created_at->toFormattedDateTime())
56+
->heading(__('donation.labels.created_at')),
57+
58+
Column::make('updated_at')
59+
->formatStateUsing(fn (Donation $record) => $record->updated_at?->toFormattedDateTime())
60+
->heading(__('donation.labels.status_updated_at')),
61+
62+
Column::make('status')
63+
->formatStateUsing(fn (Donation $record) => $record->status->label())
64+
->heading(__('donation.labels.status')),
65+
66+
])
67+
->queue(),
68+
]);
69+
} catch (\Throwable $exception) {
70+
logger()->error($exception->getMessage());
71+
Notification::make('export')
72+
->title(__('notification.export.error.title'))
73+
->body(__('notification.export.error.body'))
74+
->danger()
75+
->send();
76+
}
6777
}
6878
}

app/Filament/Resources/UserResource/Actions/ExportAction.php

Lines changed: 101 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Filament\Exports\ExcelExportWithNotificationInDB;
99
use App\Filament\Resources\UserResource;
1010
use App\Models\User;
11+
use Filament\Notifications\Notification;
1112
use Illuminate\Database\Eloquent\Builder;
1213
use Illuminate\Support\Str;
1314
use pxlrbt\FilamentExcel\Actions\Tables\ExportAction as BaseAction;
@@ -23,101 +24,110 @@ protected function setUp(): void
2324

2425
$this->color('secondary');
2526

26-
$this->exports([
27-
ExcelExportWithNotificationInDB::make()
28-
->withFilename(fn () => sprintf(
29-
'%s-%s',
30-
now()->format('Y_m_d-H_i_s'),
31-
Str::slug(UserResource::getPluralModelLabel()),
32-
))
33-
->fromTable()
34-
->modifyQueryUsing(function (Builder $query) {
35-
return $query
36-
->addSelect([
37-
'referrer',
38-
'id',
39-
'role',
40-
'name',
41-
'email',
42-
'email_verified_at',
43-
'created_at',
44-
'newsletter',
45-
'organization_id',
46-
'created_by',
47-
])
48-
->with([
49-
'donations' => fn ($q) => $q->select(['user_id', 'amount', 'status', 'created_at']),
50-
])
51-
->withCount(['donations']);
52-
})
53-
->withColumns([
54-
Column::make('id')
55-
->heading('ID'),
56-
57-
Column::make('name')
58-
->heading(__('user.labels.name')),
59-
60-
Column::make('email')
61-
->heading(__('user.labels.email')),
62-
63-
Column::make('role')
64-
->heading(__('user.labels.role'))->formatStateUsing(
65-
fn (User $record) => $record->role->label()
66-
),
67-
68-
Column::make('status')
69-
->heading(__('user.labels.status'))
70-
->formatStateUsing(
71-
fn (User $record) => $record->email_verified_at ?
72-
__('user.labels.status_display.verified') :
73-
__('user.labels.status_display.unverified')
74-
),
75-
76-
Column::make('created_at')
77-
->heading(__('user.labels.created_at'))
78-
->formatStateUsing(
79-
fn (User $record) => $record->created_at->toFormattedDateTime()
80-
),
81-
82-
Column::make('newsletter_subscription')
83-
->heading(__('user.labels.newsletter_subscription'))
84-
->formatStateUsing(
85-
fn (User $record) => $record->newsletter ?
27+
try {
28+
$this->exports([
29+
ExcelExportWithNotificationInDB::make()
30+
->withFilename(fn () => sprintf(
31+
'%s-%s',
32+
now()->format('Y_m_d-H_i_s'),
33+
Str::slug(UserResource::getPluralModelLabel()),
34+
))
35+
->fromTable()
36+
->modifyQueryUsing(function (Builder $query) {
37+
return $query
38+
->addSelect([
39+
'referrer',
40+
'id',
41+
'role',
42+
'name',
43+
'email',
44+
'email_verified_at',
45+
'created_at',
46+
'newsletter',
47+
'organization_id',
48+
'created_by',
49+
])
50+
->with([
51+
'donations' => fn ($q) => $q->select(['user_id', 'amount', 'status', 'created_at']),
52+
])
53+
->withCount(['donations']);
54+
})
55+
->withColumns([
56+
Column::make('id')
57+
->heading('ID'),
58+
59+
Column::make('name')
60+
->heading(__('user.labels.name')),
61+
62+
Column::make('email')
63+
->heading(__('user.labels.email')),
64+
65+
Column::make('role')
66+
->heading(__('user.labels.role'))->formatStateUsing(
67+
fn (User $record) => $record->role->label()
68+
),
69+
70+
Column::make('status')
71+
->heading(__('user.labels.status'))
72+
->formatStateUsing(
73+
fn (User $record) => $record->email_verified_at ?
74+
__('user.labels.status_display.verified') :
75+
__('user.labels.status_display.unverified')
76+
),
77+
78+
Column::make('created_at')
79+
->heading(__('user.labels.created_at'))
80+
->formatStateUsing(
81+
fn (User $record) => $record->created_at->toFormattedDateTime()
82+
),
83+
84+
Column::make('newsletter_subscription')
85+
->heading(__('user.labels.newsletter_subscription'))
86+
->formatStateUsing(
87+
fn (User $record) => $record->newsletter ?
8688
$record->created_at->toFormattedDateTime() :
8789
''
88-
),
89-
90-
Column::make('referrer')
91-
->heading(__('user.labels.referrer')),
92-
93-
Column::make('donations_count')
94-
->formatStateUsing(fn (User $record) => $record->donations()->whereCharged()->count() ?? 0)
95-
->heading(__('user.labels.donations_count')),
96-
97-
Column::make('donations')
98-
->heading(__('user.labels.donations_sum'))
99-
->formatStateUsing(
100-
fn (User $record) => $record->donations
101-
->reject(fn ($item) => $item->status === EuPlatescStatus::CHARGED)
102-
->sum('amount')
103-
),
104-
105-
Column::make('comments_count')
106-
//TODO:: implement comments module and add this column
107-
->formatStateUsing(fn (User $record) => 'numarul de comentarii va aparea dupa implementarea modulului')
108-
->heading(__('user.labels.comments_count')),
109-
110-
Column::make('last_donation_date')
111-
->heading(__('user.labels.last_donation_date'))
112-
->formatStateUsing(
113-
fn (User $record) => $record->donations_count ?
90+
),
91+
92+
Column::make('referrer')
93+
->heading(__('user.labels.referrer')),
94+
95+
Column::make('donations_count')
96+
->formatStateUsing(fn (User $record) => $record->donations()->whereCharged()->count() ?? 0)
97+
->heading(__('user.labels.donations_count')),
98+
99+
Column::make('donations')
100+
->heading(__('user.labels.donations_sum'))
101+
->formatStateUsing(
102+
fn (User $record) => $record->donations
103+
->reject(fn ($item) => $item->status === EuPlatescStatus::CHARGED)
104+
->sum('amount')
105+
),
106+
107+
Column::make('comments_count')
108+
//TODO:: implement comments module and add this column
109+
->formatStateUsing(fn (User $record) => 'numarul de comentarii va aparea dupa implementarea modulului')
110+
->heading(__('user.labels.comments_count')),
111+
112+
Column::make('last_donation_date')
113+
->heading(__('user.labels.last_donation_date'))
114+
->formatStateUsing(
115+
fn (User $record) => $record->donations_count ?
114116
$record->donations
115117
->reject(fn ($item) => $item->status === EuPlatescStatus::CHARGED)
116118
->last()?->created_at->toFormattedDateTime() : ''
117-
),
118-
119-
])
120-
->queue(),
121-
]);
119+
),
120+
121+
])
122+
->queue(),
123+
]);
124+
} catch (\Throwable $exception) {
125+
logger()->error($exception->getMessage());
126+
Notification::make('export')
127+
->title(__('notification.export.error.title'))
128+
->body(__('notification.export.error.body'))
129+
->danger()
130+
->send();
131+
}
122132
}
123133
}

0 commit comments

Comments
 (0)