refactor(account): move trial-end-date logic into Account::activeTrialEndsAt()

Centralizes 'what date should the UI show as trial end?' on the model.
Returns null when not on trial, the subscription's trial date when on
trial-with-card, or the generic trial date for no-card users.
This commit is contained in:
Paulo Castellano 2026-05-14 19:55:28 -03:00
parent 5c03f9cd11
commit fc90861a96
2 changed files with 17 additions and 1 deletions

View file

@ -139,7 +139,7 @@ public function index(Request $request): Response|RedirectResponse
return Inertia::render('settings/account/Billing', [
'hasSubscription' => $account->subscribed(Account::SUBSCRIPTION_NAME),
'onTrial' => $account->isOnTrial(),
'trialEndsAt' => $subscription?->trial_ends_at ?? $account->trial_ends_at,
'trialEndsAt' => $account->activeTrialEndsAt(),
'subscription' => $subscription?->only([
'stripe_status',
'ends_at',

View file

@ -9,6 +9,7 @@
use App\Features\SocialAccountLimit;
use App\Features\WorkspaceLimit;
use App\Models\Traits\HasUsage;
use Carbon\CarbonInterface;
use Database\Factories\AccountFactory;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@ -90,6 +91,21 @@ public function isOnTrial(): bool
return (bool) $this->subscription(self::SUBSCRIPTION_NAME)?->onTrial();
}
/**
* The end date of whichever trial is currently active (subscription trial
* or generic trial), or null if the account is not on any trial.
*/
public function activeTrialEndsAt(): ?CarbonInterface
{
$subscription = $this->subscription(self::SUBSCRIPTION_NAME);
return match (true) {
(bool) $subscription?->onTrial() => $subscription->trial_ends_at,
$this->onGenericTrial() => $this->trial_ends_at,
default => null,
};
}
/**
* Returns the displayable card for the billing UI. Falls back to the first
* attached payment method when the customer has no `invoice_settings.default_payment_method`