From 7d3f52ec45cd0714d61787fce55d833dd56f5891 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 25 Oct 2024 01:43:13 +0200 Subject: [PATCH] Add Nami Settings --- app/Setting/NamiSettings.php | 14 +--- config/app.php | 1 + modules/Nami/Components/SettingView.php | 69 +++++++++++++++++++ modules/Nami/NamiServiceProvider.php | 31 +++++++++ modules/Nami/SettingTest.php | 90 +++++++++++++++++++++++++ resources/js/views/setting/Nami.vue | 47 ------------- 6 files changed, 192 insertions(+), 60 deletions(-) create mode 100644 modules/Nami/Components/SettingView.php create mode 100644 modules/Nami/NamiServiceProvider.php create mode 100644 modules/Nami/SettingTest.php delete mode 100644 resources/js/views/setting/Nami.vue diff --git a/app/Setting/NamiSettings.php b/app/Setting/NamiSettings.php index 12dbd1d1..14607655 100644 --- a/app/Setting/NamiSettings.php +++ b/app/Setting/NamiSettings.php @@ -3,14 +3,10 @@ namespace App\Setting; use App\Group; -use App\Initialize\Actions\NamiLoginCheckAction; -use App\Nami\Actions\SettingSaveAction; -use App\Setting\Contracts\Storeable; -use Lorisleiva\Actions\ActionRequest; use Zoomyboy\LaravelNami\Api; use Zoomyboy\LaravelNami\Nami; -class NamiSettings extends LocalSettings implements Storeable +class NamiSettings extends LocalSettings { public int $mglnr; @@ -43,14 +39,6 @@ class NamiSettings extends LocalSettings implements Storeable ]; } - public function beforeSave(ActionRequest $request): void - { - NamiLoginCheckAction::run([ - 'mglnr' => $request->mglnr, - 'password' => $request->password, - ]); - } - public function localGroup(): ?Group { return Group::firstWhere('nami_id', $this->default_group_id); diff --git a/config/app.php b/config/app.php index a1712655..8d12c9b2 100644 --- a/config/app.php +++ b/config/app.php @@ -182,6 +182,7 @@ return [ Modules\Module\ModuleServiceProvider::class, Modules\Invoice\InvoiceServiceProvider::class, Modules\Mailgateway\MailgatewayServiceProvider::class, + Modules\Nami\NamiServiceProvider::class, ], /* diff --git a/modules/Nami/Components/SettingView.php b/modules/Nami/Components/SettingView.php new file mode 100644 index 00000000..4eb92669 --- /dev/null +++ b/modules/Nami/Components/SettingView.php @@ -0,0 +1,69 @@ + 'required|string', + 'default_group_id' => 'required', + 'mglnr' => 'required', + ]; + } + + public function mount(): void + { + $this->mglnr = app(NamiSettings::class)->mglnr; + $this->default_group_id = app(NamiSettings::class)->default_group_id; + } + + public function save(): void + { + $validated = $this->validate(); + try { + NamiLoginCheckAction::run([ + 'mglnr' => $this->mglnr, + 'password' => $this->password, + ]); + app(NamiSettings::class)->fill($validated)->save(); + $this->dispatch('success', 'Einstellungen gespeichert.'); + } catch (LoginException $e) { + throw ValidationException::withMessages([ + 'mglnr' => 'Login fehlgeschlagen.', + ]); + } + } + + public function render() + { + return <<<'HTML' + + + + +
+
+

Hier kannst du deine Zugangsdaten zu NaMi anpassen, falls sich z.B. dein Passwort geändert hat.

+
+ + + +
+
+ HTML; + } +} diff --git a/modules/Nami/NamiServiceProvider.php b/modules/Nami/NamiServiceProvider.php new file mode 100644 index 00000000..5b7347e9 --- /dev/null +++ b/modules/Nami/NamiServiceProvider.php @@ -0,0 +1,31 @@ +middleware(['web', 'auth:web'])->group(function ($router) { + $router->get('/setting/nami', SettingView::class)->name('setting.nami'); + }); + } +} diff --git a/modules/Nami/SettingTest.php b/modules/Nami/SettingTest.php new file mode 100644 index 00000000..64a715ce --- /dev/null +++ b/modules/Nami/SettingTest.php @@ -0,0 +1,90 @@ +withoutExceptionHandling()->login()->loginNami(); + + test()->get(route('setting.nami'))->assertSeeLivewire(SettingView::class); +}); + +it('it displays active credentials', function () { + test()->withoutExceptionHandling()->login()->loginNami(); + app(NamiSettings::class)->fill(['mglnr' => 903, 'password' => 'secret', 'default_group_id' => 55])->save(); + + Livewire::test(SettingView::class) + ->assertSet('mglnr', 903) + ->assertSet('password', '') + ->assertSet('default_group_id', 55) + ->assertSee('Mitgliedsnummer') + ->assertSee('Standard-Gruppierung') + ->assertSee('Passwort') + ->assertDontSee('secret'); +}); + +it('it saves credentials', function () { + test()->withoutExceptionHandling()->login()->loginNami(); + Auth::success(100, 'secretneu'); + app(NamiSettings::class)->fill(['mglnr' => 903, 'password' => 'secret', 'default_group_id' => 55])->save(); + + Livewire::test(SettingView::class) + ->set('mglnr', 100) + ->set('password', 'secretneu') + ->set('default_group_id', 80) + ->call('save') + ->assertHasNoErrors() + ->assertDispatched('success', 'Einstellungen gespeichert.'); + + $this->assertEquals(100, app(NamiSettings::class)->mglnr); + $this->assertEquals('secretneu', app(NamiSettings::class)->password); + $this->assertEquals('80', app(NamiSettings::class)->default_group_id); +}); + +it('validates fields', function ($name, $value, $errors) { + test()->withoutExceptionHandling()->login()->loginNami(); + Auth::success(100, 'secretneu'); + app(NamiSettings::class)->fill(['mglnr' => 903, 'password' => 'secret', 'default_group_id' => 55])->save(); + + Livewire::test(SettingView::class) + ->set('mglnr', 100) + ->set('password', 'secretneu') + ->set('default_group_id', 80) + ->set($name, $value) + ->call('save') + ->assertHasErrors($errors) + ->assertNotDispatched('success'); +})->with([ + ['password', '', ['password' => 'required']], + ['mglnr', '', ['mglnr' => 'required']], + ['default_group_id', '', ['default_group_id' => 'required']], + ['default_group_id', null, ['default_group_id' => 'required']], +]); + +it('it throws error when saving failed', function () { + test()->withoutExceptionHandling()->login()->loginNami(); + app(NamiSettings::class)->fill(['mglnr' => 903, 'password' => 'secret', 'default_group_id' => 55])->save(); + + Livewire::test(SettingView::class) + ->set('mglnr', 100) + ->set('password', 'secretneu') + ->set('default_group_id', 80) + ->call('save') + ->assertHasErrors(['mglnr' => 'Login fehlgeschlagen.']) + ->assertNotDispatched('success'); + + $settings = app(NamiSettings::class)->refresh(); + $this->assertEquals(903, $settings->mglnr); + $this->assertEquals('secret', $settings->password); + $this->assertEquals('55', $settings->default_group_id); +}); diff --git a/resources/js/views/setting/Nami.vue b/resources/js/views/setting/Nami.vue deleted file mode 100644 index d396325f..00000000 --- a/resources/js/views/setting/Nami.vue +++ /dev/null @@ -1,47 +0,0 @@ - - -