Skip to content

UPDATE: Email queued_at field #37

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 4 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Run tests

on:
push:
pull_request:
schedule:
- cron: '0 0 * * *'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddQueuedAtToEmailsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (Schema::hasColumn('emails', 'queued_at')) {
return;
}

Schema::table('emails', function (Blueprint $table) {
$table->timestamp('queued_at')->after('encrypted')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
25 changes: 25 additions & 0 deletions src/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @property $failed
* @property $error
* @property $encrypted
* @property $queued_at
* @property $scheduled_at
* @property $sent_at
* @property $delivered_at
Expand Down Expand Up @@ -277,6 +278,30 @@ public function getAttempts()
return $this->attempts;
}

/**
* Get the queued date.
*
* @return mixed
*/
public function getQueuedDate()
{
return $this->queued_at;
}

/**
* Get the queued date as a Carbon instance.
*
* @return Carbon
*/
public function getQueuedDateAsCarbon()
{
if ($this->queued_at instanceof Carbon) {
return $this->queued_at;
}

return Carbon::parse($this->queued_at);
}

/**
* Get the scheduled date.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Preparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function prepare(EmailComposer $composer)
$this->prepareScheduled($composer);

$this->prepareImmediately($composer);

$this->prepareQueued($composer);
}

/**
Expand Down Expand Up @@ -235,4 +237,19 @@ private function prepareImmediately(EmailComposer $composer)
$composer->getEmail()->fill(['sending' => 1]);
}
}

/**
* Prepare the queued date.
*
* @param EmailComposer $composer
*/
private function prepareQueued(EmailComposer $composer)
{
if ($composer->getData('queued', false) === true) {
$composer->getEmail()->fill([
'queued_at' => Carbon::now()->toDateTimeString(),
]);
}
}

}
1 change: 1 addition & 0 deletions src/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function getQueue()
return $query
->whereNull('deleted_at')
->whereNull('sent_at')
->whereNull('queued_at')
->where(function ($query) {
$query->whereNull('scheduled_at')
->orWhere('scheduled_at', '<=', Carbon::now()->toDateTimeString());
Expand Down
8 changes: 8 additions & 0 deletions tests/QueuedEmailsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public function queueing_an_email_will_leave_sending_on_false()
$this->assertEquals(0, $email->sending);
}

/** @test */
public function queueing_an_email_will_set_the_queued_at_column()
{
$email = $this->queueEmail();

$this->assertNotNull($email->queued_at);
}

/** @test */
public function queueing_an_email_will_dispatch_a_job()
{
Expand Down
13 changes: 13 additions & 0 deletions tests/SendEmailsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Stackkit\LaravelDatabaseEmails\Store;

class SendEmailsCommandTest extends TestCase
Expand Down Expand Up @@ -45,6 +46,18 @@ public function an_email_should_not_be_sent_once_it_is_marked_as_sent()
$this->assertEquals($firstSend, $email->fresh()->getSendDate());
}

/** @test */
public function an_email_should_not_be_sent_if_it_is_queued()
{
Queue::fake();

$email = $this->queueEmail();

$this->artisan('email:send');

$this->assertNull($email->fresh()->getSendDate());
}

/** @test */
public function if_an_email_fails_to_be_sent_it_should_be_logged_in_the_database()
{
Expand Down