diff --git a/app/Events/ProjectUserCreate.php b/app/Events/ProjectUserCreate.php new file mode 100644 index 0000000..fa5e385 --- /dev/null +++ b/app/Events/ProjectUserCreate.php @@ -0,0 +1,24 @@ +message = $message; + } +} diff --git a/app/Listeners/BusinessUserCreateNotif.php b/app/Listeners/BusinessUserCreateNotif.php index a7b06d0..5beb17d 100644 --- a/app/Listeners/BusinessUserCreateNotif.php +++ b/app/Listeners/BusinessUserCreateNotif.php @@ -34,6 +34,9 @@ class BusinessUserCreateNotif public function handle(BusinessUserCreate $event) { $payload = $event->message; + if ($payload->data->original->level === enum('levels.inactive.id')) { + return; + } $new_user = User::findOrFail($payload->data->original->user_id); $owners = Business::findOrFail($payload->business)->owners()->where('id', '!=', $new_user->id)->get(); @@ -59,7 +62,7 @@ class BusinessUserCreateNotif * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null * */ - public function getMessageLine($payload, $key, $options = null) + public function getMessageLine($payload, $key, $options = []) { return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options); } diff --git a/app/Listeners/ProjectUserCreateNotif.php b/app/Listeners/ProjectUserCreateNotif.php new file mode 100644 index 0000000..d57a2c9 --- /dev/null +++ b/app/Listeners/ProjectUserCreateNotif.php @@ -0,0 +1,86 @@ +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)); + } +} diff --git a/app/Models/Project.php b/app/Models/Project.php index d9aca59..62acd0a 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -88,13 +88,15 @@ class Project extends Model implements HasMedia public function members() { $permissions = self::$permissions; - return $this->belongsToMany( - User::class, 'project_user', 'project_id', 'user_id', - 'id', 'id', __FUNCTION__ - )->using(ReportableRelation::class) + return $this->belongsToMany(User::class)->using(ReportableRelation::class) ->withPivot($permissions); } + public function owners() + { + return $this->members()->wherePivot('level', '=', enum('levels.owner.id')); + } + public function tasks() { return $this->hasMany(Task::class, 'project_id', 'id'); diff --git a/app/Notifications/DBNotification.php b/app/Notifications/DBNotification.php index 9c76954..006b26a 100644 --- a/app/Notifications/DBNotification.php +++ b/app/Notifications/DBNotification.php @@ -34,20 +34,6 @@ class DBNotification extends Notification return ['database']; } - /** - * Get the mail representation of the notification. - * - * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage - */ - public function toMail($notifiable) - { - return (new MailMessage) - ->line('The introduction to the notification.') - ->action('Notification Action', url('/')) - ->line('Thank you for using our application!'); - } - /** * Get the array representation of the notification. * diff --git a/app/Notifications/FcmNotification.php b/app/Notifications/FcmNotification.php index cfbd38a..d9f1556 100644 --- a/app/Notifications/FcmNotification.php +++ b/app/Notifications/FcmNotification.php @@ -37,7 +37,7 @@ class FcmNotification extends Notification * Get the voice representation of the notification. * * @param mixed $notifiable - * @return FCMMessage + * @return FcmMessage */ public function toFcm($notifiable) { diff --git a/app/Notifications/MailNotification.php b/app/Notifications/MailNotification.php index af60d57..8736089 100644 --- a/app/Notifications/MailNotification.php +++ b/app/Notifications/MailNotification.php @@ -7,7 +7,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; -class MailNotification extends Notification //implements ShouldQueue +class MailNotification extends Notification implements ShouldQueue { use Queueable; @@ -44,6 +44,9 @@ class MailNotification extends Notification //implements ShouldQueue */ public function __construct($message) { + $this->connection = 'redis'; + $this->queue = 'mail-queue'; + $this->afterCommit = true; $this->message = $message; } @@ -72,17 +75,4 @@ class MailNotification extends Notification //implements ShouldQueue ->subject($this->message['subject']) ->action('Notification Action', url('/')); } - - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray($notifiable) - { - return [ - // - ]; - } } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index ecfdd03..de6d034 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,13 +2,14 @@ namespace App\Providers; +use App\Events\ProjectUserCreate; use App\Events\TagCreate; use App\Events\ModelSaved; use App\Events\BusinessUpdate; use App\Listeners\NotifHandler; +use App\Listeners\ProjectUserCreateNotif; use App\Listeners\TagCreateNotif; use App\Events\BusinessUserCreate; -use Illuminate\Support\Facades\Event; use Illuminate\Auth\Events\Registered; use App\Listeners\ActivityRegistration; use App\Listeners\BusinessUpdateListener; @@ -37,6 +38,9 @@ class EventServiceProvider extends ServiceProvider BusinessUserCreate::class => [ BusinessUserCreateNotif::class, ], + ProjectUserCreate::class => [ + ProjectUserCreateNotif::class, + ], BusinessUpdate::class => [ BusinessUpdateListener::class, ], diff --git a/resources/lang/fa/notification.php b/resources/lang/fa/notification.php index 6a7fb83..1ef6303 100644 --- a/resources/lang/fa/notification.php +++ b/resources/lang/fa/notification.php @@ -24,6 +24,20 @@ return [ 'title' => 'افزودن کاربر به کسب و کار', 'body' => 'کاربر :user به کسب و کار :business اضافه شد.' ] - ] + ], + + 'project_user' => [ + 'create' =>[ + 'greeting' => 'سلام کاربر گرامی!', + 'subject' => 'افزودن کاربر به پروژه', + 'title' => 'افزودن کاربر به پروژه', + 'body' => 'کاربر :user به پروژه :project اضافه شد.' + ] + ], + + 'business' => [ + 'update' => 'کاربر :user به کسب و کار :business اضافه شد.', + 'update_wallet' => 'پول رو میدی یا پولت کنم', + ], ];