diff --git a/src/Extend/NotificationChannel.php b/src/Extend/NotificationChannel.php new file mode 100644 index 000000000..332d6ab19 --- /dev/null +++ b/src/Extend/NotificationChannel.php @@ -0,0 +1,34 @@ +channel = $channel; + } + + public function extend(Container $container, Extension $extension = null) + { + NotificationPreference::addChannel($this->channel, $this->enabled); + } + + public function enabled(string $type, bool $enabled = true) + { + $this->enabled[$type] = $enabled; + + return $this; + } +} diff --git a/src/Extend/UserPreferences.php b/src/Extend/UserPreferences.php new file mode 100644 index 000000000..6ab08d804 --- /dev/null +++ b/src/Extend/UserPreferences.php @@ -0,0 +1,22 @@ +belongsTo(User::class); } - public static function addChannel(string $channel) + public static function addChannel(string $channel, array $defaults = []) { - static::$channels[] = $channel; + static::$channels[$channel] = $defaults; } - public static function setNotificationPreference(User $user, string $type, string $channel, bool $enabled = true) + public static function getNotificationPreferences(User $user, string $channel = null, string $type = null) { - if (in_array($channel, static::$channels)) { + $saved = $user->notificationPreferences() + ->when('type', function ($query, $type) { + $query->where('type', $type); + }) + ->whereIn('channel', $channel ? [$channel] : array_keys(static::$channels)) + ->get(); + + if ($channel && $type) { + return $saved->first(); + } + + return $saved; + } + + public static function setNotificationPreference(User $user, string $channel, string $type, bool $enabled = true) + { + if (array_key_exists($channel, static::$channels)) { $attributes = [ 'channel' => $channel, 'type' => $type diff --git a/tests/integration/extenders/NotificationChannelTest.php b/tests/integration/extenders/NotificationChannelTest.php new file mode 100644 index 000000000..291212d76 --- /dev/null +++ b/tests/integration/extenders/NotificationChannelTest.php @@ -0,0 +1,44 @@ +prepareDatabase([ + 'users' => [ + $this->normalUser() + ] + ]); + } + + private function add_channel() + { + $this->extend(new NotificationChannel('test')); + } + + /** + * @test + */ + public function can_add_notification_channel() + { + $this->add_channel(); + + /** @var User $user */ + $user = User::find(2); + + } +} diff --git a/tests/integration/extenders/UserPreferencesTest.php b/tests/integration/extenders/UserPreferencesTest.php new file mode 100644 index 000000000..3c84dc329 --- /dev/null +++ b/tests/integration/extenders/UserPreferencesTest.php @@ -0,0 +1,61 @@ +prepareDatabase([ + 'users' => [ + $this->normalUser() + ] + ]); + } + + private function add_preference() + { + $this->extend( + (new UserPreferences()) + ->add('test', 'boolval', false) + ); + } + + /** + * @test + */ + public function can_add_user_preference() + { + $this->add_preference(); + + /** @var User $user */ + $user = User::find(2); + $this->assertEquals(false, Arr::get($user->preferences, 'test')); + } + + /** + * @test + */ + public function can_store_user_preference() + { + $this->add_preference(); + + /** @var User $user */ + $user = User::find(2); + + $user->setPreference('test', true); + + $this->assertEquals(true, $user->test); + } +}