Skip to content

Commit

Permalink
feat: refactor reminders (monicahq/chandler#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Mar 18, 2022
1 parent 896da02 commit abf8757
Show file tree
Hide file tree
Showing 39 changed files with 727 additions and 558 deletions.
25 changes: 16 additions & 9 deletions app/Console/Commands/TestReminders.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\App;
use App\Models\UserNotificationChannel;
use App\Models\ScheduledContactReminder;
use App\Jobs\Notifications\SendEmailNotification;
use App\Services\Contact\ManageReminder\RescheduleContactReminder;
use App\Services\Contact\ManageReminder\RescheduleContactReminderForChannel;

class TestReminders extends Command
{
Expand Down Expand Up @@ -36,17 +36,24 @@ public function handle()
exit;
}

$scheduledReminders = ScheduledContactReminder::where('triggered_at', null)
->with('userNotificationChannel')
$scheduledContactReminders = DB::table('contact_reminder_scheduled')
->where('triggered_at', null)
->get();

foreach ($scheduledReminders as $scheduledReminder) {
if ($scheduledReminder->userNotificationChannel->type == UserNotificationChannel::TYPE_EMAIL) {
SendEmailNotification::dispatch($scheduledReminder)->onQueue('low');
foreach ($scheduledContactReminders as $scheduledReminder) {
$channel = UserNotificationChannel::findOrFail($scheduledReminder->user_notification_channel_id);

if ($channel->type == UserNotificationChannel::TYPE_EMAIL) {
SendEmailNotification::dispatch(
$scheduledReminder->user_notification_channel_id,
$scheduledReminder->contact_reminder_id
)->onQueue('low');
}

(new RescheduleContactReminder)->execute([
'scheduled_contact_reminder_id' => $scheduledReminder->id,
(new RescheduleContactReminderForChannel)->execute([
'contact_reminder_id' => $scheduledReminder->contact_reminder_id,
'user_notification_channel_id' => $scheduledReminder->user_notification_channel_id,
'contact_reminder_scheduled_id' => $scheduledReminder->id,
]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use App\Http\Controllers\Controller;
use App\Models\ContactImportantDate;
use Illuminate\Support\Facades\Auth;
use App\Services\Contact\ManageReminder\CreateReminder;
use App\Services\Contact\ManageReminder\CreateContactReminder;
use App\Http\Controllers\Vault\ViewHelpers\VaultIndexViewHelper;
use App\Services\Contact\ManageContactImportantDate\CreateContactImportantDate;
use App\Services\Contact\ManageContactImportantDate\UpdateContactImportantDate;
Expand Down Expand Up @@ -63,7 +63,7 @@ public function store(Request $request, int $vaultId, int $contactId)
]);

if ($request->input('reminder')) {
(new CreateReminder)->execute([
(new CreateContactReminder)->execute([
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'vault_id' => $vaultId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use App\Models\ContactReminder;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\Contact\ManageReminder\CreateReminder;
use App\Services\Contact\ManageReminder\UpdateReminder;
use App\Services\Contact\ManageReminder\DestroyReminder;
use App\Services\Contact\ManageReminder\CreateContactReminder;
use App\Http\Controllers\Vault\Contact\Modules\Reminder\ViewHelpers\ModuleRemindersViewHelper;

class ContactModuleReminderController extends Controller
Expand Down Expand Up @@ -49,7 +49,7 @@ public function store(Request $request, int $vaultId, int $contactId)
'frequency_number' => $frequencyNumber,
];

$reminder = (new CreateReminder)->execute($data);
$reminder = (new CreateContactReminder)->execute($data);
$contact = Contact::find($contactId);

return response()->json([
Expand Down
27 changes: 14 additions & 13 deletions app/Jobs/Notifications/SendEmailNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use Carbon\Carbon;
use App\Mail\SendReminder;
use Illuminate\Bus\Queueable;
use App\Models\ContactReminder;
use App\Models\UserNotificationSent;
use Illuminate\Support\Facades\Mail;
use Illuminate\Queue\SerializesModels;
use App\Models\ScheduledContactReminder;
use App\Models\UserNotificationChannel;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -17,17 +18,20 @@ class SendEmailNotification implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public ScheduledContactReminder $scheduledReminder;
public UserNotificationChannel $userNotificationChannel;
public ContactReminder $contactReminder;

/**
* Create a new job instance.
*
* @param ScheduledContactReminder $scheduledReminder
* @param int $userNotificationChannelId
* @param int $contactReminderId
* @return void
*/
public function __construct(ScheduledContactReminder $scheduledReminder)
public function __construct(int $userNotificationChannelId, int $contactReminderId)
{
$this->scheduledReminder = $scheduledReminder;
$this->userNotificationChannel = UserNotificationChannel::findOrFail($userNotificationChannelId);
$this->contactReminder = ContactReminder::findOrFail($contactReminderId);
}

/**
Expand All @@ -37,21 +41,18 @@ public function __construct(ScheduledContactReminder $scheduledReminder)
*/
public function handle()
{
$emailAddress = $this->scheduledReminder->userNotificationChannel->content;
$user = $this->scheduledReminder->userNotificationChannel->user;
$emailAddress = $this->userNotificationChannel->content;
$user = $this->userNotificationChannel->user;

Mail::to($emailAddress)
->queue((new SendReminder($this->scheduledReminder, $user))
->queue((new SendReminder($this->contactReminder, $user))
->onQueue('low')
);

$this->scheduledReminder->triggered_at = Carbon::now();
$this->scheduledReminder->save();

UserNotificationSent::create([
'user_notification_channel_id' => $this->scheduledReminder->userNotificationChannel->id,
'user_notification_channel_id' => $this->userNotificationChannel->id,
'sent_at' => Carbon::now(),
'subject_line' => $this->scheduledReminder->reminder->label,
'subject_line' => $this->contactReminder->label,
]);
}
}
44 changes: 35 additions & 9 deletions app/Jobs/ProcessScheduledContactReminders.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\DB;
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;
use App\Services\Contact\ManageReminder\RescheduleContactReminderForChannel;

class ProcessScheduledContactReminders implements ShouldQueue
{
Expand Down Expand Up @@ -39,19 +39,45 @@ public function handle()
$currentDate = Carbon::now();
$currentDate->second = 0;

$scheduledReminders = ScheduledContactReminder::where('scheduled_at', '<=', $currentDate)
$scheduledContactReminders = DB::table('contact_reminder_scheduled')
->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');
foreach ($scheduledContactReminders as $scheduledReminder) {
$channel = UserNotificationChannel::findOrFail($scheduledReminder->user_notification_channel_id);

if ($channel->type == UserNotificationChannel::TYPE_EMAIL) {
SendEmailNotification::dispatch(
$scheduledReminder->user_notification_channel_id,
$scheduledReminder->contact_reminder_id
)->onQueue('low');
}

(new RescheduleContactReminder)->execute([
'scheduled_contact_reminder_id' => $scheduledReminder->id,
$this->updateScheduledContactReminderTriggeredAt($scheduledReminder->id);
$this->updateNumberOfTimesTriggered($scheduledReminder->contact_reminder_id);

(new RescheduleContactReminderForChannel)->execute([
'contact_reminder_id' => $scheduledReminder->contact_reminder_id,
'user_notification_channel_id' => $scheduledReminder->user_notification_channel_id,
'contact_reminder_scheduled_id' => $scheduledReminder->id,
]);
}
}

private function updateScheduledContactReminderTriggeredAt(int $id): void
{
DB::table('contact_reminder_scheduled')
->where('id', $id)
->update([
'triggered_at' => Carbon::now(),
]);
}

private function updateNumberOfTimesTriggered(int $id): void
{
DB::table('contact_reminders')
->where('id', $id)
->increment('number_times_triggered');
}
}
4 changes: 4 additions & 0 deletions app/Jobs/SendVerificationEmailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
use Illuminate\Foundation\Bus\Dispatchable;
use App\Mail\UserNotificationChannelEmailCreated;

/**
* Send a verification email when a User Notification Channel of the Email type
* is created, so that the user can verify the email address.
*/
class SendVerificationEmailChannel implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
Expand Down
12 changes: 6 additions & 6 deletions app/Mail/SendReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
use App\Helpers\NameHelper;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use App\Models\ContactReminder;
use Illuminate\Queue\SerializesModels;
use App\Models\ScheduledContactReminder;

class SendReminder extends Mailable
{
use Queueable, SerializesModels;

public ScheduledContactReminder $scheduledContactReminder;
public ContactReminder $contactReminder;
public User $user;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct(ScheduledContactReminder $contactReminder, User $user)
public function __construct(ContactReminder $contactReminder, User $user)
{
$this->scheduledContactReminder = $contactReminder;
$this->contactReminder = $contactReminder;
$this->user = $user;
}

Expand All @@ -34,10 +34,10 @@ public function __construct(ScheduledContactReminder $contactReminder, User $use
*/
public function build()
{
$contact = $this->scheduledContactReminder->reminder->contact;
$contact = $this->contactReminder->contact;
$contactName = NameHelper::formatContactName($this->user, $contact);

$reason = $this->scheduledContactReminder->reminder->label;
$reason = $this->contactReminder->label;

return $this->subject(trans('email.notification_reminder_email', ['name' => $contactName]))
->markdown('emails.notifications.reminder', [
Expand Down
12 changes: 7 additions & 5 deletions app/Models/ContactReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class ContactReminder extends Model
{
Expand Down Expand Up @@ -34,6 +34,8 @@ class ContactReminder extends Model
'year',
'type',
'frequency_number',
'last_triggered_at',
'number_times_triggered',
];

/**
Expand All @@ -47,12 +49,12 @@ public function contact()
}

/**
* Get the scheduled reminders associated with the contact reminder.
* Get the user notification channel records associated with the contact reminder.
*
* @return HasMany
* @return BelongsToMany
*/
public function scheduledContactReminders()
public function userNotificationChannels()
{
return $this->hasMany(ScheduledContactReminder::class);
return $this->belongsToMany(UserNotificationChannel::class, 'contact_reminder_scheduled')->withTimestamps()->withPivot('scheduled_at', 'triggered');
}
}
56 changes: 0 additions & 56 deletions app/Models/ScheduledContactReminder.php

This file was deleted.

10 changes: 10 additions & 0 deletions app/Models/UserNotificationChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,14 @@ public function userNotificationSent()
{
return $this->hasMany(UserNotificationSent::class);
}

/**
* Get the contact reminder records associated with the user.
*
* @return BelongsToMany
*/
public function contactReminders()
{
return $this->belongsToMany(ContactReminder::class, 'contact_reminder_scheduled')->withTimestamps()->withPivot('scheduled_at', 'triggered');
}
}
1 change: 1 addition & 0 deletions app/Models/UserNotificationSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class UserNotificationSent extends Model
'user_notification_channel_id',
'sent_at',
'subject_line',
'payload',
];

/**
Expand Down
Loading

0 comments on commit abf8757

Please sign in to comment.