diff --git a/.env.example b/.env.example index d2c5fa3..7ac1bc6 100644 --- a/.env.example +++ b/.env.example @@ -58,12 +58,6 @@ VITE_PUSHER_PORT="${PUSHER_PORT}" VITE_PUSHER_SCHEME="${PUSHER_SCHEME}" VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" -SUPABASE_URL= -SUPABASE_API_KEY= - -ADMIN_EMAIL= -ADMIN_PASSWORD= - ASSETS_URL= WISE_API_TOKEN= diff --git a/app/Http/Controllers/Auth/AuthenticatedSessionController.php b/app/Http/Controllers/Auth/AuthenticatedSessionController.php index 56b23f5..494a106 100644 --- a/app/Http/Controllers/Auth/AuthenticatedSessionController.php +++ b/app/Http/Controllers/Auth/AuthenticatedSessionController.php @@ -8,7 +8,6 @@ use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; -use Illuminate\Validation\ValidationException; use Illuminate\View\View; class AuthenticatedSessionController extends Controller @@ -26,15 +25,7 @@ public function create(): View */ public function store(LoginRequest $request): RedirectResponse { - $credentials = config('credentials'); - - if($request->email == $credentials['email'] && $request->password == $credentials['password']) { - Auth::loginUsingId(1); - } else { - throw ValidationException::withMessages([ - 'email' => trans('auth.failed'), - ]); - } + $request->authenticate(); $request->session()->regenerate(); diff --git a/app/Http/Controllers/Dashboard.php b/app/Http/Controllers/Dashboard.php index 7c248f1..bf27219 100644 --- a/app/Http/Controllers/Dashboard.php +++ b/app/Http/Controllers/Dashboard.php @@ -9,8 +9,8 @@ class Dashboard extends Controller { public function __invoke() { + $supabaseService = new SupabaseService; $wiseService = new WiseService(config('wise.api_token'), config('wise.profile_id')); - $supabaseService = (new SupabaseService(config('supabase.api_key'), config('supabase.url'))); return Inertia::render('Dashboard', [ 'balance' => $wiseService->getBalance(), diff --git a/app/Livewire/Tokens.php b/app/Livewire/Tokens.php deleted file mode 100644 index 9571110..0000000 --- a/app/Livewire/Tokens.php +++ /dev/null @@ -1,42 +0,0 @@ -tokens = $supabaseService->getTokens(); - $this->balances = $supabaseService->getHistoricalBalances(); - - $this->dispatch('tokens-loaded'); - } - - #[On('tokens-loaded')] - public function reloadTokens() - { - $result = Http::get(config('tokens.api_url'))->object(); - - $this->tokens->shift(); - - $this->tokens->prepend( - $tokens = collect($result->balances)->sortBy(fn ($token) => $token->price * $token->balance) - ); - - $this->balances['prices'][] = $result->ethereumPrice->usd; - $this->balances['prices_eur'][] = $result->ethereumPrice->eur; - $this->balances['totals'][] = $tokens->sum(fn ($token) => $token->price * $token->balance); - $this->balances['totals_eur'][] = $tokens->sum(fn ($token) => $token->price_eur * $token->balance); - $this->balances['ethereum'][] = $tokens->sum(fn ($token) => $token->price * $token->balance / $result->ethereumPrice->usd); - } -} diff --git a/app/Models/Balance.php b/app/Models/Balance.php new file mode 100644 index 0000000..5a71f86 --- /dev/null +++ b/app/Models/Balance.php @@ -0,0 +1,10 @@ +getResult('balances?select=pool,price,price_eur,balance,parent,created_at&limit=450') - ->sortBy('pool')->groupBy('created_at')->take(30); + return Balance::orderByDesc('created_at')->limit(450)->get()->map(function ($balance) { + $balance->price = floatval($balance->price); + $balance->balance = floatval($balance->balance); + $balance->price_eur = floatval($balance->price_eur); + + return $balance; + })->sortBy('pool')->groupBy('created_at')->take(30); } public function getHistoricalBalances() { - $balances = $this->getResult('totals?select=price,price_eur,balance,created_at&limit=31') - ->reverse()->values(); + $balances = Total::orderByDesc('created_at')->limit(31)->get()->map(function ($total) { + $total->price = floatval($total->price); + $total->balance = floatval($total->balance); + $total->price_eur = floatval($total->price_eur); + + return $total; + })->reverse()->values(); return [ 'prices' => $balances->pluck('price')->toArray(), @@ -29,12 +35,7 @@ public function getHistoricalBalances() 'prices_eur' => $balances->pluck('price_eur')->toArray(), 'totals' => $balances->map(fn ($balance) => $balance->price * $balance->balance)->toArray(), 'totals_eur' => $balances->map(fn ($balance) => $balance->price_eur * $balance->balance)->toArray(), - 'dates' => $balances->map(fn ($balance) => Carbon::parse($balance->created_at)->format('M d'))->toArray() + 'dates' => $balances->map(fn ($balance) => Carbon::parse($balance->created_at)->format('M d Y'))->toArray() ]; } - - protected function getResult($uri) - { - return collect(Http::withHeaders(['apikey' => $this->apiKey])->get("{$this->url}/rest/v1/{$uri}&order=created_at.desc")->object()); - } } diff --git a/config/credentials.php b/config/credentials.php deleted file mode 100644 index 72fab4f..0000000 --- a/config/credentials.php +++ /dev/null @@ -1,6 +0,0 @@ - env('ADMIN_EMAIL'), - 'password' => env('ADMIN_PASSWORD') -]; diff --git a/config/supabase.php b/config/supabase.php deleted file mode 100644 index 46cad09..0000000 --- a/config/supabase.php +++ /dev/null @@ -1,6 +0,0 @@ - env('SUPABASE_URL'), - 'api_key' => env('SUPABASE_API_KEY'), -]; diff --git a/database/migrations/2024_01_11_120158_create_totals_table.php b/database/migrations/2024_01_11_120158_create_totals_table.php new file mode 100644 index 0000000..3e3f7de --- /dev/null +++ b/database/migrations/2024_01_11_120158_create_totals_table.php @@ -0,0 +1,30 @@ +id(); + $table->float('balance'); + $table->float('price'); + $table->float('price_eur'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('totals'); + } +}; diff --git a/database/migrations/2024_01_11_120203_create_balances_table.php b/database/migrations/2024_01_11_120203_create_balances_table.php new file mode 100644 index 0000000..25fbe35 --- /dev/null +++ b/database/migrations/2024_01_11_120203_create_balances_table.php @@ -0,0 +1,32 @@ +id(); + $table->string('pool'); + $table->float('balance'); + $table->float('price'); + $table->float('price_eur'); + $table->string('parent')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('balances'); + } +}; diff --git a/tests/Unit/SupabaseServiceTest.php b/tests/Unit/SupabaseServiceTest.php index 663e543..af955d7 100644 --- a/tests/Unit/SupabaseServiceTest.php +++ b/tests/Unit/SupabaseServiceTest.php @@ -1,11 +1,13 @@ group('supabase'); +uses(DatabaseMigrations::class)->group('supabase'); expect()->extend('toBeParsed', function () { expect(reset($this->value))->toBe($this->value[0]); @@ -14,17 +16,15 @@ }); beforeEach(function () { - $this->databaseService = new SupabaseService('fake-api-key', 'https://fake-url.supabase.co'); + $this->databaseService = new SupabaseService; - fakeRequest('https://fake-url.supabase.co/rest/v1/totals?select=price,price_eur,balance,created_at&limit=31&order=created_at.desc', 'totals'); + $totals = collect(json_decode(File::get(base_path() . "/tests/responses/totals.json"))); + + $totals->each(fn($total) => Total::create(json_decode(json_encode($total), true))); $this->balances = $this->databaseService->getHistoricalBalances(); }); -afterEach(fn () => Http::assertSent(fn (Request $request) => - $request->hasHeader('apikey', 'fake-api-key') -)); - it('retrieves_the_historical_balances', function () { expect($dates = $this->balances['dates'])->toBeParsed(); expect($totals = $this->balances['totals'])->toBeParsed(); @@ -36,14 +36,14 @@ expect(end($totals))->toBe(end($ethereum) * end($prices)); expect(end($totalsEur))->toBe(end($ethereum) * end($pricesEur)); - $lastDate = Carbon::createFromFormat('M d', end($dates)); - - expect(Carbon::parse($dates[0])->lt($lastDate))->toBetrue(); - expect(end($dates))->toStartWith($lastDate->shortMonthName)->toEndWith($lastDate->day); + expect(end($dates))->toBe('Nov 08 2023'); + expect(Carbon::parse($dates[0])->lt(Carbon::createFromFormat('M d Y', end($dates))))->toBetrue(); }); it('retrieves_the_tokens', function () { - fakeRequest('https://fake-url.supabase.co/rest/v1/balances?select=pool,price,price_eur,balance,parent,created_at&limit=450&order=created_at.desc', 'tokens'); + $tokens = collect(json_decode(File::get(base_path() . "/tests/responses/tokens.json"))); + + $tokens->each(fn($token) => Balance::create(json_decode(json_encode($token), true))); $tokens = $this->databaseService->getTokens(); @@ -52,7 +52,9 @@ expect($tokens)->toHaveCount(30); expect($weeklyTokens->first())->toHaveCount(15); expect($weeklyTokens->last())->toHaveCount(15); - expect($tokens->first()->first())->toHaveProperties(['pool', 'price', 'price_eur', 'balance', 'parent', 'created_at']); - expect(Carbon::parse($tokens->first()->first()->created_at)->isSameDay(Carbon::parse(end($this->balances['dates']))))->toBeTrue(); - expect(Carbon::parse($tokens->values()->get(1)->last()->created_at)->isSameDay(Carbon::parse(prev($this->balances['dates']))))->toBeTrue(); + + expect($tokens->first()->first()->toArray())->toHaveKeys(['id', 'pool', 'price', 'price_eur', 'balance', 'parent', 'created_at', 'updated_at']); + + expect($tokens->first()->first()->created_at->isSameDay(Carbon::parse(end($this->balances['dates']))))->toBeTrue(); + expect($tokens->values()->get(1)->last()->created_at->isSameDay(Carbon::parse(prev($this->balances['dates']))))->toBeTrue(); });