Skip to content

Commit

Permalink
refactor(Orders): Switch to using Laravel notifcations to display mes…
Browse files Browse the repository at this point in the history
…sages to assigned approver for order accounts pending approval
  • Loading branch information
Patrick Mulligan committed Apr 11, 2024
1 parent 1189cbe commit 415500c
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 41 deletions.
40 changes: 0 additions & 40 deletions app/Listeners/Users/Orders/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Orders
public function subscribe($events)
{
$events->listen(UserDisplay::class, self::class . '@handleUserDisplay');
$events->listen(UserNotifying::class, self::class . '@handleUserNotifying');
}

/**
Expand Down Expand Up @@ -273,43 +272,4 @@ public function handleUserDisplay(UserDisplay $event)
$content
);
}

/**
* Display data for a user
*
* @param UserNotifying $event
* @return void
*/
public function handleUserNotifying(UserNotifying $event)
{
$user = $event->user;

$a = (new Account)->getTable();
$o = (new Order)->getTable();

$rows = Account::query()
->select($a . '.*')
->join($o, $o . '.id', $a . '.orderid')
->where($a . '.approveruserid', '=', $user->id)
->whereNull($a . '.datetimeremoved')
->whereNull($a . '.datetimeapproved')
->whereNull($a . '.datetimedenied')
->whereNull($a . '.datetimepaid')
->whereNull($o . '.datetimeremoved')
->orderBy($o . '.datetimecreated', 'asc')
->orderBy($a . '.id', 'asc')
->get();

foreach ($rows as $row)
{
$title = trans('orders::orders.orders');

$account = $row->purchasewbse;
$account = $account ?: $row->purchaseio;

$content = '<a href="' . route('site.orders.read', ['id' => $row->orderid]) . '">' . trans('orders::orders.purchase account waiting approval', ['order' => $row->orderid, 'account' => $account]) . '</a>';

$event->addNotification(new Notification($title, $content));
}
}
}
24 changes: 24 additions & 0 deletions app/Modules/Orders/Events/NotifyApprover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Modules\Orders\Events;

use App\Modules\Orders\Models\Account;

class NotifyApprover
{
/**
* @var Account
*/
public $account;

/**
* Constructor
*
* @param Account $account
* @return void
*/
public function __construct(Account $account)
{
$this->account = $account;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Modules\Orders\Models\Order;
use App\Modules\Orders\Models\NoticeStatus;
use App\Modules\Users\Models\User;
use App\Modules\Orders\Events\NotifyApprover;
use Carbon\Carbon;

/**
Expand Down Expand Up @@ -712,7 +713,8 @@ public function update($id, Request $request)
// New approver. Reset dates and notification status.
$row->datetimeapproved = null;
$row->datetimedenied = null;

$row->approveruserid = $approveruserid;
event(new NotifyApprover($row));
$row->notice = NoticeStatus::PENDING_APPROVAL;
}

Expand Down
37 changes: 37 additions & 0 deletions app/Modules/Orders/Listeners/NotifyAccountApprover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Modules\Orders\Listeners;

use Illuminate\Events\Dispatcher;
use App\Modules\Orders\Events\NotifyApprover;
use App\Modules\Orders\Models\Account;
use App\Modules\Orders\Notifications\AccountApprovalNeeded;

/**
* Account listener
*/
class NotifyAccountApprover
{
/**
* Register the listeners for the subscriber.
*
* @param Dispatcher $events
* @return void
*/
public function subscribe(Dispatcher $events): void
{
$events->listen(NotifyApprover::class, self::class . '@handleAccountCreated');
}

/**
* Plugin that loads module positions within content
*
* @param GroupReading $event
* @return void
*/
public function handleAccountCreated(NotifyApprover $event): void
{
$account = $event->account;
$account->approver->notify(new AccountApprovalNeeded($event->account));
}
}
2 changes: 2 additions & 0 deletions app/Modules/Orders/Models/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Modules\History\Traits\Historable;
use App\Modules\Orders\Helpers\Currency;
use App\Modules\Orders\Events\AccountCreating;
use App\Modules\Orders\Events\NotifyApprover;
use App\Modules\Orders\Events\AccountUpdating;
use Carbon\Carbon;

Expand Down Expand Up @@ -86,6 +87,7 @@ class Account extends Model
protected $dispatchesEvents = [
'creating' => AccountCreating::class,
'updating' => AccountUpdating::class,
'created' => NotifyApprover::class,
];

/**
Expand Down
65 changes: 65 additions & 0 deletions app/Modules/Orders/Notifications/AccountApprovalNeeded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Modules\Orders\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use App\Modules\Orders\Models\Account;

class AccountApprovalNeeded extends Notification
{
use Queueable;

/**
* The user request
*
* @var Account $account
*/
private $account;

/**
* Constructor
*
* @param UserRequest $userrequest
* @return void
*/
public function __construct(Account $account)
{
$this->account = $account;
}

/**
* What methods can this notificaiton be sent
*
* @param object $notifiable
* @return array<int,string>
*/
public function via($notifiable)
{
return ['database'];
}

/**
* Generate a message formatted for database
*
* @param object $notifiable
* @return array<string,string>
*/
public function toArray($notifiable)
{
$title = trans('orders::orders.orders');

$account = $this->account->purchasewbse;
$account = $account ?: $this->account->purchaseio;

$content = '<a href="' . route('site.orders.read', ['id' => $this->account->orderid]) . '">';
$content .= trans('orders::orders.purchase account waiting approval', ['order' => $this->account->orderid, 'account' => $account]);
$content .= '</a>';

return [
'title' => $title,
'data' => $content
];
}
}
2 changes: 2 additions & 0 deletions app/Modules/Orders/Providers/OrdersServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Modules\Orders\Listeners\UserOrders;
use App\Modules\Orders\Listeners\RouteCollector;
use App\Modules\Orders\Listeners\SyncItemsToProduct;
use App\Modules\Orders\Listeners\NotifyAccountApprover;
use App\Modules\Orders\LogProcessors\Orders as OrdersProcessor;
use Nwidart\Modules\Facades\Module;

Expand Down Expand Up @@ -57,6 +58,7 @@ public function boot()
if (Module::isEnabled('users'))
{
$this->app['events']->subscribe(new UserOrders);
$this->app['events']->subscribe(new NotifyAccountApprover);
}

if (Module::isEnabled('menus'))
Expand Down

0 comments on commit 415500c

Please sign in to comment.