adrema/app/Initialize/InitializeMembers.php

63 lines
1.9 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-23 00:38:17 +01:00
use App\Member\Actions\InsertFullMemberAction;
use Log;
2023-02-23 00:38:17 +01:00
use App\Member\Data\FullMember;
2023-07-06 14:29:35 +02:00
use App\Member\Member;
2023-02-23 00:38:17 +01:00
use App\Nami\Api\FullMemberAction;
use App\Setting\NamiSettings;
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\MemberEntry as NamiMemberEntry;
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
{
2023-05-08 15:11:16 +02:00
$settings = app(NamiSettings::class);
2023-02-13 00:14:51 +01:00
Redis::delete('members');
2020-04-12 00:26:44 +02:00
2023-07-06 14:29:35 +02:00
$memberIds = $api->search($settings->search_params)->map(fn ($member) => $member->id)->toArray();
foreach (Member::remote()->whereNotIn('nami_id', $memberIds)->get() as $member) {
if ($member->efz !== null || $member->ps_at !== null || $member->more_ps_at !== null) {
Log::error('Mitglied ' . $member->id . ' wird nicht gelöscht werden.');
continue;
}
$member->delete();
}
2023-07-06 14:29:35 +02:00
2023-05-08 15:11:16 +02:00
$jobs = $api->search($settings->search_params)->map(function (NamiMemberEntry $member) use ($api) {
2023-02-23 00:38:17 +01:00
return FullMemberAction::makeJob($api, $member->groupId, $member->id, 'members');
2023-02-13 00:14:51 +01:00
})->toArray();
2023-02-22 22:38:48 +01:00
Bus::batch($jobs)
->finally(function () {
2023-02-23 00:38:17 +01:00
/** @var array<int, FullMember> */
$members = array_map(fn ($member) => FullMember::from(json_decode($member, true)), Redis::lrange('members', 0, -1));
foreach ($members as $data) {
InsertFullMemberAction::dispatch($data);
2023-02-13 01:06:04 +01:00
}
2023-02-13 00:14:51 +01:00
})
2023-02-13 01:33:55 +01:00
->onQueue('long')
2023-02-13 15:56:20 +01:00
->allowFailures()
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
2023-02-22 22:38:48 +01:00
public function asCommand(): int
{
$this->handle(app(NamiSettings::class)->login());
return 0;
}
2020-04-12 00:26:44 +02:00
}