API Chat
This commit is contained in:
196
Modules/Linksehat/Http/Controllers/Api/ChatController.php
Normal file
196
Modules/Linksehat/Http/Controllers/Api/ChatController.php
Normal file
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Linksehat\Http\Controllers\Api;
|
||||
|
||||
use App\Models\Channel;
|
||||
use App\Models\UserChannel;
|
||||
use App\Models\Message;
|
||||
use App\Models\File;
|
||||
use App\Models\Person;
|
||||
use App\Models\OLDLMS\User;
|
||||
use App\Models\OLDLMS\UserDetail;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Pusher\Pusher;
|
||||
|
||||
class ChatController extends Controller
|
||||
{
|
||||
public function createChannel(Request $request){
|
||||
// Validasi data yang diterima dari request
|
||||
$validatedData = $request->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
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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']);
|
||||
|
||||
39
app/Events/ChatMessageSent.php
Normal file
39
app/Events/ChatMessageSent.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class ChatMessageSent implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
|
||||
public $message;
|
||||
public function __construct($message)
|
||||
{
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should broadcast on.
|
||||
*
|
||||
* @return \Illuminate\Broadcasting\Channel|array
|
||||
*/
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel('chat');
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
32
app/Listeners/ProcessChatMessage.php
Normal file
32
app/Listeners/ProcessChatMessage.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\ChatMessageSent;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
|
||||
class ProcessChatMessage
|
||||
{
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param object $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(ChatMessageSent $event)
|
||||
{
|
||||
// Proses pesan yang diterima dari event
|
||||
$message = $event->message;
|
||||
}
|
||||
}
|
||||
17
app/Models/Channel.php
Normal file
17
app/Models/Channel.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Channel extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'type',
|
||||
'member_id',
|
||||
'doctor_id',
|
||||
];
|
||||
}
|
||||
@@ -52,6 +52,7 @@ class File extends Model
|
||||
'final-log-kondisi' => '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;
|
||||
}
|
||||
}
|
||||
|
||||
25
app/Models/Message.php
Normal file
25
app/Models/Message.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Message extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $connection = 'mysql';
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'channel_id',
|
||||
'content',
|
||||
'type',
|
||||
'from_user'
|
||||
];
|
||||
|
||||
public function files()
|
||||
{
|
||||
return $this->morphMany(File::class, 'fileable')->whereNull('deleted_at');
|
||||
}
|
||||
|
||||
}
|
||||
17
app/Models/UserChannel.php
Normal file
17
app/Models/UserChannel.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class UserChannel extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $connection = 'mysql';
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'channel_id',
|
||||
];
|
||||
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Providers;
|
||||
use App\Events\ClaimApproved;
|
||||
use App\Listeners\LogClaimJournal;
|
||||
use App\Listeners\NotifyClaimRequested;
|
||||
use App\Listeners\ProcessChatMessage;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
@@ -29,6 +30,10 @@ class EventServiceProvider extends ServiceProvider
|
||||
ClaimApproved::class => [
|
||||
LogClaimJournal::class,
|
||||
],
|
||||
|
||||
ChatMessageSent::class => [
|
||||
ProcessChatMessage::class,
|
||||
],
|
||||
|
||||
];
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('user_channels', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->timestamps();
|
||||
$table->foreignId('user_id');
|
||||
$table->foreignId('channel_id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('user_channels');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('channels', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->timestamps();
|
||||
$table->string('name');
|
||||
$table->string('type');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('channels');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('messages', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('channels', function (Blueprint $table) {
|
||||
$table->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');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user