diff --git a/app/Listeners/BusinessUserCreateNotif.php b/app/Listeners/BusinessUserCreateNotif.php index 5beb17d..29f4456 100644 --- a/app/Listeners/BusinessUserCreateNotif.php +++ b/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 = [ - '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]) - ]; + $notif = $this->makeNotif($payload,['business' => request('_business_info')['name'], 'user' => $new_user->name]); $users = $owners->prepend($new_user); - Notification::send($users, new MailNotification($notif)); - Notification::send($users, new DBNotification($notif)); - Notification::send($users, new FcmNotification($notif)); + + $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) + ]; } /** @@ -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)); + } } diff --git a/app/Listeners/ProjectUserCreateNotif.php b/app/Listeners/ProjectUserCreateNotif.php index d57a2c9..add26cb 100644 --- a/app/Listeners/ProjectUserCreateNotif.php +++ b/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'),