Skip to content

Update SuppressionResource.php #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions src/Resources/SuppressionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,36 @@ public static function getNavigationIcon(): ?string

public static function getEloquentQuery(): Builder
{
$mailTable = config('mails.database.tables.mails');
$eventTable = config('mails.database.tables.events');

return parent::getEloquentQuery()
->join('mails', 'mail_events.mail_id', '=', 'mails.id')
->from("$eventTable as events")
->join("$mailTable as mails", 'events.mail_id', '=', 'mails.id')
->where(function ($query) {
$query->where('type', EventType::HARD_BOUNCED)
->orWhere('type', EventType::COMPLAINED);
$query->where('events.type', EventType::HARD_BOUNCED)
->orWhere('events.type', EventType::COMPLAINED);
})
->whereNull('unsuppressed_at')
->whereIn('mails.to', function ($query) {
->whereNull('events.unsuppressed_at')
->whereIn('mails.to', function ($query) use ($eventTable) {
$query->select('to')
->from('mail_events')
->from($eventTable)
->where('type', EventType::HARD_BOUNCED)
->whereNull('unsuppressed_at')
->groupBy('to');
})
->select('mail_events.*', 'mails.to')
->select('events.*', 'mails.to')
->addSelect([
'has_complained' => MailEvent::select('m.id')
->from('mail_events AS me')
->leftJoin('mails As m', function ($join) {
->from("$eventTable as me")
->leftJoin("$mailTable as m", function ($join) {
$join->on('me.mail_id', '=', 'm.id')
->where('me.type', '=', EventType::COMPLAINED);
})
->take(1),
])
->latest('occurred_at')
->orderBy('occurred_at', 'desc');
->latest('events.occurred_at')
->orderBy('events.occurred_at', 'desc');
}

public static function table(Table $table): Table
Expand All @@ -101,14 +105,14 @@ public static function table(Table $table): Table
->columns([
Tables\Columns\TextColumn::make('to')
->label(__('Email address'))
->formatStateUsing(fn ($record) => key(json_decode($record->to ?? [])))
->formatStateUsing(fn($record) => key(json_decode($record->to ?? [])))
->searchable(['to']),

Tables\Columns\TextColumn::make('id')
->label(__('Reason'))
->badge()
->formatStateUsing(fn ($record) => $record->type->value == EventType::COMPLAINED->value ? 'Complained' : 'Bounced')
->color(fn ($record): string => match ($record->type->value == EventType::COMPLAINED->value) {
->formatStateUsing(fn($record) => $record->type->value == EventType::COMPLAINED->value ? 'Complained' : 'Bounced')
->color(fn($record): string => match ($record->type->value == EventType::COMPLAINED->value) {
true => 'danger',
default => 'gray',
}),
Expand All @@ -117,7 +121,7 @@ public static function table(Table $table): Table
->label(__('Occurred At'))
->dateTime('d-m-Y H:i')
->since()
->tooltip(fn (MailEvent $record) => $record->occurred_at->format('d-m-Y H:i'))
->tooltip(fn(MailEvent $record) => $record->occurred_at->format('d-m-Y H:i'))
->sortable()
->searchable(),
])
Expand All @@ -127,7 +131,7 @@ public static function table(Table $table): Table
->action(function (MailEvent $record) {
event(new MailUnsuppressed(key($record->mail->to), $record->mail->mailer == 'smtp' && filled($record->mail->transport) ? $record->mail->transport : $record->mail->mailer, $record->mail->stream_id ?? null));
})
->visible(fn ($record) => Provider::tryFrom($record->mail->mailer == 'smtp' && filled($record->mail->transport) ? $record->mail->transport : $record->mail->mailer)),
->visible(fn($record) => Provider::tryFrom($record->mail->mailer == 'smtp' && filled($record->mail->transport) ? $record->mail->transport : $record->mail->mailer)),

Tables\Actions\ViewAction::make()
->url(null)
Expand All @@ -136,7 +140,7 @@ public static function table(Table $table): Table
->label(__('View'))
->hiddenLabel()
->tooltip(__('View'))
->infolist(fn (Infolist $infolist) => EventResource::infolist($infolist)),
->infolist(fn(Infolist $infolist) => EventResource::infolist($infolist)),
]);
}

Expand Down