currency sync optimization

This commit is contained in:
Yuri Kuznetsov
2025-12-13 17:59:09 +02:00
parent b745adaaa4
commit 41db22a88d
2 changed files with 19 additions and 9 deletions
@@ -94,7 +94,7 @@ class RateEntryProvider
*
* @return ?numeric-string
*/
private function getRateByRecord(CurrencyRecord $record): ?string
public function getRateForRecord(CurrencyRecord $record): ?string
{
$rateEntry = $this->entityManager
->getRDBRepositoryByClass(CurrencyRecordRate::class)
@@ -120,7 +120,7 @@ class RateEntryProvider
{
$record = $this->getRecordByCode($code);
return $this->getRateByRecord($record);
return $this->getRateForRecord($record);
}
/**
@@ -35,6 +35,7 @@ use Espo\Entities\CurrencyRecord;
use Espo\ORM\EntityManager;
use Espo\ORM\Query\UpdateBuilder;
use Espo\Tools\Currency\Exceptions\NotEnabled;
use Traversable;
class RecordManager
{
@@ -122,14 +123,10 @@ class RecordManager
$rates = [];
foreach ($this->configDataProvider->getCurrencyList() as $code) {
try {
$rate = $this->rateEntryProvider->getRate($code) ?? '1.0';
} catch (Exceptions\NotEnabled) {
continue;
}
foreach ($this->getActiveCurrencyRecords() as $record) {
$rate = $this->rateEntryProvider->getRateForRecord($record) ?? '1.0';
$rates[$code] = (float) $rate;
$rates[$record->getCode()] = (float) $rate;
}
$this->configWriter->set('currencyRates', $rates);
@@ -150,4 +147,17 @@ class RecordManager
$this->configWriter->set('currencyRates', $rates);
$this->configWriter->save();
}
/**
* @return Traversable<int, CurrencyRecord>
*/
private function getActiveCurrencyRecords(): Traversable
{
return $this->entityManager
->getRDBRepositoryByClass(CurrencyRecord::class)
->where([
CurrencyRecord::FIELD_STATUS => CurrencyRecord::STATUS_ACTIVE,
])
->find();
}
}