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

<?php
namespace App\Listeners;
use App\Events\ProjectUserCreate;
use App\Models\Project;
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 ProjectUserCreateNotif
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param ProjectUserCreate $event
* @return void
*/
public function handle(ProjectUserCreate $event)
{
$payload = $event->message;
$new_user = User::findOrFail($payload->data->original->user_id);
$project = Project::findOrFail($payload->data->original->project_id);
$owners_id = request('_business_info')['info']['projects'][$project->id]['members']->reject(function ($item, $key) use ($new_user) {
return $item['level'] < enum('levels.owner.id') || $key === $new_user->id;
})->toArray();
$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);
$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));
}
}