From c405e993b56095b9bce5d5105dd1c552b636d32d Mon Sep 17 00:00:00 2001 From: merdan Date: Mon, 21 Nov 2022 09:54:10 +0500 Subject: [PATCH] notifications ready --- packages/Sarga/Admin/src/Config/menu.php | 6 ++ packages/Sarga/Admin/src/Config/push.php | 8 ++ .../src/DataGrids/NotificationDataGrid.php | 78 +++++++++++++++++ ...4_create_marketing_notifications_table.php | 35 ++++++++ .../src/Http/Controllers/PushController.php | 85 +++++++++++++++++++ packages/Sarga/Admin/src/Http/Firebase.php | 56 ++++++++++++ .../src/Providers/AdminServiceProvider.php | 2 + .../marketing/notification/create.blade.php | 53 ++++++++++++ .../marketing/notification/index.blade.php | 26 ++++++ .../marketing/notification/view.blade.php | 51 +++++++++++ .../Admin/src/Routes/notification-routes.php | 15 ++++ .../Sarga/Shop/src/Contracts/Notification.php | 8 ++ .../Sarga/Shop/src/Models/Notification.php | 24 ++++++ .../Shop/src/Models/NotificationProxy.php | 8 ++ .../src/Providers/ModuleServiceProvider.php | 1 + .../src/Repositories/CategoryRepository.php | 4 +- .../Repositories/NotificationRepository.php | 17 ++++ 17 files changed, 476 insertions(+), 1 deletion(-) create mode 100644 packages/Sarga/Admin/src/Config/push.php create mode 100644 packages/Sarga/Admin/src/DataGrids/NotificationDataGrid.php create mode 100644 packages/Sarga/Admin/src/Database/Migrations/2022_11_19_160644_create_marketing_notifications_table.php create mode 100644 packages/Sarga/Admin/src/Http/Controllers/PushController.php create mode 100644 packages/Sarga/Admin/src/Http/Firebase.php create mode 100644 packages/Sarga/Admin/src/Resources/views/marketing/notification/create.blade.php create mode 100644 packages/Sarga/Admin/src/Resources/views/marketing/notification/index.blade.php create mode 100644 packages/Sarga/Admin/src/Resources/views/marketing/notification/view.blade.php create mode 100644 packages/Sarga/Shop/src/Contracts/Notification.php create mode 100644 packages/Sarga/Shop/src/Models/Notification.php create mode 100644 packages/Sarga/Shop/src/Models/NotificationProxy.php create mode 100644 packages/Sarga/Shop/src/Repositories/NotificationRepository.php diff --git a/packages/Sarga/Admin/src/Config/menu.php b/packages/Sarga/Admin/src/Config/menu.php index cc385ec5f..c57332096 100644 --- a/packages/Sarga/Admin/src/Config/menu.php +++ b/packages/Sarga/Admin/src/Config/menu.php @@ -28,5 +28,11 @@ return [ 'route' => 'admin.catalog.menus.index', 'sort' => 7, 'icon-class' => '', + ],[ + 'key' => 'marketing.push-notification', + 'name' => 'Push notifications', + 'route' => 'admin.push.index', + 'sort' => 3, + 'icon-class' => '', ], ]; \ No newline at end of file diff --git a/packages/Sarga/Admin/src/Config/push.php b/packages/Sarga/Admin/src/Config/push.php new file mode 100644 index 000000000..752838847 --- /dev/null +++ b/packages/Sarga/Admin/src/Config/push.php @@ -0,0 +1,8 @@ + [ + 'url'=> env('PUSH_URL', ' https://fcm.googleapis.com/fcm/send'), + 'token' => env('PUSH_TOKEN','key=AAAA9LHgcmM:APA91bHkC0wGvNhfoD6UlzUsSilSnhAghY27PNpCwbte4AG0zN2LTnbxVtgcU4q5hwPUfcJuAiL00Ioh2XCEasDW6GYs9IZR4MbbholyASABU6aoQjq95aGpuwci-z_oD2-p1m7COwcO') + ] +]; diff --git a/packages/Sarga/Admin/src/DataGrids/NotificationDataGrid.php b/packages/Sarga/Admin/src/DataGrids/NotificationDataGrid.php new file mode 100644 index 000000000..5e37de1b9 --- /dev/null +++ b/packages/Sarga/Admin/src/DataGrids/NotificationDataGrid.php @@ -0,0 +1,78 @@ +addSelect('id','title','content','last_sent_at','updated_at'); + $this->setQueryBuilder($queryBuilder); + } + + public function addColumns() + { + $this->addColumn([ + 'index' => 'id', + 'label' => trans('admin::app.datagrid.id'), + 'type' => 'number', + 'searchable' => false, + 'sortable' => true, + 'filterable' => true, + ]); + + $this->addColumn([ + 'index' => 'title', + 'label' => 'Title', + 'type' => 'string', + 'searchable' => true, + 'sortable' => true, + 'filterable' => true, + ]); + + $this->addColumn([ + 'index' => 'content', + 'label' => "Content", + 'type' => 'string', + 'searchable' => true, + 'sortable' => true, + 'filterable' => true, + ]); + + $this->addColumn([ + 'index' => 'last_sent_at', + 'label' => 'Last sent at', + 'type' => 'datetime', + 'searchable' => false, + 'sortable' => true, + 'filterable' => true, + ]); + + $this->addColumn([ + 'index' => 'updated_at', + 'label' => 'Updated at', + 'type' => 'datetime', + 'searchable' => false, + 'sortable' => true, + 'filterable' => true, + ]); + } + public function prepareActions() + { + $this->addAction([ + 'title' => trans('admin::app.datagrid.edit'), + 'method' => 'GET', + 'route' => 'admin.notifications.edit', + 'icon' => 'icon pencil-lg-icon', + ]); + + } +} \ No newline at end of file diff --git a/packages/Sarga/Admin/src/Database/Migrations/2022_11_19_160644_create_marketing_notifications_table.php b/packages/Sarga/Admin/src/Database/Migrations/2022_11_19_160644_create_marketing_notifications_table.php new file mode 100644 index 000000000..01eb4de88 --- /dev/null +++ b/packages/Sarga/Admin/src/Database/Migrations/2022_11_19_160644_create_marketing_notifications_table.php @@ -0,0 +1,35 @@ +increments('id'); + $table->string('title'); + $table->string('content'); + $table->dateTime('last_sent_at')->nullable(); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('marketing_notifications'); + } +} diff --git a/packages/Sarga/Admin/src/Http/Controllers/PushController.php b/packages/Sarga/Admin/src/Http/Controllers/PushController.php new file mode 100644 index 000000000..01a3cbee1 --- /dev/null +++ b/packages/Sarga/Admin/src/Http/Controllers/PushController.php @@ -0,0 +1,85 @@ +_config = request('_config'); + } + + /** + * Display a listing of the resource. + * + * @return \Illuminate\View\View + */ + public function index() + { + return view($this->_config['view']); + } + + /** + * Show the view for the specified resource. + * + * @param int $id + * @return \Illuminate\View\View + */ + public function edit($id) + { + $notification = $this->notificationRepository->findOrFail($id); + + return view($this->_config['view'],compact('notification')); + } + /** + * Show the form for creating a new resource. + * + * @param int $orderId + * @return \Illuminate\View\View + */ + public function create() + { + return view($this->_config['view']); + } + + /** + * Store a newly created resource in storage. + * + * @param int $orderId + * @return \Illuminate\Http\Response + */ + public function store() + { + $this->validate(request(),[ + 'title' => 'required|max:500', + 'content' => 'required|max:3000' + ]); + + if($note = $this->notificationRepository->create(request()->all())){ + $this->sendNotification($note); + + return redirect()->route($this->_config['redirect']); + }else { + session()->flash('error', trans('Error on creating notification')); + + return redirect()->back(); + } + } + + private function sendNotification($note){ + try { + $data = $note->only(['id','title','content'])+['type'=>'topic']; + (new Firebase('/topics/notifications',$data))->send(); + session()->flash('success', trans('Notification sent successfully')); + }catch (\Exception $ex){ + report($ex); + session()->flash('error', trans('Notification does not sent')); + } + + } +} \ No newline at end of file diff --git a/packages/Sarga/Admin/src/Http/Firebase.php b/packages/Sarga/Admin/src/Http/Firebase.php new file mode 100644 index 000000000..564dfa27b --- /dev/null +++ b/packages/Sarga/Admin/src/Http/Firebase.php @@ -0,0 +1,56 @@ +data = [ + 'click_action' => 'FLUTTER_NOTIFICATION_CLICK', + + ] + $content; + + $this->notification = [ + 'sound' => 'default', + 'title' => $content['title'], + 'body' =>$content['content'] + ]; + + $this->to = $to; + + $this->priority = $priority; + + } + + public function send(){ + $body = json_encode((object)$this); + + $response = Http::withHeaders([ + 'Authorization' => config('notification.push.token'), + 'Content-Type' => 'application/json' + ])->withBody($body,'application/json') + ->timeout(30) + ->post(config('notification.push.url')); + + + if($response->failed()) + { + Log::error($response); + $response->throw(); + } + } +} \ No newline at end of file diff --git a/packages/Sarga/Admin/src/Providers/AdminServiceProvider.php b/packages/Sarga/Admin/src/Providers/AdminServiceProvider.php index 54dad5d23..a346bdfc9 100644 --- a/packages/Sarga/Admin/src/Providers/AdminServiceProvider.php +++ b/packages/Sarga/Admin/src/Providers/AdminServiceProvider.php @@ -28,6 +28,8 @@ class AdminServiceProvider extends ServiceProvider __DIR__ . '/../Resources/views/customers/edit.blade.php' => resource_path('views/vendor/admin/customers/edit.blade.php'), ],'admin'); + $this->publishes([ __DIR__ . '/../Config/push.php','config/push.php'],'sarga_config'); + $this->app->register(EventServiceProvider::class); } diff --git a/packages/Sarga/Admin/src/Resources/views/marketing/notification/create.blade.php b/packages/Sarga/Admin/src/Resources/views/marketing/notification/create.blade.php new file mode 100644 index 000000000..288465ca5 --- /dev/null +++ b/packages/Sarga/Admin/src/Resources/views/marketing/notification/create.blade.php @@ -0,0 +1,53 @@ +@extends('admin::layouts.content') + +@section('page_title') + {{ __('Add notifications') }} +@stop +@section('content') +
+ +
+ + +
+
+ @csrf() + + + +
+
+ + + @{{ errors.first('title') }} +
+ + +
+ + + @{{ errors.first('content') }} +
+
+
+ + +
+
+
+
+@stop \ No newline at end of file diff --git a/packages/Sarga/Admin/src/Resources/views/marketing/notification/index.blade.php b/packages/Sarga/Admin/src/Resources/views/marketing/notification/index.blade.php new file mode 100644 index 000000000..2fde39e16 --- /dev/null +++ b/packages/Sarga/Admin/src/Resources/views/marketing/notification/index.blade.php @@ -0,0 +1,26 @@ +@extends('admin::layouts.content') + +@section('page_title') + {{ __('Notification Messages') }} +@stop + +@section('content') +
+ + +
+ @inject('notificationGrid','Sarga\Admin\DataGrids\NotificationDataGrid') + {!! $notificationGrid->render() !!} +
+
+@endsection \ No newline at end of file diff --git a/packages/Sarga/Admin/src/Resources/views/marketing/notification/view.blade.php b/packages/Sarga/Admin/src/Resources/views/marketing/notification/view.blade.php new file mode 100644 index 000000000..5eb4ccea1 --- /dev/null +++ b/packages/Sarga/Admin/src/Resources/views/marketing/notification/view.blade.php @@ -0,0 +1,51 @@ +@extends('admin::layouts.content') + +@section('page_title') + {{ __('Edit notification Message') }} +@stop + +@section('content') +
+
+ + +
+
+ @csrf() + + +
+
+ + + @{{ errors.first('title') }} +
+ + +
+ + + @{{ errors.first('content') }} +
+
+
+ +
+
+
+
+@endsection \ No newline at end of file diff --git a/packages/Sarga/Admin/src/Routes/notification-routes.php b/packages/Sarga/Admin/src/Routes/notification-routes.php index 099010653..4597a3d5a 100644 --- a/packages/Sarga/Admin/src/Routes/notification-routes.php +++ b/packages/Sarga/Admin/src/Routes/notification-routes.php @@ -10,5 +10,20 @@ Route::group(['middleware' => ['web', 'admin'], 'prefix' => config('app.admin_ur Route::get('delete-notifications', [Notifications::class, 'deleteNotification'])->defaults('_config', [ 'redirect' => 'admin.notification.index', ])->name('admin.notification.delete-notification'); + //Notifications + Route::get('push-notifications', [Notifications::class,'index'])->defaults('_config', [ + 'view' => 'sarga_admin::marketing.notification.index', + ])->name('admin.push.index'); + Route::get('push-notifications/create', [Notifications::class,'create'])->defaults('_config', [ + 'view' => 'sarga_admin::marketing.notification.create', + ])->name('admin.push.create'); + + Route::post('push-notifications/create', [Notifications::class,'store'])->defaults('_config', [ + 'redirect' => 'sarga_admin.notifications.index', + ])->name('admin.push.store'); + + Route::get('push-notifications/edit/{id}', [Notifications::class,'edit'])->defaults('_config', [ + 'view' => 'sarga_admin::marketing.notification.view', + ])->name('admin.push.edit'); }); diff --git a/packages/Sarga/Shop/src/Contracts/Notification.php b/packages/Sarga/Shop/src/Contracts/Notification.php new file mode 100644 index 000000000..46be40e12 --- /dev/null +++ b/packages/Sarga/Shop/src/Contracts/Notification.php @@ -0,0 +1,8 @@ +where('display_mode','description_only'); if(request()->has('vendor')){ - + $query->whereHas('vendors', function($q){ + $q->where('id',request()->get('vendor')); + }); } return $query->get(); diff --git a/packages/Sarga/Shop/src/Repositories/NotificationRepository.php b/packages/Sarga/Shop/src/Repositories/NotificationRepository.php new file mode 100644 index 000000000..48a2f524a --- /dev/null +++ b/packages/Sarga/Shop/src/Repositories/NotificationRepository.php @@ -0,0 +1,17 @@ +