Add Nami Settings

This commit is contained in:
philipp lang 2024-10-25 01:43:13 +02:00
parent f6b1c5a45f
commit 35b28a218e
6 changed files with 192 additions and 60 deletions
app/Setting
config
modules/Nami
resources/js/views/setting

View File

@ -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);

View File

@ -182,6 +182,7 @@ return [
Modules\Module\ModuleServiceProvider::class,
Modules\Invoice\InvoiceServiceProvider::class,
Modules\Mailgateway\MailgatewayServiceProvider::class,
Modules\Nami\NamiServiceProvider::class,
],
/*

View File

@ -0,0 +1,69 @@
<?php
namespace Modules\Nami\Components;
use App\Initialize\Actions\NamiLoginCheckAction;
use App\Setting\NamiSettings;
use Illuminate\Validation\ValidationException;
use Livewire\Component;
use Zoomyboy\LaravelNami\LoginException;
class SettingView extends Component
{
public $settingClass = NamiSettings::class;
public string $password = '';
public string $mglnr = '';
public string $default_group_id = '';
public function rules(): array
{
return [
'password' => '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'
<x-page::setting-layout :active="$settingClass">
<x-slot:right>
<x-form::save-button form="namisettingform"></x-form::save-button>
</x-slot:right>
<form id="namisettingform" class="grow p-6 grid grid-cols-2 gap-3 items-start content-start" wire:submit.prevent="save">
<div class="col-span-full text-gray-100 mb-3">
<p class="text-sm">Hier kannst du deine Zugangsdaten zu NaMi anpassen, falls sich z.B. dein Passwort geändert hat.</p>
</div>
<x-form::text name="mglnr" wire:model="mglnr" label="Mitgliedsnummer"></x-form::text>
<x-form::text name="default_group_id" wire:model="default_group_id" label="Standard-Gruppierung"></x-form::text>
<x-form::text name="password" wire:model="password" label="Passwort" name="password" type="password"></x-form::text>
</form>
</x-page::setting-layout>
HTML;
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Modules\Nami;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Modules\Nami\Components\SettingView;
class NamiServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
app(Router::class)->middleware(['web', 'auth:web'])->group(function ($router) {
$router->get('/setting/nami', SettingView::class)->name('setting.nami');
});
}
}

View File

@ -0,0 +1,90 @@
<?php
namespace Modules\Module;
use App\Setting\NamiSettings;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Livewire\Livewire;
use Modules\Nami\Components\SettingView;
use Tests\TestCase;
use Zoomyboy\LaravelNami\Authentication\Auth;
use Zoomyboy\LaravelNami\Fakes\LoginFake;
uses(TestCase::class);
uses(DatabaseTransactions::class);
it('it renders page', function () {
test()->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);
});

View File

@ -1,47 +0,0 @@
<template>
<page-layout>
<template #right>
<f-save-button form="namisettingform"></f-save-button>
</template>
<setting-layout>
<form id="namisettingform" class="grow p-6 grid grid-cols-2 gap-3 items-start content-start" @submit.prevent="submit">
<div class="col-span-full text-gray-100 mb-3">
<p class="text-sm">Hier kannst du deine Zugangsdaten zu NaMi anpassen, falls sich z.B. dein Passwort geändert hat.</p>
</div>
<f-text id="mglnr" v-model="inner.mglnr" label="Mitgliedsnummer"></f-text>
<f-text id="default_group_id" v-model="inner.default_group_id" label="Standard-Gruppierung"></f-text>
<f-text id="password" v-model="inner.password" label="Passwort" name="password" type="password"></f-text>
</form>
</setting-layout>
</page-layout>
</template>
<script>
import SettingLayout from './Layout.vue';
export default {
components: {
SettingLayout,
},
props: {
data: {
type: Object,
default: () => {
return {};
},
},
},
data: function () {
return {
inner: {...this.data},
};
},
methods: {
submit() {
this.$inertia.post('/setting/nami', this.inner, {
onSuccess: () => this.$success('Einstellungen gespeichert.'),
});
},
},
};
</script>