Implement multi-tenancy support with OAuth2 authentication for tenant admins, Stripe integration for event purchases and credits ledger, new Filament resources for event purchases, updated API routes and middleware for tenant isolation and token guarding, added factories/seeders/migrations for new models (Tenant, EventPurchase, OAuth entities, etc.), enhanced tests, and documentation updates. Removed outdated DemoAchievementsSeeder.
This commit is contained in:
@@ -2,17 +2,30 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Models\EventPurchase;
|
||||
use App\Models\EventCreditsLedger;
|
||||
|
||||
class Tenant extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'tenants';
|
||||
protected $guarded = [];
|
||||
protected $casts = [
|
||||
'name' => 'array',
|
||||
'settings' => 'array',
|
||||
'features' => 'array',
|
||||
'last_activity_at' => 'datetime',
|
||||
'event_credits_balance' => 'integer',
|
||||
'subscription_tier' => 'string',
|
||||
'subscription_expires_at' => 'datetime',
|
||||
'total_revenue' => 'decimal:2',
|
||||
];
|
||||
|
||||
public function events(): HasMany
|
||||
@@ -31,4 +44,60 @@ class Tenant extends Model
|
||||
'id' // Local key on events table...
|
||||
);
|
||||
}
|
||||
|
||||
public function purchases(): HasMany
|
||||
{
|
||||
return $this->hasMany(PurchaseHistory::class);
|
||||
}
|
||||
|
||||
public function eventPurchases(): HasMany
|
||||
{
|
||||
return $this->hasMany(EventPurchase::class);
|
||||
}
|
||||
|
||||
public function creditsLedger(): HasMany
|
||||
{
|
||||
return $this->hasMany(EventCreditsLedger::class);
|
||||
}
|
||||
|
||||
public function activeSubscription(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn () => $this->subscription_expires_at && $this->subscription_expires_at->isFuture(),
|
||||
);
|
||||
}
|
||||
|
||||
public function decrementCredits(int $amount, string $reason = 'event_create', ?string $note = null, ?int $relatedPurchaseId = null): bool
|
||||
{
|
||||
if ($this->event_credits_balance < $amount) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($amount, $reason, $note, $relatedPurchaseId) {
|
||||
EventCreditsLedger::create([
|
||||
'tenant_id' => $this->id,
|
||||
'delta' => -$amount,
|
||||
'reason' => $reason,
|
||||
'related_purchase_id' => $relatedPurchaseId,
|
||||
'note' => $note,
|
||||
]);
|
||||
|
||||
return $this->decrement('event_credits_balance', $amount);
|
||||
});
|
||||
}
|
||||
|
||||
public function incrementCredits(int $amount, string $reason = 'manual_adjust', ?string $note = null, ?int $relatedPurchaseId = null): bool
|
||||
{
|
||||
return DB::transaction(function () use ($amount, $reason, $note, $relatedPurchaseId) {
|
||||
EventCreditsLedger::create([
|
||||
'tenant_id' => $this->id,
|
||||
'delta' => $amount,
|
||||
'reason' => $reason,
|
||||
'related_purchase_id' => $relatedPurchaseId,
|
||||
'note' => $note,
|
||||
]);
|
||||
|
||||
return $this->increment('event_credits_balance', $amount);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user