Fixed: run initialize members as command

This commit is contained in:
philipp lang 2023-02-07 23:55:46 +01:00
parent ae922bbee1
commit ecffc258df
3 changed files with 32 additions and 7 deletions

View File

@ -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,
];
/**

View File

@ -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;
}
}

View File

@ -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');
}
}