From 25bee98dac1a87b1cb1f843695a641e753d89c50 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Mon, 3 Jan 2022 01:17:24 +0100 Subject: [PATCH] Add initialize command --- .../Commands/NamiInitializeCommand.php | 55 ++++++ app/Initialize/InitializeActivities.php | 8 +- app/Initialize/InitializeConfessions.php | 12 +- app/Initialize/InitializeCountries.php | 12 +- app/Initialize/InitializeCourses.php | 10 +- app/Initialize/InitializeFees.php | 18 +- app/Initialize/InitializeGenders.php | 10 +- app/Initialize/InitializeJob.php | 21 +-- app/Initialize/InitializeMembers.php | 177 +++++++++--------- app/Initialize/InitializeNationalities.php | 10 +- app/Initialize/InitializeRegions.php | 10 +- app/Initialize/Initializer.php | 30 +++ public/index.php | 2 + tests/Feature/Initialize/InitializeTest.php | 20 ++ tests/TestCase.php | 8 +- 15 files changed, 233 insertions(+), 170 deletions(-) create mode 100644 app/Console/Commands/NamiInitializeCommand.php create mode 100644 app/Initialize/Initializer.php diff --git a/app/Console/Commands/NamiInitializeCommand.php b/app/Console/Commands/NamiInitializeCommand.php new file mode 100644 index 00000000..fe22b94c --- /dev/null +++ b/app/Console/Commands/NamiInitializeCommand.php @@ -0,0 +1,55 @@ +run(NamiUser::fromPayload([ + 'credentials' => [ + 'mglnr' => $this->option('mglnr'), + 'password' => $this->option('password'), + ], + 'firstname' => 'Console', + 'lastname' => 'Console', + 'group_id' => $this->option('group_id'), + ])); + + return 0; + } +} diff --git a/app/Initialize/InitializeActivities.php b/app/Initialize/InitializeActivities.php index c7fdc2ec..4dbfa0be 100644 --- a/app/Initialize/InitializeActivities.php +++ b/app/Initialize/InitializeActivities.php @@ -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()); } } diff --git a/app/Initialize/InitializeConfessions.php b/app/Initialize/InitializeConfessions.php index a2402b0b..48b79a81 100644 --- a/app/Initialize/InitializeConfessions.php +++ b/app/Initialize/InitializeConfessions.php @@ -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]); }); } } diff --git a/app/Initialize/InitializeCountries.php b/app/Initialize/InitializeCountries.php index db674a22..450a3a33 100644 --- a/app/Initialize/InitializeCountries.php +++ b/app/Initialize/InitializeCountries.php @@ -1,22 +1,18 @@ -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]); }); } } diff --git a/app/Initialize/InitializeCourses.php b/app/Initialize/InitializeCourses.php index 3e5e1fed..bea52ff0 100644 --- a/app/Initialize/InitializeCourses.php +++ b/app/Initialize/InitializeCourses.php @@ -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]); }); } } diff --git a/app/Initialize/InitializeFees.php b/app/Initialize/InitializeFees.php index 5e3b3da7..2556bfd3 100644 --- a/app/Initialize/InitializeFees.php +++ b/app/Initialize/InitializeFees.php @@ -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, + ]); }); } } diff --git a/app/Initialize/InitializeGenders.php b/app/Initialize/InitializeGenders.php index 9afe7ed7..a6818666 100644 --- a/app/Initialize/InitializeGenders.php +++ b/app/Initialize/InitializeGenders.php @@ -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]); }); } } diff --git a/app/Initialize/InitializeJob.php b/app/Initialize/InitializeJob.php index 9128d8a0..cfbcaab2 100644 --- a/app/Initialize/InitializeJob.php +++ b/app/Initialize/InitializeJob.php @@ -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); } } diff --git a/app/Initialize/InitializeMembers.php b/app/Initialize/InitializeMembers.php index 78bf25c2..0a4ae70b 100644 --- a/app/Initialize/InitializeMembers.php +++ b/app/Initialize/InitializeMembers.php @@ -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); + } }); } } diff --git a/app/Initialize/InitializeNationalities.php b/app/Initialize/InitializeNationalities.php index c1115096..74b0ef47 100644 --- a/app/Initialize/InitializeNationalities.php +++ b/app/Initialize/InitializeNationalities.php @@ -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]); }); } } diff --git a/app/Initialize/InitializeRegions.php b/app/Initialize/InitializeRegions.php index 48cb533a..af7ba9cf 100644 --- a/app/Initialize/InitializeRegions.php +++ b/app/Initialize/InitializeRegions.php @@ -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]); }); } } diff --git a/app/Initialize/Initializer.php b/app/Initialize/Initializer.php new file mode 100644 index 00000000..f95736ea --- /dev/null +++ b/app/Initialize/Initializer.php @@ -0,0 +1,30 @@ +api()))->handle($namiUser); + } + } + +} diff --git a/public/index.php b/public/index.php index 4584cbcd..1ec96eca 100644 --- a/public/index.php +++ b/public/index.php @@ -1,5 +1,7 @@ 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(); diff --git a/tests/TestCase.php b/tests/TestCase.php index eef736c9..ccdb63bb 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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',