fix: dispatch time series updates

This commit is contained in:
hackerESQ
2025-05-16 19:31:44 -05:00
parent 689aa4d50b
commit 3589242996
3 changed files with 44 additions and 9 deletions
+10 -1
View File
@@ -111,7 +111,7 @@ class CurrencyRate extends Model
*
* @return array<string, float>
*/
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)) {
@@ -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();
+31
View File
@@ -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()
{