diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index f6d1c729..f86490ed 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -3,6 +3,7 @@ namespace App\Console; use App\Initialize\Actions\InitializeAction; +use App\Initialize\InitializeMembers; use App\Letter\Actions\LetterSendAction; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -18,6 +19,7 @@ class Kernel extends ConsoleKernel protected $commands = [ LetterSendAction::class, InitializeAction::class, + InitializeMembers::class, ]; /** diff --git a/app/Initialize/InitializeMembers.php b/app/Initialize/InitializeMembers.php index 3c152412..b144515d 100644 --- a/app/Initialize/InitializeMembers.php +++ b/app/Initialize/InitializeMembers.php @@ -5,24 +5,24 @@ namespace App\Initialize; use App\Actions\PullCoursesAction; use App\Actions\PullMemberAction; use App\Actions\PullMembershipsAction; +use App\Setting\NamiSettings; use DB; +use Illuminate\Console\Command; +use Lorisleiva\Actions\Concerns\AsAction; use Zoomyboy\LaravelNami\Api; use Zoomyboy\LaravelNami\Exceptions\Skippable; class InitializeMembers { - private Api $api; + use AsAction; - public function __construct(Api $api) - { - $this->api = $api; - } + public $commandSignature = 'member:pull'; - public function handle(): void + public function handle(Api $api): void { $allMembers = collect([]); - $this->api->search([])->each(function ($member) { + $api->search([])->each(function ($member) { try { $localMember = app(PullMemberAction::class)->handle($member->groupId, $member->id); } catch (Skippable $e) { @@ -41,4 +41,11 @@ class InitializeMembers DB::table('memberships')->delete(); DB::table('members')->delete(); } + + public function asCommand(Command $command): int + { + $this->handle(app(NamiSettings::class)->login()); + + return 0; + } } diff --git a/tests/Feature/Initialize/InitializeMembersTest.php b/tests/Feature/Initialize/InitializeMembersTest.php index c7536052..1b2874b8 100644 --- a/tests/Feature/Initialize/InitializeMembersTest.php +++ b/tests/Feature/Initialize/InitializeMembersTest.php @@ -9,6 +9,7 @@ use App\Initialize\InitializeMembers; use App\Member\Member; use App\Setting\NamiSettings; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Support\Facades\Artisan; use Tests\TestCase; use Zoomyboy\LaravelNami\Data\MemberEntry; use Zoomyboy\LaravelNami\Exceptions\MemberDataCorruptedException; @@ -48,4 +49,19 @@ class InitializeMembersTest extends TestCase app(InitializeMembers::class)->handle($api); } + + public function testFetchesMembersViaCommandLine(): void + { + $this->loginNami(); + $api = app(NamiSettings::class)->login(); + + app(SearchFake::class)->fetches(1, 0, [ + MemberEntry::factory()->toMember(['groupId' => 100, 'id' => 20]), + ]); + PullMemberAction::shouldRun()->once()->with(100, 20); + PullMembershipsAction::shouldRun()->once(); + PullCoursesAction::shouldRun()->once(); + + Artisan::call('member:pull'); + } }