From ff725e0119b3a67fd2277347faddfe39b6ff1f0f Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Wed, 23 Oct 2024 16:57:55 -0500 Subject: [PATCH] feat:adds user option to sync and refresh commands --- app/Console/Commands/RefreshDividendData.php | 7 ++++++- app/Console/Commands/RefreshMarketData.php | 12 ++++++++---- app/Console/Commands/SyncHoldingData.php | 11 ++++++++--- app/Models/Holding.php | 6 +++--- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/Console/Commands/RefreshDividendData.php b/app/Console/Commands/RefreshDividendData.php index 652af38..fa97c9b 100644 --- a/app/Console/Commands/RefreshDividendData.php +++ b/app/Console/Commands/RefreshDividendData.php @@ -14,7 +14,8 @@ class RefreshDividendData extends Command * @var string */ protected $signature = 'refresh:dividend-data - {--force : Refresh all holdings}'; + {--force : Refresh all holdings} + {--user= : Limit refresh to user\'s holdings}'; /** * The console command description. @@ -46,6 +47,10 @@ class RefreshDividendData extends Command $holdings->where('quantity', '>', 0); } + if ($this->option('user')) { + $holdings->myHoldings($this->option('user')); + } + foreach ($holdings->get(['symbol']) as $holding) { $this->line('Refreshing ' . $holding->symbol); diff --git a/app/Console/Commands/RefreshMarketData.php b/app/Console/Commands/RefreshMarketData.php index 8816b78..f36a090 100644 --- a/app/Console/Commands/RefreshMarketData.php +++ b/app/Console/Commands/RefreshMarketData.php @@ -14,7 +14,8 @@ class RefreshMarketData extends Command * @var string */ protected $signature = 'refresh:market-data - {--force : Ignore refresh delay}'; + {--force : Ignore refresh delay} + {--user= : Limit refresh to user\'s holdings}'; /** * The console command description. @@ -45,10 +46,13 @@ class RefreshMarketData extends Command // get all symbols from market data $holdings = Holding::where('quantity', '>', 0) ->select(['symbol']) - ->distinct() - ->get(); + ->distinct(); + + if ($this->option('user')) { + $holdings->myHoldings($this->option('user')); + } - foreach ($holdings as $holding) { + foreach ($holdings->get() as $holding) { $this->line('Refreshing ' . $holding->symbol); MarketData::getMarketData($holding->symbol, $force); diff --git a/app/Console/Commands/SyncHoldingData.php b/app/Console/Commands/SyncHoldingData.php index dfd5f3d..fe82398 100644 --- a/app/Console/Commands/SyncHoldingData.php +++ b/app/Console/Commands/SyncHoldingData.php @@ -12,7 +12,8 @@ class SyncHoldingData extends Command * * @var string */ - protected $signature = 'sync:holdings'; + protected $signature = 'sync:holdings + {--user= : Limit refresh to user\'s holdings}'; /** * The console command description. @@ -39,9 +40,13 @@ class SyncHoldingData extends Command public function handle() { // get all holdings - $holdings = Holding::get(); + $holdings = Holding::query(); - foreach ($holdings as $holding) { + if ($this->option('user')) { + $holdings->myHoldings($this->option('user')); + } + + foreach ($holdings->get() as $holding) { $this->line('Refreshing ' . $holding->symbol); $holding->syncTransactionsAndDividends(); diff --git a/app/Models/Holding.php b/app/Models/Holding.php index 2340c17..b2dc3ef 100644 --- a/app/Models/Holding.php +++ b/app/Models/Holding.php @@ -159,10 +159,10 @@ class Holding extends Model ->where('portfolios.wishlist', 0); } - public function scopeMyHoldings($query) + public function scopeMyHoldings($query, $userId = null) { - return $query->whereHas('portfolio', function($query) { - $query->whereRelation('users', 'id', auth()->user()->id); + return $query->whereHas('portfolio', function($query) use ($userId) { + $query->whereRelation('users', 'id', $userId ?? auth()->user()->id); }); }