diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 8f68b21..adca7fe 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,7 +1,7 @@ name: Run tests on: - push: + pull_request: schedule: - cron: '0 0 * * *' diff --git a/database/migrations/2021_12_02_21000_add_queued_at_to_emails_table.php b/database/migrations/2021_12_02_21000_add_queued_at_to_emails_table.php new file mode 100644 index 0000000..080bf35 --- /dev/null +++ b/database/migrations/2021_12_02_21000_add_queued_at_to_emails_table.php @@ -0,0 +1,34 @@ +timestamp('queued_at')->after('encrypted')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/src/Email.php b/src/Email.php index f4a30bf..8dbcd0d 100644 --- a/src/Email.php +++ b/src/Email.php @@ -23,6 +23,7 @@ * @property $failed * @property $error * @property $encrypted + * @property $queued_at * @property $scheduled_at * @property $sent_at * @property $delivered_at @@ -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. * diff --git a/src/Preparer.php b/src/Preparer.php index e2fba6b..60b5fec 100644 --- a/src/Preparer.php +++ b/src/Preparer.php @@ -36,6 +36,8 @@ public function prepare(EmailComposer $composer) $this->prepareScheduled($composer); $this->prepareImmediately($composer); + + $this->prepareQueued($composer); } /** @@ -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(), + ]); + } + } + } diff --git a/src/Store.php b/src/Store.php index 656040f..a6b3eb2 100644 --- a/src/Store.php +++ b/src/Store.php @@ -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()); diff --git a/tests/QueuedEmailsTest.php b/tests/QueuedEmailsTest.php index 340fdb2..0bb7b96 100644 --- a/tests/QueuedEmailsTest.php +++ b/tests/QueuedEmailsTest.php @@ -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() { diff --git a/tests/SendEmailsCommandTest.php b/tests/SendEmailsCommandTest.php index 486f0e8..5aacca4 100644 --- a/tests/SendEmailsCommandTest.php +++ b/tests/SendEmailsCommandTest.php @@ -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 @@ -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() {