Create unassignUser, unassignSelf methods and Bruno requests. Add call events when needed
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\EventParticipationCancelled;
|
||||
use App\Models\Events;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -83,12 +84,29 @@ class EventsController extends Controller
|
||||
}
|
||||
|
||||
try {
|
||||
$event = Events::with('tasks.users')->find($id);
|
||||
|
||||
if (!$event) {
|
||||
return response()->json(['message' => 'Event not found'], 404);
|
||||
}
|
||||
|
||||
$participants = $event->tasks->flatMap(function ($task) {
|
||||
return $task->users;
|
||||
})->unique('id');
|
||||
|
||||
foreach ($participants as $user) {
|
||||
broadcast(new EventParticipationCancelled(
|
||||
$user->id,
|
||||
$event
|
||||
));
|
||||
}
|
||||
|
||||
$event = Events::find($id);
|
||||
$event->delete();
|
||||
return response()->json(["message" => "Event deleted"]);
|
||||
|
||||
return response()->json(["message" => "Event deleted and participants notified"]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::info($e->getMessage());
|
||||
Log::error("Event delete error: " . $e->getMessage());
|
||||
return response()->json(['message' => "Server error"], 500);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\TaskParticipationCancelled;
|
||||
use App\Events\VolunteerAssignedToTask;
|
||||
use App\Events\VolunteerUnassignedFromTask;
|
||||
use App\Models\Events;
|
||||
use App\Models\Task;
|
||||
use App\Models\User;
|
||||
@@ -46,7 +49,7 @@ class TasksController extends Controller
|
||||
|
||||
$task = Task::find($request["task_id"]);
|
||||
|
||||
if($task->max_participants = $task->users()->count()) {
|
||||
if($task->max_participants === $task->users()->count()) {
|
||||
return response()->json(["message" => "Max participants reached"], 403);
|
||||
}
|
||||
|
||||
@@ -69,13 +72,24 @@ class TasksController extends Controller
|
||||
|
||||
$task = Task::find($request["task_id"]);
|
||||
|
||||
if ($task->users()->where('user_id', $id)->exists()) {
|
||||
return response()->json(["message" => "User already assigned to this task"], 400);
|
||||
}
|
||||
|
||||
if($task->max_participants === $task->users()->count()) {
|
||||
return response()->json(["message" => "Max participants reached"], 403);
|
||||
}
|
||||
|
||||
$user = User::find($id);
|
||||
|
||||
if($user) $task->users()->attach($id);
|
||||
if($user) {
|
||||
$task->users()->attach($id);
|
||||
broadcast(new VolunteerAssignedToTask(
|
||||
$user->id,
|
||||
$task,
|
||||
$task->event
|
||||
));
|
||||
}
|
||||
|
||||
return response()->json(['message' => "Task assigned"], 200);
|
||||
|
||||
@@ -101,19 +115,33 @@ class TasksController extends Controller
|
||||
}
|
||||
|
||||
public function deleteTask(Request $request, int $id): JsonResponse {
|
||||
|
||||
if(Gate::denies('delete', Task::class)) {
|
||||
return response()->json(["message" => "Can't delete task"], 403);
|
||||
}
|
||||
|
||||
try {
|
||||
$task = Task::with(['users', 'event'])->find($id);
|
||||
|
||||
if (!$task) {
|
||||
return response()->json(["message" => "Task not found"], 404);
|
||||
}
|
||||
|
||||
$participants = $task->users;
|
||||
|
||||
foreach ($participants as $user) {
|
||||
broadcast(new TaskParticipationCancelled(
|
||||
$user->id,
|
||||
$task,
|
||||
$task->event
|
||||
));
|
||||
}
|
||||
|
||||
$task = Task::find($id);
|
||||
$task->delete();
|
||||
return response()->json(['message' => "Task deleted"], 200);
|
||||
|
||||
return response()->json(['message' => "Task deleted and participants notified"], 200);
|
||||
|
||||
} catch(\Exception $e) {
|
||||
Log::info($e->getMessage());
|
||||
Log::error($e->getMessage());
|
||||
return response()->json(['message' => "Server error"], 500);
|
||||
}
|
||||
}
|
||||
@@ -142,4 +170,67 @@ class TasksController extends Controller
|
||||
return response()->json(['message' => "Server error"], 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function unassignSelf(Request $request): JsonResponse {
|
||||
try {
|
||||
$task = Task::find($request["task_id"]);
|
||||
$userId = $request->user()->id;
|
||||
|
||||
if (!$task) {
|
||||
return response()->json(["message" => "Task not found"], 404);
|
||||
}
|
||||
|
||||
$task->users()->detach($userId);
|
||||
|
||||
return response()->json(['message' => "Successfully unassigned from task"], 200);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error("UnassignSelf error: " . $e->getMessage());
|
||||
return response()->json(['message' => "Server error"], 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function unassignUser(Request $request, int $id): JsonResponse {
|
||||
|
||||
if(Gate::denies('unassignOther', Task::class)) {
|
||||
return response()->json(["message" => "Unauthorized"], 403);
|
||||
}
|
||||
|
||||
try {
|
||||
$task = Task::with('event')->find($request["task_id"]);
|
||||
|
||||
if (!$task) {
|
||||
return response()->json(["message" => "Task not found"], 404);
|
||||
}
|
||||
|
||||
$isAssigned = $task->users()->where('user_id', $id)->exists();
|
||||
if (!$isAssigned) {
|
||||
return response()->json(["message" => "User is not assigned to this task"], 400);
|
||||
}
|
||||
|
||||
$user = User::find($id);
|
||||
|
||||
if ($user) {
|
||||
$task->users()->detach($id);
|
||||
|
||||
broadcast(new VolunteerUnassignedFromTask(
|
||||
$user->id,
|
||||
$task,
|
||||
$task->event
|
||||
));
|
||||
}
|
||||
|
||||
return response()->json(['message' => "User unassigned successfully"], 200);
|
||||
|
||||
} catch(\Exception $e) {
|
||||
Log::error("UnassignUser error: " . $e->getMessage());
|
||||
return response()->json(['message' => "Server error"], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,11 @@ class TasksPolicy
|
||||
return $user->role === 1 || $user->role === 2;
|
||||
}
|
||||
|
||||
public function unassignOther(User $user, Task $model): bool
|
||||
{
|
||||
return $user->role === 1 || $user->role === 2;
|
||||
}
|
||||
|
||||
public function delete(User $user, Task $model): bool
|
||||
{
|
||||
return $user->role === 1 || $user->role === 2;
|
||||
|
||||
Reference in New Issue
Block a user