Skip to content

Commit c812778

Browse files
Update SuppressionResource.php (#46)
* Update SuppressionResource.php Fix: Use configured mail and event table names instead of hardcoded values - Replaced hardcoded 'mails' and 'mail_events' table names with dynamic config-based values - Added table aliases ('events' and 'mails') for clarity and consistency - Ensured compatibility with custom table names defined in config/mails.php * Remove comments --------- Co-authored-by: Baspa <[email protected]>
1 parent aac8337 commit c812778

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/Resources/SuppressionResource.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,36 @@ public static function getNavigationIcon(): ?string
6666

6767
public static function getEloquentQuery(): Builder
6868
{
69+
$mailTable = config('mails.database.tables.mails');
70+
$eventTable = config('mails.database.tables.events');
71+
6972
return parent::getEloquentQuery()
70-
->join('mails', 'mail_events.mail_id', '=', 'mails.id')
73+
->from("$eventTable as events")
74+
->join("$mailTable as mails", 'events.mail_id', '=', 'mails.id')
7175
->where(function ($query) {
72-
$query->where('type', EventType::HARD_BOUNCED)
73-
->orWhere('type', EventType::COMPLAINED);
76+
$query->where('events.type', EventType::HARD_BOUNCED)
77+
->orWhere('events.type', EventType::COMPLAINED);
7478
})
75-
->whereNull('unsuppressed_at')
76-
->whereIn('mails.to', function ($query) {
79+
->whereNull('events.unsuppressed_at')
80+
->whereIn('mails.to', function ($query) use ($eventTable) {
7781
$query->select('to')
78-
->from('mail_events')
82+
->from($eventTable)
7983
->where('type', EventType::HARD_BOUNCED)
8084
->whereNull('unsuppressed_at')
8185
->groupBy('to');
8286
})
83-
->select('mail_events.*', 'mails.to')
87+
->select('events.*', 'mails.to')
8488
->addSelect([
8589
'has_complained' => MailEvent::select('m.id')
86-
->from('mail_events AS me')
87-
->leftJoin('mails As m', function ($join) {
90+
->from("$eventTable as me")
91+
->leftJoin("$mailTable as m", function ($join) {
8892
$join->on('me.mail_id', '=', 'm.id')
8993
->where('me.type', '=', EventType::COMPLAINED);
9094
})
9195
->take(1),
9296
])
93-
->latest('occurred_at')
94-
->orderBy('occurred_at', 'desc');
97+
->latest('events.occurred_at')
98+
->orderBy('events.occurred_at', 'desc');
9599
}
96100

97101
public static function table(Table $table): Table
@@ -101,14 +105,14 @@ public static function table(Table $table): Table
101105
->columns([
102106
Tables\Columns\TextColumn::make('to')
103107
->label(__('Email address'))
104-
->formatStateUsing(fn ($record) => key(json_decode($record->to ?? [])))
108+
->formatStateUsing(fn($record) => key(json_decode($record->to ?? [])))
105109
->searchable(['to']),
106110

107111
Tables\Columns\TextColumn::make('id')
108112
->label(__('Reason'))
109113
->badge()
110-
->formatStateUsing(fn ($record) => $record->type->value == EventType::COMPLAINED->value ? 'Complained' : 'Bounced')
111-
->color(fn ($record): string => match ($record->type->value == EventType::COMPLAINED->value) {
114+
->formatStateUsing(fn($record) => $record->type->value == EventType::COMPLAINED->value ? 'Complained' : 'Bounced')
115+
->color(fn($record): string => match ($record->type->value == EventType::COMPLAINED->value) {
112116
true => 'danger',
113117
default => 'gray',
114118
}),
@@ -117,7 +121,7 @@ public static function table(Table $table): Table
117121
->label(__('Occurred At'))
118122
->dateTime('d-m-Y H:i')
119123
->since()
120-
->tooltip(fn (MailEvent $record) => $record->occurred_at->format('d-m-Y H:i'))
124+
->tooltip(fn(MailEvent $record) => $record->occurred_at->format('d-m-Y H:i'))
121125
->sortable()
122126
->searchable(),
123127
])
@@ -127,7 +131,7 @@ public static function table(Table $table): Table
127131
->action(function (MailEvent $record) {
128132
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));
129133
})
130-
->visible(fn ($record) => Provider::tryFrom($record->mail->mailer == 'smtp' && filled($record->mail->transport) ? $record->mail->transport : $record->mail->mailer)),
134+
->visible(fn($record) => Provider::tryFrom($record->mail->mailer == 'smtp' && filled($record->mail->transport) ? $record->mail->transport : $record->mail->mailer)),
131135

132136
Tables\Actions\ViewAction::make()
133137
->url(null)
@@ -136,7 +140,7 @@ public static function table(Table $table): Table
136140
->label(__('View'))
137141
->hiddenLabel()
138142
->tooltip(__('View'))
139-
->infolist(fn (Infolist $infolist) => EventResource::infolist($infolist)),
143+
->infolist(fn(Infolist $infolist) => EventResource::infolist($infolist)),
140144
]);
141145
}
142146

0 commit comments

Comments
 (0)