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}
This commit is contained in:
wtrinkl
2025-11-19 19:36:58 +01:00
parent c65643ac1f
commit cb495e18e3
38 changed files with 1045 additions and 823 deletions

View File

@@ -3,7 +3,7 @@
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\{GatewayUser, ApiKey, GatewayUserCredential, Budget, LlmRequest};
use App\Models\{GatewayUser, ApiKey, GatewayUserCredential, LlmRequest};
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
@@ -51,12 +51,11 @@ class AccountController extends Controller
->get()
->map(function ($key) {
return [
'id' => $key->id,
'name' => $key->name ?? 'Default Key',
'key_preview' => substr($key->api_key, 0, 8) . '...' . substr($key->api_key, -4),
'token_preview' => substr($key->token, 0, 8) . '...' . substr($key->token, -4),
'name' => $key->key_name ?? $key->key_alias ?? 'Default Key',
'alias' => $key->key_alias,
'created_at' => $key->created_at->toIso8601String(),
'last_used' => $key->last_used_at?->toIso8601String(),
'expires_at' => $key->expires_at?->toIso8601String(),
'expires_at' => $key->expires?->toIso8601String(),
];
});
@@ -65,20 +64,19 @@ class AccountController extends Controller
->where('is_active', true)
->count();
// Get budget info
$budget = Budget::where('gateway_user_id', $user->user_id)->first();
$monthlySpending = LlmRequest::where('gateway_user_id', $user->user_id)
->whereYear('created_at', now()->year)
->whereMonth('created_at', now()->month)
->where('status', 'success')
->sum('total_cost') ?? 0;
$budgetInfo = $budget ? [
'total' => round($budget->monthly_limit, 2),
'used' => round($monthlySpending, 4),
'remaining' => round($budget->monthly_limit - $monthlySpending, 4),
'currency' => 'USD',
] : null;
// Get budget info directly from gateway_user
// The gateway_users table has budget fields: monthly_budget_limit, current_month_spending
$budgetInfo = null;
if ($user->monthly_budget_limit !== null) {
$budgetInfo = [
'total' => round($user->monthly_budget_limit, 2),
'used' => round($user->current_month_spending, 4),
'remaining' => round($user->monthly_budget_limit - $user->current_month_spending, 4),
'currency' => 'USD',
'alert_threshold' => $user->budget_alert_threshold,
];
}
// Get statistics
$stats = LlmRequest::where('gateway_user_id', $user->user_id)
@@ -111,7 +109,7 @@ class AccountController extends Controller
'rate_limits' => [
'requests_per_minute' => 100, // TODO: Get from rate_limits table
'tokens_per_request' => 10000,
'daily_budget_limit' => $budget ? round($budget->monthly_limit / 30, 2) : null,
'daily_budget_limit' => $user->monthly_budget_limit ? round($user->monthly_budget_limit / 30, 2) : null,
],
],
]);