Browse Source

Merge with mahdi

pull/3/head
Mohammad Akbari 4 years ago
parent
commit
4c9e066280
Signed by: akbarjimi GPG Key ID: 55726AEFECE5E683
  1. 24
      app/Events/ProjectUserCreate.php
  2. 5
      app/Listeners/BusinessUserCreateNotif.php
  3. 86
      app/Listeners/ProjectUserCreateNotif.php
  4. 10
      app/Models/Project.php
  5. 14
      app/Notifications/DBNotification.php
  6. 2
      app/Notifications/FcmNotification.php
  7. 18
      app/Notifications/MailNotification.php
  8. 6
      app/Providers/EventServiceProvider.php
  9. 16
      resources/lang/fa/notification.php

24
app/Events/ProjectUserCreate.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;
}
}

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

86
app/Listeners/ProjectUserCreateNotif.php

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

10
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');

14
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.
*

2
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)
{

18
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 [
//
];
}
}

6
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,
],

16
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' => 'پول رو میدی یا پولت کنم',
],
];
Loading…
Cancel
Save