From 46c0e92daebbdf0f5480c2f3c2bc6b7b2a4bc9f0 Mon Sep 17 00:00:00 2001 From: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> Date: Mon, 24 Jun 2024 15:46:37 +0100 Subject: [PATCH] Code refactor Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> --- resources/lang/en/default.php | 1 + src/Extension.php | 52 +++++++++++++++++++++++ src/Models/Address.php | 28 ------------ src/Models/Notification.php | 13 ------ src/Models/Observers/CustomerObserver.php | 5 +++ 5 files changed, 58 insertions(+), 41 deletions(-) diff --git a/resources/lang/en/default.php b/resources/lang/en/default.php index ce35cde..882021c 100644 --- a/resources/lang/en/default.php +++ b/resources/lang/en/default.php @@ -23,6 +23,7 @@ 'text_no_reservations' => 'There are no reservations available to show.', 'text_no_inbox' => 'There are no messages available to show', 'text_no_cart_items' => 'There are no menus added in your cart.', + 'text_charts_customers' => 'Customers', 'text_mail_admin_password_reset' => 'Password reset email to admin', 'text_mail_admin_password_reset_request' => 'Password reset request email to admin', diff --git a/src/Extension.php b/src/Extension.php index edf3a1e..c30bd94 100644 --- a/src/Extension.php +++ b/src/Extension.php @@ -4,16 +4,20 @@ use Igniter\Admin\Classes\MainMenuItem; use Igniter\Admin\Classes\Navigation; +use Igniter\Admin\DashboardWidgets\Charts; +use Igniter\Admin\DashboardWidgets\Statistics; use Igniter\Admin\Facades\AdminMenu; use Igniter\Admin\Facades\Template; use Igniter\Flame\Igniter; use Igniter\Local\Models\Location; +use Igniter\System\Contracts\StickyNotification; use Igniter\System\Models\Settings; use Igniter\User\Classes\BladeExtension; use Igniter\User\Console\Commands\AllocatorCommand; use Igniter\User\Console\Commands\ClearUserStateCommand; use Igniter\User\Facades\Auth; use Igniter\User\Models\Customer; +use Igniter\User\Models\Notification; use Igniter\User\Models\Observers\CustomerObserver; use Igniter\User\Models\Observers\UserObserver; use Igniter\User\Models\User; @@ -21,6 +25,7 @@ use Igniter\User\Subscribers\ConsoleSubscriber; use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Foundation\AliasLoader; +use Illuminate\Notifications\Events\NotificationSent; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\RateLimiter; use Illuminate\Support\Facades\Route; @@ -83,6 +88,7 @@ public function boot() }); $this->registerAdminUserPanel(); + $this->extendDashboardChartsDatasets(); Location::extend(function($model) { $model->relation['morphedByMany']['users'] = [User::class, 'name' => 'locationable']; @@ -91,6 +97,33 @@ public function boot() Template::registerHook('endBody', function() { return view('igniter.user::_partials.impersonate_banner'); }); + + Event::listen(NotificationSent::class, function(NotificationSent $event) { + if ($event->response instanceof Notification && is_subclass_of($event->notification, StickyNotification::class)) { + $event->notifiable->notifications() + ->where('type', method_exists($event->notification, 'databaseType') + ? $event->notification->databaseType($event->notifiable) + : get_class($event->notification)) + ->where('id', '!=', $event->response->getKey()) + ->delete(); + } + }); + + Statistics::registerCards(function() { + return [ + 'customer' => [ + 'label' => 'lang:igniter::admin.dashboard.text_total_customer', + 'icon' => ' text-info fa fa-4x fa-users', + 'valueFrom' => function(string $cardCode, $start, $end, $callback): int { + $query = Customer::query(); + + $callback($query); + + return $query->count(); + } + ], + ]; + }); } public function registerAutomationRules() @@ -328,4 +361,23 @@ protected function defineRoutes() (new Classes\RouteRegistrar($router))->all(); }); } + + protected function extendDashboardChartsDatasets() + { + Charts::extend(function($charts) { + $charts->bindEvent('charts.extendDatasets', function() use ($charts) { + $charts->addDataset('reports', [ + 'sets' => [ + 'customers' => [ + 'label' => 'lang:igniter.user::default.text_charts_customers', + 'color' => '#4DB6AC', + 'model' => Customer::class, + 'column' => 'created_at', + 'priority' => 10, + ], + ], + ]); + }); + }); + } } diff --git a/src/Models/Address.php b/src/Models/Address.php index afba3ac..12adb0a 100644 --- a/src/Models/Address.php +++ b/src/Models/Address.php @@ -67,32 +67,4 @@ public function getFormattedAddressAttribute($value) { return format_address($this->toArray(), false); } - - public function forceDelete() - { - $this->forceDeleting = true; - - return tap($this->delete(), function($deleted) { - $this->forceDeleting = false; - }); - } - - protected function performDeleteOnModel() - { - if ($this->forceDeleting) { - return tap($this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(), function() { - $this->exists = false; - }); - } - - $query = $this->setKeysForSaveQuery($this->newModelQuery()); - - $columns = ['customer_id' => null]; - - $query->update($columns); - - $this->syncOriginalAttributes(array_keys($columns)); - - return $this->runSoftDelete(); - } } diff --git a/src/Models/Notification.php b/src/Models/Notification.php index bf09e8a..24b7844 100644 --- a/src/Models/Notification.php +++ b/src/Models/Notification.php @@ -3,7 +3,6 @@ namespace Igniter\User\Models; use Igniter\Flame\Database\Model; -use Igniter\System\Contracts\CriticalNotification; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Prunable; use Illuminate\Notifications\DatabaseNotification; @@ -12,18 +11,6 @@ class Notification extends DatabaseNotification { use Prunable; - protected static function booted(): void - { - static::created(function(self $model) { - if (is_subclass_of($model->type, CriticalNotification::class)) { - $model->notifiable->notifications() - ->where('type', $model->type) - ->where('id', '!=', $model->getKey()) - ->delete(); - } - }); - } - public function getTitleAttribute() { return array_get($this->data ?? [], 'title'); diff --git a/src/Models/Observers/CustomerObserver.php b/src/Models/Observers/CustomerObserver.php index 8764c4e..879b36c 100644 --- a/src/Models/Observers/CustomerObserver.php +++ b/src/Models/Observers/CustomerObserver.php @@ -27,4 +27,9 @@ public function saved(Customer $customer) $customer->saveAddresses(array_get($customer->getAttributes(), 'addresses', [])); } } + + public function deleting(Customer $customer) + { + $customer->addresses()->delete(); + } }