adrema/app/Initialize/InitializeMembers.php

76 lines
2.4 KiB
PHP
Raw Normal View History

2022-01-03 10:33:48 +01:00
<?php
2020-04-12 00:26:44 +02:00
namespace App\Initialize;
2023-02-13 01:06:04 +01:00
use App\Actions\InsertCoursesAction;
2023-02-13 00:14:51 +01:00
use App\Actions\InsertMemberAction;
2023-02-13 01:06:04 +01:00
use App\Actions\InsertMembershipsAction;
2023-02-13 00:14:51 +01:00
use App\Nami\Api\CompleteMemberToRedisJob;
use App\Setting\NamiSettings;
2022-09-02 00:45:39 +02:00
use DB;
2023-02-13 00:14:51 +01:00
use Illuminate\Bus\Batch;
use Illuminate\Console\Command;
2023-02-13 00:14:51 +01:00
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Redis;
use Lorisleiva\Actions\Concerns\AsAction;
2022-02-12 16:16:56 +01:00
use Zoomyboy\LaravelNami\Api;
2023-02-13 01:06:04 +01:00
use Zoomyboy\LaravelNami\Data\Course as NamiCourse;
use Zoomyboy\LaravelNami\Data\Member as NamiMember;
use Zoomyboy\LaravelNami\Data\MemberEntry as NamiMemberEntry;
use Zoomyboy\LaravelNami\Data\MembershipEntry as NamiMembershipEntry;
2021-04-10 01:39:39 +02:00
2022-03-11 20:19:17 +01:00
class InitializeMembers
{
use AsAction;
2022-01-03 10:33:48 +01:00
2023-02-08 00:14:59 +01:00
public string $commandSignature = 'member:pull';
2023-02-13 01:33:55 +01:00
public string $jobQueue = 'long';
2020-04-12 00:26:44 +02:00
public function handle(Api $api): void
2022-02-12 16:16:56 +01:00
{
2020-04-12 00:26:44 +02:00
$allMembers = collect([]);
2023-02-13 00:14:51 +01:00
Redis::delete('members');
2020-04-12 00:26:44 +02:00
2023-02-13 01:06:04 +01:00
$jobs = $api->search([])->map(function (NamiMemberEntry $member) use ($api) {
2023-02-13 00:14:51 +01:00
return new CompleteMemberToRedisJob($api, $member->groupId, $member->id);
})->toArray();
2023-02-13 00:14:51 +01:00
$batch = Bus::batch($jobs)
->finally(function (Batch $batch) {
2023-02-13 01:06:04 +01:00
foreach (Redis::lrange('members', 0, -1) as $data) {
try {
$data = json_decode($data, true);
$localMember = InsertMemberAction::run(NamiMember::from($data['member']));
InsertMembershipsAction::run(
$localMember,
collect($data['memberships'])->map(fn ($membership) => NamiMembershipEntry::from($membership)),
);
InsertCoursesAction::run(
$localMember,
collect($data['courses'])->map(fn ($course) => NamiCourse::from($course)),
);
} catch (Skippable $e) {
continue;
}
}
2023-02-13 00:14:51 +01:00
})
2023-02-13 01:33:55 +01:00
->onQueue('long')
2023-02-13 00:14:51 +01:00
->dispatch();
2022-01-03 10:33:48 +01:00
}
2022-09-02 00:45:39 +02:00
public function restore(): void
{
DB::table('payments')->delete();
DB::table('course_members')->delete();
DB::table('memberships')->delete();
DB::table('members')->delete();
}
public function asCommand(Command $command): int
{
$this->handle(app(NamiSettings::class)->login());
return 0;
}
2020-04-12 00:26:44 +02:00
}