Skip to content

Commit 689aa4d

Browse files
committed
fix: multi currency seeders
1 parent 26370c0 commit 689aa4d

6 files changed

Lines changed: 66 additions & 225 deletions

File tree

app/Models/CurrencyRate.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static function historic(string $currency, mixed $date = null): float
111111
*
112112
* @return array<string, float>
113113
*/
114-
public static function timeSeriesRates(string|array $currency, mixed $start = null, mixed $end = null): array
114+
public static function timeSeriesRates(?string $currency = null, mixed $start = null, mixed $end = null): array
115115
{
116116
if (empty($start)) {
117117
return [];
@@ -132,19 +132,18 @@ public static function timeSeriesRates(string|array $currency, mixed $start = nu
132132
return $dateRange;
133133
}
134134

135-
[$currency, $adjustment] = self::getCurrencyAliasAdjustments($currency);
136-
135+
// handle currency alias
137136
if (! empty($currency)) {
138137

139-
$currencies = Arr::wrap($currency);
138+
[$currency, $adjustment] = self::getCurrencyAliasAdjustments($currency);
140139

141140
} else {
142141

143-
$currencies = Currency::all()->pluck('currency')->toArray();
142+
$currency = Currency::all()->pluck('currency')->toArray();
144143
}
145144

146145
// get rates
147-
$rates = Frankfurter::setSymbols($currencies)->timeSeries($period->first(), $period->last());
146+
$rates = Frankfurter::setSymbols($currency)->timeSeries($period->first(), $period->last());
148147

149148
$rates = collect(Arr::get($rates, 'rates', []))->sortKeys()->toArray();
150149

@@ -177,13 +176,18 @@ public static function timeSeriesRates(string|array $currency, mixed $start = nu
177176
// persist
178177
self::chunkInsert($updates);
179178

180-
return collect($updates)
181-
->whereBetween('date', [$start, $end ?? now()])
182-
->where('currency', $currency)
183-
->mapWithKeys(fn ($rate) => [
184-
$rate['date'] => $rate['rate'] * $adjustment,
185-
])
186-
->toArray();
179+
if (is_string($currency)) {
180+
181+
return collect($updates)
182+
->whereBetween('date', [$start, $end ?? now()])
183+
->where('currency', $currency)
184+
->mapWithKeys(fn ($rate) => [
185+
$rate['date'] => $rate['rate'] * ($adjustment ?? 1),
186+
])
187+
->toArray();
188+
}
189+
190+
return [];
187191
}
188192

189193
private static function getNearestPastDate(CarbonInterface $date, array $datesOnly, array $rates): ?CarbonInterface
@@ -265,7 +269,7 @@ public static function chunkInsert(array $updates): void
265269
}
266270
}
267271

268-
protected static function getCurrencyAliasAdjustments($currency)
272+
protected static function getCurrencyAliasAdjustments(string $currency)
269273
{
270274
$adjustment = 1;
271275

database/migrations/2025_12_28_000001_add_multi_currency_support.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,14 @@ public function up(): void
9797
'--force' => true,
9898
]);
9999

100-
CurrencyRate::timeSeriesRates(
101-
Holding::all()->groupBy('market_data.currency')->keys()->toArray(),
102-
Transaction::min('date')
100+
Holding::all()->groupBy('market_data.currency')->keys()->each(
101+
fn ($currency) => dispatch(
102+
function () use ($currency) {
103+
CurrencyRate::timeSeriesRates(
104+
$currency,
105+
Transaction::min('date')
106+
);
107+
})
103108
);
104109

105110
CurrencyRate::refreshCurrencyData();

database/seeders/MarketDataSeeder.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public function run(): void
5454
$rowCount++;
5555

5656
if ($rowCount % $chunkSize == 0) {
57-
DB::table('market_data')->upsert($this->rows, ['symbol'], ['name', 'currency', 'meta_data']);
58-
$this->rows = [];
57+
$this->bulkInsert($this->rows);
5958
}
6059
}
6160
}
@@ -77,11 +76,14 @@ public function run(): void
7776
}
7877
}
7978

80-
public function bulkInsert(array $rows)
79+
public function bulkInsert($rows)
8180
{
8281
try {
8382

84-
DB::table('market_data')->insertOrIgnore($rows);
83+
dispatch(
84+
fn () => DB::table('market_data')->upsert($rows, ['symbol'], ['name', 'currency', 'meta_data'])
85+
);
86+
8587
$this->rows = [];
8688

8789
} catch (\Throwable $e) {

0 commit comments

Comments
 (0)