feat(models): add core domain models
Creates Workspace, WorkspaceMember, WorkspaceInvitation, Link, LinkVariant, Click (append-only), Plan, and Domain Eloquent models with correct relationships. Updates User model with SoftDeletes, ULID auto-generation on create, guarded/casts, and workspace relationships. Fixes ProfileTest assertion to match SoftDeletes behavior on User. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>main
parent
3b19bbe88f
commit
d0bb8f92ee
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Analytics\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Click extends Model
|
||||||
|
{
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected $guarded = [];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'clicked_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
// Prevent updates — clicks are append-only
|
||||||
|
public function save(array $options = []): bool
|
||||||
|
{
|
||||||
|
if ($this->exists) {
|
||||||
|
throw new \LogicException('Click records are append-only.');
|
||||||
|
}
|
||||||
|
return parent::save($options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function link()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(\App\Domains\Link\Models\Link::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Domain\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class Domain extends Model
|
||||||
|
{
|
||||||
|
protected $guarded = ['id', 'workspace_id'];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'verified_at' => 'datetime',
|
||||||
|
'ssl_issued_at' => 'datetime',
|
||||||
|
'ssl_expires_at' => 'datetime',
|
||||||
|
'is_default' => 'boolean',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function workspace(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(\App\Domains\Workspace\Models\Workspace::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isVerified(): bool
|
||||||
|
{
|
||||||
|
return $this->verified_at !== null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Link\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class Link extends Model
|
||||||
|
{
|
||||||
|
use SoftDeletes;
|
||||||
|
|
||||||
|
protected $guarded = ['id', 'workspace_id', 'created_by'];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'rules' => 'array',
|
||||||
|
'pixel_config' => 'array',
|
||||||
|
'tags' => 'array',
|
||||||
|
'expires_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function workspace(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(\App\Domains\Workspace\Models\Workspace::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function domain(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(\App\Domains\Domain\Models\Domain::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function variants(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(LinkVariant::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clicks(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(\App\Domains\Analytics\Models\Click::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isActive(): bool
|
||||||
|
{
|
||||||
|
if ($this->status !== 'active') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ($this->expires_at && $this->expires_at->isPast()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Link\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class LinkVariant extends Model
|
||||||
|
{
|
||||||
|
protected $guarded = ['id'];
|
||||||
|
|
||||||
|
public function link(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Link::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Subscription\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
|
||||||
|
class Plan extends Model
|
||||||
|
{
|
||||||
|
protected $guarded = ['id'];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'features' => 'array',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function workspaces(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(\App\Domains\Workspace\Models\Workspace::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Workspace\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class Workspace extends Model
|
||||||
|
{
|
||||||
|
use SoftDeletes;
|
||||||
|
|
||||||
|
protected $guarded = ['id', 'owner_id'];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'branding' => 'array',
|
||||||
|
'stripe_customer_metadata' => 'array',
|
||||||
|
'trial_ends_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function owner(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(\App\Models\User::class, 'owner_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function members(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(WorkspaceMember::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function invitations(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(WorkspaceInvitation::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function links(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(\App\Domains\Link\Models\Link::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function domains(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(\App\Domains\Domain\Models\Domain::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function plan(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(\App\Domains\Subscription\Models\Plan::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Workspace\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class WorkspaceInvitation extends Model
|
||||||
|
{
|
||||||
|
protected $guarded = ['id'];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'expires_at' => 'datetime',
|
||||||
|
'accepted_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function workspace(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Workspace::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function invitedBy(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(\App\Models\User::class, 'invited_by');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isExpired(): bool
|
||||||
|
{
|
||||||
|
return $this->expires_at->isPast();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isAccepted(): bool
|
||||||
|
{
|
||||||
|
return $this->accepted_at !== null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Workspace\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class WorkspaceMember extends Model
|
||||||
|
{
|
||||||
|
protected $guarded = ['id'];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'joined_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function workspace(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Workspace::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(\App\Models\User::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,24 +5,25 @@ namespace App\Models;
|
||||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||||
use Database\Factories\UserFactory;
|
use Database\Factories\UserFactory;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
|
|
||||||
class User extends Authenticatable
|
class User extends Authenticatable
|
||||||
{
|
{
|
||||||
/** @use HasFactory<UserFactory> */
|
/** @use HasFactory<UserFactory> */
|
||||||
use HasFactory, Notifiable;
|
use HasFactory, Notifiable, SoftDeletes;
|
||||||
|
|
||||||
/**
|
protected $guarded = ['id'];
|
||||||
* The attributes that are mass assignable.
|
|
||||||
*
|
protected static function booted(): void
|
||||||
* @var list<string>
|
{
|
||||||
*/
|
static::creating(function (self $user) {
|
||||||
protected $fillable = [
|
if (empty($user->ulid)) {
|
||||||
'name',
|
$user->ulid = \Illuminate\Support\Str::ulid();
|
||||||
'email',
|
}
|
||||||
'password',
|
});
|
||||||
];
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes that should be hidden for serialization.
|
* The attributes that should be hidden for serialization.
|
||||||
|
|
@ -43,7 +44,23 @@ class User extends Authenticatable
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'email_verified_at' => 'datetime',
|
'email_verified_at' => 'datetime',
|
||||||
|
'two_factor_confirmed_at' => 'datetime',
|
||||||
'password' => 'hashed',
|
'password' => 'hashed',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function workspaces()
|
||||||
|
{
|
||||||
|
return $this->hasMany(\App\Domains\Workspace\Models\Workspace::class, 'owner_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function defaultWorkspace()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(\App\Domains\Workspace\Models\Workspace::class, 'default_workspace_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function workspaceMemberships()
|
||||||
|
{
|
||||||
|
return $this->hasMany(\App\Domains\Workspace\Models\WorkspaceMember::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ test('user can delete their account', function () {
|
||||||
->assertRedirect('/');
|
->assertRedirect('/');
|
||||||
|
|
||||||
$this->assertGuest();
|
$this->assertGuest();
|
||||||
$this->assertNull($user->fresh());
|
$this->assertNotNull($user->fresh()->deleted_at);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('correct password must be provided to delete account', function () {
|
test('correct password must be provided to delete account', function () {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue