From 8af376cd371e774c5c7ba317707beb8feaf4aac5 Mon Sep 17 00:00:00 2001 From: Tb Fajri Date: Fri, 24 May 2024 09:09:21 +0700 Subject: [PATCH] update send notification fcm --- .../Http/Controllers/Api/AuthController.php | 4 + .../Api/Doctor/ChatDoctorController.php | 88 ++++++++++++++++++- .../Http/Controllers/Api/DuitkuController.php | 38 +++++++- .../Controllers/Api/LivechatController.php | 57 ++++++++---- Modules/Linksehat/Routes/api.php | 2 +- app/Models/OLDLMS/NotificationToken.php | 5 +- app/Models/OLDLMS/User.php | 7 +- app/Notifications/SendNotification.php | 39 ++++++++ config/fcm.php | 5 ++ ...05_22_150529_add_column_to_users_table.php | 32 +++++++ 10 files changed, 253 insertions(+), 24 deletions(-) create mode 100644 app/Notifications/SendNotification.php create mode 100644 config/fcm.php create mode 100644 database/migrations/2024_05_22_150529_add_column_to_users_table.php diff --git a/Modules/Linksehat/Http/Controllers/Api/AuthController.php b/Modules/Linksehat/Http/Controllers/Api/AuthController.php index b5a25a85..29aafd30 100644 --- a/Modules/Linksehat/Http/Controllers/Api/AuthController.php +++ b/Modules/Linksehat/Http/Controllers/Api/AuthController.php @@ -266,6 +266,10 @@ class AuthController extends Controller $token = $request->token; $status = $request->status; + $user->fcm_token = $request->token; + $user->save(); + // $userUpdate = User::where + $user->notificationTokens()->updateOrCreate([ 'device_id' => $device_id, ], [ diff --git a/Modules/Linksehat/Http/Controllers/Api/Doctor/ChatDoctorController.php b/Modules/Linksehat/Http/Controllers/Api/Doctor/ChatDoctorController.php index 91376fb4..22d2d46c 100644 --- a/Modules/Linksehat/Http/Controllers/Api/Doctor/ChatDoctorController.php +++ b/Modules/Linksehat/Http/Controllers/Api/Doctor/ChatDoctorController.php @@ -3,6 +3,7 @@ namespace Modules\Linksehat\Http\Controllers\Api\Doctor; use App\Http\Controllers\Controller; +use App\Notifications\SendNotification; use App\Models\User; use App\Models\OLDLMS\User as UserLMS; use App\Models\Livechat; @@ -132,6 +133,34 @@ class ChatDoctorController extends Controller $livechat->status = 3; // Decline // Menyimpan perubahan ke database $livechat->save(); + + // Send Notification + $doctorId = $livechat->doctor_id; + $title = 'Decline Livechat'; + $body = 'Decline Livechat'; + $channel = Channel::where([ + 'member_id' => $livechat->patient_id, + 'doctor_id' => $livechat->doctor_id + ])->first(); + $dataNotif = [ + 'channel_id' => $channel->id, + 'livechat_id' => $livechat->id, + 'type' => 'decline-chat' + ]; + + $user = UserLMS::where('nID',$livechat->patient_id)->first(); + if ($user) { + $user->notify(new SendNotification($title, $body, $dataNotif)); + return ApiResponse::apiResponse("Success",['message' => 'Livechat updated successfully'], trans('Message.success'), 200); + } else { + return Helper::responseJson( + status: 'Not Found', + statusCode: 404, + message: 'Doctor not found.' + ); + } + + return ApiResponse::apiResponse("Success",['message' => 'Livechat updated successfully'], trans('Message.success'), 200); } else { return response()->json(['message' => 'Livechat not found'], 404); @@ -147,7 +176,34 @@ class ChatDoctorController extends Controller $livechat->accept_date = date('Y-m-d H:i:s'); // Accept // Menyimpan perubahan ke database $livechat->save(); - return ApiResponse::apiResponse("Success",['message' => 'Livechat updated successfully'], trans('Message.success'), 200); + + // Send Notification + $doctorId = $livechat->doctor_id; + $title = 'Approve Request Livechat'; + $body = 'Approve Livechat'; + $channel = Channel::where([ + 'member_id' => $livechat->patient_id, + 'doctor_id' => $livechat->doctor_id + ])->first(); + $dataNotif = [ + 'channel_id' => $channel->id, + 'livechat_id' => $livechat->id, + 'type' => 'approve-chat' + ]; + + $user = UserLMS::where('nID',$livechat->patient_id)->first(); + + if ($user) { + $user->notify(new SendNotification($title, $body, $dataNotif)); + return ApiResponse::apiResponse("Success",['message' => 'Livechat updated successfully'], trans('Message.success'), 200); + } else { + return Helper::responseJson( + status: 'Not Found', + statusCode: 404, + message: 'Doctor not found.' + ); + } + } else { return response()->json(['message' => 'Livechat not found'], 404); } @@ -159,10 +215,36 @@ class ChatDoctorController extends Controller if ($livechat) { // Memperbarui atribut model $livechat->status = 6; // End Chat - $livechat->end_date = date('Y-m-d H:i:s'); // Accept + $livechat->end_date = date('Y-m-d H:i:s'); // Endchat // Menyimpan perubahan ke database $livechat->save(); - return ApiResponse::apiResponse("Success",['message' => 'Livechat updated successfully'], trans('Message.success'), 200); + + // Send Notification + $doctorId = $livechat->doctor_id; + $title = 'End Livechat'; + $body = 'End Livechat'; + $channel = Channel::where([ + 'member_id' => $livechat->patient_id, + 'doctor_id' => $livechat->doctor_id + ])->first(); + $dataNotif = [ + 'channel_id' => $channel->id, + 'livechat_id' => $livechat->id, + 'type' => 'end-chat' + ]; + + $user = UserLMS::where('nID',$livechat->patient_id)->first(); + + if ($user) { + $user->notify(new SendNotification($title, $body, $dataNotif)); + return ApiResponse::apiResponse("Success",['message' => 'Livechat updated successfully'], trans('Message.success'), 200); + } else { + return Helper::responseJson( + status: 'Not Found', + statusCode: 404, + message: 'Doctor not found.' + ); + } } else { return response()->json(['message' => 'Livechat not found'], 404); } diff --git a/Modules/Linksehat/Http/Controllers/Api/DuitkuController.php b/Modules/Linksehat/Http/Controllers/Api/DuitkuController.php index 2d941340..964a45ff 100644 --- a/Modules/Linksehat/Http/Controllers/Api/DuitkuController.php +++ b/Modules/Linksehat/Http/Controllers/Api/DuitkuController.php @@ -3,11 +3,14 @@ namespace Modules\Linksehat\Http\Controllers\Api; use App\Helpers\Helper; +use App\Notifications\SendNotification; use App\Models\Organization; use App\Models\Speciality; use App\Models\Livechat; use App\Models\Channel; use App\Models\UserChannel; +use App\Models\User as UserAso; +use App\Models\OLDLMS\User; use Illuminate\Contracts\Support\Renderable; use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; @@ -285,7 +288,7 @@ class DuitkuController extends Controller 'created_at' => date('Y-m-d H:i:s') ]); - if ($notif->resultCode == "00") { + if ($notif->resultCode == "00") { // berhasil melakukan pembayaran // Action Success $livechat = Livechat::where('uuid', $notif->merchantOrderId)->first(); // Update status pembayaran @@ -329,6 +332,23 @@ class DuitkuController extends Controller ] ); + // Send Notification + $doctorId = $livechat->doctor_id; + $userDokter = UserAso::find($doctorId); + $title = 'Payment Succes Livechat'; + $patient = User::where('nID', $livechat->patient_id)->first(); + $body = 'Payment Succes Livechat from ' . $patient->sFirstName . ' ' . $patient->sLastName; + $channel = Channel::where([ + 'member_id' => $livechat->patient_id, + 'doctor_id' => $livechat->doctor_id + ])->first(); + $dataNotif = [ + 'channel_id' => $channel->id, + 'livechat_id' => $livechat->id, + 'type' => 'success-payment' + ]; + $userDokter->notify(new SendNotification($title, $body, $dataNotif)); + // Berikan respons yang sesuai ke klien return response()->json(['message' => 'Channel created successfully', 'channel' => $channel]); @@ -340,6 +360,22 @@ class DuitkuController extends Controller $livechat->status = 7; // failed payment $livechat->save(); + // Send Notification + $doctorId = $livechat->doctor_id; + $userDokter = UserAso::find($doctorId); + $title = 'Payment Failed Livechat'; + $patient = User::where('nID', $livechat->patient_id)->first(); + $body = 'Payment Failed Livechat from ' . $patient->sFirstName . ' ' . $patient->sLastName; + $channel = Channel::where([ + 'member_id' => $livechat->patient_id, + 'doctor_id' => $livechat->doctor_id + ])->first(); + $dataNotif = [ + 'channel_id' => $channel->id, + 'livechat_id' => $livechat->id, + 'type' => 'failed-payment' + ]; + $userDokter->notify(new SendNotification($title, $body, $dataNotif)); return response()->json(['message' => 'User Gagal melakukan pembayaran']); } diff --git a/Modules/Linksehat/Http/Controllers/Api/LivechatController.php b/Modules/Linksehat/Http/Controllers/Api/LivechatController.php index b1e2d7e0..d0ecdfa7 100644 --- a/Modules/Linksehat/Http/Controllers/Api/LivechatController.php +++ b/Modules/Linksehat/Http/Controllers/Api/LivechatController.php @@ -2,6 +2,7 @@ namespace Modules\Linksehat\Http\Controllers\Api; +use App\Notifications\SendNotification; use App\Helpers\Helper; use App\Helpers\DuitkuHelper; use App\Services\Duitku; @@ -9,8 +10,10 @@ use App\Models\Organization; use App\Models\PractitionerRole; use App\Models\Invoice; use App\Models\PaymentsMethods; +use App\Models\Channel; use App\Models\Livechat; use App\Models\OLDLMS\User; +use App\Models\User as UserAso; use Illuminate\Routing\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -126,18 +129,18 @@ class LivechatController extends Controller 'organization_id' => $request->organization_id, 'descriptions' => $request->descriptions ]; - + $validator = Validator::make($request->all(), [ 'doctor_id' => 'required', 'patient_id' => 'required', 'descriptions' => 'required', ], [ 'doctor_id.required' => 'ID Dokter harus diisi', - 'patient_id.required' => 'ID Dokter harus diisi', - 'descriptions.required' => 'Description harus diisi', + 'patient_id.required' => 'ID Pasien harus diisi', + 'descriptions.required' => 'Deskripsi harus diisi', ]); - - if ($validator->fails()){ + + if ($validator->fails()) { return Helper::responseJson( status: 'Bad Request', statusCode: 400, @@ -145,29 +148,54 @@ class LivechatController extends Controller ); } else { // insert table livechat - + /** * Status Livechat - * 1=Request, 2=Accept, 3=Decline, 4=Waiting Payment, 5=Success Payment, 6 = End Chat + * 1=Request, 2=Accept, 3=Decline, 4=Waiting Payment, 5=Success Payment, 6 = End Chat, 7=Payment Failed */ - + $timezone = date_default_timezone_get(); $data['request_date'] = date('Y-m-d H:i:s'); $data['timezone'] = $timezone; $data['uuid'] = (string) Str::orderedUuid(); - $data['status'] = 1; + $data['status'] = 1; // Request + $livechat = Livechat::create($data); $doctor = $livechat->doctor; - $data = [ + $responseData = [ 'id' => $livechat->id, 'request_date' => $livechat->request_date, - 'image_path' =>'https' + 'image_path' => 'https' // Ganti dengan path yang benar jika ada + ]; + + // Send Notification + $doctorId = $livechat->doctor_id; + $user = UserAso::find($doctorId); + $title = 'New Request Livechat'; + $patient = User::where('nID', $livechat->patient_id)->first(); + $body = 'Request Livechat from ' . $patient->sFirstName . ' ' . $patient->sLastName; + $channel = Channel::where([ + 'member_id' => $livechat->patient_id, + 'doctor_id' => $livechat->doctor_id + ])->first(); + $dataNotif = [ + 'channel_id' => $channel->id, + 'livechat_id' => $livechat->id, + 'type' => 'request-chat' ]; - - return Helper::responseJson(data: $data); + if ($user) { + $user->notify(new SendNotification($title, $body, $dataNotif)); + return Helper::responseJson(data: $responseData); + } else { + return Helper::responseJson( + status: 'Not Found', + statusCode: 404, + message: 'Doctor not found.' + ); + } } - } + } public function consultation_request_show($id){ $livechat = Livechat::where('id', $id)->with(['doctor', 'practitioner'])->first(); @@ -290,7 +318,6 @@ class LivechatController extends Controller // Membuat invoice menggunakan DuitkuHelper $duitku = DuitkuHelper::createInvoice($data); - return response()->json(['success' => true, 'data' => $duitku], 200); } catch (Exception $e) { // Menangkap error dan mengembalikan respon error diff --git a/Modules/Linksehat/Routes/api.php b/Modules/Linksehat/Routes/api.php index 9d50ff63..349ba5aa 100644 --- a/Modules/Linksehat/Routes/api.php +++ b/Modules/Linksehat/Routes/api.php @@ -44,7 +44,6 @@ Route::prefix('linksehat')->group(function () { Route::controller(AuthController::class)->group(function () { Route::post('otp-request', 'otpRequest'); - Route::post('notification-token', 'notificationToken'); Route::post('mock-otp', 'mockOtp'); Route::post('login', 'login'); Route::post('logout', 'logout'); @@ -74,6 +73,7 @@ Route::prefix('linksehat')->group(function () { }); Route::middleware('auth:sanctum')->group(function () { + Route::post('notification-token', [AuthController::class, 'notificationToken']); Route::get('profile/{id}', [ProfileController::class, 'index'])->name('profile'); Route::get('change-profile/{id}', [ProfileController::class, 'changeProfile'])->name('change-profile'); Route::post('profile', [ProfileController::class, 'update'])->name('profile.update'); diff --git a/app/Models/OLDLMS/NotificationToken.php b/app/Models/OLDLMS/NotificationToken.php index 7979b36d..e740c8c2 100644 --- a/app/Models/OLDLMS/NotificationToken.php +++ b/app/Models/OLDLMS/NotificationToken.php @@ -1,6 +1,6 @@ morphMany(NotificationToken::class, 'notifiabletoken'); } + + } diff --git a/app/Notifications/SendNotification.php b/app/Notifications/SendNotification.php new file mode 100644 index 00000000..ea9ca6d0 --- /dev/null +++ b/app/Notifications/SendNotification.php @@ -0,0 +1,39 @@ +title = $title; + $this->body = $body; + $this->data = $data; + } + + public function via($notifiable) + { + return [FcmChannel::class]; + } + + public function toFcm($notifiable) + { + return FcmMessage::create() + ->setData($this->data) // Menggunakan $this->data + ->setNotification([ + 'title' => $this->title, + 'body' => $this->body, + ]); + } +} diff --git a/config/fcm.php b/config/fcm.php new file mode 100644 index 00000000..79133a66 --- /dev/null +++ b/config/fcm.php @@ -0,0 +1,5 @@ + env('FCM_SERVER_KEY'), +]; \ No newline at end of file diff --git a/database/migrations/2024_05_22_150529_add_column_to_users_table.php b/database/migrations/2024_05_22_150529_add_column_to_users_table.php new file mode 100644 index 00000000..0afa02ae --- /dev/null +++ b/database/migrations/2024_05_22_150529_add_column_to_users_table.php @@ -0,0 +1,32 @@ +string('fcm_token')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('fcm_token'); + }); + } +};