You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.7 KiB
93 lines
2.7 KiB
<?php
|
|
|
|
namespace App\Listeners;
|
|
|
|
use App\Channels\FcmChannel;
|
|
use App\Events\BusinessUserCreate;
|
|
use App\Models\Business;
|
|
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;
|
|
|
|
class BusinessUserCreateNotif
|
|
{
|
|
/**
|
|
* Create the event listener.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Handle the event.
|
|
*
|
|
* @param BusinessUserCreate $event
|
|
* @return void
|
|
*/
|
|
public function handle(BusinessUserCreate $event)
|
|
{
|
|
$payload = $event->message;
|
|
if ($payload->data->original->level === enum('levels.inactive.id')) {
|
|
// When user level in business is zero, probably user added to business by system
|
|
// And not necessary send notification to stockholders
|
|
return;
|
|
}
|
|
$new_user = User::findOrFail($payload->data->original->user_id);
|
|
$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);
|
|
|
|
$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));
|
|
}
|
|
}
|