Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Jun 24, 2024
1 parent 459c4d9 commit 46c0e92
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 41 deletions.
1 change: 1 addition & 0 deletions resources/lang/en/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
52 changes: 52 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@

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;
use Igniter\User\Subscribers\AssigneeUpdatedSubscriber;
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;
Expand Down Expand Up @@ -83,6 +88,7 @@ public function boot()
});

$this->registerAdminUserPanel();
$this->extendDashboardChartsDatasets();

Location::extend(function($model) {
$model->relation['morphedByMany']['users'] = [User::class, 'name' => 'locationable'];
Expand All @@ -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()
Expand Down Expand Up @@ -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,
],
],
]);
});
});
}
}
28 changes: 0 additions & 28 deletions src/Models/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
13 changes: 0 additions & 13 deletions src/Models/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand Down
5 changes: 5 additions & 0 deletions src/Models/Observers/CustomerObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public function saved(Customer $customer)
$customer->saveAddresses(array_get($customer->getAttributes(), 'addresses', []));
}
}

public function deleting(Customer $customer)
{
$customer->addresses()->delete();

Check failure on line 33 in src/Models/Observers/CustomerObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - Static Analysis

Call to an undefined method Igniter\User\Models\Customer::addresses().

Check failure on line 33 in src/Models/Observers/CustomerObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - Static Analysis

Call to an undefined method Igniter\User\Models\Customer::addresses().
}
}

0 comments on commit 46c0e92

Please sign in to comment.