master task edited email notification

This commit is contained in:
StudioTem committed 2023-04-12 08:50:18 +02:00
1 parent ba5a6a2bd8
commit 38eab4f9b7
6 files changed
+190

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
<?php
namespace App\Events;
use App\Models\MasterTask;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class MasterTaskEdited
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $mastertask;
public $originalMastertask;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(MasterTask $mastertask, array $originalMastertask)
{
$this->mastertask = $mastertask;
$this->originalMastertask = $originalMastertask;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
@@ -0,0 +1,69 @@
<?php
namespace App\Listeners;
use App\Events\MasterTaskEdited;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Mail;
class SendEditedMasterTaskNotification
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(MasterTaskEdited $event)
{
$difference = [];
// get the difference between $mastertask and $originalMastertask
$modelDiff = array_diff($event->mastertask->toArray(), $event->originalMastertask);
foreach ( $modelDiff as $key => $diff ) {
if (in_array($key, ['created_at', 'updated_at', 'status'])) {
continue;
}
// znovuotvorenie tasku
if ($key == 'is_finished' && $diff == 0) {
foreach ( $event->mastertask->subtasks as $subtask ) {
// Mail::to( $subtask->user->email )->send( new \App\Mail\MasterTaskEdited( $event->mastertask, $difference ) );
Mail::to( 'feher@studiotem.com' )->send( new \App\Mail\MasterTaskEdited( $event->mastertask, $difference ) );
}
}
// ukoncenie master tasku - neposielame notifikaciu
if ($key == 'is_finished' && $diff == 1) {
return;
}
$difference[$key] = $diff;
}
if (empty($difference)) {
return;
}
// send notification to all users
foreach ( $event->mastertask->subtasks as $subtask ) {
// Mail::to( $subtask->user->email )->send( new \App\Mail\MasterTaskEdited( $event->mastertask, $difference ) );
Mail::to( 'feher@studiotem.com' )->send( new \App\Mail\MasterTaskEdited( $event->mastertask, $difference ) );
}
}
}
+64
View File
@@ -0,0 +1,64 @@
<?php
namespace App\Mail;
use App\Models\MasterTask;
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 MasterTaskEdited extends Mailable
{
use Queueable, SerializesModels;
public $mastertask;
public $difference;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(MasterTask $mastertask, $difference)
{
$this->mastertask = $mastertask;
$this->difference = $difference;
}
/**
* Get the message envelope.
*
* @return \Illuminate\Mail\Mailables\Envelope
*/
public function envelope()
{
return new Envelope(
subject: 'Master Task Edited',
);
}
/**
* Get the message content definition.
*
* @return \Illuminate\Mail\Mailables\Content
*/
public function content()
{
return new Content(
view: 'emails.task.master_task_edited',
);
}
/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments()
{
return [];
}
}
+4
View File
@@ -2,6 +2,7 @@
namespace App\Observers;
use App\Events\MasterTaskEdited;
use App\Models\MasterTask;
class MasterTaskObserver
@@ -15,5 +16,8 @@ class MasterTaskObserver
{
// to prevent updating created_by field. this field cannot be updated once created.
$mastertask->created_by = $mastertask->getOriginal('created_by');
MasterTaskEdited::dispatch($mastertask, $mastertask->getOriginal());
}
}
@@ -2,7 +2,9 @@
namespace App\Providers;
use App\Events\MasterTaskEdited;
use App\Events\SubTaskCreated;
use App\Listeners\SendEditedMasterTaskNotification;
use App\Listeners\SendNewTaskEmailNotification;
use App\Models\MasterTask;
use App\Models\SubTask;
@@ -28,6 +30,9 @@ class EventServiceProvider extends ServiceProvider
SendNewTaskEmailNotification::class,
//LogEvent::class,
],
MasterTaskEdited::class => [
SendEditedMasterTaskNotification::class
],
];
/**
@@ -0,0 +1,6 @@
Master Task edited. <br><br>
Edited fields: <br> <br>
@foreach($difference as $key => $diff)
{{ $key }}: {{ $diff }} <br>
@endforeach