refactor: extract rate formatting logic to RateService and add tests

هذا الالتزام موجود في:
2025-05-31 15:22:50 +03:00
الأصل b19748ef20
التزام cc4e74f3f1
13 ملفات معدلة مع 278 إضافات و56 حذوفات

عرض الملف

@@ -41,9 +41,9 @@ class FetchCurrencyCommand extends Command
'SAR' => Currency::where('code', 'SAR')->first(),
};
$rate = Rate::updateOrCreate([
'city_id' => $city->id,
'currency_id' => $currency->id,
Rate::updateOrCreate([
'city_id' => $city?->id,
'currency_id' => $currency?->id,
'date' => $item['date'],
], [
'buy_price' => $item['price_buy'],
@@ -61,6 +61,8 @@ class FetchCurrencyCommand extends Command
'trace' => $e->getTraceAsString(),
]);
// todo mail when failing using resend
return Command::FAILURE;
}
}
@@ -110,7 +112,7 @@ class FetchCurrencyCommand extends Command
if (empty($data)) {
$this->warn('No historical currency data available.');
return;
return [];
}
$this->info('Last 20 Days Currency Data:');

عرض الملف

@@ -4,31 +4,16 @@ namespace App\Http\Controllers;
use App\Models\City;
use App\Models\Rate;
use App\Services\RateService;
class RateController extends Controller
{
public function __invoke()
public function __invoke( RateService $rateService)
{
$rates = $rateService->getFormattedRates();
$supportedCities = City::query()
->whereIn('name', City::supportedCities())
->get()
->sortByDesc(fn ($city) => $city->name === 'sanaa')
->values();
$rates = Rate::query()
->with('currency', 'city')
->orderBy('city_id', 'asc')
->orderBy('currency_id', 'asc')
->orderBy('date', 'desc')
->get()
->groupBy(fn ($rate) => $rate->city_id.'-'.$rate->currency_id)
->map(fn ($group) => $group->first())
->groupBy('city_id');
return view('rates', [
'rates' => $rates,
'supportedCities' => $supportedCities,
return view('rates', [
'rates' => $rates,
]);
}
}

عرض الملف

@@ -2,6 +2,11 @@
namespace App\Providers;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@@ -19,6 +24,12 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot(): void
{
//
DB::prohibitDestructiveCommands(app()->isProduction());
URL::forceHttps(app()->isProduction());
Model::shouldBeStrict(! app()->isProduction());
Date::use(CarbonImmutable::class);
}
}

عرض الملف

@@ -0,0 +1,51 @@
<?php
namespace App\Services;
use App\Models\City;
use App\Models\Rate;
class RateService
{
public function getFormattedRates(): array
{
$supportedCities = City::query()
->whereIn('name', City::supportedCities()) // Filter only supported cities
->get()
->sortByDesc(fn($city) => $city->name === 'sanaa') // Sort to put 'sanaa' first
->values(); // Reset array keys to be sequential
// Get the latest exchange rates for each currency in each city
$latestRates = Rate::query()
->with('currency', 'city')
->orderBy('city_id', 'asc')
->orderBy('currency_id', 'asc')
->orderBy('date', 'desc')
->get()
->groupBy(fn($rate) => $rate->city_id . '-' . $rate->currency_id)
->map(fn($group) => $group->first())
->groupBy('city_id');
// Format the rates
$rates = $supportedCities->map(function ($city) use ($latestRates) {
$cityRates = $latestRates->get($city->id, collect())->map(function ($rate) {
return [
'currency' => $rate->currency->name,
'buy_price' => $rate->buy_price,
'sell_price' => $rate->sell_price,
'date' => $rate->date->toDateString(),
'day' => $rate->date->locale('ar')->isoFormat('dddd'),
'last_update' => $rate->updated_at->diffForHumans(),
];
})->values()->toArray();
return [
'city' => $city->label ,
'rates' => $cityRates,
];
})->toArray();
return $rates;
}
}