use TaskDateUpdated notification
This commit is contained in:
@@ -2,11 +2,10 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\TaskDateUpdated;
|
||||
use App\Notifications\TaskDateUpdated;
|
||||
use App\Notifications\TaskDeleted;
|
||||
use App\Notifications\VolunteerAssignedToTask;
|
||||
use App\Events\VolunteerUnassignedFromTask;
|
||||
use App\Mail\TaskDateUpdateMail;
|
||||
use App\Models\Event;
|
||||
use App\Models\Notification;
|
||||
use App\Mail\VolunteerUnassignToTaskMail;
|
||||
@@ -143,70 +142,44 @@ class TasksController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
public function update(Request $request, int $id): JsonResponse {
|
||||
|
||||
if(Gate::denies('update', Task::class)) {
|
||||
return response()->json(["message" => "Forbidden"], 403);
|
||||
public function update(Request $request, int $id): JsonResponse
|
||||
{
|
||||
if (Gate::denies('update', Task::class)) {
|
||||
return response()->json(['message' => 'Forbidden'], 403);
|
||||
}
|
||||
|
||||
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);
|
||||
$task = Task::with('users')->findOrFail($id);
|
||||
|
||||
foreach ($participants as $user) {
|
||||
if ($user->web_notifications) {
|
||||
$notification = Notification::create([
|
||||
'content' => "La date de la tâche {$task->name} de l'événement {$event->name} à été mis à jour. Cette tâche aura maintenant lieu du {$formatedTaskStart} au {$formatedTaskEnd}."
|
||||
]);
|
||||
$notification->users()->attach($user->id);
|
||||
$datesChanged = $task->start !== $request['start'] || $task->end !== $request['end'];
|
||||
|
||||
broadcast(new TaskDateUpdated(
|
||||
$user->id,
|
||||
$task,
|
||||
$event,
|
||||
FormatDate::formatDate($request["start"]),
|
||||
FormatDate::formatDate($request["end"]),
|
||||
$notification->id,
|
||||
));
|
||||
}
|
||||
if ($datesChanged && Carbon::now()->lessThanOrEqualTo($task->end)) {
|
||||
$event = Event::findOrFail($task->events_id);
|
||||
|
||||
if ($user->email_notifications) {
|
||||
$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));
|
||||
}
|
||||
}
|
||||
Notification::send($task->users, new TaskDateUpdated(
|
||||
task: $task,
|
||||
event: $event,
|
||||
oldStart: FormatDate::formatDate($task->start),
|
||||
oldEnd: FormatDate::formatDate($task->end),
|
||||
newStart: FormatDate::formatDate($request['start']),
|
||||
newEnd: FormatDate::formatDate($request['end']),
|
||||
));
|
||||
}
|
||||
|
||||
$task->name = $request["name"];
|
||||
$task->description = $request["description"];
|
||||
$task->start = $request["start"];
|
||||
$task->end = $request["end"];
|
||||
$task->location = $request["location"];
|
||||
$task->max_participants = $request["max_participants"];
|
||||
$task->save();
|
||||
$task->update([
|
||||
'name' => $request['name'],
|
||||
'description' => $request['description'],
|
||||
'start' => $request['start'],
|
||||
'end' => $request['end'],
|
||||
'location' => $request['location'],
|
||||
'max_participants' => $request['max_participants'],
|
||||
]);
|
||||
|
||||
return response()->json(['message' => "Task updated"], 200);
|
||||
return response()->json(['message' => 'Task updated'], 200);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::info($e->getMessage());
|
||||
return response()->json(['message' => "Server error"], 500);
|
||||
Log::error($e->getMessage());
|
||||
return response()->json(['message' => 'Server error'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user