diff --git a/.app.env.example b/.app.env.example index f8014d2a..241e1308 100644 --- a/.app.env.example +++ b/.app.env.example @@ -3,7 +3,6 @@ APP_ENV=production APP_KEY=YOUR_APP_KEY APP_DEBUG=false APP_URL=http://localhost:8000 -APP_MODE=stamm MAIL_MAILER=smtp MAIL_HOST=smtp.mailtrap.io diff --git a/.drone.yml b/.drone.yml index 5f6c150e..a7f7b74a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -41,7 +41,6 @@ steps: APP_ENV: local APP_DEBUG: true APP_URL: http://scoutrobot.test - APP_MODE: stamm LOG_CHANNEL: stack DB_CONNECTION: mysql DB_HOST: db diff --git a/app/Http/Middleware/HandleInertiaRequests.php b/app/Http/Middleware/HandleInertiaRequests.php index 6504553c..5263579a 100644 --- a/app/Http/Middleware/HandleInertiaRequests.php +++ b/app/Http/Middleware/HandleInertiaRequests.php @@ -3,7 +3,7 @@ namespace App\Http\Middleware; use App\Http\Resources\UserResource; -use App\Setting\GeneralSettings; +use App\Module\ModuleSettings; use Illuminate\Http\Request; use Illuminate\Support\Facades\Session; use Inertia\Middleware; @@ -53,7 +53,7 @@ class HandleInertiaRequests extends Middleware return session()->get('title', ''); }, 'settings' => [ - 'modules' => app(GeneralSettings::class)->modules, + 'modules' => app(ModuleSettings::class)->modules, ], ]; } diff --git a/app/Module/Module.php b/app/Module/Module.php new file mode 100644 index 00000000..33b40508 --- /dev/null +++ b/app/Module/Module.php @@ -0,0 +1,35 @@ + 'Zahlungs-Management', + static::COURSE => 'Ausbildung', + }; + } + + /** + * @return array + */ + public static function forSelect(): array + { + return array_map(fn ($module) => ['id' => $module->value, 'name' => $module->title()], static::cases()); + } + + + /** + * @return array + */ + public static function values(): array + { + return array_map(fn ($module) => $module->value, static::cases()); + } +} diff --git a/app/Module/ModuleIndexAction.php b/app/Module/ModuleIndexAction.php new file mode 100644 index 00000000..7005e061 --- /dev/null +++ b/app/Module/ModuleIndexAction.php @@ -0,0 +1,35 @@ + + */ + public function handle(ModuleSettings $settings): array + { + return [ + 'data' => [ + 'modules' => $settings->modules, + ], + 'meta' => ['modules' => Module::forSelect()], + ]; + } + + public function asController(ModuleSettings $settings): Response + { + session()->put('menu', 'setting'); + session()->put('title', 'Module'); + + return Inertia::render('setting/Module', [ + 'data' => $this->handle($settings), + ]); + } +} diff --git a/app/Module/ModuleSettings.php b/app/Module/ModuleSettings.php new file mode 100644 index 00000000..13e057c2 --- /dev/null +++ b/app/Module/ModuleSettings.php @@ -0,0 +1,43 @@ + */ + public array $modules; + + public static function group(): string + { + return 'module'; + } + + public static function slug(): string + { + return 'module'; + } + + public static function title(): string + { + return 'Module'; + } + + public static function indexAction(): string + { + return ModuleIndexAction::class; + } + + public static function storeAction(): string + { + return ModuleStoreAction::class; + } + + public function hasModule(string $module): bool + { + return in_array($module, $this->modules); + } +} diff --git a/app/Module/ModuleStoreAction.php b/app/Module/ModuleStoreAction.php new file mode 100644 index 00000000..aab3dff2 --- /dev/null +++ b/app/Module/ModuleStoreAction.php @@ -0,0 +1,45 @@ + $input + */ + public function handle(array $input): void + { + $settings = app(ModuleSettings::class); + + $settings->fill([ + 'modules' => $input['modules'], + ]); + + $settings->save(); + } + + /** + * @return array + */ + public function rules(): array + { + return [ + 'modules' => 'present|array', + 'modules.*' => ['string', Rule::in(Module::values())], + ]; + } + + public function asController(ActionRequest $request): RedirectResponse + { + $this->handle($request->validated()); + + return redirect()->back(); + } +} diff --git a/app/Setting/GeneralSettings.php b/app/Setting/GeneralSettings.php deleted file mode 100644 index 0d2a4f89..00000000 --- a/app/Setting/GeneralSettings.php +++ /dev/null @@ -1,26 +0,0 @@ - */ - public array $modules; - - public bool $single_view; - - /** @var array */ - public array $allowed_nami_accounts; - - public static function group(): string - { - return 'general'; - } - - public function hasModule(string $module): bool - { - return in_array($module, $this->modules); - } -} diff --git a/app/Setting/SettingServiceProvider.php b/app/Setting/SettingServiceProvider.php index c6e454db..d5fd0e85 100644 --- a/app/Setting/SettingServiceProvider.php +++ b/app/Setting/SettingServiceProvider.php @@ -4,6 +4,7 @@ namespace App\Setting; use App\Invoice\InvoiceSettings; use App\Mailgateway\MailgatewaySettings; +use App\Module\ModuleSettings; use Illuminate\Support\ServiceProvider; class SettingServiceProvider extends ServiceProvider @@ -25,6 +26,7 @@ class SettingServiceProvider extends ServiceProvider */ public function boot() { + app(SettingFactory::class)->register(ModuleSettings::class); app(SettingFactory::class)->register(InvoiceSettings::class); app(SettingFactory::class)->register(MailgatewaySettings::class); app(SettingFactory::class)->register(NamiSettings::class); diff --git a/config/app.php b/config/app.php index 4a911b07..5f2a4bbd 100644 --- a/config/app.php +++ b/config/app.php @@ -94,18 +94,6 @@ return [ 'fallback_locale' => 'en', - /* - |-------------------------------------------------------------------------- - | App Mode - |-------------------------------------------------------------------------- - | - | The mode of the app will set some default settings for you on initial - | database setup. You can change these Settings anytime later, but it - | usually defines a good starting point for you to set up the world. - | - */ - 'mode' => env('APP_MODE', 'stamm'), - /* |-------------------------------------------------------------------------- | Faker Locale diff --git a/database/settings/2021_11_18_230152_create_general_settings.php b/database/settings/2021_11_18_230152_create_general_settings.php index 92b86096..79ebe103 100644 --- a/database/settings/2021_11_18_230152_create_general_settings.php +++ b/database/settings/2021_11_18_230152_create_general_settings.php @@ -4,29 +4,9 @@ use Spatie\LaravelSettings\Migrations\SettingsMigration; class CreateGeneralSettings extends SettingsMigration { - /** - * @return array|bool> - */ - public function defaults(string $mode): array - { - $defaults = [ - 'diözese' => [ - 'modules' => ['courses'], - 'single_view' => false, - ], - 'stamm' => [ - 'modules' => ['bill', 'courses'], - 'single_view' => true, - ], - ]; - - return $defaults[$mode]; - } - public function up(): void { - $defaults = $this->defaults(config('app.mode')); - $this->migrator->add('general.modules', $defaults['modules']); - $this->migrator->add('general.single_view', $defaults['single_view']); + $this->migrator->add('general.modules', []); + $this->migrator->add('general.single_view', false); } } diff --git a/database/settings/2023_11_16_101137_create_module_settings.php b/database/settings/2023_11_16_101137_create_module_settings.php new file mode 100644 index 00000000..320e86c0 --- /dev/null +++ b/database/settings/2023_11_16_101137_create_module_settings.php @@ -0,0 +1,14 @@ +migrator->delete('general.modules'); + $this->migrator->delete('general.single_view'); + $this->migrator->add('module.modules', collect(Module::cases())->map(fn ($module) => $module->value)); + } +}; diff --git a/resources/js/views/setting/Module.vue b/resources/js/views/setting/Module.vue new file mode 100644 index 00000000..099ca54e --- /dev/null +++ b/resources/js/views/setting/Module.vue @@ -0,0 +1,51 @@ + + + diff --git a/tests/Feature/ModuleTest.php b/tests/Feature/ModuleTest.php new file mode 100644 index 00000000..eb5423f2 --- /dev/null +++ b/tests/Feature/ModuleTest.php @@ -0,0 +1,63 @@ +login()->loginNami(); + ModuleSettings::fake(['modules' => ['bill']]); + + $response = $this->get('/setting/module'); + + $response->assertOk(); + $this->assertCount(count(Module::cases()), $this->inertia($response, 'data.meta.modules')); + $this->assertInertiaHas([ + 'name' => 'Zahlungs-Management', + 'id' => 'bill', + ], $response, 'data.meta.modules.0'); + $this->assertEquals(['bill'], $this->inertia($response, 'data.data.modules')); + } + + public function testItSavesSettings(): void + { + $this->login()->loginNami(); + + $response = $this->from('/setting/module')->post('/setting/module', [ + 'modules' => ['bill'], + ]); + + $response->assertRedirect('/setting/module'); + $this->assertEquals(['bill'], app(ModuleSettings::class)->modules); + } + + public function testModuleMustExists(): void + { + $this->login()->loginNami(); + + $response = $this->from('/setting/module')->post('/setting/module', [ + 'modules' => ['lalala'], + ]); + + $response->assertSessionHasErrors('modules.0'); + } + + public function testItReturnsModulesOnEveryPage(): void + { + $this->login()->loginNami(); + ModuleSettings::fake(['modules' => ['bill']]); + + $response = $this->get('/'); + + $this->assertEquals(['bill'], $this->inertia($response, 'settings.modules')); + } +}