Skip to content

Commit

Permalink
Task Scheduler For Updating Delayed Projects
Browse files Browse the repository at this point in the history
  • Loading branch information
alnahian2003 committed Apr 7, 2023
1 parent 07538bb commit 62c81e2
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 2 deletions.
45 changes: 45 additions & 0 deletions app/Console/Commands/UpdateDelayedProjectsStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Console\Commands;

use App\Enums\ProjectStatus;
use App\Events\ProjectStatusUpdated;
use App\Models\Project;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class UpdateDelayedProjectsStatus extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'projects:update-delayed';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update the status of delayed projects.';

/**
* Execute the console command.
*/
public function handle()
{
$pendingDelayedProjects = Project::where([
['deadline', '<', now()],
['status', ProjectStatus::Pending],
])->get();

foreach ($pendingDelayedProjects as $project) {
$project->status = ProjectStatus::Delayed;
$project->saveOrFail();

event(new ProjectStatusUpdated($project));
Log::info('Project Status Updated', ['project' => ['project_id' => $project->id, 'status' => $project->status]]);
}
}
}
4 changes: 2 additions & 2 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
$schedule->command('projects:update-delayed')->everyTenMinutes();
}

/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this->load(__DIR__.'/Commands');
$this->load(__DIR__ . '/Commands');

require base_path('routes/console.php');
}
Expand Down
34 changes: 34 additions & 0 deletions app/Events/ProjectStatusUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Events;

use App\Models\Project;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ProjectStatusUpdated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(public Project $project)
{
//
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('project-status'),
];
}
}
28 changes: 28 additions & 0 deletions app/Listeners/NotifyAdminOnProjectStatusChange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Listeners;

use App\Events\ProjectStatusUpdated;
use App\Mail\ProjectStatusUpdatedMail;
use App\Models\User;
use Illuminate\Support\Facades\Mail;

class NotifyAdminOnProjectStatusChange
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}

/**
* Handle the event.
*/
public function handle(ProjectStatusUpdated $event): void
{
Mail::to(User::admin()->first())
->send(new ProjectStatusUpdatedMail($event->project));
}
}
44 changes: 44 additions & 0 deletions app/Mail/ProjectStatusUpdatedMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Mail;

use App\Models\Project;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class ProjectStatusUpdatedMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*/
public function __construct(public Project $project)
{
$this->project->load('client');
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: "Project Status Updated to {$this->project->status->name}",
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'mail.project.status-updated',
);
}
}
5 changes: 5 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Events\ProjectStatusUpdated;
use App\Listeners\NotifyAdminOnProjectStatusChange;
use App\Models\Project;
use App\Models\Task;
use App\Observers\ProjectObserver;
Expand All @@ -22,6 +24,9 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
ProjectStatusUpdated::class => [
NotifyAdminOnProjectStatusChange::class,
],
];

/**
Expand Down
26 changes: 26 additions & 0 deletions resources/views/mail/project/status-updated.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<x-mail::message>
# Project Status Updated to {{ $project->status->name }}

A project status has been updated since offered by {{ $project->client->name }} at {{ $project->created_at }}.

Here's an overview of current status of the project:

<x-mail::panel>
**Project Name:** "{{ str($project->title)->limit(60, '...') }}"

**Client Name:** {{ $project->client->name }}

**Budget:** ${{ $project->budget }}

**Last Updated:** {{ $project->updated_at }}

**Deadline:** {{ $project->deadline }} ({{ $project->status->name }})
</x-mail::panel>

<x-mail::button url="{{ route('projects.show', $project->slug) }}">
View Project
</x-mail::button>

Thanks,<br>
{{ config('app.name') }}
</x-mail::message>

0 comments on commit 62c81e2

Please sign in to comment.