Browse Source

add task create notification

pull/1/head
mahdihty 4 years ago
parent
commit
c2064f96fc
  1. 28
      app/Events/TaskCreate.php
  2. 104
      app/Listeners/TaskCreateNotif.php
  3. 5
      app/Providers/EventServiceProvider.php
  4. 37
      resources/lang/fa/notification.php

28
app/Events/TaskCreate.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;
}
}

104
app/Listeners/TaskCreateNotif.php

@ -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));
}
}

5
app/Providers/EventServiceProvider.php

@ -6,10 +6,12 @@ use App\Events\ProjectUserCreate;
use App\Events\TagCreate;
use App\Events\ModelSaved;
use App\Events\BusinessUpdate;
use App\Events\TaskCreate;
use App\Listeners\NotifHandler;
use App\Listeners\ProjectUserCreateNotif;
use App\Listeners\TagCreateNotif;
use App\Events\BusinessUserCreate;
use App\Listeners\TaskCreateNotif;
use Illuminate\Auth\Events\Registered;
use App\Listeners\ActivityRegistration;
use App\Listeners\BusinessUpdateListener;
@ -44,6 +46,9 @@ class EventServiceProvider extends ServiceProvider
BusinessUpdate::class => [
BusinessUpdateListener::class,
],
TaskCreate::class => [
TaskCreateNotif::class,
],
];
/**

37
resources/lang/fa/notification.php

@ -35,6 +35,43 @@ return [
]
],
'tasks' => [
'create' => [
'assignee' => [
'greeting' => 'سلام کاربر گرامی!',
'subject' => 'افزودن تسک جدید',
'title' => 'افزودن تسک جدید',
'body' => 'تسک :task به شما واگذار شد.'
],
'approver' => [
'greeting' => 'سلام کاربر گرامی!',
'subject' => 'افزودن تسک جدید',
'title' => 'افزودن تسک جدید',
'body' => 'شما تایید کننده تسک :task شدید.'
]
],
'update' => [
'assignee' => [
'greeting' => 'سلام کاربر گرامی!',
'subject' => 'افزودن تسک جدید',
'title' => 'افزودن تسک جدید',
'body' => 'تسک :task به شما واگذار شد.'
],
'approver' => [
'greeting' => 'سلام کاربر گرامی!',
'subject' => 'افزودن تسک جدید',
'title' => 'افزودن تسک جدید',
'body' => 'شما تایید کننده تسک :task شدید.'
],
'watcher' => [
'greeting' => 'سلام کاربر گرامی!',
'subject' => 'افزودن تسک جدید',
'title' => 'افزودن تسک جدید',
'body' => 'شما تایید کننده تسک :task شدید.'
]
]
],
'business' => [
'update' => 'کاربر :user به کسب و کار :business اضافه شد.',
'update_wallet' => 'پول رو میدی یا پولت کنم',

Loading…
Cancel
Save