Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
wip
  • Loading branch information
djaiss committed Mar 11, 2022
commit 69ad7c17b97a36cdf33af3532d03ba5fd3c1d1c8
46 changes: 46 additions & 0 deletions app/Jobs/Notifications/SendEmailNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Jobs\Notifications;

use App\Models\ScheduledContactReminder;
use App\Models\UserNotificationChannel;
use App\Models\UserNotificationSent;
use App\Notifications\SendEmailReminder;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Notification;

class SendEmailNotification implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

private ScheduledContactReminder $scheduledReminder;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct(ScheduledContactReminder $scheduledReminder)
{
$this->scheduledReminder = $scheduledReminder;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$emailAddress = $this->scheduledReminder->userNotificationChannel->content;

Notification::route('mail', $emailAddress)
->notify(new SendEmailReminder($this->scheduledReminder));
}
}
44 changes: 44 additions & 0 deletions app/Jobs/ProcessScheduledContactReminders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Jobs;

use App\Jobs\Notifications\SendEmailNotification;
use App\Models\ScheduledContactReminder;
use App\Models\UserNotificationChannel;
use App\Models\UserNotificationSent;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessScheduledContactReminders implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* 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)
->with('userNotificationChannel')
->get();

foreach ($scheduledReminders as $scheduledReminder) {
if ($scheduledReminder->userNotificationChannel->type == UserNotificationChannel::TYPE_EMAIL) {
SendEmailNotification::dispatch($scheduledReminder)->onQueue('low');
}
}
}
}
12 changes: 2 additions & 10 deletions app/Models/ScheduledContactReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class ScheduledContactReminder extends Model
protected $fillable = [
'contact_reminder_id',
'user_notification_channel_id',
'scheduled_at',
'triggered_at',
'triggered',
];

/**
Expand All @@ -30,18 +30,10 @@ class ScheduledContactReminder extends Model
* @var array
*/
protected $dates = [
'scheduled_at',
'triggered_at',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'triggered' => 'boolean',
];

/**
* Get the contact reminder associated with the scheduled contact reminder.
*
Expand Down
64 changes: 64 additions & 0 deletions app/Notifications/SendEmailReminder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Notifications;

use App\Helpers\NameHelper;
use App\Models\ScheduledContactReminder;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class SendEmailReminder extends Notification
{
use Queueable;

private ScheduledContactReminder $scheduledReminder;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(ScheduledContactReminder $scheduledReminder)
{
$this->scheduledReminder = $scheduledReminder;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$reminder = $this->scheduledReminder->reminder;
$contact = $reminder->contact;
$user = $this->scheduledReminder->userNotificationChannel->user;
$name = NameHelper::formatContactName($user, $contact);
$url = route('contact.show', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]);

return (new MailMessage)
->subject('Reminder about ' . $name)
->greeting('Hi!')
->line('Reminder about '.$name.'.')
->line($reminder->label)
->action('View contact', $url)
->line('Thank you for using Monica.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private function schedule(): void
'contact_reminder_id' => $this->contactReminder->id,
'user_notification_channel_id' => $channel->id,
'user_id' => $user->id,
'triggered_at' => $this->upcomingDate->tz('UTC'),
'scheduled_at' => $this->upcomingDate->tz('UTC'),
]);
}
}
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"http-interop/http-factory-guzzle": "^1.2",
"inertiajs/inertia-laravel": "^0.5.4",
"itsgoingd/clockwork": "^5.1",
"laravel-notification-channels/telegram": "^2.0",
"laravel/framework": "^9.0",
"laravel/sanctum": "^2.6",
"laravel/scout": "^9.4",
Expand Down
Loading