196 lines
5.6 KiB
PHP
196 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\TodoStatus;
|
|
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
|
|
* @property int|null $organization_id
|
|
* @property int $folder_id
|
|
* @property string $description
|
|
* @property string|null $notes
|
|
* @property TodoStatus $status
|
|
* @property int|null $position
|
|
* @property Carbon|null $due_date
|
|
* @property Carbon|null $completed_at
|
|
* @property Folder $folder
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
*/
|
|
#[Fillable(['user_id', 'organization_id', 'folder_id', 'description', 'notes', 'status', 'position', 'due_date', 'completed_at'])]
|
|
class Todo extends Model
|
|
{
|
|
/** @use HasFactory<TodoFactory> */
|
|
use HasFactory;
|
|
|
|
/**
|
|
* 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');
|
|
});
|
|
}
|
|
|
|
/** @return BelongsTo<User, $this> */
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/** @return BelongsTo<Organization, $this> */
|
|
public function organization(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Organization::class);
|
|
}
|
|
|
|
/** @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,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* 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'));
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*
|
|
* @param Builder<Todo> $query
|
|
* @return Builder<Todo>
|
|
*/
|
|
public function scopeOverdue(Builder $query): Builder
|
|
{
|
|
return $query->active()->whereDate('due_date', '<', now());
|
|
}
|
|
|
|
/**
|
|
* Scope the query to active todos due today.
|
|
*
|
|
* @param Builder<Todo> $query
|
|
* @return Builder<Todo>
|
|
*/
|
|
public function scopeDueToday(Builder $query): Builder
|
|
{
|
|
return $query->active()->whereDate('due_date', today());
|
|
}
|
|
|
|
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
|
|
* 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.
|
|
*/
|
|
public static function nextPositionFor(Folder $folder): int
|
|
{
|
|
$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);
|
|
}
|
|
}
|