feat(tasks): complete task management system with Kanban, Calendar, notifications

- Add Task model with subtasks, priorities, status transitions, dates, hours
- Add Comment polymorphic model for tasks/issues/projects
- Livewire components: TaskManager (list+filters), TaskForm (modal), TaskDetail, TaskKanban (drag&drop), TaskCalendar (FullCalendar)
- TaskPolicy with permissions (view/create/edit/delete/assign/manage all)
- Notifications: assigned, status change, overdue, comment added
- Daily overdue notification job scheduled
- Dashboard widget unifies IssueTask + Task
- i18n: en/es/fr/ru (393 keys each)
- Routes, navigation, offline sync support
- 101 tests passing, Pint compliant on new files
This commit is contained in:
Javier Braña
2026-08-03 13:44:10 +02:00
parent a65d4b12f2
commit ba614bddbc
62 changed files with 5878 additions and 382 deletions
+167
View File
@@ -0,0 +1,167 @@
<?php
namespace App\Livewire\Tasks;
use App\Models\Project;
use App\Models\Task;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Layout;
use Livewire\Attributes\On;
use Livewire\Component;
#[Layout('layouts.app')]
class TaskCalendar extends Component
{
public ?Project $project = null;
public $viewMode = 'dayGridMonth'; // dayGridMonth, timeGridWeek, timeGridDay, listWeek
public $search = '';
public $assigneeFilter = '';
public $phaseFilter = '';
public $priorityFilter = '';
public $statusFilter = '';
protected $queryString = [
'viewMode' => ['except' => 'dayGridMonth'],
'search' => ['except' => ''],
'assigneeFilter' => ['except' => ''],
'phaseFilter' => ['except' => ''],
'priorityFilter' => ['except' => ''],
'statusFilter' => ['except' => ''],
];
public function mount(?Project $project = null): void
{
$this->project = $project;
if ($project) {
abort_unless($this->canAccessProject() && Auth::user()->can('view tasks'), 403);
} else {
abort_unless(Auth::user()->can('view tasks'), 403);
}
}
private function canAccessProject(): bool
{
$user = Auth::user();
return $user->can('manage all')
|| ($this->project && $this->project->users()->where('user_id', $user->id)->exists());
}
#[On('tasksChanged')]
public function refreshCalendar(): void
{
// Trigger re-render
}
public function render()
{
$user = Auth::user();
$query = Task::with(['project', 'phase', 'assignee'])
->where(function ($q) use ($user) {
if (! $user->can('manage all tasks')) {
$q->whereHas('project', function ($sub) use ($user) {
$sub->whereHas('users', function ($sub2) use ($user) {
$sub2->where('user_id', $user->id);
});
});
}
});
if ($this->project) {
$query->where('project_id', $this->project->id);
}
if ($this->search) {
$query->where(function ($q) {
$q->where('title', 'like', '%'.$this->search.'%')
->orWhere('description', 'like', '%'.$this->search.'%');
});
}
if ($this->assigneeFilter) {
$query->where('assigned_to', $this->assigneeFilter);
}
if ($this->phaseFilter) {
$query->where('phase_id', $this->phaseFilter);
}
if ($this->priorityFilter) {
$query->where('priority', $this->priorityFilter);
}
if ($this->statusFilter) {
$query->where('status', $this->statusFilter);
}
$tasks = $query->get();
// Transform tasks to FullCalendar events
$events = $tasks->map(function ($task) {
$start = $task->start_date ?? $task->due_date ?? $task->created_at->toDateString();
$end = $task->due_date ?? $task->start_date;
return [
'id' => $task->id,
'title' => $task->title,
'start' => $start,
'end' => $end ? $end->addDay()->toDateString() : null, // FullCalendar end is exclusive
'url' => route('projects.tasks.show', [$task->project_id, $task->id]),
'backgroundColor' => $task->priority_color,
'borderColor' => $task->priority_color,
'textColor' => '#ffffff',
'extendedProps' => [
'status' => $task->status,
'statusLabel' => $task->status_label,
'priority' => $task->priority,
'priorityLabel' => $task->priority_label,
'project' => $task->project->name ?? '—',
'phase' => $task->phase->name ?? '—',
'assignee' => $task->assignee->name ?? 'Sin asignar',
'dueDate' => $task->due_date?->format('d/m/Y'),
'isOverdue' => $task->is_overdue,
'isDueToday' => $task->is_due_today,
'progress' => $task->progress,
'subtasksCount' => $task->subtasks->count(),
'completedSubtasksCount' => $task->subtasks->where('status', 'completed')->count(),
],
'classNames' => [
'fc-task-'.$task->status,
$task->is_overdue ? 'fc-task-overdue' : '',
$task->is_due_today ? 'fc-task-due-today' : '',
],
];
})->toArray();
// Filter options
$assignees = User::whereHas('assignedTasks', function ($q) use ($user) {
if (! $user->can('manage all tasks')) {
$q->whereHas('project', function ($sub) use ($user) {
$sub->whereHas('users', function ($sub2) use ($user) {
$sub2->where('user_id', $user->id);
});
});
}
})->get(['id', 'name']);
$phases = $this->project
? $this->project->phases()->get(['id', 'name'])
: collect();
return view('livewire.tasks.task-calendar', [
'events' => json_encode($events),
'assignees' => $assignees,
'phases' => $phases,
'statusOptions' => Task::statusOptions(),
'priorityOptions' => Task::priorityOptions(),
]);
}
}
+177
View File
@@ -0,0 +1,177 @@
<?php
namespace App\Livewire\Tasks;
use App\Models\Comment;
use App\Models\Project;
use App\Models\Task;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Livewire\Attributes\Layout;
use Livewire\Component;
use Livewire\WithFileUploads;
#[Layout('layouts.app')]
class TaskDetail extends Component
{
use WithFileUploads;
public Task $task;
public ?Project $project = null;
// Comment form
public $commentBody = '';
public $editingCommentId = null;
public $editingCommentBody = '';
// Subtask form
public $subtaskTitle = '';
protected $listeners = ['tasksChanged' => '$refresh', 'commentAdded' => '$refresh', 'subtaskAdded' => '$refresh'];
public function mount(Task $task): void
{
$this->task = $task->load(['project', 'phase', 'assignee', 'creator', 'completer', 'subtasks', 'media', 'comments.user', 'comments.replies.user']);
$this->project = $this->task->project;
abort_unless(Auth::user()->can('view', $this->task), 403);
}
public function addComment(): void
{
$this->validate(['commentBody' => 'required|string|max:5000']);
$this->task->comments()->create([
'user_id' => Auth::id(),
'body' => $this->commentBody,
]);
$this->commentBody = '';
$this->dispatch('commentAdded');
$this->dispatch('tasksChanged');
}
public function startEditComment($commentId): void
{
$comment = $this->task->comments()->find($commentId);
if ($comment && $comment->user_id === Auth::id()) {
$this->editingCommentId = $commentId;
$this->editingCommentBody = $comment->body;
}
}
public function saveEditComment(): void
{
$this->validate(['editingCommentBody' => 'required|string|max:5000']);
$comment = Comment::find($this->editingCommentId);
if ($comment && $comment->user_id === Auth::id()) {
$comment->update(['body' => $this->editingCommentBody]);
}
$this->editingCommentId = null;
$this->editingCommentBody = '';
$this->dispatch('commentAdded');
}
public function cancelEditComment(): void
{
$this->editingCommentId = null;
$this->editingCommentBody = '';
}
public function deleteComment($commentId): void
{
$comment = Comment::find($commentId);
if ($comment && ($comment->user_id === Auth::id() || Auth::user()->can('manage all tasks'))) {
$comment->delete();
$this->dispatch('commentAdded');
}
}
public function addSubtask(): void
{
$this->validate(['subtaskTitle' => 'required|string|max:255']);
$maxOrder = $this->task->subtasks()->max('order') ?? 0;
$this->task->subtasks()->create([
'title' => $this->subtaskTitle,
'status' => 'pending',
'priority' => 'medium',
'project_id' => $this->task->project_id,
'phase_id' => $this->task->phase_id,
'parent_task_id' => $this->task->id,
'created_by' => Auth::id(),
'order' => $maxOrder + 1,
'uuid' => Str::uuid(),
]);
$this->subtaskTitle = '';
$this->dispatch('subtaskAdded');
$this->dispatch('tasksChanged');
}
public function updateSubtaskStatus(Task $subtask, string $status): void
{
abort_unless(Auth::user()->can('update', $subtask), 403);
$data = ['status' => $status];
if ($status === 'completed') {
$data['completed_at'] = now();
$data['completed_by'] = Auth::id();
} elseif ($subtask->status === 'completed') {
$data['completed_at'] = null;
$data['completed_by'] = null;
}
$subtask->update($data);
$this->dispatch('tasksChanged');
}
public function deleteSubtask(Task $subtask): void
{
abort_unless(Auth::user()->can('delete', $subtask), 403);
$subtask->delete();
$this->dispatch('tasksChanged');
}
public function changeStatus(string $status): void
{
abort_unless(Auth::user()->can('update', $this->task), 403);
$oldStatus = $this->task->status;
$data = ['status' => $status];
if ($oldStatus !== 'completed' && $status === 'completed') {
$data['completed_at'] = now();
$data['completed_by'] = Auth::id();
} elseif ($oldStatus === 'completed' && $status !== 'completed') {
$data['completed_at'] = null;
$data['completed_by'] = null;
}
$this->task->update($data);
$this->task->refresh();
$this->dispatch('tasksChanged');
}
public function render()
{
$assignees = User::whereHas('assignedTasks', function ($q) {
if ($this->project) {
$q->where('project_id', $this->project->id);
}
})->get(['id', 'name']);
return view('livewire.tasks.task-detail', [
'assignees' => $assignees,
'statusOptions' => Task::statusOptions(),
'priorityOptions' => Task::priorityOptions(),
]);
}
}
+206
View File
@@ -0,0 +1,206 @@
<?php
namespace App\Livewire\Tasks;
use App\Models\Project;
use App\Models\Task;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Livewire\WithFileUploads;
#[Layout('layouts.app')]
class TaskForm extends Component
{
use WithFileUploads;
public ?Task $task = null;
public ?Project $project = null;
// Form fields
#[Validate('required|string|max:255')]
public $title = '';
#[Validate('nullable|string')]
public $description = '';
#[Validate('nullable|exists:phases,id')]
public $phase_id = '';
#[Validate('required|in:pending,in_progress,completed,cancelled')]
public $status = 'pending';
#[Validate('required|in:low,medium,high,critical')]
public $priority = 'medium';
#[Validate('nullable|exists:users,id')]
public $assigned_to = '';
#[Validate('nullable|date|after_or_equal:start_date')]
public $due_date = '';
#[Validate('nullable|date|before_or_equal:due_date')]
public $start_date = '';
#[Validate('nullable|integer|min:0|max:10000')]
public $estimated_hours = '';
#[Validate('nullable|integer|min:0|max:10000')]
public $actual_hours = '';
#[Validate('nullable|integer|min:0')]
public $order = 0;
// For subtasks
#[Validate('nullable|exists:tasks,id')]
public $parent_task_id = '';
// UI state
public $showModal = false;
public $mode = 'create'; // 'create' or 'edit'
protected $listeners = ['openTaskModal' => 'openModal'];
public function mount(?Project $project = null, ?Task $task = null): void
{
$this->project = $project;
$this->task = $task;
if ($task) {
$this->mode = 'edit';
$this->fillFromTask($task);
abort_unless(Auth::user()->can('update', $task), 403);
} else {
$this->mode = 'create';
abort_unless(Auth::user()->can('create tasks', $project), 403);
}
}
public function openModal(array $params = []): void
{
$this->resetForm();
$this->mode = $params['mode'] ?? 'create';
$this->project = $params['project'] ?? $this->project;
$this->task = $params['task'] ?? null;
if ($this->task) {
$this->fillFromTask($this->task);
} else {
$this->status = 'pending';
$this->priority = 'medium';
$this->order = 0;
}
$this->showModal = true;
}
private function fillFromTask(Task $task): void
{
$this->title = $task->title;
$this->description = $task->description;
$this->phase_id = $task->phase_id ?? '';
$this->status = $task->status;
$this->priority = $task->priority;
$this->assigned_to = $task->assigned_to ?? '';
$this->due_date = $task->due_date?->format('Y-m-d') ?? '';
$this->start_date = $task->start_date?->format('Y-m-d') ?? '';
$this->estimated_hours = $task->estimated_hours ?? '';
$this->actual_hours = $task->actual_hours ?? '';
$this->order = $task->order ?? 0;
$this->parent_task_id = $task->parent_task_id ?? '';
}
private function resetForm(): void
{
$this->reset(['title', 'description', 'phase_id', 'status', 'priority', 'assigned_to', 'due_date', 'start_date', 'estimated_hours', 'actual_hours', 'order', 'parent_task_id']);
$this->resetValidation();
}
public function save(): void
{
$validated = $this->validate();
$data = [
'title' => $validated['title'],
'description' => $validated['description'] ?? null,
'phase_id' => $validated['phase_id'] ?? null,
'status' => $validated['status'],
'priority' => $validated['priority'],
'assigned_to' => $validated['assigned_to'] ?? null,
'due_date' => $validated['due_date'] ?? null,
'start_date' => $validated['start_date'] ?? null,
'estimated_hours' => $validated['estimated_hours'] ?? null,
'actual_hours' => $validated['actual_hours'] ?? null,
'order' => $validated['order'],
'parent_task_id' => $validated['parent_task_id'] ?? null,
'project_id' => $this->project?->id,
'created_by' => Auth::id(),
];
// Handle status transitions
if ($this->mode === 'edit' && $this->task) {
$oldStatus = $this->task->status;
$newStatus = $data['status'];
if ($oldStatus !== 'completed' && $newStatus === 'completed') {
$data['completed_at'] = now();
$data['completed_by'] = Auth::id();
} elseif ($oldStatus === 'completed' && $newStatus !== 'completed') {
$data['completed_at'] = null;
$data['completed_by'] = null;
}
$this->task->update($data);
$task = $this->task;
session()->flash('message', 'Tarea actualizada correctamente');
} else {
$data['uuid'] = Str::uuid();
$task = Task::create($data);
session()->flash('message', 'Tarea creada correctamente');
}
$this->dispatch('tasksChanged');
$this->dispatch('taskSaved', taskId: $task->id);
$this->closeModal();
}
public function closeModal(): void
{
$this->showModal = false;
$this->resetForm();
$this->mode = 'create';
$this->task = null;
}
public function render()
{
$assignees = User::whereHas('assignedTasks', function ($q) {
if ($this->project) {
$q->where('project_id', $this->project->id);
}
})->get(['id', 'name']);
$phases = $this->project ? $this->project->phases->pluck('name', 'id') : collect();
// For parent task selector (only sibling tasks in same project)
$parentTasks = $this->project
? Task::where('project_id', $this->project->id)
->whereNull('parent_task_id')
->when($this->task, fn ($q) => $q->where('id', '!=', $this->task->id))
->get(['id', 'title'])
: collect();
return view('livewire.tasks.task-form', [
'assignees' => $assignees,
'phases' => $phases,
'parentTasks' => $parentTasks,
'statusOptions' => Task::statusOptions(),
'priorityOptions' => Task::priorityOptions(),
]);
}
}
+194
View File
@@ -0,0 +1,194 @@
<?php
namespace App\Livewire\Tasks;
use App\Models\Project;
use App\Models\Task;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Layout;
use Livewire\Attributes\On;
use Livewire\Component;
use Livewire\WithPagination;
#[Layout('layouts.app')]
class TaskKanban extends Component
{
use WithPagination;
public ?Project $project = null;
public $search = '';
public $assigneeFilter = '';
public $phaseFilter = '';
public $priorityFilter = '';
public $showOverdueOnly = false;
public $perPage = 50;
protected $queryString = [
'search' => ['except' => ''],
'assigneeFilter' => ['except' => ''],
'phaseFilter' => ['except' => ''],
'priorityFilter' => ['except' => ''],
'showOverdueOnly' => ['except' => false],
];
public function mount(?Project $project = null): void
{
$this->project = $project;
if ($project) {
abort_unless($this->canAccessProject() && Auth::user()->can('view tasks'), 403);
} else {
abort_unless(Auth::user()->can('view tasks'), 403);
}
}
private function canAccessProject(): bool
{
$user = Auth::user();
return $user->can('manage all')
|| ($this->project && $this->project->users()->where('user_id', $user->id)->exists());
}
#[On('tasksChanged')]
public function refreshTasks(): void
{
// Trigger re-render
}
public function updateTaskStatus(Task $task, string $status): void
{
abort_unless(Auth::user()->can('update', $task), 403);
$oldStatus = $task->status;
$data = ['status' => $status];
if ($oldStatus !== 'completed' && $status === 'completed') {
$data['completed_at'] = now();
$data['completed_by'] = Auth::id();
} elseif ($oldStatus === 'completed' && $status !== 'completed') {
$data['completed_at'] = null;
$data['completed_by'] = null;
}
$task->update($data);
$this->dispatch('tasksChanged');
}
public function updateTaskOrder(int $taskId, int $order): void
{
$task = Task::find($taskId);
if ($task && Auth::user()->can('update', $task)) {
$task->update(['order' => $order]);
}
}
public function resetFilters(): void
{
$this->reset(['search', 'assigneeFilter', 'phaseFilter', 'priorityFilter', 'showOverdueOnly']);
$this->resetPage();
}
public function render()
{
$user = Auth::user();
$baseQuery = Task::with(['project', 'phase', 'assignee', 'creator', 'subtasks'])
->where(function ($q) use ($user) {
if (! $user->can('manage all tasks')) {
$q->whereHas('project', function ($sub) use ($user) {
$sub->whereHas('users', function ($sub2) use ($user) {
$sub2->where('user_id', $user->id);
});
});
}
});
if ($this->project) {
$baseQuery->where('project_id', $this->project->id);
}
if ($this->search) {
$baseQuery->where(function ($q) {
$q->where('title', 'like', '%'.$this->search.'%')
->orWhere('description', 'like', '%'.$this->search.'%');
});
}
if ($this->assigneeFilter) {
$baseQuery->where('assigned_to', $this->assigneeFilter);
}
if ($this->phaseFilter) {
$baseQuery->where('phase_id', $this->phaseFilter);
}
if ($this->priorityFilter) {
$baseQuery->where('priority', $this->priorityFilter);
}
if ($this->showOverdueOnly) {
$baseQuery->overdue();
}
// Get tasks grouped by status
$statuses = ['pending', 'in_progress', 'completed', 'cancelled'];
$columns = [];
foreach ($statuses as $status) {
$query = clone $baseQuery;
$query->where('status', $status)
->orderBy('order')
->orderBy('created_at', 'desc');
$tasks = $query->get();
$columns[$status] = [
'label' => Task::statusOptions()[$status],
'color' => $this->getStatusColor($status),
'tasks' => $tasks,
'count' => $tasks->count(),
];
}
// Filter options
$assignees = User::whereHas('assignedTasks', function ($q) use ($user) {
if (! $user->can('manage all tasks')) {
$q->whereHas('project', function ($sub) use ($user) {
$sub->whereHas('users', function ($sub2) use ($user) {
$sub2->where('user_id', $user->id);
});
});
}
})->get(['id', 'name']);
$phases = $this->project
? $this->project->phases()->get(['id', 'name'])
: collect();
return view('livewire.tasks.task-kanban', [
'columns' => $columns,
'assignees' => $assignees,
'phases' => $phases,
'statusOptions' => Task::statusOptions(),
'priorityOptions' => Task::priorityOptions(),
]);
}
private function getStatusColor(string $status): string
{
return match ($status) {
'pending' => '#6b7280', // gray
'in_progress' => '#3b82f6', // blue
'completed' => '#10b981', // green
'cancelled' => '#ef4444', // red
default => '#6b7280',
};
}
}
+186
View File
@@ -0,0 +1,186 @@
<?php
namespace App\Livewire\Tasks;
use App\Models\Project;
use App\Models\Task;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Layout;
use Livewire\Attributes\On;
use Livewire\Component;
use Livewire\WithPagination;
#[Layout('layouts.app')]
class TaskManager extends Component
{
use WithPagination;
public ?Project $project = null;
public $search = '';
public $statusFilter = '';
public $priorityFilter = '';
public $assigneeFilter = '';
public $phaseFilter = '';
public $showOverdueOnly = false;
public $perPage = 25;
public $sortField = 'created_at';
public $sortDirection = 'desc';
public $viewMode = 'table'; // 'table' or 'kanban'
protected $queryString = [
'search' => ['except' => ''],
'statusFilter' => ['except' => ''],
'priorityFilter' => ['except' => ''],
'assigneeFilter' => ['except' => ''],
'phaseFilter' => ['except' => ''],
'showOverdueOnly' => ['except' => false],
'perPage' => ['except' => 25],
'sortField' => ['except' => 'created_at'],
'sortDirection' => ['except' => 'desc'],
'viewMode' => ['except' => 'table'],
];
public function mount(?Project $project = null): void
{
$this->project = $project;
if ($project) {
abort_unless($this->canAccessProject() && Auth::user()->can('view tasks'), 403);
} else {
abort_unless(Auth::user()->can('view tasks'), 403);
}
}
private function canAccessProject(): bool
{
$user = Auth::user();
return $user->can('manage all')
|| ($this->project && $this->project->users()->where('user_id', $user->id)->exists());
}
#[On('tasksChanged')]
public function refreshTasks(): void
{
// Trigger re-render
}
public function sortBy(string $field): void
{
if ($this->sortField === $field) {
$this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
} else {
$this->sortField = $field;
$this->sortDirection = 'asc';
}
}
public function resetFilters(): void
{
$this->reset(['search', 'statusFilter', 'priorityFilter', 'assigneeFilter', 'phaseFilter', 'showOverdueOnly']);
$this->resetPage();
}
public function deleteTask(Task $task): void
{
abort_unless(Auth::user()->can('delete', $task), 403);
$task->delete();
session()->flash('message', 'Tarea eliminada');
$this->dispatch('tasksChanged');
}
public function toggleViewMode(): void
{
$this->viewMode = $this->viewMode === 'table' ? 'kanban' : 'table';
}
public function render()
{
$user = Auth::user();
$query = Task::with(['project', 'phase', 'assignee', 'creator', 'subtasks'])
->where(function ($q) use ($user) {
if (! $user->can('manage all tasks')) {
$q->whereHas('project', function ($sub) use ($user) {
$sub->whereHas('users', function ($sub2) use ($user) {
$sub2->where('user_id', $user->id);
});
});
}
});
if ($this->project) {
$query->where('project_id', $this->project->id);
}
if ($this->search) {
$query->where(function ($q) {
$q->where('title', 'like', '%'.$this->search.'%')
->orWhere('description', 'like', '%'.$this->search.'%');
});
}
if ($this->statusFilter) {
$query->where('status', $this->statusFilter);
}
if ($this->priorityFilter) {
$query->where('priority', $this->priorityFilter);
}
if ($this->assigneeFilter) {
$query->where('assigned_to', $this->assigneeFilter);
}
if ($this->phaseFilter) {
$query->where('phase_id', $this->phaseFilter);
}
if ($this->showOverdueOnly) {
$query->overdue();
}
// Sorting
$allowedSorts = ['title', 'status', 'priority', 'due_date', 'created_at', 'order'];
if (in_array($this->sortField, $allowedSorts)) {
$query->orderBy($this->sortField, $this->sortDirection);
} else {
$query->orderBy('order')->orderBy('created_at', 'desc');
}
$tasks = $query->paginate($this->perPage);
// Filter options
$assignees = User::whereHas('assignedTasks', function ($q) use ($user) {
if (! $user->can('manage all tasks')) {
$q->whereHas('project', function ($sub) use ($user) {
$sub->whereHas('users', function ($sub2) use ($user) {
$sub2->where('user_id', $user->id);
});
});
}
})->get(['id', 'name']);
$phases = $this->project
? $this->project->phases()->get(['id', 'name'])
: collect();
return view('livewire.tasks.task-manager', [
'tasks' => $tasks,
'assignees' => $assignees,
'phases' => $phases,
'statusOptions' => Task::statusOptions(),
'priorityOptions' => Task::priorityOptions(),
]);
}
}