Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

1.9 KB
Raw Blame
<?php

declare(strict_types=1);

namespace App\Models;

use Database\Factories\InviteFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;

class Invite extends Model
{
    /** @use HasFactory<InviteFactory> */
    use HasFactory, HasUuids;

    protected $fillable = [
        'account_id',
        'invited_by',
        'email',
        'is_admin',
        'requires_approval',
        'workspaces',
        'accepted_at',
    ];

    protected function casts(): array
    {
        return [
            'is_admin' => 'boolean',
            'requires_approval' => 'boolean',
            'workspaces' => 'array',
            'accepted_at' => 'datetime',
        ];
    }

    protected function email(): Attribute
    {
        return Attribute::make(
            set: fn (?string $value): ?string => $value === null ? null : Str::lower($value),
        );
    }

    public function scopeForEmail(Builder $query, string $email): void
    {
        $query->where('email', Str::lower($email));
    }

    public function isFor(?string $email): bool
    {
        return $email !== null && Str::lower($this->email) === Str::lower($email);
    }

    public function account(): BelongsTo
    {
        return $this->belongsTo(Account::class);
    }

    public function invitedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'invited_by');
    }

    /**
     * `id` is a native Postgres uuid column — find() on a non-UUID string
     * raises a type-cast error rather than returning null.
     */
    public static function fromId(?string $id): ?self
    {
        if (! $id || ! Str::isUuid($id)) {
            return null;
        }

        return self::find($id);
    }
}