Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Http/Requests/TaskCreateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function rules(): array
{
return [
'title' => ['required', 'string', 'max:255'],
'description' => ['nullable', 'string'],
'due_date' => ['nullable', 'date'],
'column_id' => ['nullable', 'exists:columns,id'],
];
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/TaskUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function rules(): array
{
return [
'title' => ['sometimes', 'string', 'max:255'],
'due_date' => ['sometimes', 'date'],
'completed' => ['sometimes', 'boolean'],
'description' => ['nullable', 'string', 'max:1000'],
'due_date' => ['sometimes', 'nullable', 'date'],
];
}
}
6 changes: 5 additions & 1 deletion app/Http/Resources/TaskResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ class TaskResource extends JsonResource
*/
public function toArray(Request $request): array
{
// Calculate days cleanly or fallback to 0 if null
$daysInColumn = $this->column_updated_at ? (int) $this->column_updated_at->diffInDays(now()) : 0;

return [
'id' => $this->id,
'team_id' => $this->team_id,
'column_id' => $this->column_id,
'order' => $this->order,
'title' => $this->title,
'completed' => (bool)$this->completed,
'description' => $this->description,
'days_in_column' => $daysInColumn,
'due_date' => $this->due_date,
'created_by' => $this->created_by,
'creator' => $this->whenLoaded('creator', fn () => [
Expand Down
27 changes: 0 additions & 27 deletions app/Jobs/IncrementTeamCompletedTasks.php

This file was deleted.

10 changes: 6 additions & 4 deletions app/Models/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ class Task extends Model
* @var list<string>
*/
protected $fillable = [
'title',
'due_date',
'completed',
'team_id',
'created_by',
'column_id',
'column_updated_at',
'order',
'title',
'description',
'due_date',
'created_by',
];

/**
Expand All @@ -33,6 +34,7 @@ class Task extends Model
protected function casts(): array
{
return [
'column_updated_at' => 'datetime',
'due_date' => 'datetime',
];
}
Expand Down
7 changes: 7 additions & 0 deletions app/Services/ColumnService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Column;
use App\Models\User;
use Illuminate\Validation\ValidationException;

class ColumnService
{
Expand Down Expand Up @@ -35,6 +36,12 @@ public function updateColumn(Column $column, array $data): bool
*/
public function deleteColumn(Column $column): ?bool
{
if ($column->tasks()->exists()) {
throw ValidationException::withMessages([
'column' => 'Cannot delete a column that contains tasks. Please move or delete the tasks first.',
]);
}

return $column->delete();
}
}
26 changes: 14 additions & 12 deletions app/Services/TaskService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Models\Task;
use App\Models\Column;
use App\Models\User;
use App\Jobs\IncrementTeamCompletedTasks;
use Illuminate\Support\Facades\DB;

class TaskService
Expand All @@ -31,6 +30,7 @@ public function createTask(array $data, User $user): Task
'created_by' => $user->id,
'column_id' => $columnId,
'order' => $order,
'column_updated_at' => now(), // Initialize column timing
]));
}

Expand All @@ -39,13 +39,7 @@ public function createTask(array $data, User $user): Task
*/
public function updateTask(Task $task, array $data): bool
{
$updated = $task->update($data);

if ($updated && isset($data['completed']) && $data['completed']) {
IncrementTeamCompletedTasks::dispatch($task->team_id);
}

return $updated;
return $task->update($data);
}

/**
Expand All @@ -55,9 +49,10 @@ public function updateSequence(Task $task, int $newColumnId, int $newOrder): voi
{
$oldColumnId = $task->column_id;
$oldOrder = $task->order;
$isDifferentColumn = $oldColumnId != $newColumnId;

DB::transaction(function () use ($task, $newColumnId, $newOrder, $oldColumnId, $oldOrder) {
if ($oldColumnId == $newColumnId) {
DB::transaction(function () use ($task, $newColumnId, $newOrder, $oldColumnId, $oldOrder, $isDifferentColumn) {
if (!$isDifferentColumn) {
// Moving within the same column
if ($oldOrder < $newOrder) {
Task::where('column_id', $newColumnId)
Expand All @@ -79,10 +74,17 @@ public function updateSequence(Task $task, int $newColumnId, int $newOrder): voi
->increment('order');
}

$task->update([
$updateData = [
'column_id' => $newColumnId,
'order' => $newOrder,
]);
];

// If it moved to a new column, reset its timer
if ($isDifferentColumn) {
$updateData['column_updated_at'] = now();
}

$task->update($updateData);
});
}

Expand Down
1 change: 0 additions & 1 deletion database/factories/TeamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public function definition(): array
{
return [
'name' => fake()->company(),
'count_completed_tasks' => 0,
];
}
}
38 changes: 38 additions & 0 deletions database/migrations/2026_03_22_183704_refactor_task_completion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('tasks', function (Blueprint $table) {
$table->dropColumn('completed');
$table->timestamp('column_updated_at')->nullable();
});

Schema::table('teams', function (Blueprint $table) {
$table->dropColumn('count_completed_tasks');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('tasks', function (Blueprint $table) {
$table->boolean('completed')->default(false);
$table->dropColumn('column_updated_at');
});

Schema::table('teams', function (Blueprint $table) {
$table->integer('count_completed_tasks')->default(0);
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('tasks', function (Blueprint $table) {
$table->text('description')->nullable()->after('title');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('tasks', function (Blueprint $table) {
$table->dropColumn('description');
});
}
};
27 changes: 14 additions & 13 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace Database\Seeders;

use App\Models\Task;
use App\Models\Team;
use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Team;
use App\Models\Task;
use App\Models\Column;
use Illuminate\Support\Str;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;

class DatabaseSeeder extends Seeder
{
Expand All @@ -18,7 +17,7 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
$teams = Team::factory(3)->create(['count_completed_tasks' => 0]);
$teams = Team::factory(3)->create();

$usersPerTeam = [3, 2, 1];
$userIndex = 1;
Expand All @@ -37,17 +36,19 @@ public function run(): void
$todoColumn = Column::create([
'team_id' => $team->id,
'name' => 'To Do',
'order' => 0,
'order' => 1
]);
$inProgressColumn = Column::create([

$progressColumn = Column::create([
'team_id' => $team->id,
'name' => 'In Progress',
'order' => 1,
'order' => 2
]);

$doneColumn = Column::create([
'team_id' => $team->id,
'name' => 'Done',
'order' => 2,
'order' => 3
]);

$teamUsers = User::where('team_id', $team->id)->pluck('id');
Expand All @@ -62,14 +63,14 @@ public function run(): void
$doneOrder = 0;

foreach ($tasks as $task) {
// Randomly assign to a column
$rand = rand(0, 2);
if ($rand === 0) {
$task->update(['column_id' => $todoColumn->id, 'order' => $todoOrder++, 'completed' => false]);
$task->update(['column_id' => $todoColumn->id, 'order' => $todoOrder++, 'column_updated_at' => now()]);
} elseif ($rand === 1) {
$task->update(['column_id' => $inProgressColumn->id, 'order' => $progressOrder++, 'completed' => false]);
$task->update(['column_id' => $progressColumn->id, 'order' => $progressOrder++, 'column_updated_at' => now()->subDays(rand(1, 5))]);
} else {
$task->update(['column_id' => $doneColumn->id, 'order' => $doneOrder++, 'completed' => true]);
DB::table('teams')->where('id', $team->id)->increment('count_completed_tasks');
$task->update(['column_id' => $doneColumn->id, 'order' => $doneOrder++, 'column_updated_at' => now()->subDays(rand(2, 10))]);
}
}
}
Expand Down
Loading
Loading