use TaskDateUpdated notification
This commit is contained in:
1 parent
0a9b4874db
commit
3e5dc3f377
1 file changed
+28
-55
@@ -2,11 +2,10 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Events\TaskDateUpdated;
|
use App\Notifications\TaskDateUpdated;
|
||||||
use App\Notifications\TaskDeleted;
|
use App\Notifications\TaskDeleted;
|
||||||
use App\Notifications\VolunteerAssignedToTask;
|
use App\Notifications\VolunteerAssignedToTask;
|
||||||
use App\Events\VolunteerUnassignedFromTask;
|
use App\Events\VolunteerUnassignedFromTask;
|
||||||
use App\Mail\TaskDateUpdateMail;
|
|
||||||
use App\Models\Event;
|
use App\Models\Event;
|
||||||
use App\Models\Notification;
|
use App\Models\Notification;
|
||||||
use App\Mail\VolunteerUnassignToTaskMail;
|
use App\Mail\VolunteerUnassignToTaskMail;
|
||||||
@@ -143,70 +142,44 @@ class TasksController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(Request $request, int $id): JsonResponse {
|
public function update(Request $request, int $id): JsonResponse
|
||||||
|
{
|
||||||
if(Gate::denies('update', Task::class)) {
|
if (Gate::denies('update', Task::class)) {
|
||||||
return response()->json(["message" => "Forbidden"], 403);
|
return response()->json(['message' => 'Forbidden'], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$task = Task::find($id);
|
$task = Task::with('users')->findOrFail($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) {
|
$datesChanged = $task->start !== $request['start'] || $task->end !== $request['end'];
|
||||||
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);
|
|
||||||
|
|
||||||
broadcast(new TaskDateUpdated(
|
if ($datesChanged && Carbon::now()->lessThanOrEqualTo($task->end)) {
|
||||||
$user->id,
|
$event = Event::findOrFail($task->events_id);
|
||||||
$task,
|
|
||||||
$event,
|
Notification::send($task->users, new TaskDateUpdated(
|
||||||
FormatDate::formatDate($request["start"]),
|
task: $task,
|
||||||
FormatDate::formatDate($request["end"]),
|
event: $event,
|
||||||
$notification->id,
|
oldStart: FormatDate::formatDate($task->start),
|
||||||
|
oldEnd: FormatDate::formatDate($task->end),
|
||||||
|
newStart: FormatDate::formatDate($request['start']),
|
||||||
|
newEnd: FormatDate::formatDate($request['end']),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->email_notifications) {
|
$task->update([
|
||||||
$data = [
|
'name' => $request['name'],
|
||||||
'name' => $user->name,
|
'description' => $request['description'],
|
||||||
'lastname' => $user->lastname,
|
'start' => $request['start'],
|
||||||
'taskName' => $task->name,
|
'end' => $request['end'],
|
||||||
'oldStart' => $formatedTaskStart,
|
'location' => $request['location'],
|
||||||
'oldEnd' => $formatedTaskEnd,
|
'max_participants' => $request['max_participants'],
|
||||||
'newStart' => $request["start"],
|
]);
|
||||||
'newEnd' => $request["end"],
|
|
||||||
'eventName' => $event->name,
|
|
||||||
];
|
|
||||||
Mail::to($user->email)->send(new TaskDateUpdateMail($data));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$task->name = $request["name"];
|
return response()->json(['message' => 'Task updated'], 200);
|
||||||
$task->description = $request["description"];
|
|
||||||
$task->start = $request["start"];
|
|
||||||
$task->end = $request["end"];
|
|
||||||
$task->location = $request["location"];
|
|
||||||
$task->max_participants = $request["max_participants"];
|
|
||||||
$task->save();
|
|
||||||
|
|
||||||
return response()->json(['message' => "Task updated"], 200);
|
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Log::info($e->getMessage());
|
Log::error($e->getMessage());
|
||||||
return response()->json(['message' => "Server error"], 500);
|
return response()->json(['message' => 'Server error'], 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user