diff --git a/Modules/Linksehat/Http/Controllers/Api/ChatController.php b/Modules/Linksehat/Http/Controllers/Api/ChatController.php new file mode 100644 index 00000000..5a6624d5 --- /dev/null +++ b/Modules/Linksehat/Http/Controllers/Api/ChatController.php @@ -0,0 +1,196 @@ +validate([ + 'member_id' => 'required', + 'doctor_id' => 'required', + ], [ + 'member_id.required' => 'Member ID harus diisi.', + 'doctor_id.required' => 'Doctor ID harus diisi.', + ]); + + // Buat dan simpan data channel ke dalam tabel + $channel = Channel::updateOrCreate([ + 'name' => $request->member_id .'_' . $request->doctor_id, + ], + [ + 'name' => $request->member_id .'_' . $request->doctor_id, + 'type' => $request->type, + 'member_id' => $request->member_id, + 'doctor_id' => $request->doctor_id, + // Jika ada kolom tambahan, tambahkan di sini + ]); + + // Menggunakan updateOrCreate untuk menambahkan data UserChannel untuk member_id + $userChannelMember = UserChannel::updateOrCreate( + [ + 'user_id' => $request->member_id, + 'channel_id' => $channel->id + ], + [ + 'user_id' => $request->member_id, + 'channel_id' => $channel->id + ] + ); + + // Menggunakan updateOrCreate untuk menambahkan data UserChannel untuk doctor_id + $userChannelDoctor = UserChannel::updateOrCreate( + [ + 'user_id' => $request->doctor_id, + 'channel_id' => $channel->id + ], + [ + 'user_id' => $request->doctor_id, + 'channel_id' => $channel->id + ] + ); + + // Berikan respons yang sesuai ke klien + return response()->json(['message' => 'Channel created successfully', 'channel' => $channel]); + } + + public function listChannel(Request $request){ + // Validasi request jika diperlukan + $channel = Channel::where('member_id',$request->user_id)->get()->toArray(); + + if (!$channel) { + $dataChannel = Channel::where('doctor_id',$request->user_id)->get()->toArray(); + if ($dataChannel){ + $data = []; + foreach($dataChannel as $d){ + $user = User::with('detail')->where('nID', $d['member_id'])->first(); + $lastMessage = Message::where('channel_id', $d['id']) + ->latest('created_at') + ->first(); + $urlAvatarDefault = $user->detail->nIDJenisKelamin == 1 ? 'https://linksehat.dev/assets/img/users/male-avatar.png' : 'https://linksehat.dev/assets/img/users/female-avatar.png'; + $avatarMember = $user->detail->sImage ?? $urlAvatarDefault; + + $arr['id'] = $d['id']; + $arr['avatar'] = $avatarMember; + $arr['name'] = $user->sFirstName .' '.$user->sLastName; + $arr['last_message'] = $lastMessage; + + array_push($data, $arr); + } + } + $channel = $data; + } + + + return response()->json(['message' => 'Get List Channel successfully', 'channel' => $channel]); + } + + public function sendMessage(Request $request) + { + // Validasi request jika diperlukan + $validatedData = $request->validate([ + 'user_id' => 'required' + ]); + + // Ambil data dari request + $message = Message::create([ + 'content' => $request->message, + 'from_user' => $request->user_id, + 'channel_id' => $request->channel_id, + 'type' => $request->message ? 'text' : 'file' + ]); + + $pathFile = null; + if ($request->hasFile('file_chat')) { + foreach ($request->file_chat as $file) { + $pathFile = File::storeFile('chat', $message->id, $file); + File::updateOrCreate([ + 'fileable_type'=>'App\Models\Message', + 'fileable_id' => $message->id, + 'type' => 'chat', + 'name' => File::getFileName('chat', $message->id, $file), + 'original_name' => $file->getClientOriginalName(), + 'extension' => $file->getClientOriginalExtension(), + 'path' => $pathFile, + 'created_by' => auth()->user()->id, + 'updated_by' => auth()->user()->id, + ]); + } + + $message->update([ + 'content' => env('LMS_APP_STORAGE') . 'storage/' . $pathFile, + 'from_user' => $request->user_id, + 'channel_id' => $request->channel_id, + 'type' => 'file', + ]); + } + + $data = Message::where('channel_id', $request->channel_id)->get()->toArray(); + // Berikan respons yang sesuai ke klien + + $channel = Channel::where('id',$request->channel_id)->first(); + if($channel->member_id == $request->user_id){ + // Get nama dokter + $person = Person::where('id', $channel->doctor_id)->first(); + $name = $person->name; + } else { + // Get nama pasien + $person = User::where('nID', $channel->member_id)->first(); + $name = $person->sFirstName . ' ' . $person->sLastName; + } + + return response()->json([ + 'message' => 'Message sent successfully', + 'data' => [ + 'header' => $name, + 'chat' => $data + ] + ]); + } + + public function getMessage(Request $request) + { + // Buat instance Pusher dengan konfigurasi yang sesuai + $pusher = new Pusher( + env('PUSHER_APP_KEY'), + env('PUSHER_APP_SECRET'), + env('PUSHER_APP_ID'), + [ + 'cluster' => env('PUSHER_APP_CLUSTER'), + 'useTLS' => true, + ] + ); + $channel = Channel::where('id',$request->channel_id)->first(); + if($channel->member_id == $request->user_id){ + // Get nama dokter + $person = Person::where('id', $channel->doctor_id)->first(); + $name = $person->name; + } else { + // Get nama pasien + $person = User::where('nID', $channel->member_id)->first(); + $name = $person->sFirstName . ' ' . $person->sLastName; + } + + $data = Message::where('channel_id', $request->channel_id)->get()->toArray(); + // Berikan respons yang sesuai ke klien + return response()->json([ + 'message' => 'Message sent successfully', + 'data' => [ + 'header' => $name, + 'chat' => $data + ] + ]); + } +} diff --git a/Modules/Linksehat/Http/Controllers/Api/DuitkuController.php b/Modules/Linksehat/Http/Controllers/Api/DuitkuController.php index c2ecb497..33285b04 100644 --- a/Modules/Linksehat/Http/Controllers/Api/DuitkuController.php +++ b/Modules/Linksehat/Http/Controllers/Api/DuitkuController.php @@ -271,14 +271,6 @@ class DuitkuController extends Controller header('Content-Type: application/json'); $notif = json_decode($callback); - // var_dump($callback); - - if ($notif->resultCode == "00") { - // Action Success - } else if ($notif->resultCode == "01") { - // Action Failed - } - DB::table('api_logs') ->insert([ 'type' => 'in', @@ -288,6 +280,14 @@ class DuitkuController extends Controller 'created_at' => date('Y-m-d H:i:s') ]); + + if ($notif->resultCode == "00") { + // Action Success + return Redirect::to('https://linksehat.com/'); + } else if ($notif->resultCode == "01") { + // Action Failed + } + } catch (Exception $e) { http_response_code(400); echo $e->getMessage(); diff --git a/Modules/Linksehat/Routes/api.php b/Modules/Linksehat/Routes/api.php index bda1361f..b84bfbba 100644 --- a/Modules/Linksehat/Routes/api.php +++ b/Modules/Linksehat/Routes/api.php @@ -6,6 +6,7 @@ use Modules\Linksehat\Http\Controllers\Api\DashboardController; use Modules\Linksehat\Http\Controllers\Api\AutocompleteController; use Modules\Linksehat\Http\Controllers\Api\DoctorController; use Modules\Linksehat\Http\Controllers\Api\DuitkuController; +use Modules\Linksehat\Http\Controllers\Api\ChatController; use Modules\Linksehat\Http\Controllers\Api\HospitalController; use Modules\Linksehat\Http\Controllers\Api\NotificationTokenController; use Modules\Linksehat\Http\Controllers\Api\PersonController; @@ -105,6 +106,11 @@ Route::prefix('linksehat')->group(function () { Route::post('livechat/consultation-payment', 'consultation_payment'); }); + + Route::post('livechat/send-message', [ChatController::class, 'sendMessage']); + Route::get('livechat/get-message', [ChatController::class, 'getMessage']); + Route::post('livechat/channel',[ChatController::class, 'createChannel']); + Route::get('livechat/channel',[ChatController::class, 'listChannel']); Route::post('create-invoice-duitku', [DuitkuController::class, 'createInvoice']); Route::post('check-status-duitku', [DuitkuController::class, 'checkStatus']); diff --git a/app/Events/ChatMessageSent.php b/app/Events/ChatMessageSent.php new file mode 100644 index 00000000..b249aa12 --- /dev/null +++ b/app/Events/ChatMessageSent.php @@ -0,0 +1,39 @@ +message = $message; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('chat'); + + } +} diff --git a/app/Helpers/DuitkuHelper.php b/app/Helpers/DuitkuHelper.php index 84e9a1f8..48a31e05 100644 --- a/app/Helpers/DuitkuHelper.php +++ b/app/Helpers/DuitkuHelper.php @@ -39,25 +39,23 @@ class DuitkuHelper ]; try { $transactionList = \Duitku\Pop::transactionStatus($merchantOrderId, $duitkuConfig); - - header('Content-Type: application/json'); + $transaction = json_decode($transactionList); - // var_dump($transactionList); - - if ($transaction->statusCode == "00") { - // Action Success - } else if ($transaction->statusCode == "01") { - // Action Pending + if ($transaction->statusCode == "00" || $transaction->statusCode == "01") { + // Transaksi berhasil atau dalam proses + return $transaction; } else { - // Action Failed Or Expired + // Transaksi gagal atau kedaluwarsa + return ['error' => true]; } - return $transaction; - // return json_decode($transaction); + } catch (\Duitku\Exceptions\DuitkuException $e) { + // Tangani pengecualian yang terkait dengan Duitku + return ['error' => true, 'message' => $e->getMessage()]; } catch (Exception $e) { - return $e->getMessage(); + // Tangani pengecualian umum + return ['error' => true, 'message' => $e->getMessage()]; } - } public static function createInvoice($data) diff --git a/app/Listeners/ProcessChatMessage.php b/app/Listeners/ProcessChatMessage.php new file mode 100644 index 00000000..a0fc7afc --- /dev/null +++ b/app/Listeners/ProcessChatMessage.php @@ -0,0 +1,32 @@ +message; + } +} diff --git a/app/Models/Channel.php b/app/Models/Channel.php new file mode 100644 index 00000000..a318ca67 --- /dev/null +++ b/app/Models/Channel.php @@ -0,0 +1,17 @@ + 'final-log/', 'docs' => 'docs/', 'additional-files' => 'additional-files/', + 'chat' => 'chat/', ]; public function fileable() @@ -89,4 +90,15 @@ class File extends Model $file->storeAs('public/' . $directory, $fileName . '.' . $extension); return $path; } + + public static function storeFileChat($type, $id, $file) + { + // $fileName = self::getFileName($type, $id); + $fileName = $file->getClientOriginalName(); + $directory = self::getDirectory($type); + $extension = $file->getClientOriginalExtension(); + $path = $directory . $fileName . '.' . $extension; + $file->store('public/' .$path); + return $path; + } } diff --git a/app/Models/Message.php b/app/Models/Message.php new file mode 100644 index 00000000..587cb957 --- /dev/null +++ b/app/Models/Message.php @@ -0,0 +1,25 @@ +morphMany(File::class, 'fileable')->whereNull('deleted_at'); + } + +} diff --git a/app/Models/UserChannel.php b/app/Models/UserChannel.php new file mode 100644 index 00000000..7c45aad3 --- /dev/null +++ b/app/Models/UserChannel.php @@ -0,0 +1,17 @@ + [ LogClaimJournal::class, ], + + ChatMessageSent::class => [ + ProcessChatMessage::class, + ], ]; diff --git a/config/broadcasting.php b/config/broadcasting.php index 9e4d4aa4..e2550c4c 100644 --- a/config/broadcasting.php +++ b/config/broadcasting.php @@ -40,7 +40,9 @@ return [ 'port' => env('PUSHER_PORT', 443), 'scheme' => env('PUSHER_SCHEME', 'https'), 'encrypted' => true, - 'useTLS' => env('PUSHER_SCHEME', 'https') === 'https', + 'cluster' => 'ap1', + 'useTLS' => true + ], 'client_options' => [ // Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html diff --git a/database/migrations/2024_05_08_143444_create_user_channels_table.php b/database/migrations/2024_05_08_143444_create_user_channels_table.php new file mode 100644 index 00000000..a1709558 --- /dev/null +++ b/database/migrations/2024_05_08_143444_create_user_channels_table.php @@ -0,0 +1,33 @@ +id(); + $table->timestamps(); + $table->foreignId('user_id'); + $table->foreignId('channel_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('user_channels'); + } +}; diff --git a/database/migrations/2024_05_08_143558_create_channels_table.php b/database/migrations/2024_05_08_143558_create_channels_table.php new file mode 100644 index 00000000..2f65aeec --- /dev/null +++ b/database/migrations/2024_05_08_143558_create_channels_table.php @@ -0,0 +1,33 @@ +id(); + $table->timestamps(); + $table->string('name'); + $table->string('type'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('channels'); + } +}; diff --git a/database/migrations/2024_05_08_143716_create_messages_table.php b/database/migrations/2024_05_08_143716_create_messages_table.php new file mode 100644 index 00000000..fe9cd3d4 --- /dev/null +++ b/database/migrations/2024_05_08_143716_create_messages_table.php @@ -0,0 +1,36 @@ +id(); + $table->timestamps(); + $table->foreignId('from_user'); + $table->foreignId('channel_id'); + $table->text('content'); + $table->string('type'); + + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('messages'); + } +}; diff --git a/database/migrations/2024_05_14_100058_add_column_to_channels_table.php b/database/migrations/2024_05_14_100058_add_column_to_channels_table.php new file mode 100644 index 00000000..3043fdaa --- /dev/null +++ b/database/migrations/2024_05_14_100058_add_column_to_channels_table.php @@ -0,0 +1,34 @@ +foreignId('member_id'); + $table->foreignId('doctor_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('channels', function (Blueprint $table) { + $table->dropColumn('member_id'); + $table->dropColumn('doctor_id'); + }); + } +};