Files

196 lines
5.6 KiB
PHP
Raw Permalink Normal View History

2026-09-17 23:01:26 -07:00
<?php
namespace App\Models;
use App\TodoStatus;
2026-09-17 23:01:26 -07:00
use Database\Factories\TodoFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;
/**
* @property int $id
* @property int $user_id
2026-10-15 20:59:32 -07:00
* @property int|null $organization_id
2026-09-17 23:01:26 -07:00
* @property int $folder_id
* @property string $description
* @property string|null $notes
* @property TodoStatus $status
* @property int|null $position
2026-09-17 23:01:26 -07:00
* @property Carbon|null $due_date
* @property Carbon|null $completed_at
* @property Folder $folder
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*/
2026-10-15 20:59:32 -07:00
#[Fillable(['user_id', 'organization_id', 'folder_id', 'description', 'notes', 'status', 'position', 'due_date', 'completed_at'])]
2026-09-17 23:01:26 -07:00
class Todo extends Model
{
/** @use HasFactory<TodoFactory> */
use HasFactory;
2026-10-15 20:59:32 -07:00
/**
* Todos belong to a workspace through the folder they sit in. The
* organization id is filled in here for anything inserted outside
* Eloquent, which stores a column that is only nullable in the schema.
*/
protected static function booted(): void
{
static::creating(function (Todo $todo): void {
$todo->organization_id ??= Folder::query()
->whereKey($todo->folder_id)
->value('organization_id');
});
}
2026-09-17 23:01:26 -07:00
/** @return BelongsTo<User, $this> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
2026-10-15 20:59:32 -07:00
/** @return BelongsTo<Organization, $this> */
public function organization(): BelongsTo
{
return $this->belongsTo(Organization::class);
}
2026-09-17 23:01:26 -07:00
/** @return BelongsTo<Folder, $this> */
public function folder(): BelongsTo
{
return $this->belongsTo(Folder::class);
}
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'due_date' => 'date',
'completed_at' => 'datetime',
'status' => TodoStatus::class,
2026-09-17 23:01:26 -07:00
];
}
/**
* Scope the query to todos owned by the given user.
*
* @param Builder<Todo> $query
* @return Builder<Todo>
*/
public function scopeForUser(Builder $query, User|int $user): Builder
{
return $query->where('user_id', $user instanceof User ? $user->id : $user);
}
2026-10-15 20:59:32 -07:00
/**
* Scope the query to todos the given user can access, which follows the
* folder the todo sits in. Pass a workspace id to narrow the query to the
* active workspace, matching {@see Folder::scopeVisibleTo()}.
*
* @param Builder<Todo> $query
* @return Builder<Todo>
*/
public function scopeVisibleTo(Builder $query, User|int $user, ?int $workspaceId = null): Builder
{
return $query->whereIn('folder_id', Folder::query()->visibleTo($user, $workspaceId)->select('folders.id'));
}
2026-09-17 23:01:26 -07:00
/**
* Scope the query to todos that have not been completed.
*
* @param Builder<Todo> $query
* @return Builder<Todo>
*/
public function scopeIncomplete(Builder $query): Builder
{
return $query->whereNull('completed_at');
}
/**
* Scope the query to todos that have been completed.
*
* @param Builder<Todo> $query
* @return Builder<Todo>
*/
public function scopeCompleted(Builder $query): Builder
{
return $query->whereNotNull('completed_at');
}
/**
* Scope the query to todos that still need attention: not completed
* and not cancelled or deferred.
*
* @param Builder<Todo> $query
* @return Builder<Todo>
*/
public function scopeActive(Builder $query): Builder
{
return $query->incomplete()->whereNotIn('status', [
TodoStatus::Cancelled->value,
TodoStatus::Deferred->value,
TodoStatus::Completed->value,
]);
}
/**
* Scope the query to active todos whose due date has already passed.
2026-09-17 23:01:26 -07:00
*
* @param Builder<Todo> $query
* @return Builder<Todo>
*/
public function scopeOverdue(Builder $query): Builder
{
return $query->active()->whereDate('due_date', '<', now());
2026-09-17 23:01:26 -07:00
}
/**
* Scope the query to active todos due today.
2026-09-17 23:01:26 -07:00
*
* @param Builder<Todo> $query
* @return Builder<Todo>
*/
public function scopeDueToday(Builder $query): Builder
{
return $query->active()->whereDate('due_date', today());
2026-09-17 23:01:26 -07:00
}
public function scopePriority(Builder $query): Builder
{
return $query
->orderByRaw('completed_at IS NOT NULL')
->orderByRaw('position IS NULL')
->orderBy('position')
->orderByDesc('id');
}
/**
* Get the position to assign to a new todo so it appears at the top of the
2026-10-15 20:59:32 -07:00
* priority order for its folder. Ordering is shared by everyone with
* access to the folder, so the position is computed across all of the
* folder's todos regardless of who created them.
*/
2026-10-15 20:59:32 -07:00
public static function nextPositionFor(Folder $folder): int
{
2026-10-15 20:59:32 -07:00
$min = static::query()->where('folder_id', $folder->id)->min('position');
return $min === null ? 1 : $min - 1;
}
/**
* Get the number of days between creation and completion, or null when the
* todo has not been completed.
*/
public function daysToComplete(): ?int
{
return $this->completed_at === null ? null : $this->created_at->diffInDays($this->completed_at);
}
2026-09-17 23:01:26 -07:00
}