diff --git a/app/Events/TaskCreate.php b/app/Events/TaskCreate.php new file mode 100644 index 0000000..e51dff2 --- /dev/null +++ b/app/Events/TaskCreate.php @@ -0,0 +1,28 @@ +message = $message; + } +} 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/TaskCreateNotif.php b/app/Listeners/TaskCreateNotif.php new file mode 100644 index 0000000..3bba2d8 --- /dev/null +++ b/app/Listeners/TaskCreateNotif.php @@ -0,0 +1,104 @@ +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)); + } +} diff --git a/app/Listeners/TaskUpdateNotif.php b/app/Listeners/TaskUpdateNotif.php new file mode 100644 index 0000000..118036c --- /dev/null +++ b/app/Listeners/TaskUpdateNotif.php @@ -0,0 +1,155 @@ +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 de6d034..cdd86b4 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -6,10 +6,14 @@ use App\Events\ProjectUserCreate; 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; @@ -44,6 +48,12 @@ class EventServiceProvider extends ServiceProvider BusinessUpdate::class => [ BusinessUpdateListener::class, ], + TaskCreate::class => [ + TaskCreateNotif::class, + ], + TaskUpdate::class => [ + TaskUpdateNotif::class, + ], ]; /** diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 927be5d..dc58891 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -38,14 +38,14 @@ class RouteServiceProvider extends ServiceProvider $this->configureRateLimiting(); $this->routes(function () { - Route::prefix('api') + Route::prefix('user/v1') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); - Route::middleware('web') - ->namespace($this->namespace) - ->group(base_path('routes/web.php')); + // Route::middleware('web') + // ->namespace($this->namespace) + // ->group(base_path('routes/web.php')); }); } diff --git a/app/Utilities/HelperClass/NotificationHelper.php b/app/Utilities/HelperClass/NotificationHelper.php new file mode 100644 index 0000000..4231cbe --- /dev/null +++ b/app/Utilities/HelperClass/NotificationHelper.php @@ -0,0 +1,72 @@ + $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/bootstrap/app.php b/bootstrap/app.php index fdcb8bd..037e17d 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -52,11 +52,4 @@ $app->singleton( | */ -$app->router->group([ - 'namespace' => 'App\Http\Controllers', - 'prefix' => '/user/v1/' -], function ($router) { - require __DIR__.'/../routes/api.php'; -}); - return $app; diff --git a/config/app.php b/config/app.php index d517cdc..57ba658 100644 --- a/config/app.php +++ b/config/app.php @@ -65,6 +65,7 @@ return [ App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, App\Utilities\Zarinpal\Laravel\ZarinpalServiceProvider::class, + Fruitcake\Cors\CorsServiceProvider::class ], 'aliases' => [ diff --git a/config/cors.php b/config/cors.php index b2930d0..4598a66 100644 --- a/config/cors.php +++ b/config/cors.php @@ -1,27 +1,13 @@ ['api/*', 'sanctum/csrf-cookie'], + 'paths' => ['user/v1/*'], 'allowed_methods' => ['*'], 'allowed_origins' => ['*'], - 'allowed_origins_patterns' => ['Content-Type', 'X-Requested-With'], + 'allowedOriginsPatterns' => ['Content-Type', 'X-Requested-With'], 'allowed_headers' => ['*'], diff --git a/config/cors.php.laravel b/config/cors.php.laravel index c3ded3c..756b4ee 100644 --- a/config/cors.php.laravel +++ b/config/cors.php.laravel @@ -1,59 +1,19 @@ ['/*'], - /* - * Matches the request method. `['*']` allows all methods. - */ 'allowed_methods' => ['*'], - /* - * Matches the request origin. `['*']` allows all origins. Wildcards can be used, eg `*.mydomain.com` - */ 'allowed_origins' => ['*'], - /* - * Patterns that can be used with `preg_match` to match the origin. - */ 'allowedOriginsPatterns' => ['Content-Type', 'X-Requested-With'], - /* - * Sets the Access-Control-Allow-Headers response header. `['*']` allows all headers. - */ 'allowed_headers' => ['*'], - /* - * Sets the Access-Control-Expose-Headers response header with these headers. - */ 'exposed_headers' => [], - /* - * Sets the Access-Control-Max-Age response header when > 0. - */ 'max_age' => 0, - /* - * Sets the Access-Control-Allow-Credentials header. - */ 'supports_credentials' => false, ]; diff --git a/config/cors.php.lumen b/config/cors.php.lumen index 8a39e6d..aa2f995 100644 --- a/config/cors.php.lumen +++ b/config/cors.php.lumen @@ -1,20 +1,6 @@ ['api/*', 'sanctum/csrf-cookie'], 'allowed_methods' => ['*'], diff --git a/database/migrations/2020_08_18_085017_fingerprints.php b/database/migrations/2020_08_18_085017_fingerprints.php index 6a72833..d44e60d 100644 --- a/database/migrations/2020_08_18_085017_fingerprints.php +++ b/database/migrations/2020_08_18_085017_fingerprints.php @@ -22,7 +22,7 @@ class Fingerprints extends Migration $table->decimal('latitude', 10, 4); $table->decimal('longitude', 11, 4); $table->char('token', 60)->unique(); - $table->text('fcm_token')->unique()->nullable(); + $table->text('fcm_token')->nullable(); $table->timestamps(); }); } diff --git a/resources/lang/fa/notification.php b/resources/lang/fa/notification.php index 740602e..856ea0d 100644 --- a/resources/lang/fa/notification.php +++ b/resources/lang/fa/notification.php @@ -35,6 +35,49 @@ 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 شدید.' + ], + 'ready' => [ + 'greeting' => 'سلام کاربر گرامی!', + 'subject' => 'افزودن تسک جدید', + 'title' => 'افزودن تسک جدید', + 'body' => 'تسک :task در حالت انتظار برای تست قرار دارد.' + ], + 'completed' => [ + 'greeting' => 'سلام کاربر گرامی!', + 'subject' => 'افزودن تسک جدید', + 'title' => 'افزودن تسک جدید', + 'body' => 'تسک :task به اتمام رسید.' + ] + ] + ], + 'business' => [ 'update' => 'کاربر :user به کسب و کار :business اضافه شد.', 'wallet_almost_empty' => 'کیف پول تقریبا خالی است.',