232 lines
7.7 KiB
PHP
232 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Notifications\TaskDateUpdated;
|
|
use App\Notifications\TaskDeleted;
|
|
use App\Notifications\VolunteerAssignedToTask;
|
|
use App\Notifications\VolunteerUnassignedFromTask;
|
|
use App\Models\Event;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use App\Models\Task;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\Log;
|
|
use App\Services\FormatDate;
|
|
|
|
class TasksController extends Controller
|
|
{
|
|
public function create(Request $request): JsonResponse {
|
|
|
|
if(Gate::denies('create', Task::class)) {
|
|
return response()->json(["message" => "Can't create task"], 403);
|
|
}
|
|
|
|
try {
|
|
|
|
$event = Event::find($request["event_id"]);
|
|
|
|
$event->tasks()->create([
|
|
"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 created"], 200);
|
|
|
|
} catch(\Exception $e) {
|
|
Log::info($e->getMessage());
|
|
return response()->json(['message' => "Server error"], 500);
|
|
}
|
|
}
|
|
|
|
public function assignSelf(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$task = Task::findOrFail($request["task_id"]);
|
|
|
|
if ($task->users()->count() >= $task->max_participants) {
|
|
return response()->json(["message" => "Max participants reached"], 403);
|
|
}
|
|
|
|
if ($task->users()->where('users.id', $request->user()->id)->exists()) {
|
|
return response()->json(["message" => "Already registered"], 409);
|
|
}
|
|
|
|
$task->users()->attach($request->user()->id);
|
|
|
|
return response()->json(['message' => "Task assigned"], 200);
|
|
|
|
} catch (\Throwable $e) {
|
|
Log::error($e->getMessage());
|
|
return response()->json(['message' => "Server error"], 500);
|
|
}
|
|
}
|
|
|
|
public function assignUser(Request $request, int $id): JsonResponse
|
|
{
|
|
if (Gate::denies('assignOther', Task::class)) {
|
|
return response()->json(['message' => "Can't create task"], 403);
|
|
}
|
|
|
|
try {
|
|
$task = Task::findOrFail($request['task_id']);
|
|
$event = Event::findOrFail($task->events_id);
|
|
$user = User::findOrFail($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);
|
|
}
|
|
|
|
$task->users()->attach($id);
|
|
|
|
if (Carbon::now()->lessThanOrEqualTo($task->end)) {
|
|
$user->notify(new VolunteerAssignedToTask($task, $event));
|
|
}
|
|
|
|
return response()->json(['message' => 'Task assigned'], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error($e->getMessage());
|
|
return response()->json(['message' => 'Server error'], 500);
|
|
}
|
|
}
|
|
|
|
public function getEventTasks(Request $request, int $id): JsonResponse {
|
|
|
|
try {
|
|
|
|
$event = Event::find($id);
|
|
|
|
return response()->json(['data' => $event->tasks()->get()]);
|
|
|
|
} catch(\Exception $e) {
|
|
Log::info($e->getMessage());
|
|
return response()->json(['message' => "Server error"], 500);
|
|
}
|
|
}
|
|
|
|
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')->findOrFail($id);
|
|
$event = Event::findOrFail($task->events_id);
|
|
|
|
if (Carbon::now()->lessThanOrEqualTo($task->end) && $task->users->isNotEmpty()) {
|
|
Notification::send($task->users, new TaskDeleted($task, $event));
|
|
}
|
|
|
|
$task->delete();
|
|
|
|
return response()->json(['message' => 'Task deleted and participants notified'], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error($e->getMessage());
|
|
return response()->json(['message' => 'Server error'], 500);
|
|
}
|
|
}
|
|
|
|
public function update(Request $request, int $id): JsonResponse
|
|
{
|
|
if (Gate::denies('update', Task::class)) {
|
|
return response()->json(['message' => 'Forbidden'], 403);
|
|
}
|
|
|
|
try {
|
|
$task = Task::with('users')->findOrFail($id);
|
|
|
|
$datesChanged = $task->start !== $request['start'] || $task->end !== $request['end'];
|
|
|
|
if ($datesChanged && Carbon::now()->lessThanOrEqualTo($task->end)) {
|
|
$event = Event::findOrFail($task->events_id);
|
|
|
|
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->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);
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error($e->getMessage());
|
|
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::findOrFail($request['task_id']);
|
|
$user = User::findOrFail($id);
|
|
|
|
if (!$task->users()->where('user_id', $id)->exists()) {
|
|
return response()->json(['message' => 'User is not assigned to this task'], 400);
|
|
}
|
|
|
|
$task->users()->detach($id);
|
|
|
|
if (Carbon::now()->lessThanOrEqualTo($task->end)) {
|
|
$event = Event::findOrFail($task->events_id);
|
|
$user->notify(new VolunteerUnassignedFromTask($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);
|
|
}
|
|
}
|
|
}
|