Files
laravel-llm-gateway/laravel-app/app/Models/GatewayUser.php
wtrinkl cb495e18e3 Fix API controllers to use correct database column names
- Fix model_pricing table references (model_id -> model, display_name -> model)
- Fix price columns (output_price_per_1k -> output_price_per_million)
- Add price conversion (per_million / 1000 = per_1k) in all API responses
- Add whereNotNull('model') filters to exclude invalid entries
- Add getModelDisplayName() helper method to all controllers
- Fix AccountController to use gateway_users budget fields directly
- Remove Budget model dependencies from AccountController
- Add custom Scramble server URL configuration for API docs
- Create ScrambleServiceProvider to set correct /api prefix
- Add migration to rename user_id to gateway_user_id in llm_requests
- Add custom ApiGuard for gateway_users authentication
- Update all API controllers: AccountController, ModelController, PricingController, ProviderController

All API endpoints now working correctly:
- GET /api/account
- GET /api/models
- GET /api/pricing
- GET /api/providers/{provider}
2025-11-19 19:36:58 +01:00

108 lines
2.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticatableTrait;
class GatewayUser extends Model implements Authenticatable
{
use HasFactory, AuthenticatableTrait;
protected $table = 'gateway_users';
protected $primaryKey = 'user_id';
public $incrementing = false;
protected $keyType = 'string';
protected $fillable = [
'user_id',
'alias',
'monthly_budget_limit',
'current_month_spending',
'budget_alert_threshold',
'rate_limit_per_hour',
'blocked',
'metadata',
];
protected $casts = [
'metadata' => 'array',
'blocked' => 'boolean',
'monthly_budget_limit' => 'decimal:2',
'current_month_spending' => 'decimal:2',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
// Relationships
public function apiKeys()
{
return $this->hasMany(ApiKey::class, 'gateway_user_id', 'user_id');
}
public function credentials()
{
return $this->hasMany(GatewayUserCredential::class, 'user_id', 'user_id');
}
public function usageLogs()
{
return $this->hasMany(UsageLog::class, 'gateway_user_id', 'user_id');
}
// Scopes
public function scopeActive($query)
{
return $query->where('blocked', false);
}
public function scopeBlocked($query)
{
return $query->where('blocked', true);
}
// Helper methods for budget management
public function isBlocked(): bool
{
return $this->blocked;
}
public function hasExceededBudget(): bool
{
if (!$this->monthly_budget_limit) {
return false;
}
return $this->current_month_spending >= $this->monthly_budget_limit;
}
public function incrementSpending(float $amount): void
{
$this->increment('current_month_spending', $amount);
}
public function resetMonthlySpending(): void
{
$this->update(['current_month_spending' => 0]);
}
public function getBudgetUsagePercentage(): ?float
{
if (!$this->monthly_budget_limit || $this->monthly_budget_limit == 0) {
return null;
}
return ($this->current_month_spending / $this->monthly_budget_limit) * 100;
}
public function shouldSendBudgetAlert(): bool
{
if (!$this->budget_alert_threshold || !$this->monthly_budget_limit) {
return false;
}
$percentage = $this->getBudgetUsagePercentage();
return $percentage !== null && $percentage >= $this->budget_alert_threshold;
}
}