Skip to content

Commit

Permalink
Close #44. Initializes scheduled jobs for notifications and fixes som…
Browse files Browse the repository at this point in the history
…e issues with them. Also handles keeping track of notifications so they don't get sent out again.
  • Loading branch information
cdterry87 committed Jul 7, 2024
1 parent 8ddb587 commit 2e0de84
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 12 deletions.
8 changes: 4 additions & 4 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->job(new \App\Jobs\IssuesNotificationsJob())->dailyAt('08:00');
// $schedule->job(new \App\Jobs\ProjectsNotificationsJob())->dailyAt('08:00');
// $schedule->job(new \App\Jobs\ProjectsTasksNotificationsJob())->dailyAt('08:00');

$schedule->job(new \App\Jobs\RoutineCleanupJob())->dailyAt('00:00');

$schedule->job(new \App\Jobs\IssuesNotificationsJob())->dailyAt('02:00');
$schedule->job(new \App\Jobs\ProjectsNotificationsJob())->dailyAt('04:00');
$schedule->job(new \App\Jobs\ProjectsTasksNotificationsJob())->dailyAt('06:00');
}

/**
Expand Down
47 changes: 44 additions & 3 deletions app/Jobs/IssuesNotificationsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Jobs;

use App\Models\Issue;
use App\Models\IssueNotificationSent;
use Illuminate\Bus\Queueable;
use App\Models\UserNotification;
use Illuminate\Queue\SerializesModels;
Expand All @@ -27,17 +28,18 @@ public function __construct()
*/
public function handle(): void
{
$this->handleCompletedIssueNotifications();
$this->handleResolvedIssueNotifications();
$this->handleCriticalPriorityIssueNotifications();
$this->handleHighPriorityIssueNotifications();
$this->handleMediumPriorityIssueNotifications();
$this->handleLowPriorityIssueNotifications();
}

protected function handleCompletedIssueNotifications()
protected function handleResolvedIssueNotifications()
{
$issues = Issue::query()
->whereDate('resolved_date', now()->toDateString())
->where('resolved_date', '<=', now()->toDateString())
->whereDoesntHave('resolvedNotificationSent')
->get();

foreach ($issues as $issue) {
Expand All @@ -46,6 +48,13 @@ protected function handleCompletedIssueNotifications()
'subject' => 'Issue Resolved',
'message' => 'Your issue has been resolved: ' . $issue->name,
]);

IssueNotificationSent::create([
'user_id' => $issue->user_id,
'issue_id' => $issue->id,
'priority' => $issue->priority,
'resolved' => $issue->resolved_date ? true : false,
]);
}
}

Expand All @@ -55,6 +64,7 @@ protected function handleCriticalPriorityIssueNotifications()
->where('priority', 4)
->whereNull('resolved_date')
->where('created_at', '<', now()->subDays(3))
->whereDoesntHave('criticalPriorityNotificationSent')
->get();

foreach ($issues as $issue) {
Expand All @@ -63,6 +73,13 @@ protected function handleCriticalPriorityIssueNotifications()
'subject' => 'Critical Issue',
'message' => 'You have an unresolved critical priority issue that has been open for 3 days: ' . $issue->name,
]);

IssueNotificationSent::create([
'user_id' => $issue->user_id,
'issue_id' => $issue->id,
'priority' => $issue->priority,
'resolved' => $issue->resolved_date ? true : false,
]);
}
}

Expand All @@ -72,6 +89,7 @@ protected function handleHighPriorityIssueNotifications()
->where('priority', 3)
->whereNull('resolved_date')
->where('created_at', '<', now()->subDays(7))
->whereDoesntHave('highPriorityNotificationSent')
->get();

foreach ($issues as $issue) {
Expand All @@ -80,6 +98,13 @@ protected function handleHighPriorityIssueNotifications()
'subject' => 'High Issue',
'message' => 'You have an unresolved high priority issue that has been open for 7 days: ' . $issue->name,
]);

IssueNotificationSent::create([
'user_id' => $issue->user_id,
'issue_id' => $issue->id,
'priority' => $issue->priority,
'resolved' => $issue->resolved_date ? true : false,
]);
}
}

Expand All @@ -89,6 +114,7 @@ protected function handleMediumPriorityIssueNotifications()
->where('priority', 2)
->whereNull('resolved_date')
->where('created_at', '<', now()->subDays(14))
->whereDoesntHave('mediumPriorityNotificationSent')
->get();

foreach ($issues as $issue) {
Expand All @@ -97,6 +123,13 @@ protected function handleMediumPriorityIssueNotifications()
'subject' => 'Medium Issue',
'message' => 'You have an unresolved medium priority issue that has been open for 14 days: ' . $issue->name,
]);

IssueNotificationSent::create([
'user_id' => $issue->user_id,
'issue_id' => $issue->id,
'priority' => $issue->priority,
'resolved' => $issue->resolved_date ? true : false,
]);
}
}

Expand All @@ -106,6 +139,7 @@ protected function handleLowPriorityIssueNotifications()
->where('priority', 1)
->whereNull('resolved_date')
->where('created_at', '<', now()->subDays(30))
->whereDoesntHave('lowPriorityNotificationSent')
->get();

foreach ($issues as $issue) {
Expand All @@ -114,6 +148,13 @@ protected function handleLowPriorityIssueNotifications()
'subject' => 'Low Priority Issue',
'message' => 'You have an unresolved low priority issue that has been open for 30 days: ' . $issue->name,
]);

IssueNotificationSent::create([
'user_id' => $issue->user_id,
'issue_id' => $issue->id,
'priority' => $issue->priority,
'resolved' => $issue->resolved_date ? true : false,
]);
}
}
}
32 changes: 30 additions & 2 deletions app/Jobs/ProjectsNotificationsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Jobs;

use App\Models\Project;
use App\Models\ProjectNotificationSent;
use Illuminate\Bus\Queueable;
use App\Models\UserNotification;
use Illuminate\Queue\SerializesModels;
Expand Down Expand Up @@ -35,8 +36,9 @@ public function handle(): void
protected function handleProjectDueReminderNotifications()
{
$projects = Project::query()
->whereDate('due_date', now()->addDays(5)->toDateString())
->whereDate('due_date', now()->addDays(10)->toDateString())
->whereNull('completed_date')
->whereDoesntHave('dueNotificationSent')
->get();

foreach ($projects as $project) {
Expand All @@ -45,6 +47,14 @@ protected function handleProjectDueReminderNotifications()
'subject' => 'Project Due Reminder',
'message' => 'Your project is due in 5 days: ' . $project->name,
]);

ProjectNotificationSent::create([
'user_id' => $project->user_id,
'project_id' => $project->id,
'due' => true,
'overdue' => false,
'completed' => false,
]);
}
}

Expand All @@ -53,6 +63,7 @@ protected function handleProjectOverdueNotifications()
$projects = Project::query()
->whereDate('due_date', '<', now()->toDateString())
->whereNull('completed_date')
->whereDoesntHave('overdueNotificationSent')
->get();

foreach ($projects as $project) {
Expand All @@ -61,13 +72,22 @@ protected function handleProjectOverdueNotifications()
'subject' => 'Project Overdue',
'message' => 'Your project is overdue: ' . $project->name,
]);

ProjectNotificationSent::create([
'user_id' => $project->user_id,
'project_id' => $project->id,
'due' => false,
'overdue' => true,
'completed' => false,
]);
}
}

protected function handleCompletedProjectNotifications()
{
$projects = Project::query()
->whereDate('completed_date', now()->toDateString())
->where('completed_date', '<=', now()->toDateString())
->whereDoesntHave('completeNotificationSent')
->get();

foreach ($projects as $project) {
Expand All @@ -76,6 +96,14 @@ protected function handleCompletedProjectNotifications()
'subject' => 'Project Completed',
'message' => 'Your project has been completed: ' . $project->name,
]);

ProjectNotificationSent::create([
'user_id' => $project->user_id,
'project_id' => $project->id,
'due' => false,
'overdue' => false,
'completed' => true,
]);
}
}
}
50 changes: 47 additions & 3 deletions app/Jobs/ProjectsTasksNotificationsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\UserNotification;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use App\Models\ProjectTaskNotificationSent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

Expand All @@ -29,21 +30,31 @@ public function handle(): void
{
$this->handleProjectTaskDueReminderNotifications();
$this->handleProjectTaskOverdueNotifications();
$this->handleProjectTaskCompletedNotifications();
}

protected function handleProjectTaskDueReminderNotifications()
{
$tasks = ProjectTask::query()
->whereDate('due_date', now()->addDays(5)->toDateString())
->whereDate('due_date', now()->addDays(10)->toDateString())
->whereNull('completed_date')
->whereDoesntHave('dueNotificationSent')
->get();

foreach ($tasks as $task) {
UserNotification::create([
'user_id' => $task->user_id,
'user_id' => $task->project->user_id,
'subject' => 'Project Task Due Reminder',
'message' => 'Your project\'s task is due in 5 days: ' . $task->name . ' in project ' . $task->project->name,
]);

ProjectTaskNotificationSent::create([
'user_id' => $task->project->user_id,
'project_task_id' => $task->id,
'due' => true,
'overdue' => false,
'completed' => false,
]);
}
}

Expand All @@ -52,14 +63,47 @@ protected function handleProjectTaskOverdueNotifications()
$tasks = ProjectTask::query()
->whereDate('due_date', '<', now()->toDateString())
->whereNull('completed_date')
->whereDoesntHave('overdueNotificationSent')
->get();

foreach ($tasks as $task) {
UserNotification::create([
'user_id' => $task->user_id,
'user_id' => $task->project->user_id,
'subject' => 'Project Task Overdue',
'message' => 'Your project\'s task is overdue: ' . $task->name . ' in project ' . $task->project->name,
]);

ProjectTaskNotificationSent::create([
'user_id' => $task->project->user_id,
'project_task_id' => $task->id,
'due' => false,
'overdue' => true,
'completed' => false,
]);
}
}

protected function handleProjectTaskCompletedNotifications()
{
$tasks = ProjectTask::query()
->where('completed_date', '<=', now()->toDateString())
->whereDoesntHave('completeNotificationSent')
->get();

foreach ($tasks as $task) {
UserNotification::create([
'user_id' => $task->project->user_id,
'subject' => 'Project Task Completed',
'message' => 'Your project\'s task is completed: ' . $task->name . ' in project ' . $task->project->name,
]);

ProjectTaskNotificationSent::create([
'user_id' => $task->project->user_id,
'project_task_id' => $task->id,
'due' => false,
'overdue' => false,
'completed' => true,
]);
}
}
}
34 changes: 34 additions & 0 deletions app/Models/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,38 @@ public static function getResolvedCodes()
['value' => 1, 'label' => 'Closed']
];
}

public function resolvedNotificationSent()
{
return $this->hasOne(IssueNotificationSent::class)
->where('resolved', true);
}

public function lowPriorityNotificationSent()
{
return $this->hasOne(IssueNotificationSent::class)
->where('resolved', false)
->where('priority', 1);
}

public function mediumPriorityNotificationSent()
{
return $this->hasOne(IssueNotificationSent::class)
->where('resolved', false)
->where('priority', 2);
}

public function highPriorityNotificationSent()
{
return $this->hasOne(IssueNotificationSent::class)
->where('resolved', false)
->where('priority', 3);
}

public function criticalPriorityNotificationSent()
{
return $this->hasOne(IssueNotificationSent::class)
->where('resolved', false)
->where('priority', 4);
}
}
14 changes: 14 additions & 0 deletions app/Models/IssueNotificationSent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class IssueNotificationSent extends Model
{
use HasFactory;

protected $guarded = [];
protected $table = 'issues_notifications_sent';
}
20 changes: 20 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,24 @@ public function uploads()
{
return $this->hasMany(ProjectUpload::class);
}

public function dueNotificationSent()
{
return $this->hasOne(ProjectNotificationSent::class)
->where('due', true)
->where('completed', false);
}

public function overdueNotificationSent()
{
return $this->hasOne(ProjectNotificationSent::class)
->where('overdue', true)
->where('completed', false);
}

public function completeNotificationSent()
{
return $this->hasOne(ProjectNotificationSent::class)
->where('completed', true);
}
}
Loading

0 comments on commit 2e0de84

Please sign in to comment.