Files
investbrain/app/Actions/EnsureDailyChangeIsSynced.php
T
hackerESQ 1ef8dd9378 Feat: Adds multi currency to imports and exports (#89)
* Also adds ability for user to export configurations
2025-04-10 20:47:35 -05:00

34 lines
928 B
PHP

<?php
declare(strict_types=1);
namespace App\Actions;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class EnsureDailyChangeIsSynced
{
public function __invoke(Model $model, callable $next)
{
if (config('app.env') != 'testing') {
$cacheKey = 'daily_change_synced'.$model->portfolio_id;
if (
! Cache::has($cacheKey)
&& $model->date->lessThan(now())
&& ($model->date->lessThan($model->portfolio->daily_change()->min('date') ?? now())
|| $model->date->lessThan($model->portfolio->transactions()->where('id', '!=', $model->id)->min('date') ?? now())
)
) {
defer(fn () => $model->portfolio->syncDailyChanges());
Cache::put($cacheKey, now(), now()->addMinutes(5));
}
}
return $next($model);
}
}