From 358924299654b54751bf6c6974fdc9e19d84065f Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Fri, 16 May 2025 19:31:44 -0500 Subject: [PATCH] fix: dispatch time series updates --- app/Models/CurrencyRate.php | 11 ++++++- ...2_28_000001_add_multi_currency_support.php | 11 ++----- tests/MultiCurrencyTest.php | 31 +++++++++++++++++++ 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/app/Models/CurrencyRate.php b/app/Models/CurrencyRate.php index 1476143..35c1e4c 100644 --- a/app/Models/CurrencyRate.php +++ b/app/Models/CurrencyRate.php @@ -111,7 +111,7 @@ class CurrencyRate extends Model * * @return array */ - public static function timeSeriesRates(?string $currency = null, mixed $start = null, mixed $end = null): array + public static function timeSeriesRates(string|array|null $currency = null, mixed $start = null, mixed $end = null): array { if (empty($start)) { return []; @@ -132,6 +132,15 @@ class CurrencyRate extends Model return $dateRange; } + if (is_array($currency)) { + + foreach ($currency as $curr) { + dispatch(self::timeSeriesRates($curr, $start, $end)); + } + + return []; + } + // handle currency alias if (! empty($currency)) { diff --git a/database/migrations/2025_12_28_000001_add_multi_currency_support.php b/database/migrations/2025_12_28_000001_add_multi_currency_support.php index 55f78d8..477e0a8 100644 --- a/database/migrations/2025_12_28_000001_add_multi_currency_support.php +++ b/database/migrations/2025_12_28_000001_add_multi_currency_support.php @@ -97,14 +97,9 @@ return new class extends Migration '--force' => true, ]); - Holding::all()->groupBy('market_data.currency')->keys()->each( - fn ($currency) => dispatch( - function () use ($currency) { - CurrencyRate::timeSeriesRates( - $currency, - Transaction::min('date') - ); - }) + CurrencyRate::timeSeriesRates( + Holding::all()->groupBy('market_data.currency')->keys()->toArray(), + Transaction::min('date') ); CurrencyRate::refreshCurrencyData(); diff --git a/tests/MultiCurrencyTest.php b/tests/MultiCurrencyTest.php index d1107f4..04a8f2b 100644 --- a/tests/MultiCurrencyTest.php +++ b/tests/MultiCurrencyTest.php @@ -261,6 +261,37 @@ class MultiCurrencyTest extends TestCase $this->assertEquals(count($period), count($result)); } + public function test_can_get_time_series_rates_with_currencies() + { + + $start = now()->subWeeks(2); + $end = now(); + + $period = CarbonPeriod::create($start, $end); + + // mock response from Frankfurter + $results = []; + collect($period->copy()->filter('isWeekday'))->each(function ($date) use (&$results) { + $date = $date->toDateString(); + + $results[$date] = [ + 'FOO' => random_int(10, 150) / 1000, + 'BAR' => random_int(10, 150) / 1000, + ]; + }); + + Frankfurter::expects('setSymbols') + ->andReturnSelf(); + Frankfurter::expects('timeSeries') + ->andReturn(['rates' => $results]); + + $result = CurrencyRate::timeSeriesRates(null, $start, $end); + $this->assertEquals(0, count($result)); + + $result = CurrencyRate::all(); + $this->assertEquals(count($period) * 2, count($result)); + } + public function test_time_series_rate_calls_are_chunked() {