Browse Source

Merge pull request 'mahdi' (#5) from mahdi into master

Reviewed-on: #5
mohammad
mahdihty 4 years ago
parent
commit
a6b86ab751
  1. 33
      app/Listeners/BusinessUpdateListener.php
  2. 66
      app/Listeners/BusinessUserCreateNotif.php
  3. 64
      app/Listeners/ProjectUserCreateNotif.php
  4. 68
      app/Listeners/TaskCreateNotif.php
  5. 92
      app/Listeners/TaskUpdateNotif.php
  6. 1
      app/Models/User.php
  7. 37
      app/Utilities/HelperClass/NotificationHelper.php

33
app/Listeners/BusinessUpdateListener.php

@ -2,26 +2,10 @@
namespace App\Listeners; namespace App\Listeners;
use App\Models\User;
use App\Models\Business;
use Illuminate\Support\Arr;
<<<<<<< Updated upstream
use App\Channels\FcmChannel;
use App\Events\BusinessUpdate;
use App\Events\BusinessUserCreate;
use App\Notifications\DBNotification;
use App\Notifications\FcmNotification;
use App\Notifications\MailNotification;
use Illuminate\Queue\InteractsWithQueue;
=======
use App\Events\BusinessUpdate; use App\Events\BusinessUpdate;
use App\Events\BusinessUserCreate;
use App\Models\Business;
use App\Notifications\DBNotification; use App\Notifications\DBNotification;
use App\Notifications\MailNotification; use App\Notifications\MailNotification;
use Illuminate\Queue\InteractsWithQueue;
>>>>>>> Stashed changes
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Notification;
class BusinessUpdateListener class BusinessUpdateListener
@ -29,7 +13,6 @@ class BusinessUpdateListener
public function handle(BusinessUpdate $event) public function handle(BusinessUpdate $event)
{ {
$payload = $event->message; $payload = $event->message;
<<<<<<< Updated upstream
$business = Business::findOrFail($payload->business)->load('owners'); $business = Business::findOrFail($payload->business)->load('owners');
$this->checkWalletIsRunningLow($business); $this->checkWalletIsRunningLow($business);
@ -44,7 +27,7 @@ class BusinessUpdateListener
$hours = $moving_average_days * 24; $hours = $moving_average_days * 24;
$business->load([ $business->load([
'cost' => fn ($query) => $query->where('created_at', '>=', now('Asia/Tehran')->subHours($hours)),
'cost' => fn($query) => $query->where('created_at', '>=', now('Asia/Tehran')->subHours($hours)),
]); ]);
// The wallet is running out // The wallet is running out
@ -78,7 +61,7 @@ class BusinessUpdateListener
$recent_payments_number = 10; $recent_payments_number = 10;
$negativity_threshold = 20; $negativity_threshold = 20;
$business->load([ $business->load([
'transactions' => fn ($query) => $query->where('succeeded', '=', true)
'transactions' => fn($query) => $query->where('succeeded', '=', true)
->orderBy('created_at') ->orderBy('created_at')
->take($recent_payments_number), ->take($recent_payments_number),
]); ]);
@ -93,16 +76,6 @@ class BusinessUpdateListener
$business->update(['suspended_at' => now(),]); $business->update(['suspended_at' => now(),]);
Notification::send($business->owners, new MailNotification($message)); Notification::send($business->owners, new MailNotification($message));
Notification::send($business->owners, new DBNotification($message)); Notification::send($business->owners, new DBNotification($message));
=======
$wallet = $payload?->data?->diff?->wallet;
$owners = Business::findOrFail($payload->business)->owners;
$message = ['body' => 'Test'];
if ($wallet < 0) {
Notification::send($owners, new MailNotification($message));
Notification::send($owners, new DBNotification($message));
>>>>>>> Stashed changes
} }
} }
} }

66
app/Listeners/BusinessUserCreateNotif.php

@ -2,27 +2,27 @@
namespace App\Listeners; namespace App\Listeners;
use App\Channels\FcmChannel;
use App\Events\BusinessUserCreate; use App\Events\BusinessUserCreate;
use App\Models\Business; use App\Models\Business;
use App\Models\User; use App\Models\User;
use App\Notifications\DBNotification;
use App\Notifications\FcmNotification;
use App\Notifications\MailNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Notification;
use App\Utilities\HelperClass\NotificationHelper;
class BusinessUserCreateNotif class BusinessUserCreateNotif
{ {
/**
* @var NotificationHelper
*/
public $helper;
/** /**
* Create the event listener. * Create the event listener.
* *
* @return void * @return void
*/ */
public function __construct()
public function __construct(NotificationHelper $helper)
{ {
//
$this->helper = $helper;
} }
/** /**
@ -42,52 +42,10 @@ class BusinessUserCreateNotif
$new_user = User::findOrFail($payload->data->original->user_id); $new_user = User::findOrFail($payload->data->original->user_id);
$owners = Business::findOrFail($payload->business)->owners()->where('id', '!=', $new_user->id)->get(); $owners = Business::findOrFail($payload->business)->owners()->where('id', '!=', $new_user->id)->get();
$notif = $this->makeNotif($payload,['business' => request('_business_info')['name'], 'user' => $new_user->name]);
$users = $owners->prepend($new_user); $users = $owners->prepend($new_user);
$this->sendNotifications($users, $notif);
}
/**
* Make notification object
*
* @param $payload
* @param array $options
* @return array
*/
public function makeNotif($payload, $options = []) {
return [
'greeting' => $this->getMessageLine($payload, 'greeting'),
'subject' => $this->getMessageLine($payload, 'subject'),
'title' => $this->getMessageLine($payload, 'title'),
'body' => $this->getMessageLine($payload, 'body', $options)
];
}
/**
* Fetch message from notifications lang file
*
* @param $payload
* @param $key
* @param null $options
* @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
*
*/
public function getMessageLine($payload, $key, $options = [])
{
return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options);
}
/**
* Call notifications
*
* @param $users
* @param $notif
*/
public function sendNotifications($users, $notif) {
Notification::send($users, new MailNotification($notif));
Notification::send($users, new DBNotification($notif));
Notification::send($users, new FcmNotification($notif));
$this->helper->makeNotif($payload, null,
['business' => request('_business_info')['name'], 'user' => $new_user->name]
)->sendNotifications($users, 'critical');
} }
} }

64
app/Listeners/ProjectUserCreateNotif.php

@ -5,23 +5,23 @@ namespace App\Listeners;
use App\Events\ProjectUserCreate; use App\Events\ProjectUserCreate;
use App\Models\Project; use App\Models\Project;
use App\Models\User; use App\Models\User;
use App\Notifications\DBNotification;
use App\Notifications\FcmNotification;
use App\Notifications\MailNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Notification;
use App\Utilities\HelperClass\NotificationHelper;
class ProjectUserCreateNotif class ProjectUserCreateNotif
{ {
/**
* @var NotificationHelper
*/
public $helper;
/** /**
* Create the event listener. * Create the event listener.
* *
* @return void * @return void
*/ */
public function __construct()
public function __construct(NotificationHelper $helper)
{ {
//
$this->helper = $helper;
} }
/** /**
@ -42,52 +42,10 @@ class ProjectUserCreateNotif
$owners = User::whereIn('id', array_keys($owners_id))->get(); $owners = User::whereIn('id', array_keys($owners_id))->get();
$notif = $this->makeNotif($payload,['project' => $project->name, 'user' => $new_user->name]);
$users = $owners->prepend($new_user); $users = $owners->prepend($new_user);
$this->sendNotifications($users, $notif);
}
/**
* Make notification object
*
* @param $payload
* @param array $options
* @return array
*/
public function makeNotif($payload, $options = []) {
return [
'greeting' => $this->getMessageLine($payload, 'greeting'),
'subject' => $this->getMessageLine($payload, 'subject'),
'title' => $this->getMessageLine($payload, 'title'),
'body' => $this->getMessageLine($payload, 'body', $options)
];
}
/**
* Fetch message from notifications lang file
*
* @param $payload
* @param $key
* @param null $options
* @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
*
*/
public function getMessageLine($payload, $key, $options = [])
{
return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options);
}
/**
* Call notifications
*
* @param $users
* @param $notif
*/
public function sendNotifications($users, $notif) {
Notification::send($users, new MailNotification($notif));
Notification::send($users, new DBNotification($notif));
Notification::send($users, new FcmNotification($notif));
$this->helper->makeNotif($payload, null,
['project' => $project->name, 'user' => $new_user->name]
)->sendNotifications($users, 'critical');
} }
} }

68
app/Listeners/TaskCreateNotif.php

@ -5,21 +5,23 @@ namespace App\Listeners;
use App\Events\TaskCreate; use App\Events\TaskCreate;
use App\Models\Task; use App\Models\Task;
use App\Models\User; use App\Models\User;
use App\Notifications\DBNotification;
use App\Notifications\FcmNotification;
use App\Notifications\MailNotification;
use Illuminate\Support\Facades\Notification;
use App\Utilities\HelperClass\NotificationHelper;
class TaskCreateNotif class TaskCreateNotif
{ {
/**
* @var NotificationHelper
*/
public $helper;
/** /**
* Create the event listener. * Create the event listener.
* *
* @return void * @return void
*/ */
public function __construct()
public function __construct(NotificationHelper $helper)
{ {
//
$this->helper = $helper;
} }
/** /**
@ -44,61 +46,15 @@ class TaskCreateNotif
$user = User::findOrFail($payload->data->original->assignee_id); $user = User::findOrFail($payload->data->original->assignee_id);
$task = Task::findOrFail($payload->data->original->id); $task = Task::findOrFail($payload->data->original->id);
$notif = $this->makeNotif($payload, 'assignee', ['task' => $task->title]);
$this->sendNotifications($user, $notif);
$this->helper->makeNotif($payload, 'assignee', ['task' => $task->title])
->sendNotifications($user, 'critical');
} }
public function approverNotifHandler($payload) { public function approverNotifHandler($payload) {
$user = User::findOrFail($payload->data->original->approver_id); $user = User::findOrFail($payload->data->original->approver_id);
$task = Task::findOrFail($payload->data->original->id); $task = Task::findOrFail($payload->data->original->id);
$notif = $this->makeNotif($payload, 'approver', ['task' => $task->title]);
$this->sendNotifications($user, $notif);
}
/**
* Make notification object
*
* @param $payload
* @param null $route
* @param array $options
* @return array
*/
public function makeNotif($payload, $route = null, $options = []) {
$route = $route == null ? "" : $route.'.';
return [
'greeting' => $this->getMessageLine($payload, $route.'greeting'),
'subject' => $this->getMessageLine($payload, $route.'subject'),
'title' => $this->getMessageLine($payload, $route.'title'),
'body' => $this->getMessageLine($payload, $route.'body', $options)
];
}
/**
* Fetch message from notifications lang file
*
* @param $payload
* @param $key
* @param null $options
* @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
*
*/
public function getMessageLine($payload, $key, $options = [])
{
return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options);
}
/**
* Call notifications
*
* @param $users
* @param $notif
*/
public function sendNotifications($users, $notif) {
Notification::send($users, new MailNotification($notif));
Notification::send($users, new DBNotification($notif));
Notification::send($users, new FcmNotification($notif));
$this->helper->makeNotif($payload, 'approver', ['task' => $task->title])
->sendNotifications($user, 'critical');
} }
} }

92
app/Listeners/TaskUpdateNotif.php

@ -5,21 +5,23 @@ namespace App\Listeners;
use App\Events\TaskUpdate; use App\Events\TaskUpdate;
use App\Models\Task; use App\Models\Task;
use App\Models\User; use App\Models\User;
use App\Notifications\DBNotification;
use App\Notifications\FcmNotification;
use App\Notifications\MailNotification;
use Illuminate\Support\Facades\Notification;
use App\Utilities\HelperClass\NotificationHelper;
class TaskUpdateNotif class TaskUpdateNotif
{ {
/**
* @var NotificationHelper
*/
public $helper;
/** /**
* Create the event listener. * Create the event listener.
* *
* @return void * @return void
*/ */
public function __construct()
public function __construct(NotificationHelper $helper)
{ {
//
$this->helper = $helper;
} }
/** /**
@ -49,18 +51,16 @@ class TaskUpdateNotif
$user = User::findOrFail($payload->data->diff->assignee_id); $user = User::findOrFail($payload->data->diff->assignee_id);
$task = Task::findOrFail($payload->data->original->id); $task = Task::findOrFail($payload->data->original->id);
$notif = $this->makeNotif($payload, 'assignee', ['task' => $task->title]);
$this->sendNotifications($user, $notif);
$this->helper->makeNotif($payload, 'assignee', ['task' => $task->title])
->sendNotifications($user, 'critical');
} }
public function approverNotifHandler($payload) { public function approverNotifHandler($payload) {
$user = User::findOrFail($payload->data->diff->approver_id); $user = User::findOrFail($payload->data->diff->approver_id);
$task = Task::findOrFail($payload->data->original->id); $task = Task::findOrFail($payload->data->original->id);
$notif = $this->makeNotif($payload, 'approver', ['task' => $task->title]);
$this->sendNotifications($user, $notif);
$this->helper->makeNotif($payload, 'approver', ['task' => $task->title])
->sendNotifications($user, 'critical');
} }
public function completedNotifHandler($payload){ public function completedNotifHandler($payload){
@ -74,9 +74,8 @@ class TaskUpdateNotif
$users = User::whereIn('id', $user_ids)->where('id', '!=', auth()->id())->get(); $users = User::whereIn('id', $user_ids)->where('id', '!=', auth()->id())->get();
$notif = $this->makeNotif($payload, 'completed', ['task' => $task->title]);
$this->sendNotifications($users, $notif);
$this->helper->makeNotif($payload, 'completed', ['task' => $task->title])
->sendNotifications($users, 'critical');
} }
public function readyNotifHandler($payload) public function readyNotifHandler($payload)
@ -90,66 +89,7 @@ class TaskUpdateNotif
$users = User::whereIn('id', $user_ids)->where('id', '!=', auth()->id())->get(); $users = User::whereIn('id', $user_ids)->where('id', '!=', auth()->id())->get();
$notif = $this->makeNotif($payload, 'ready', ['task' => $task->title]);
$this->sendNotifications($users, $notif);
}
/**
* Make notification object
*
* @param $payload
* @param null $route
* @param array $options
* @return array
*/
public function makeNotif($payload, $route = null, $options = []) {
$route = $route == null ? "" : $route.'.';
return [
'greeting' => $this->getMessageLine($payload, $route.'greeting'),
'subject' => $this->getMessageLine($payload, $route.'subject'),
'title' => $this->getMessageLine($payload, $route.'title'),
'body' => $this->getMessageLine($payload, $route.'body', $options)
];
}
/**
* Fetch message from notifications lang file
*
* @param $payload
* @param $key
* @param null $options
* @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
*
*/
public function getMessageLine($payload, $key, $options = [])
{
return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options);
}
/**
* Call notifications
*
* @param $users
* @param $notif
*/
public function sendNotifications($users, $notif) {
// switch ($level) {
// case "emergency":
//// Notification::send($users, new SmsNotification($notif));
// case "critical":
// Notification::send($users, new MailNotification($notif));
// case "high":
// Notification::send($users, new DBNotification($notif));
// case "medium":
// Notification::send($users, new FcmNotification($notif));
// case "low":
//// Notification::send($users, new SocketNotification($notif));
// default:
//// Notification::send($users, new SocketNotification($notif));
// }
Notification::send($users, new MailNotification($notif));
Notification::send($users, new DBNotification($notif));
Notification::send($users, new FcmNotification($notif));
$this->helper->makeNotif($payload, 'ready', ['task' => $task->title])
->sendNotifications($users, 'critical');
} }
} }

1
app/Models/User.php

@ -11,7 +11,6 @@ use Illuminate\Validation\Rule;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\HasMedia;
use Illuminate\Auth\Authenticatable; use Illuminate\Auth\Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\MediaLibrary\InteractsWithMedia; use Spatie\MediaLibrary\InteractsWithMedia;
use Illuminate\Foundation\Auth\Access\Authorizable; use Illuminate\Foundation\Auth\Access\Authorizable;
use Spatie\MediaLibrary\MediaCollections\Models\Media; use Spatie\MediaLibrary\MediaCollections\Models\Media;

37
app/Utilities/HelperClass/NotificationHelper.php

@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Notification;
class NotificationHelper class NotificationHelper
{ {
protected $notif;
/** /**
* Make notification object * Make notification object
* *
@ -21,12 +22,13 @@ class NotificationHelper
*/ */
public function makeNotif($payload, $route = null, $options = []) { public function makeNotif($payload, $route = null, $options = []) {
$route = $route == null ? "" : $route.'.'; $route = $route == null ? "" : $route.'.';
return [
$this->notif = [
'greeting' => $this->getMessageLine($payload, $route.'greeting'), 'greeting' => $this->getMessageLine($payload, $route.'greeting'),
'subject' => $this->getMessageLine($payload, $route.'subject'), 'subject' => $this->getMessageLine($payload, $route.'subject'),
'title' => $this->getMessageLine($payload, $route.'title'), 'title' => $this->getMessageLine($payload, $route.'title'),
'body' => $this->getMessageLine($payload, $route.'body', $options) 'body' => $this->getMessageLine($payload, $route.'body', $options)
]; ];
return $this;
} }
/** /**
@ -38,7 +40,7 @@ class NotificationHelper
* @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
* *
*/ */
public function getMessageLine($payload, $key, $options = [])
protected function getMessageLine($payload, $key, $options = [])
{ {
return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options); return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options);
} }
@ -49,24 +51,25 @@ class NotificationHelper
* @param $users * @param $users
* @param $notif * @param $notif
*/ */
public function sendNotifications($users, $notif) {
// switch ($level) {
// case "emergency":
//// Notification::send($users, new SmsNotification($notif));
// case "critical":
// Notification::send($users, new MailNotification($notif));
// case "high":
// Notification::send($users, new DBNotification($notif));
// case "medium":
// Notification::send($users, new FcmNotification($notif));
// case "low":
//// Notification::send($users, new SocketNotification($notif));
// default:
//// Notification::send($users, new SocketNotification($notif));
// }
public function sendNotifications($users, $level = null) {
switch ($level) {
case "emergency":
// Notification::send($users, new SmsNotification($notif));
case "critical":
Notification::send($users, new MailNotification($notif)); Notification::send($users, new MailNotification($notif));
case "high":
Notification::send($users, new DBNotification($notif)); Notification::send($users, new DBNotification($notif));
case "medium":
Notification::send($users, new FcmNotification($notif)); Notification::send($users, new FcmNotification($notif));
case "low":
// Notification::send($users, new SocketNotification($notif));
default:
// Notification::send($users, new SocketNotification($notif));
}
// Notification::send($users, new MailNotification($this->notif));
// Notification::send($users, new DBNotification($this->notif));
// Notification::send($users, new FcmNotification($this->notif));
} }
} }
Loading…
Cancel
Save