Skip to content

Commit

Permalink
Fix newsletters marked as finished
Browse files Browse the repository at this point in the history
Beam marked newsletters as finished also if they
were scheduled to be sent in the future. It was
enough that they weren't sent "now" and they were
marked as finished.

remp/respekt#289
  • Loading branch information
rootpd committed Sep 12, 2024
1 parent 4506268 commit 55cc0cb
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/Console/Commands/SendNewslettersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public function __construct(MailerContract $mailer)

public function handle()
{
$this->line('');
$this->line('<info>***** Sending newsletters *****</info>');
$this->line('');
$this->line(Carbon::now() . ': Processing newsletters');

$newsletters = Newsletter::where('state', Newsletter::STATE_STARTED)
->where('starts_at', '<=', Carbon::now())
Expand All @@ -49,6 +47,7 @@ public function handle()
}

foreach ($newsletters as $newsletter) {
$this->line(Carbon::now() . ": * {$newsletter->name}");
$nextSending = $newsletter->starts_at;
$hasMore = false;

Expand All @@ -57,26 +56,23 @@ public function handle()
}

if ($nextSending) {
if ($nextSending->gt(Carbon::now())) {
// Not sending, date is in future
continue;
}

$this->line(sprintf("Processing newsletter: %s", $newsletter->name));
$this->line(Carbon::now() . ": * sending");
$this->sendNewsletter($newsletter);
$this->line(Carbon::now() . ": * sent");
$newsletter->last_sent_at = Carbon::now();

if (!$hasMore) {
$newsletter->state = Newsletter::STATE_FINISHED;
}
} else {
} elseif (!$hasMore) {
$this->line(Carbon::now() . ": * marking as finished");
$newsletter->state = Newsletter::STATE_FINISHED;
}

$newsletter->save();
}

$this->line('<info>Done!</info>');
$this->line(Carbon::now() . ': Done!');
return 0;
}

Expand Down

0 comments on commit 55cc0cb

Please sign in to comment.