create notification, broadcast notification and mail when a futur or ongoing date of a task is updated
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user