*/ 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 */ public function user(): BelongsTo { return $this->belongsTo(User::class); } /** @return BelongsTo */ public function organization(): BelongsTo { return $this->belongsTo(Organization::class); } /** @return BelongsTo */ public function folder(): BelongsTo { return $this->belongsTo(Folder::class); } /** * Get the attributes that should be cast. * * @return array */ 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 $query * @return Builder */ 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 $query * @return Builder */ 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 $query * @return Builder */ public function scopeIncomplete(Builder $query): Builder { return $query->whereNull('completed_at'); } /** * Scope the query to todos that have been completed. * * @param Builder $query * @return Builder */ 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 $query * @return Builder */ 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 $query * @return Builder */ public function scopeOverdue(Builder $query): Builder { return $query->active()->whereDate('due_date', '<', now()); } /** * Scope the query to active todos due today. * * @param Builder $query * @return Builder */ 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); } }