2024-09-06 22:26:54 -05:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-28 17:33:54 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2024-09-06 22:26:54 -05:00
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
2024-09-11 23:04:21 -05:00
|
|
|
use App\Models\Portfolio;
|
2024-09-06 22:26:54 -05:00
|
|
|
use Illuminate\Console\Command;
|
2024-09-11 23:04:21 -05:00
|
|
|
use Illuminate\Contracts\Console\PromptsForMissingInput;
|
2025-01-28 17:14:49 -06:00
|
|
|
|
2024-09-11 23:04:21 -05:00
|
|
|
use function Laravel\Prompts\search;
|
2024-09-06 22:26:54 -05:00
|
|
|
|
2024-09-11 23:04:21 -05:00
|
|
|
class SyncDailyChange extends Command implements PromptsForMissingInput
|
2024-09-06 22:26:54 -05:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The name and signature of the console command.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2024-09-11 23:04:21 -05:00
|
|
|
protected $signature = 'sync:daily-change
|
|
|
|
|
{portfolio_id : The ID of the portfolio to re-calculate.}
|
|
|
|
|
{--force : Don\'t ask to confirm.}';
|
2024-09-06 22:26:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The console command description.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $description = 'Re-calculates daily snapshots of your portfolio\'s daily performance. Use discretion as this is a resource intensive command.';
|
|
|
|
|
|
2024-09-11 23:04:21 -05:00
|
|
|
/**
|
|
|
|
|
* Prompt for missing input arguments using the returned questions.
|
|
|
|
|
*
|
|
|
|
|
* @return array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected function promptForMissingArgumentsUsing(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'portfolio_id' => fn () => search(
|
|
|
|
|
label: 'Choose the portfolio you wish to re-calculate:',
|
|
|
|
|
placeholder: 'E.g. My favorite stocks',
|
|
|
|
|
options: fn ($value) => strlen($value) > 0
|
|
|
|
|
? Portfolio::where('title', 'like', "%{$value}%")->pluck('title', 'id')->all()
|
|
|
|
|
: []
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-06 22:26:54 -05:00
|
|
|
/**
|
|
|
|
|
* Create a new command instance.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
parent::__construct();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the console command.
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
2024-09-11 23:04:21 -05:00
|
|
|
try {
|
2025-01-28 17:14:49 -06:00
|
|
|
|
2024-09-11 23:04:21 -05:00
|
|
|
$portfolio = Portfolio::findOrFail($this->argument('portfolio_id'));
|
|
|
|
|
|
2024-09-15 23:46:24 -05:00
|
|
|
$this->line('Syncing daily change history... This may take a moment.');
|
2024-09-15 11:28:13 -05:00
|
|
|
|
2024-09-11 23:04:21 -05:00
|
|
|
$portfolio->syncDailyChanges();
|
|
|
|
|
|
2025-01-28 17:14:49 -06:00
|
|
|
$this->line('Awesome! Daily change history for '.$portfolio->title.' has been completed.');
|
2024-09-11 23:04:21 -05:00
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
|
|
|
|
|
$this->error($e->getMessage());
|
|
|
|
|
}
|
2024-09-06 22:26:54 -05:00
|
|
|
}
|
|
|
|
|
}
|