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 { class InitializeActivities {
private $bar;
private $api; private $api;
public function __construct($bar, $api) { public function __construct($api) {
$this->bar = $bar;
$this->api = $api; $this->api = $api;
} }
public function handle(NamiUser $user) { 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; namespace App\Initialize;
use App\Confession;
class InitializeConfessions { class InitializeConfessions {
private $bar;
private $api; private $api;
public $nullName = 'ohne Konfession'; public $nullName = 'ohne Konfession';
public function __construct($bar, $api) { public function __construct($api) {
$this->bar = $bar;
$this->api = $api; $this->api = $api;
} }
public function handle() { public function handle() {
$this->bar->task('Synchronisiere Konfessionen', function() {
$this->api->confessions()->each(function($confession) { $this->api->confessions()->each(function($confession) {
\App\Confession::create(['nami_id' => $confession->id, 'name' => $confession->name, 'is_null' => $this->nullName === $confession->name]); Confession::create(['nami_id' => $confession->id, 'name' => $confession->name, 'is_null' => $this->nullName === $confession->name]);
});
}); });
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -17,18 +17,6 @@ class InitializeJob implements ShouldQueue
public $user; 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) public function __construct(NamiUser $user)
{ {
$this->user = $user; $this->user = $user;
@ -41,13 +29,6 @@ class InitializeJob implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
$api = $this->user->api(); app(Initializer::class)->run($this->user);
$bar = $this->createProgressBar('Initialisiere');
foreach (static::$initializers as $initializer) {
(new $initializer($bar, $api))->handle($this->user);
}
$bar->finish('Initialisierung abgeschlossen');
} }
} }

View File

@ -15,16 +15,15 @@ use App\Region;
use App\Subactivity; use App\Subactivity;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Zoomyboy\LaravelNami\Exceptions\RightException;
use Zoomyboy\LaravelNami\Member as NamiMember; use Zoomyboy\LaravelNami\Member as NamiMember;
use Zoomyboy\LaravelNami\NamiException; use Zoomyboy\LaravelNami\NamiException;
class InitializeMembers { class InitializeMembers {
private $bar;
private $api; private $api;
public function __construct($bar, $api) { public function __construct($api) {
$this->bar = $bar;
$this->api = $api; $this->api = $api;
} }
@ -40,7 +39,6 @@ class InitializeMembers {
public function handle() { public function handle() {
$allMembers = collect([]); $allMembers = collect([]);
$this->bar->task('Synchronisiere Mitglieder', function() {
$this->api->search([])->each(function($member) { $this->api->search([])->each(function($member) {
$member = NamiMember::fromNami($this->api->member($member->group_id, $member->id)); $member = NamiMember::fromNami($this->api->member($member->group_id, $member->id));
if (!$member->joined_at) { if (!$member->joined_at) {
@ -107,7 +105,11 @@ class InitializeMembers {
: Subactivity::where('name', $membership['entries_untergliederung'])->firstOrFail()->id; : Subactivity::where('name', $membership['entries_untergliederung'])->firstOrFail()->id;
$activity = Activity::where('nami_id', (int) $activityMatches[1])->first(); $activity = Activity::where('nami_id', (int) $activityMatches[1])->first();
if (!$activity) { if (!$activity) {
try {
$singleMembership = $this->api->membership($member->id, $membership['id']); $singleMembership = $this->api->membership($member->id, $membership['id']);
} catch (RightException $e) {
continue;
}
app(ActivityCreator::class)->createFor($this->api, $singleMembership['gruppierungId']); app(ActivityCreator::class)->createFor($this->api, $singleMembership['gruppierungId']);
$activity = Activity::where('nami_id', $singleMembership['taetigkeitId'])->first(); $activity = Activity::where('nami_id', $singleMembership['taetigkeitId'])->first();
$group = Group::firstOrCreate(['nami_id' => $singleMembership['gruppierungId']], [ $group = Group::firstOrCreate(['nami_id' => $singleMembership['gruppierungId']], [
@ -127,6 +129,5 @@ class InitializeMembers {
dd($e->getMessage(), $member); dd($e->getMessage(), $member);
} }
}); });
});
} }
} }

View File

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

View File

@ -4,20 +4,16 @@ namespace App\Initialize;
class InitializeRegions { class InitializeRegions {
private $bar;
private $api; private $api;
private $nullName = 'Nicht-DE'; private $nullName = 'Nicht-DE';
public function __construct($bar, $api) { public function __construct($api) {
$this->bar = $bar;
$this->api = $api; $this->api = $api;
} }
public function handle() { public function handle() {
$this->bar->task('Synchronisiere Bundesländer', function() {
$this->api->regions()->each(function($region) { $this->api->regions()->each(function($region) {
\App\Region::create(['nami_id' => $region->id, 'name' => $region->name, 'is_null' => $region->name == $this->nullName]); \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 <?php
phpinfo();exit;
/** /**
* Laravel - A PHP Framework For Web Artisans * Laravel - A PHP Framework For Web Artisans
* *

View File

@ -9,9 +9,11 @@ use App\Gender;
use App\Group; use App\Group;
use App\Member\Member; use App\Member\Member;
use App\Nationality; use App\Nationality;
use App\Setting\GeneralSettings;
use App\Subactivity; use App\Subactivity;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Tests\TestCase; use Tests\TestCase;
use Zoomyboy\LaravelNami\Backend\FakeBackend; use Zoomyboy\LaravelNami\Backend\FakeBackend;
@ -120,6 +122,24 @@ class InitializeTest extends TestCase
Http::assertSentCount(16); 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 public function testSyncCoursesOfMember(): void
{ {
$this->withoutExceptionHandling(); $this->withoutExceptionHandling();

View File

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