diff --git a/src/Controllers/SentEmailsController.php b/src/Controllers/SentEmailsController.php index 587ae6f..8f99c5f 100644 --- a/src/Controllers/SentEmailsController.php +++ b/src/Controllers/SentEmailsController.php @@ -2,43 +2,19 @@ namespace Dcblogdev\LaravelSentEmails\Controllers; -use Citco\Carbon; use Dcblogdev\LaravelSentEmails\Models\SentEmail; use Dcblogdev\LaravelSentEmails\Models\SentEmailAttachment; use Illuminate\Contracts\View\View; +use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\BinaryFileResponse; class SentEmailsController { - public function index(): View + public function index(Request $request): View { - $emails = SentEmail::orderby('id', 'desc'); - - if (request()->exists('date')) { - $date = request('date'); - $from = request('from'); - $to = request('to'); - $subject = request('subject'); - - if ($date != '') { - $date = Carbon::parse($date)->format('Y-m-d'); - $emails->where('date', '=', $date); - } - - if ($from != '') { - $emails->where('from', 'like', "%$from%"); - } - - if ($to != '') { - $emails->where('to', 'like', "%$to%"); - } - - if ($subject != '') { - $emails->where('subject', 'like', "%$subject%"); - } - } - - $emails = $emails->paginate(config('sentemails.perPage')); + $emails = SentEmail::orderby('id', 'desc') + ->applyFilters($request) + ->paginate(config('sentemails.perPage')); return view('sentemails::index', compact('emails')); } diff --git a/src/Models/SentEmail.php b/src/Models/SentEmail.php index eb721af..22bd3f2 100644 --- a/src/Models/SentEmail.php +++ b/src/Models/SentEmail.php @@ -6,6 +6,8 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Http\Request; +use Illuminate\Support\Carbon; class SentEmail extends Model { @@ -46,8 +48,24 @@ public function attachments(): HasMany return $this->hasMany(SentEmailAttachment::class); } - public function applyFilters() + public function scopeApplyFilters($query, Request $request) { + $date = $request->input('date'); + $from = $request->input('from'); + $to = $request->input('to'); + $subject = $request->input('subject'); + $query->when($date, function ($query, $date) { + $query->where('date', '=', Carbon::parse($date)->format('Y-m-d')); + }) + ->when($from, function ($query, $from) { + $query->where('from', 'like', "%$from%"); + }) + ->when($to, function ($query, $to) { + $query->where('to', 'like', "%$to%"); + }) + ->when($subject, function ($query, $subject) { + $query->where('subject', 'like', "%$subject%"); + }); } } diff --git a/tests/SentEmailsTest.php b/tests/SentEmailsTest.php index 913c3ee..9d34089 100644 --- a/tests/SentEmailsTest.php +++ b/tests/SentEmailsTest.php @@ -4,7 +4,66 @@ use Dcblogdev\LaravelSentEmails\Models\SentEmailAttachment; test('can see emails inbox', function () { - $this->get(route('sentemails.index'))->assertOk(); + SentEmail::factory()->count(10)->create(); + $this->get(route('sentemails.index')) + ->assertOk() + ->assertViewHas('emails', function ($emails) { + return $emails->count() === 10; + } + ); +}); + +test('can filter emails by date', function () { + + SentEmail::factory()->create(['date' => '2023-10-20']); + SentEmail::factory()->create(['date' => '2023-10-20']); + SentEmail::factory()->create(); + + $this->get(route('sentemails.index', ['date' => '2023-10-20'])) + ->assertOk() + ->assertViewHas('emails', function ($emails) { + return $emails->count() === 2; + } + ); +}); + +test('can filter emails by from', function () { + + SentEmail::factory()->create(['from' => 'demo@demo.com']); + SentEmail::factory()->create(); + + $this->get(route('sentemails.index', ['from' => 'demo@demo.com'])) + ->assertOk() + ->assertViewHas('emails', function ($emails) { + return $emails->count() === 1; + } + ); +}); + +test('can filter emails by to', function () { + + SentEmail::factory()->create(['to' => 'demo@demo.com']); + SentEmail::factory()->create(); + + $this->get(route('sentemails.index', ['to' => 'demo@demo.com'])) + ->assertOk() + ->assertViewHas('emails', function ($emails) { + return $emails->count() === 1; + } + ); +}); + +test('can filter emails by subject', function () { + + SentEmail::factory()->create(['subject' => 'Demo']); + SentEmail::factory()->create(['subject' => 'Other']); + + $this->get(route('sentemails.index', ['subject' => 'Demo'])) + ->assertOk() + ->assertViewHas('emails', function ($emails) { + return $emails->count() === 1; + } + ); }); test('can see email', function () {