Mohammad Akbari
4 years ago
9 changed files with 145 additions and 36 deletions
-
24app/Events/ProjectUserCreate.php
-
5app/Listeners/BusinessUserCreateNotif.php
-
86app/Listeners/ProjectUserCreateNotif.php
-
10app/Models/Project.php
-
14app/Notifications/DBNotification.php
-
2app/Notifications/FcmNotification.php
-
18app/Notifications/MailNotification.php
-
6app/Providers/EventServiceProvider.php
-
16resources/lang/fa/notification.php
@ -0,0 +1,24 @@ |
|||
<?php |
|||
|
|||
namespace App\Events; |
|||
|
|||
use Illuminate\Broadcasting\InteractsWithSockets; |
|||
use Illuminate\Foundation\Events\Dispatchable; |
|||
use Illuminate\Queue\SerializesModels; |
|||
|
|||
class ProjectUserCreate |
|||
{ |
|||
use Dispatchable, InteractsWithSockets, SerializesModels; |
|||
|
|||
public $message; |
|||
|
|||
/** |
|||
* Create a new event instance. |
|||
* |
|||
* @return void |
|||
*/ |
|||
public function __construct($message) |
|||
{ |
|||
$this->message = $message; |
|||
} |
|||
} |
@ -0,0 +1,86 @@ |
|||
<?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); |
|||
} |
|||
|
|||
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)); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue