Add initialize command

This commit is contained in:
philipp lang 2022-01-03 01:17:24 +01:00
parent 91265a3f55
commit 25bee98dac
15 changed files with 233 additions and 170 deletions

View File

@ -0,0 +1,55 @@
<?php
namespace App\Console\Commands;
use App\Initialize\Initializer;
use Illuminate\Console\Command;
use Zoomyboy\LaravelNami\Nami;
use Zoomyboy\LaravelNami\NamiUser;
class NamiInitializeCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'nami:initialize {--mglnr=} {--password=} {--group_id=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Initializes nami';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
app(Initializer::class)->run(NamiUser::fromPayload([
'credentials' => [
'mglnr' => $this->option('mglnr'),
'password' => $this->option('password'),
],
'firstname' => 'Console',
'lastname' => 'Console',
'group_id' => $this->option('group_id'),
]));
return 0;
}
}

View File

@ -6,17 +6,13 @@ use Zoomyboy\LaravelNami\NamiUser;
class InitializeActivities {
private $bar;
private $api;
public function __construct($bar, $api) {
$this->bar = $bar;
public function __construct($api) {
$this->api = $api;
}
public function handle(NamiUser $user) {
$this->bar->task('Synchronisiere Tätigkeiten', function() use ($user) {
app(ActivityCreator::class)->createFor($this->api, $user->getNamiGroupId());
});
app(ActivityCreator::class)->createFor($this->api, $user->getNamiGroupId());
}
}

View File

@ -2,22 +2,20 @@
namespace App\Initialize;
use App\Confession;
class InitializeConfessions {
private $bar;
private $api;
public $nullName = 'ohne Konfession';
public function __construct($bar, $api) {
$this->bar = $bar;
public function __construct($api) {
$this->api = $api;
}
public function handle() {
$this->bar->task('Synchronisiere Konfessionen', function() {
$this->api->confessions()->each(function($confession) {
\App\Confession::create(['nami_id' => $confession->id, 'name' => $confession->name, 'is_null' => $this->nullName === $confession->name]);
});
$this->api->confessions()->each(function($confession) {
Confession::create(['nami_id' => $confession->id, 'name' => $confession->name, 'is_null' => $this->nullName === $confession->name]);
});
}
}

View File

@ -1,22 +1,18 @@
<?php
<?php
namespace App\Initialize;
class InitializeCountries {
private $bar;
private $api;
public function __construct($bar, $api) {
$this->bar = $bar;
public function __construct($api) {
$this->api = $api;
}
public function handle() {
$this->bar->task('Synchronisiere Länder', function() {
$this->api->countries()->each(function($country) {
\App\Country::create(['nami_id' => $country->id, 'name' => $country->name]);
});
$this->api->countries()->each(function($country) {
\App\Country::create(['nami_id' => $country->id, 'name' => $country->name]);
});
}
}

View File

@ -10,19 +10,15 @@ use Zoomyboy\LaravelNami\NamiUser;
class InitializeCourses {
private Api $api;
private Progress $bar;
public function __construct(Progress $bar, Api $api) {
$this->bar = $bar;
public function __construct(Api $api) {
$this->api = $api;
}
public function handle(NamiUser $user): void
{
$this->bar->task('Synchronisiere Kurse', function() {
$this->api->courses()->each(function($course) {
Course::create(['nami_id' => $course->id, 'name' => $course->name]);
});
$this->api->courses()->each(function($course) {
Course::create(['nami_id' => $course->id, 'name' => $course->name]);
});
}
}

View File

@ -6,23 +6,19 @@ use Zoomyboy\LaravelNami\NamiUser;
class InitializeFees {
private $bar;
private $api;
public function __construct($bar, $api) {
$this->bar = $bar;
public function __construct($api) {
$this->api = $api;
}
public function handle(NamiUser $user) {
$this->bar->task('Synchronisiere Beiträge', function() use ($user) {
$this->api->feesOf($user->getNamiGroupId())->each(function($fee) {
\App\Fee::create(['nami_id' => $fee->id, 'name' => $fee->name])
->subscriptions()->create([
'name' => $fee->name,
'amount' => 1000,
]);
});
$this->api->feesOf($user->getNamiGroupId())->each(function($fee) {
\App\Fee::create(['nami_id' => $fee->id, 'name' => $fee->name])
->subscriptions()->create([
'name' => $fee->name,
'amount' => 1000,
]);
});
}
}

View File

@ -6,19 +6,15 @@ use Zoomyboy\LaravelNami\NamiUser;
class InitializeGenders {
private $bar;
private $api;
public function __construct($bar, $api) {
$this->bar = $bar;
public function __construct($api) {
$this->api = $api;
}
public function handle(NamiUser $user) {
$this->bar->task('Synchronisiere Geschlechter', function() {
$this->api->genders()->each(function($gender) {
\App\Gender::create(['nami_id' => $gender->id, 'name' => $gender->name]);
});
$this->api->genders()->each(function($gender) {
\App\Gender::create(['nami_id' => $gender->id, 'name' => $gender->name]);
});
}
}

View File

@ -17,18 +17,6 @@ class InitializeJob implements ShouldQueue
public $user;
public static $initializers = [
InitializeNationalities::class,
InitializeFees::class,
InitializeConfessions::class,
InitializeCountries::class,
InitializeGenders::class,
InitializeRegions::class,
InitializeActivities::class,
InitializeCourses::class,
InitializeMembers::class,
];
public function __construct(NamiUser $user)
{
$this->user = $user;
@ -41,13 +29,6 @@ class InitializeJob implements ShouldQueue
*/
public function handle()
{
$api = $this->user->api();
$bar = $this->createProgressBar('Initialisiere');
foreach (static::$initializers as $initializer) {
(new $initializer($bar, $api))->handle($this->user);
}
$bar->finish('Initialisierung abgeschlossen');
app(Initializer::class)->run($this->user);
}
}

View File

@ -15,16 +15,15 @@ use App\Region;
use App\Subactivity;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Zoomyboy\LaravelNami\Exceptions\RightException;
use Zoomyboy\LaravelNami\Member as NamiMember;
use Zoomyboy\LaravelNami\NamiException;
class InitializeMembers {
private $bar;
private $api;
public function __construct($bar, $api) {
$this->bar = $bar;
public function __construct($api) {
$this->api = $api;
}
@ -40,93 +39,95 @@ class InitializeMembers {
public function handle() {
$allMembers = collect([]);
$this->bar->task('Synchronisiere Mitglieder', function() {
$this->api->search([])->each(function($member) {
$member = NamiMember::fromNami($this->api->member($member->group_id, $member->id));
if (!$member->joined_at) {
return;
}
try {
$m = Member::create([
'firstname' => $member->firstname,
'lastname' => $member->lastname,
'joined_at' => $member->joined_at,
'birthday' => $member->birthday,
'send_newspaper' => $member->send_newspaper,
'address' => $member->address,
'zip' => $member->zip,
'location' => $member->location,
'nickname' => $member->nickname,
'other_country' => $member->other_country,
'further_address' => $member->further_address,
'main_phone' => $member->main_phone,
'mobile_phone' => $member->mobile_phone,
'work_phone' => $member->work_phone,
'fax' => $member->fax,
'email' => $member->email,
'email_parents' => $member->email_parents,
'nami_id' => $member->id,
'group_id' => Group::firstOrCreate(['nami_id' => $member->group_id], ['nami_id' => $member->group_id, 'name' => $member->group_name])->id,
'gender_id' => optional(Gender::firstWhere('nami_id', $member->gender_id ?: -1))->id,
'confession_id' => optional(Confession::firstWhere('nami_id', $member->confession_id ?: -1))->id,
'region_id' => optional(Region::firstWhere('nami_id', $member->region_id ?: -1))->id,
'country_id' => optional(Country::where('nami_id', $member->country_id)->first())->id,
'subscription_id' => $this->getSubscriptionId($member),
'nationality_id' => Nationality::where('nami_id', $member->nationality_id)->firstOrFail()->id,
'version' => $member->version,
$this->api->search([])->each(function($member) {
$member = NamiMember::fromNami($this->api->member($member->group_id, $member->id));
if (!$member->joined_at) {
return;
}
try {
$m = Member::create([
'firstname' => $member->firstname,
'lastname' => $member->lastname,
'joined_at' => $member->joined_at,
'birthday' => $member->birthday,
'send_newspaper' => $member->send_newspaper,
'address' => $member->address,
'zip' => $member->zip,
'location' => $member->location,
'nickname' => $member->nickname,
'other_country' => $member->other_country,
'further_address' => $member->further_address,
'main_phone' => $member->main_phone,
'mobile_phone' => $member->mobile_phone,
'work_phone' => $member->work_phone,
'fax' => $member->fax,
'email' => $member->email,
'email_parents' => $member->email_parents,
'nami_id' => $member->id,
'group_id' => Group::firstOrCreate(['nami_id' => $member->group_id], ['nami_id' => $member->group_id, 'name' => $member->group_name])->id,
'gender_id' => optional(Gender::firstWhere('nami_id', $member->gender_id ?: -1))->id,
'confession_id' => optional(Confession::firstWhere('nami_id', $member->confession_id ?: -1))->id,
'region_id' => optional(Region::firstWhere('nami_id', $member->region_id ?: -1))->id,
'country_id' => optional(Country::where('nami_id', $member->country_id)->first())->id,
'subscription_id' => $this->getSubscriptionId($member),
'nationality_id' => Nationality::where('nami_id', $member->nationality_id)->firstOrFail()->id,
'version' => $member->version,
]);
foreach ($this->api->coursesFor($member->id) as $course) {
$m->courses()->create([
'course_id' => Course::where('nami_id', $course->course_id)->firstOrFail()->id,
'organizer' => $course->organizer,
'event_name' => $course->event_name,
'completed_at' => $course->completed_at,
'nami_id' => $course->id,
]);
foreach ($this->api->coursesFor($member->id) as $course) {
$m->courses()->create([
'course_id' => Course::where('nami_id', $course->course_id)->firstOrFail()->id,
'organizer' => $course->organizer,
'event_name' => $course->event_name,
'completed_at' => $course->completed_at,
'nami_id' => $course->id,
]);
}
foreach ($this->api->membershipsOf($member->id) as $membership) {
if (Carbon::parse($membership['entries_aktivVon'])->addYears(200)->isPast()) {
continue;
}
if ($membership['entries_aktivBis'] !== '') {
continue;
}
if (preg_match('/\(([0-9]+)\)/', $membership['entries_taetigkeit'], $activityMatches) !== 1) {
throw new NamiException("ID in taetigkeit string not found: {$membership['entries_taetigkeit']}");
}
$group = Group::where('name', $membership['entries_gruppierung'])->first();
if (!$group) {
preg_match('/(.*?) ([0-9]+)$/', $membership['entries_gruppierung'], $groupMatches);
[$groupAll, $groupName, $groupId] = $groupMatches;
$group = Group::create(['name' => $groupName, 'nami_id' => $groupId]);
}
$subactivityId = $membership['entries_untergliederung'] === ''
? null
: Subactivity::where('name', $membership['entries_untergliederung'])->firstOrFail()->id;
$activity = Activity::where('nami_id', (int) $activityMatches[1])->first();
if (!$activity) {
$singleMembership = $this->api->membership($member->id, $membership['id']);
app(ActivityCreator::class)->createFor($this->api, $singleMembership['gruppierungId']);
$activity = Activity::where('nami_id', $singleMembership['taetigkeitId'])->first();
$group = Group::firstOrCreate(['nami_id' => $singleMembership['gruppierungId']], [
'nami_id' => $singleMembership['gruppierungId'],
'name' => $singleMembership['gruppierung'],
]);
}
$m->memberships()->create([
'nami_id' => $membership['id'],
'created_at' => $membership['entries_aktivVon'],
'group_id' => $group->id,
'activity_id' => $activity->id,
'subactivity_id' => $subactivityId,
]);
}
} catch (ModelNotFoundException $e) {
dd($e->getMessage(), $member);
}
});
foreach ($this->api->membershipsOf($member->id) as $membership) {
if (Carbon::parse($membership['entries_aktivVon'])->addYears(200)->isPast()) {
continue;
}
if ($membership['entries_aktivBis'] !== '') {
continue;
}
if (preg_match('/\(([0-9]+)\)/', $membership['entries_taetigkeit'], $activityMatches) !== 1) {
throw new NamiException("ID in taetigkeit string not found: {$membership['entries_taetigkeit']}");
}
$group = Group::where('name', $membership['entries_gruppierung'])->first();
if (!$group) {
preg_match('/(.*?) ([0-9]+)$/', $membership['entries_gruppierung'], $groupMatches);
[$groupAll, $groupName, $groupId] = $groupMatches;
$group = Group::create(['name' => $groupName, 'nami_id' => $groupId]);
}
$subactivityId = $membership['entries_untergliederung'] === ''
? null
: Subactivity::where('name', $membership['entries_untergliederung'])->firstOrFail()->id;
$activity = Activity::where('nami_id', (int) $activityMatches[1])->first();
if (!$activity) {
try {
$singleMembership = $this->api->membership($member->id, $membership['id']);
} catch (RightException $e) {
continue;
}
app(ActivityCreator::class)->createFor($this->api, $singleMembership['gruppierungId']);
$activity = Activity::where('nami_id', $singleMembership['taetigkeitId'])->first();
$group = Group::firstOrCreate(['nami_id' => $singleMembership['gruppierungId']], [
'nami_id' => $singleMembership['gruppierungId'],
'name' => $singleMembership['gruppierung'],
]);
}
$m->memberships()->create([
'nami_id' => $membership['id'],
'created_at' => $membership['entries_aktivVon'],
'group_id' => $group->id,
'activity_id' => $activity->id,
'subactivity_id' => $subactivityId,
]);
}
} catch (ModelNotFoundException $e) {
dd($e->getMessage(), $member);
}
});
}
}

View File

@ -4,19 +4,15 @@ namespace App\Initialize;
class InitializeNationalities {
private $bar;
private $api;
public function __construct($bar, $api) {
$this->bar = $bar;
public function __construct($api) {
$this->api = $api;
}
public function handle() {
$this->bar->task('Synchronisiere Nationalitäten', function() {
$this->api->nationalities()->each(function($nationality) {
\App\Nationality::create(['nami_id' => $nationality->id, 'name' => $nationality->name]);
});
$this->api->nationalities()->each(function($nationality) {
\App\Nationality::create(['nami_id' => $nationality->id, 'name' => $nationality->name]);
});
}
}

View File

@ -4,20 +4,16 @@ namespace App\Initialize;
class InitializeRegions {
private $bar;
private $api;
private $nullName = 'Nicht-DE';
public function __construct($bar, $api) {
$this->bar = $bar;
public function __construct($api) {
$this->api = $api;
}
public function handle() {
$this->bar->task('Synchronisiere Bundesländer', function() {
$this->api->regions()->each(function($region) {
\App\Region::create(['nami_id' => $region->id, 'name' => $region->name, 'is_null' => $region->name == $this->nullName]);
});
$this->api->regions()->each(function($region) {
\App\Region::create(['nami_id' => $region->id, 'name' => $region->name, 'is_null' => $region->name == $this->nullName]);
});
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Initialize;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\NamiUser;
class Initializer {
public $user;
public static $initializers = [
InitializeNationalities::class,
InitializeFees::class,
InitializeConfessions::class,
InitializeCountries::class,
InitializeGenders::class,
InitializeRegions::class,
InitializeActivities::class,
InitializeCourses::class,
InitializeMembers::class,
];
public function run(NamiUser $namiUser) {
foreach (static::$initializers as $initializer) {
(new $initializer($namiUser->api()))->handle($namiUser);
}
}
}

View File

@ -1,5 +1,7 @@
<?php
phpinfo();exit;
/**
* Laravel - A PHP Framework For Web Artisans
*

View File

@ -9,9 +9,11 @@ use App\Gender;
use App\Group;
use App\Member\Member;
use App\Nationality;
use App\Setting\GeneralSettings;
use App\Subactivity;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
use Zoomyboy\LaravelNami\Backend\FakeBackend;
@ -120,6 +122,24 @@ class InitializeTest extends TestCase
Http::assertSentCount(16);
}
public function testItInitializesFromCommandLine(): void
{
$this->withoutExceptionHandling();
$this->initializeProvider();
GeneralSettings::fake(['allowed_nami_accounts' => [123]]);
Artisan::call('nami:initialize', [
'--mglnr' => 90166,
'--password' => 'secret',
'--group_id' => 1000,
]);
$this->assertDatabaseHas('regions', [
'name' => 'nrw',
'nami_id' => 304
]);
}
public function testSyncCoursesOfMember(): void
{
$this->withoutExceptionHandling();

View File

@ -21,11 +21,15 @@ abstract class TestCase extends BaseTestCase
TestResponse::mixin(new InertiaMixin());
}
public function login(): self
{
public function fakeAuthUser() {
app(FakeBackend::class)
->fakeLogin('123')
->addSearch(123, ['entries_vorname' => '::firstname::', 'entries_nachname' => '::lastname::', 'entries_gruppierungId' => 1000]);
}
public function login(): self
{
$this->fakeAuthUser();
auth()->loginNami([
'mglnr' => 123,
'password' => 'secret',