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

@@ -0,0 +1,45 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('gateway_users', function (Blueprint $table) {
// Add new budget fields
$table->decimal('monthly_budget_limit', 10, 2)->nullable()->after('alias');
$table->decimal('current_month_spending', 10, 2)->default(0)->after('monthly_budget_limit');
$table->integer('budget_alert_threshold')->nullable()->after('current_month_spending')->comment('Alert when spending reaches X% of budget');
$table->integer('rate_limit_per_hour')->default(60)->after('budget_alert_threshold');
// Remove old budget fields
$table->dropColumn(['spend', 'budget_id']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('gateway_users', function (Blueprint $table) {
// Restore old fields
$table->string('budget_id')->nullable()->after('alias');
$table->decimal('spend', 10, 2)->default(0)->after('budget_id');
// Remove new fields
$table->dropColumn([
'monthly_budget_limit',
'current_month_spending',
'budget_alert_threshold',
'rate_limit_per_hour'
]);
});
}
};

View File

@@ -0,0 +1,50 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('gateway_user_credentials', function (Blueprint $table) {
$table->id();
$table->string('gateway_user_id');
$table->string('provider')->comment('openai, anthropic, google, deepseek, mistral');
$table->text('api_key')->comment('Encrypted API key');
$table->string('organization_id')->nullable();
$table->boolean('is_active')->default(true);
$table->timestamp('last_used_at')->nullable();
$table->timestamp('last_tested_at')->nullable();
$table->string('test_status')->nullable()->comment('success, failed');
$table->text('test_error')->nullable();
$table->timestamps();
// Foreign key to gateway_users
$table->foreign('gateway_user_id')
->references('user_id')
->on('gateway_users')
->onDelete('cascade');
// Unique constraint: one credential per provider per gateway user
$table->unique(['gateway_user_id', 'provider']);
// Indexes for performance
$table->index('gateway_user_id');
$table->index('provider');
$table->index('is_active');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('gateway_user_credentials');
}
};

View File

@@ -0,0 +1,61 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('usage_logs', function (Blueprint $table) {
// Rename user_id to gateway_user_id for clarity
$table->renameColumn('user_id', 'gateway_user_id');
// Add missing columns if they don't exist
if (!Schema::hasColumn('usage_logs', 'request_payload')) {
$table->json('request_payload')->nullable()->after('metadata');
}
if (!Schema::hasColumn('usage_logs', 'response_payload')) {
$table->json('response_payload')->nullable()->after('request_payload');
}
if (!Schema::hasColumn('usage_logs', 'response_time_ms')) {
$table->integer('response_time_ms')->nullable()->after('response_payload');
}
if (!Schema::hasColumn('usage_logs', 'ip_address')) {
$table->string('ip_address', 45)->nullable()->after('response_time_ms');
}
if (!Schema::hasColumn('usage_logs', 'user_agent')) {
$table->string('user_agent')->nullable()->after('ip_address');
}
if (!Schema::hasColumn('usage_logs', 'status')) {
$table->string('status')->default('success')->after('user_agent')->comment('success, error, timeout');
}
if (!Schema::hasColumn('usage_logs', 'error_message')) {
$table->text('error_message')->nullable()->after('status');
}
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('usage_logs', function (Blueprint $table) {
// Rename back
$table->renameColumn('gateway_user_id', 'user_id');
// Remove added columns
$columns = ['request_payload', 'response_payload', 'response_time_ms', 'ip_address', 'user_agent', 'status', 'error_message'];
foreach ($columns as $column) {
if (Schema::hasColumn('usage_logs', $column)) {
$table->dropColumn($column);
}
}
});
}
};

View File

@@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('api_keys', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropIndex('api_keys_user_id_index');
$table->renameColumn('user_id', 'gateway_user_id');
});
Schema::table('api_keys', function (Blueprint $table) {
$table->index('gateway_user_id');
$table->foreign('gateway_user_id')->references('user_id')->on('gateway_users')->onDelete('cascade');
});
}
public function down(): void
{
Schema::table('api_keys', function (Blueprint $table) {
$table->dropForeign(['gateway_user_id']);
$table->dropIndex('api_keys_gateway_user_id_index');
$table->renameColumn('gateway_user_id', 'user_id');
});
Schema::table('api_keys', function (Blueprint $table) {
$table->index('user_id');
$table->foreign('user_id')->references('user_id')->on('gateway_users')->onDelete('cascade');
});
}
};

View File

@@ -0,0 +1,56 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
* Drop budgets and user_budgets tables as budget functionality
* is now integrated directly in gateway_users table.
*/
public function up(): void
{
Schema::dropIfExists('budgets');
Schema::dropIfExists('user_budgets');
}
/**
* Reverse the migrations.
*/
public function down(): void
{
// Recreate user_budgets table
Schema::create('user_budgets', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id')->unique();
$table->decimal('monthly_limit', 10, 2)->default(0);
$table->decimal('daily_limit', 10, 2)->nullable();
$table->decimal('current_month_spending', 10, 2)->default(0)->index();
$table->decimal('current_day_spending', 10, 2)->default(0);
$table->date('month_started_at');
$table->date('day_started_at');
$table->unsignedInteger('alert_threshold_percentage')->default(80);
$table->timestamp('last_alert_sent_at')->nullable();
$table->boolean('is_budget_exceeded')->default(false)->index();
$table->boolean('is_active')->default(true)->index();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
// Recreate budgets table
Schema::create('budgets', function (Blueprint $table) {
$table->string('budget_id')->primary();
$table->string('name');
$table->decimal('monthly_limit', 10, 2)->nullable();
$table->decimal('daily_limit', 10, 2)->nullable();
$table->string('created_by')->nullable();
$table->timestamps();
$table->index('name');
});
}
};

View File

@@ -0,0 +1,70 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*
* Rename user_id to gateway_user_id and change type from bigint to varchar(255)
*/
public function up(): void
{
// Step 1: Drop existing constraints and indexes
Schema::table('llm_requests', function (Blueprint $table) {
// Drop foreign key if exists (might not exist)
try {
$table->dropForeign(['user_id']);
} catch (\Exception $e) {
// Foreign key might not exist, that's okay
}
// Drop index
$table->dropIndex(['user_id']);
});
// Step 2: Change column type and rename using raw SQL
// We can't do both in one operation with Laravel's schema builder
DB::statement('ALTER TABLE llm_requests MODIFY user_id VARCHAR(255) NOT NULL');
DB::statement('ALTER TABLE llm_requests CHANGE user_id gateway_user_id VARCHAR(255) NOT NULL');
// Step 3: Add new foreign key and index
Schema::table('llm_requests', function (Blueprint $table) {
$table->index('gateway_user_id');
$table->foreign('gateway_user_id')
->references('user_id')
->on('gateway_users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
// Drop foreign key and index
Schema::table('llm_requests', function (Blueprint $table) {
$table->dropForeign(['gateway_user_id']);
$table->dropIndex(['gateway_user_id']);
});
// Rename back and change type
DB::statement('ALTER TABLE llm_requests CHANGE gateway_user_id user_id BIGINT(20) UNSIGNED NOT NULL');
// Restore original index and foreign key
Schema::table('llm_requests', function (Blueprint $table) {
$table->index('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
};