Browse Source

some minor change and clean code

pull/1/head
mahdihty 4 years ago
parent
commit
8bc2d8e3d7
  1. 38
      app/Listeners/BusinessUserCreateNotif.php
  2. 7
      app/Listeners/ProjectUserCreateNotif.php

38
app/Listeners/BusinessUserCreateNotif.php

@ -35,22 +35,34 @@ class BusinessUserCreateNotif
{
$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 = [
$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', ['business' => request('_business_info')['name'], 'user' => $new_user->name])
'body' => $this->getMessageLine($payload, 'body', $options)
];
$users = $owners->prepend($new_user);
Notification::send($users, new MailNotification($notif));
Notification::send($users, new DBNotification($notif));
Notification::send($users, new FcmNotification($notif));
}
/**
@ -66,4 +78,16 @@ class BusinessUserCreateNotif
{
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));
}
}

7
app/Listeners/ProjectUserCreateNotif.php

@ -49,6 +49,13 @@ class ProjectUserCreateNotif
$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'),

Loading…
Cancel
Save