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.

3.9 KB
Raw Blame
<?php

declare(strict_types=1);

namespace App\Models;

use Database\Factories\OfferFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class Offer extends Model
{
    /** @use HasFactory<OfferFactory> */
    use HasFactory, HasUuids;

    protected $fillable = [
        'slug',
        'stripe_coupon_id',
        'trial_days',
        'allow_promotion_codes',
        'requires_card',
        'starts_at',
        'ends_at',
        'max_redemptions',
        'first_subscription_only',
        'show_banner',
    ];

    protected function casts(): array
    {
        return [
            'starts_at' => 'datetime',
            'ends_at' => 'datetime',
            'trial_days' => 'integer',
            'allow_promotion_codes' => 'boolean',
            'requires_card' => 'boolean',
            'max_redemptions' => 'integer',
            'first_subscription_only' => 'boolean',
            'show_banner' => 'boolean',
        ];
    }

    public function accounts(): BelongsToMany
    {
        return $this->belongsToMany(Account::class)->withPivot('claimed_at', 'redeemed_at');
    }

    /**
     * The accounts that redeemed the offer. Each one uses up a place.
     */
    public function redemptions(): BelongsToMany
    {
        return $this->accounts()->wherePivotNotNull('redeemed_at');
    }

    /**
     * Started, not ended, and with a place left. Only redemptions use up a
     * place; a claim alone never does.
     */
    public function scopeAvailable(Builder $query): Builder
    {
        return $query
            ->whereNowOrPast('offers.starts_at')
            ->where(fn (Builder $query): Builder => $query->whereNull('offers.ends_at')->orWhereFuture('offers.ends_at'))
            ->where(fn (Builder $query): Builder => $query->whereNull('offers.max_redemptions')->orHas('redemptions', '<', DB::raw('offers.max_redemptions')));
    }

    /**
     * Available offers the account may still use: never one it redeemed, and
     * a first-subscription offer only while it has never subscribed or
     * started a trial.
     */
    public function scopeAvailableTo(Builder $query, Account $account): Builder
    {
        return $query
            ->available()
            ->whereDoesntHave('redemptions', fn (Builder $query): Builder => $query->whereKey($account->id))
            ->when(! $account->isFirstTimeSubscriber(), fn (Builder $query): Builder => $query->where('offers.first_subscription_only', false));
    }

    /**
     * A trial that needs no card starts in the app (Cashier's generic trial)
     * instead of at Stripe Checkout.
     */
    public function startsWithoutCard(): bool
    {
        return ! $this->requires_card && $this->hasTrial();
    }

    public function hasTrial(): bool
    {
        return $this->trial_days > 0;
    }

    /**
     * Offers without a trial: a coupon, the promotion code field, or both.
     */
    public function scopeWithoutTrial(Builder $query): Builder
    {
        return $query->where(fn (Builder $query): Builder => $query->whereNull('offers.trial_days')->orWhere('offers.trial_days', 0));
    }

    /**
     * The available offer with exactly this slug, for an account when one is
     * given. MySQL compares strings without case, so the match is confirmed
     * here, where both engines agree.
     */
    public static function findAvailable(string $slug, ?Account $account = null): ?self
    {
        $offers = filled($account) ? self::availableTo($account) : self::available();

        return $offers->where('slug', $slug)->get()->firstWhere('slug', $slug);
    }

    /**
     * The key of the offer's copy in lang/<locale>/offers.php.
     */
    public function langKey(): string
    {
        return Str::replace('-', '_', $this->slug);
    }
}