diff --git a/app/Events/TaskUpdate.php b/app/Events/TaskUpdate.php new file mode 100644 index 0000000..91e18bd --- /dev/null +++ b/app/Events/TaskUpdate.php @@ -0,0 +1,28 @@ +message = $message; + } +} diff --git a/app/Listeners/TaskUpdateNotif.php b/app/Listeners/TaskUpdateNotif.php new file mode 100644 index 0000000..65981a2 --- /dev/null +++ b/app/Listeners/TaskUpdateNotif.php @@ -0,0 +1,157 @@ +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)); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index ea8a6b4..cdd86b4 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -7,11 +7,13 @@ use App\Events\TagCreate; use App\Events\ModelSaved; use App\Events\BusinessUpdate; use App\Events\TaskCreate; +use App\Events\TaskUpdate; use App\Listeners\NotifHandler; use App\Listeners\ProjectUserCreateNotif; use App\Listeners\TagCreateNotif; use App\Events\BusinessUserCreate; use App\Listeners\TaskCreateNotif; +use App\Listeners\TaskUpdateNotif; use Illuminate\Auth\Events\Registered; use App\Listeners\ActivityRegistration; use App\Listeners\BusinessUpdateListener; @@ -49,6 +51,9 @@ class EventServiceProvider extends ServiceProvider TaskCreate::class => [ TaskCreateNotif::class, ], + TaskUpdate::class => [ + TaskUpdateNotif::class, + ], ]; /** diff --git a/resources/lang/fa/notification.php b/resources/lang/fa/notification.php index 12f9d99..9e5b113 100644 --- a/resources/lang/fa/notification.php +++ b/resources/lang/fa/notification.php @@ -63,11 +63,17 @@ return [ 'title' => 'افزودن تسک جدید', 'body' => 'شما تایید کننده تسک :task شدید.' ], - 'watcher' => [ + 'ready' => [ 'greeting' => 'سلام کاربر گرامی!', 'subject' => 'افزودن تسک جدید', 'title' => 'افزودن تسک جدید', - 'body' => 'شما تایید کننده تسک :task شدید.' + 'body' => 'تسک :task در حالت انتظار برای تست قرار دارد.' + ], + 'completed' => [ + 'greeting' => 'سلام کاربر گرامی!', + 'subject' => 'افزودن تسک جدید', + 'title' => 'افزودن تسک جدید', + 'body' => 'تسک :task به اتمام رسید.' ] ] ],