-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reminder backend (monicahq/chandler#48)
- Loading branch information
Showing
38 changed files
with
1,509 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\App; | ||
use App\Models\UserNotificationChannel; | ||
use App\Models\ScheduledContactReminder; | ||
use App\Jobs\Notifications\SendEmailNotification; | ||
use App\Services\Contact\ManageReminder\RescheduleContactReminder; | ||
|
||
class TestReminders extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'test:send-reminders'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Send all the scheduled contact reminders regardless of the time they should be send.'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
if (App::environment('production')) { | ||
exit; | ||
} | ||
|
||
$scheduledReminders = ScheduledContactReminder::where('triggered_at', null) | ||
->with('userNotificationChannel') | ||
->get(); | ||
|
||
foreach ($scheduledReminders as $scheduledReminder) { | ||
if ($scheduledReminder->userNotificationChannel->type == UserNotificationChannel::TYPE_EMAIL) { | ||
SendEmailNotification::dispatch($scheduledReminder)->onQueue('low'); | ||
} | ||
|
||
(new RescheduleContactReminder)->execute([ | ||
'scheduled_contact_reminder_id' => $scheduledReminder->id, | ||
]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace App\Jobs\Notifications; | ||
|
||
use Carbon\Carbon; | ||
use App\Mail\SendReminder; | ||
use Illuminate\Bus\Queueable; | ||
use App\Models\UserNotificationSent; | ||
use Illuminate\Support\Facades\Mail; | ||
use Illuminate\Queue\SerializesModels; | ||
use App\Models\ScheduledContactReminder; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
|
||
class SendEmailNotification implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
public ScheduledContactReminder $scheduledReminder; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param ScheduledContactReminder $scheduledReminder | ||
* @return void | ||
*/ | ||
public function __construct(ScheduledContactReminder $scheduledReminder) | ||
{ | ||
$this->scheduledReminder = $scheduledReminder; | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
$emailAddress = $this->scheduledReminder->userNotificationChannel->content; | ||
$user = $this->scheduledReminder->userNotificationChannel->user; | ||
|
||
Mail::to($emailAddress) | ||
->queue((new SendReminder($this->scheduledReminder, $user)) | ||
->onQueue('low') | ||
); | ||
|
||
$this->scheduledReminder->triggered_at = Carbon::now(); | ||
$this->scheduledReminder->save(); | ||
|
||
UserNotificationSent::create([ | ||
'user_notification_channel_id' => $this->scheduledReminder->userNotificationChannel->id, | ||
'sent_at' => Carbon::now(), | ||
'subject_line' => $this->scheduledReminder->reminder->label, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Queue\SerializesModels; | ||
use App\Models\UserNotificationChannel; | ||
use App\Models\ScheduledContactReminder; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use App\Jobs\Notifications\SendEmailNotification; | ||
use App\Services\Contact\ManageReminder\RescheduleContactReminder; | ||
|
||
class ProcessScheduledContactReminders implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
// this cron job runs every five minutes, so we must make sure that | ||
// the date we run this cron against, has no seconds, otherwise getting | ||
// the scheduled reminders will not return any results | ||
$currentDate = Carbon::now(); | ||
$currentDate->second = 0; | ||
|
||
$scheduledReminders = ScheduledContactReminder::where('scheduled_at', '<=', $currentDate) | ||
->where('triggered_at', null) | ||
->with('userNotificationChannel') | ||
->get(); | ||
|
||
foreach ($scheduledReminders as $scheduledReminder) { | ||
if ($scheduledReminder->userNotificationChannel->type == UserNotificationChannel::TYPE_EMAIL) { | ||
SendEmailNotification::dispatch($scheduledReminder)->onQueue('low'); | ||
} | ||
|
||
(new RescheduleContactReminder)->execute([ | ||
'scheduled_contact_reminder_id' => $scheduledReminder->id, | ||
]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace App\Mail; | ||
|
||
use App\Models\User; | ||
use App\Helpers\NameHelper; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Queue\SerializesModels; | ||
use App\Models\ScheduledContactReminder; | ||
|
||
class SendReminder extends Mailable | ||
{ | ||
use Queueable, SerializesModels; | ||
|
||
public ScheduledContactReminder $scheduledContactReminder; | ||
public User $user; | ||
|
||
/** | ||
* Create a new message instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(ScheduledContactReminder $contactReminder, User $user) | ||
{ | ||
$this->scheduledContactReminder = $contactReminder; | ||
$this->user = $user; | ||
} | ||
|
||
/** | ||
* Build the message. | ||
* | ||
* @return $this | ||
*/ | ||
public function build() | ||
{ | ||
$contact = $this->scheduledContactReminder->reminder->contact; | ||
$contactName = NameHelper::formatContactName($this->user, $contact); | ||
|
||
$reason = $this->scheduledContactReminder->reminder->label; | ||
|
||
return $this->subject(trans('email.notification_reminder_email', ['name' => $contactName])) | ||
->markdown('emails.notifications.reminder', [ | ||
'name' => $this->user->name, | ||
'reason' => $reason, | ||
'contactName' => $contactName, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.