+40
-4
@@ -14,6 +14,7 @@ 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
|
||||
@@ -25,18 +26,38 @@ use Illuminate\Support\Carbon;
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*/
|
||||
#[Fillable(['user_id', 'folder_id', 'description', 'notes', 'status', 'position', 'due_date', 'completed_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
|
||||
{
|
||||
@@ -68,6 +89,19 @@ class Todo extends Model
|
||||
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.
|
||||
*
|
||||
@@ -139,11 +173,13 @@ class Todo extends Model
|
||||
|
||||
/**
|
||||
* Get the position to assign to a new todo so it appears at the top of the
|
||||
* priority order for the given user.
|
||||
* 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(User|int $user): int
|
||||
public static function nextPositionFor(Folder $folder): int
|
||||
{
|
||||
$min = static::query()->forUser($user)->min('position');
|
||||
$min = static::query()->where('folder_id', $folder->id)->min('position');
|
||||
|
||||
return $min === null ? 1 : $min - 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user