masoud
4 years ago
14 changed files with 448 additions and 82 deletions
-
28app/Events/TaskCreate.php
-
28app/Events/TaskUpdate.php
-
104app/Listeners/TaskCreateNotif.php
-
155app/Listeners/TaskUpdateNotif.php
-
10app/Providers/EventServiceProvider.php
-
8app/Providers/RouteServiceProvider.php
-
72app/Utilities/HelperClass/NotificationHelper.php
-
7bootstrap/app.php
-
1config/app.php
-
18config/cors.php
-
40config/cors.php.laravel
-
14config/cors.php.lumen
-
2database/migrations/2020_08_18_085017_fingerprints.php
-
43resources/lang/fa/notification.php
@ -0,0 +1,28 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Events; |
||||
|
|
||||
|
use Illuminate\Broadcasting\Channel; |
||||
|
use Illuminate\Broadcasting\InteractsWithSockets; |
||||
|
use Illuminate\Broadcasting\PresenceChannel; |
||||
|
use Illuminate\Broadcasting\PrivateChannel; |
||||
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; |
||||
|
use Illuminate\Foundation\Events\Dispatchable; |
||||
|
use Illuminate\Queue\SerializesModels; |
||||
|
|
||||
|
class TaskCreate |
||||
|
{ |
||||
|
use Dispatchable, InteractsWithSockets, SerializesModels; |
||||
|
|
||||
|
public $message; |
||||
|
|
||||
|
/** |
||||
|
* Create a new event instance. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function __construct($message) |
||||
|
{ |
||||
|
$this->message = $message; |
||||
|
} |
||||
|
} |
@ -0,0 +1,28 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Events; |
||||
|
|
||||
|
use Illuminate\Broadcasting\Channel; |
||||
|
use Illuminate\Broadcasting\InteractsWithSockets; |
||||
|
use Illuminate\Broadcasting\PresenceChannel; |
||||
|
use Illuminate\Broadcasting\PrivateChannel; |
||||
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; |
||||
|
use Illuminate\Foundation\Events\Dispatchable; |
||||
|
use Illuminate\Queue\SerializesModels; |
||||
|
|
||||
|
class TaskUpdate |
||||
|
{ |
||||
|
use Dispatchable, InteractsWithSockets, SerializesModels; |
||||
|
|
||||
|
public $message; |
||||
|
|
||||
|
/** |
||||
|
* Create a new event instance. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function __construct($message) |
||||
|
{ |
||||
|
$this->message = $message; |
||||
|
} |
||||
|
} |
@ -0,0 +1,104 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Listeners; |
||||
|
|
||||
|
use App\Events\TaskCreate; |
||||
|
use App\Models\Task; |
||||
|
use App\Models\User; |
||||
|
use App\Notifications\DBNotification; |
||||
|
use App\Notifications\FcmNotification; |
||||
|
use App\Notifications\MailNotification; |
||||
|
use Illuminate\Support\Facades\Notification; |
||||
|
|
||||
|
class TaskCreateNotif |
||||
|
{ |
||||
|
/** |
||||
|
* Create the event listener. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function __construct() |
||||
|
{ |
||||
|
//
|
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Handle the event. |
||||
|
* |
||||
|
* @param TaskCreate $event |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function handle(TaskCreate $event) |
||||
|
{ |
||||
|
$payload = $event->message; |
||||
|
|
||||
|
if ($payload->data->original->assignee_id !== null) { |
||||
|
$this->assigneeNotifHandler($payload); |
||||
|
} |
||||
|
if ($payload->data->original->approver_id !== null) { |
||||
|
$this->approverNotifHandler($payload); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public function assigneeNotifHandler($payload) { |
||||
|
$user = User::findOrFail($payload->data->original->assignee_id); |
||||
|
$task = Task::findOrFail($payload->data->original->id); |
||||
|
|
||||
|
$notif = $this->makeNotif($payload, 'assignee', ['task' => $task->title]); |
||||
|
|
||||
|
$this->sendNotifications($user, $notif); |
||||
|
} |
||||
|
|
||||
|
public function approverNotifHandler($payload) { |
||||
|
$user = User::findOrFail($payload->data->original->approver_id); |
||||
|
$task = Task::findOrFail($payload->data->original->id); |
||||
|
|
||||
|
$notif = $this->makeNotif($payload, 'approver', ['task' => $task->title]); |
||||
|
|
||||
|
$this->sendNotifications($user, $notif); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Make notification object |
||||
|
* |
||||
|
* @param $payload |
||||
|
* @param null $route |
||||
|
* @param array $options |
||||
|
* @return array |
||||
|
*/ |
||||
|
public function makeNotif($payload, $route = null, $options = []) { |
||||
|
$route = $route == null ? "" : $route.'.'; |
||||
|
return [ |
||||
|
'greeting' => $this->getMessageLine($payload, $route.'greeting'), |
||||
|
'subject' => $this->getMessageLine($payload, $route.'subject'), |
||||
|
'title' => $this->getMessageLine($payload, $route.'title'), |
||||
|
'body' => $this->getMessageLine($payload, $route.'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)); |
||||
|
} |
||||
|
} |
@ -0,0 +1,155 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Listeners; |
||||
|
|
||||
|
use App\Events\TaskUpdate; |
||||
|
use App\Models\Task; |
||||
|
use App\Models\User; |
||||
|
use App\Notifications\DBNotification; |
||||
|
use App\Notifications\FcmNotification; |
||||
|
use App\Notifications\MailNotification; |
||||
|
use Illuminate\Support\Facades\Notification; |
||||
|
|
||||
|
class TaskUpdateNotif |
||||
|
{ |
||||
|
/** |
||||
|
* Create the event listener. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function __construct() |
||||
|
{ |
||||
|
//
|
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Handle the event. |
||||
|
* |
||||
|
* @param TaskUpdate $event |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function handle(TaskUpdate $event) |
||||
|
{ |
||||
|
$payload = $event->message; |
||||
|
if (isset($payload->data->diff->assignee_id)) { |
||||
|
$this->assigneeNotifHandler($payload); |
||||
|
} |
||||
|
if (isset($payload->data->diff->approver_id)) { |
||||
|
$this->approverNotifHandler($payload); |
||||
|
} |
||||
|
if (isset($payload->data->diff->completed_at)) { |
||||
|
$this->completedNotifHandler($payload); |
||||
|
} |
||||
|
if (isset($payload->data->diff->ready_to_test)) { |
||||
|
$this->readyNotifHandler($payload); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public function assigneeNotifHandler($payload) { |
||||
|
$user = User::findOrFail($payload->data->diff->assignee_id); |
||||
|
$task = Task::findOrFail($payload->data->original->id); |
||||
|
|
||||
|
$notif = $this->makeNotif($payload, 'assignee', ['task' => $task->title]); |
||||
|
|
||||
|
$this->sendNotifications($user, $notif); |
||||
|
} |
||||
|
|
||||
|
public function approverNotifHandler($payload) { |
||||
|
$user = User::findOrFail($payload->data->diff->approver_id); |
||||
|
$task = Task::findOrFail($payload->data->original->id); |
||||
|
|
||||
|
$notif = $this->makeNotif($payload, 'approver', ['task' => $task->title]); |
||||
|
|
||||
|
$this->sendNotifications($user, $notif); |
||||
|
} |
||||
|
|
||||
|
public function completedNotifHandler($payload){ |
||||
|
$task = Task::findOrFail($payload->data->original->id); |
||||
|
$user_ids = array_filter([ |
||||
|
$task->approver_id, |
||||
|
$task->assignee_id, |
||||
|
$task->creator_id |
||||
|
]); |
||||
|
$user_ids = array_unique(array_merge($user_ids, $task->watchers)); |
||||
|
|
||||
|
$users = User::whereIn('id', $user_ids)->where('id', '!=', auth()->id())->get(); |
||||
|
|
||||
|
$notif = $this->makeNotif($payload, 'completed', ['task' => $task->title]); |
||||
|
|
||||
|
$this->sendNotifications($users, $notif); |
||||
|
} |
||||
|
|
||||
|
public function readyNotifHandler($payload) |
||||
|
{ |
||||
|
$task = Task::findOrFail($payload->data->original->id); |
||||
|
$user_ids = array_unique(array_filter([ |
||||
|
$task->approver_id, |
||||
|
$task->assignee_id, |
||||
|
$task->creator_id |
||||
|
])); |
||||
|
|
||||
|
$users = User::whereIn('id', $user_ids)->where('id', '!=', auth()->id())->get(); |
||||
|
|
||||
|
$notif = $this->makeNotif($payload, 'ready', ['task' => $task->title]); |
||||
|
|
||||
|
$this->sendNotifications($users, $notif); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Make notification object |
||||
|
* |
||||
|
* @param $payload |
||||
|
* @param null $route |
||||
|
* @param array $options |
||||
|
* @return array |
||||
|
*/ |
||||
|
public function makeNotif($payload, $route = null, $options = []) { |
||||
|
$route = $route == null ? "" : $route.'.'; |
||||
|
return [ |
||||
|
'greeting' => $this->getMessageLine($payload, $route.'greeting'), |
||||
|
'subject' => $this->getMessageLine($payload, $route.'subject'), |
||||
|
'title' => $this->getMessageLine($payload, $route.'title'), |
||||
|
'body' => $this->getMessageLine($payload, $route.'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) { |
||||
|
// switch ($level) {
|
||||
|
// case "emergency":
|
||||
|
//// Notification::send($users, new SmsNotification($notif));
|
||||
|
// case "critical":
|
||||
|
// Notification::send($users, new MailNotification($notif));
|
||||
|
// case "high":
|
||||
|
// Notification::send($users, new DBNotification($notif));
|
||||
|
// case "medium":
|
||||
|
// Notification::send($users, new FcmNotification($notif));
|
||||
|
// case "low":
|
||||
|
//// Notification::send($users, new SocketNotification($notif));
|
||||
|
// default:
|
||||
|
//// Notification::send($users, new SocketNotification($notif));
|
||||
|
// }
|
||||
|
Notification::send($users, new MailNotification($notif)); |
||||
|
Notification::send($users, new DBNotification($notif)); |
||||
|
Notification::send($users, new FcmNotification($notif)); |
||||
|
} |
||||
|
} |
@ -0,0 +1,72 @@ |
|||||
|
<?php |
||||
|
|
||||
|
|
||||
|
namespace App\Utilities\HelperClass; |
||||
|
|
||||
|
|
||||
|
use App\Notifications\DBNotification; |
||||
|
use App\Notifications\FcmNotification; |
||||
|
use App\Notifications\MailNotification; |
||||
|
use Illuminate\Support\Facades\Notification; |
||||
|
|
||||
|
class NotificationHelper |
||||
|
{ |
||||
|
/** |
||||
|
* Make notification object |
||||
|
* |
||||
|
* @param $payload |
||||
|
* @param null $route |
||||
|
* @param array $options |
||||
|
* @return array |
||||
|
*/ |
||||
|
public function makeNotif($payload, $route = null, $options = []) { |
||||
|
$route = $route == null ? "" : $route.'.'; |
||||
|
return [ |
||||
|
'greeting' => $this->getMessageLine($payload, $route.'greeting'), |
||||
|
'subject' => $this->getMessageLine($payload, $route.'subject'), |
||||
|
'title' => $this->getMessageLine($payload, $route.'title'), |
||||
|
'body' => $this->getMessageLine($payload, $route.'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) { |
||||
|
// switch ($level) {
|
||||
|
// case "emergency":
|
||||
|
//// Notification::send($users, new SmsNotification($notif));
|
||||
|
// case "critical":
|
||||
|
// Notification::send($users, new MailNotification($notif));
|
||||
|
// case "high":
|
||||
|
// Notification::send($users, new DBNotification($notif));
|
||||
|
// case "medium":
|
||||
|
// Notification::send($users, new FcmNotification($notif));
|
||||
|
// case "low":
|
||||
|
//// Notification::send($users, new SocketNotification($notif));
|
||||
|
// default:
|
||||
|
//// Notification::send($users, new SocketNotification($notif));
|
||||
|
// }
|
||||
|
Notification::send($users, new MailNotification($notif)); |
||||
|
Notification::send($users, new DBNotification($notif)); |
||||
|
Notification::send($users, new FcmNotification($notif)); |
||||
|
} |
||||
|
|
||||
|
} |
@ -1,59 +1,19 @@ |
|||||
<?php |
<?php |
||||
return [ |
return [ |
||||
|
|
||||
/* |
|
||||
|-------------------------------------------------------------------------- |
|
||||
| Laravel CORS Options |
|
||||
|-------------------------------------------------------------------------- |
|
||||
| |
|
||||
| The allowed_methods and allowed_headers options are case-insensitive. |
|
||||
| |
|
||||
| You don't need to provide both allowed_origins and allowed_origins_patterns. |
|
||||
| If one of the strings passed matches, it is considered a valid origin. |
|
||||
| |
|
||||
| If ['*'] is provided to allowed_methods, allowed_origins or allowed_headers |
|
||||
| all methods / origins / headers are allowed. |
|
||||
| |
|
||||
*/ |
|
||||
|
|
||||
/* |
|
||||
* You can enable CORS for 1 or multiple paths. |
|
||||
* Example: ['api/*'] |
|
||||
*/ |
|
||||
'paths' => ['/*'], |
'paths' => ['/*'], |
||||
|
|
||||
/* |
|
||||
* Matches the request method. `['*']` allows all methods. |
|
||||
*/ |
|
||||
'allowed_methods' => ['*'], |
'allowed_methods' => ['*'], |
||||
|
|
||||
/* |
|
||||
* Matches the request origin. `['*']` allows all origins. Wildcards can be used, eg `*.mydomain.com` |
|
||||
*/ |
|
||||
'allowed_origins' => ['*'], |
'allowed_origins' => ['*'], |
||||
|
|
||||
/* |
|
||||
* Patterns that can be used with `preg_match` to match the origin. |
|
||||
*/ |
|
||||
'allowedOriginsPatterns' => ['Content-Type', 'X-Requested-With'], |
'allowedOriginsPatterns' => ['Content-Type', 'X-Requested-With'], |
||||
|
|
||||
/* |
|
||||
* Sets the Access-Control-Allow-Headers response header. `['*']` allows all headers. |
|
||||
*/ |
|
||||
'allowed_headers' => ['*'], |
'allowed_headers' => ['*'], |
||||
|
|
||||
/* |
|
||||
* Sets the Access-Control-Expose-Headers response header with these headers. |
|
||||
*/ |
|
||||
'exposed_headers' => [], |
'exposed_headers' => [], |
||||
|
|
||||
/* |
|
||||
* Sets the Access-Control-Max-Age response header when > 0. |
|
||||
*/ |
|
||||
'max_age' => 0, |
'max_age' => 0, |
||||
|
|
||||
/* |
|
||||
* Sets the Access-Control-Allow-Credentials header. |
|
||||
*/ |
|
||||
'supports_credentials' => false, |
'supports_credentials' => false, |
||||
]; |
]; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue