create notification, broadcast notification and mail when a futur or ongoing date of a task is updated
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\Event;
|
||||
use App\Models\Task;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class TaskDateUpdated implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public $task;
|
||||
protected $userId;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*/
|
||||
public function __construct($userId, Task $task)
|
||||
{
|
||||
$this->userId = $userId;
|
||||
$this->task = $task;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should broadcast on.
|
||||
*
|
||||
* @return array<int, \Illuminate\Broadcasting\Channel>
|
||||
*/
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [
|
||||
new PrivateChannel('user.' . $this->userId),
|
||||
];
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'task.date.updated';
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\TaskDateUpdated;
|
||||
use App\Events\TaskParticipationCancelled;
|
||||
use App\Events\VolunteerAssignedToTask;
|
||||
use App\Events\VolunteerUnassignedFromTask;
|
||||
use App\Mail\TaskDateUpdateMail;
|
||||
use App\Models\Event;
|
||||
use App\Models\Notification;
|
||||
use App\Mail\VolunteerAssignToTaskMail;
|
||||
@@ -18,6 +20,7 @@ use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use App\Mail\TaskParticipationCancelledMail;
|
||||
use App\Services\FormatDate;
|
||||
|
||||
class TasksController extends Controller
|
||||
{
|
||||
@@ -80,7 +83,7 @@ class TasksController extends Controller
|
||||
|
||||
try {
|
||||
$task = Task::find($request["task_id"]);
|
||||
$event = \App\Models\Event::find($task->events_id);
|
||||
$event = Event::find($task->events_id);
|
||||
|
||||
if ($task->users()->where('user_id', $id)->exists()) {
|
||||
return response()->json(["message" => "User already assigned to this task"], 400);
|
||||
@@ -112,6 +115,7 @@ class TasksController extends Controller
|
||||
'taskName' => $task->name,
|
||||
'start' => $task->start,
|
||||
'end' => $task->end,
|
||||
'eventName' => $event->name,
|
||||
];
|
||||
Mail::to($user->email)->send(new VolunteerAssignToTaskMail($data));
|
||||
}
|
||||
@@ -146,7 +150,7 @@ class TasksController extends Controller
|
||||
|
||||
try {
|
||||
$task = Task::with(['users'])->find($id);
|
||||
$event = \App\Models\Event::find($task->events_id);
|
||||
$event = Event::find($task->events_id);
|
||||
|
||||
if (!$task) {
|
||||
return response()->json(["message" => "Task not found"], 404);
|
||||
@@ -175,6 +179,7 @@ class TasksController extends Controller
|
||||
'taskName' => $task->name,
|
||||
'start' => $event->start,
|
||||
'end' => $task->end,
|
||||
'eventName' => $event->name,
|
||||
];
|
||||
Mail::to($user->email)->send(new TaskParticipationCancelledMail($data));
|
||||
}
|
||||
@@ -197,8 +202,41 @@ class TasksController extends Controller
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
$task = Task::find($id);
|
||||
if((
|
||||
$task->start !== $request["start"] || $task->end !== $request["end"]
|
||||
) &&
|
||||
Carbon::now()->lessThanOrEqualTo($task->end)
|
||||
){
|
||||
$participants = $task->users;
|
||||
$formatedTaskStart = FormatDate::formatDate($task->start);
|
||||
$formatedTaskEnd = FormatDate::formatDate($task->end);
|
||||
$event = Event::find($task->events_id);
|
||||
|
||||
foreach ($participants as $user) {
|
||||
$notification = Notification::create([
|
||||
'content' => "La date de la tâche {$task->title} de l'événement {$task->event->name} à été mis à jour. Cette tâche aura maintenant lieu du {$formatedTaskStart} au {$formatedTaskEnd}."
|
||||
]);
|
||||
$notification->users()->attach($user->id);
|
||||
broadcast(new TaskDateUpdated(
|
||||
$user->id,
|
||||
$task,
|
||||
));
|
||||
|
||||
$data = [
|
||||
'name' => $user->name,
|
||||
'lastname' => $user->lastname,
|
||||
'taskName' => $task->name,
|
||||
'oldStart' => $formatedTaskStart,
|
||||
'oldEnd' => $formatedTaskEnd,
|
||||
'newStart' => $request["start"],
|
||||
'newEnd' => $request["end"],
|
||||
'eventName' => $event->name,
|
||||
];
|
||||
Mail::to($user->email)->send(new TaskDateUpdateMail($data));
|
||||
}
|
||||
}
|
||||
|
||||
$task->name = $request["name"];
|
||||
$task->description = $request["description"];
|
||||
$task->start = $request["start"];
|
||||
@@ -242,7 +280,7 @@ class TasksController extends Controller
|
||||
|
||||
try {
|
||||
$task = Task::find($request["task_id"]);
|
||||
$event = \App\Models\Event::find($task->events_id);
|
||||
$event = Event::find($task->events_id);
|
||||
|
||||
if (!$task) {
|
||||
return response()->json(["message" => "Task not found"], 404);
|
||||
@@ -273,6 +311,7 @@ class TasksController extends Controller
|
||||
'taskName' => $task->name,
|
||||
'start' => $task->start,
|
||||
'end' => $task->end,
|
||||
'eventName' => $event->name,
|
||||
];
|
||||
Mail::to($user->email)->send(new VolunteerUnassignToTaskMail($data));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
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 TaskDateUpdateMail extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*/
|
||||
public function __construct($data)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message envelope.
|
||||
*/
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: 'Date de tâche modifiée - Comité des fêtes de Beaupont',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message content definition.
|
||||
*/
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(
|
||||
view: 'mails.updateRole',
|
||||
with: [
|
||||
'name' => $this->data['name'],
|
||||
'lastname' => $this->data['lastname'],
|
||||
'taskName' => $this->data['taskName'],
|
||||
'oldStart' => $this->data['oldStart'],
|
||||
'oldEnd' => $this->data['oldEnd'],
|
||||
'newStart' => $this->data['newStart'],
|
||||
'newEnd' => $this->data['newEnd'],
|
||||
'eventName' => $this->data['eventName'],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attachments for the message.
|
||||
*
|
||||
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
|
||||
*/
|
||||
public function attachments(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Date de tâche modifiée</title>
|
||||
</head>
|
||||
<body style="padding: 20px; font-family: Arial, sans-serif;">
|
||||
<div>
|
||||
<p>Bonjour {{ $name }} {{ $lastname }},</p>
|
||||
|
||||
<p>
|
||||
La date de la tâche {{ $taskName }} de l'événement {{ $eventName }} a été modifié par un administrateur.
|
||||
Cette tâche avait lieu du {{ $oldStart }} au {{ $oldEnd }}. Et aura maintenant lieu du {{ $newStart }} au {{ $newEnd }}.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Si vous pensez qu’il s’agit d’une erreur ou si vous souhaitez plus d’informations,
|
||||
merci de contacter l’équipe du Comité des fêtes de Beaupont.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
À très bientôt,<br>
|
||||
L’équipe du Comité des fêtes de Beaupont
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<p style="font-size: 14px; color: #999; text-align: center; padding-top: 20px;">
|
||||
Cet email a été envoyé automatiquement, merci de ne pas y répondre.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user