File: /mnt/data/ghayatcom/ghayatcom-api/app/Http/Controllers/Api/AdminChatController.php
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Chat;
use App\AdminUserChat;
use App\Events\SendChat;
use App\Http\Requests\ChatRequest;
use App\Http\Resources\ChatResource;
use Illuminate\Support\Facades\Validator;
use App\Library\S3Library;
use App\User;
use Exception;
use Illuminate\Database\QueryException;
use Throwable;
class AdminChatController extends Controller
{
public function getAdminChatLists(Request $request)
{
try {
$chat = new AdminUserChat();
$user_id = auth()->user()->id;
// $chatLists=$chat->where('recipient_id',$user_id)
// ->with(['sender:id,first_name,last_name,profile_image'])->get()
// ->groupBy('sender_id')
// ->map(function ($chat) {
// return [
// 'user_id' => $chat->sender->id,
// 'first_name' => $chat->sender->first_name ?? '',
// 'last_name' => $chat->sender->last_name ?? '',
// 'profile_image' => $chat->sender->profile_image ?? '',
// 'last_message_time' => ($chat->updated_at ?? $chat->created_at)->format('Y-m-d H:i:s'),
// 'unread_count' => AdminUserChat::where('recipient_id', $chat->recipient_id)
// ->where('sender_id', $chat->sender_id)
// ->where('read_status', 0) // Assuming 'is_read' indicates message read status
// ->count(),
// ];
// });
//new
$chatLists = $chat->where('recipient_id', $user_id)
->with(['sender:id,first_name,last_name,profile_image'])
->get() // Retrieve the chats
->filter(function ($chat) use ($user_id) {
return $chat->sender_id != $user_id;
})
->groupBy('sender_id')
->map(function ($groupedChats) {
$latestChat = $groupedChats->sortByDesc('updated_at')->first();
return [
'user_id' => $latestChat->sender->id,
'first_name' => $latestChat->sender->first_name ?? '',
'last_name' => $latestChat->sender->last_name ?? '',
'profile_image' => $latestChat->sender->profile_image ?? '',
'last_message_time' => ($latestChat->updated_at ?? $latestChat->created_at)->format('Y-m-d H:i:s'),
'unread_count' => AdminUserChat::where('recipient_id', $latestChat->recipient_id)
->where('sender_id', $latestChat->sender_id)
->where('read_status', 0) // Assuming 'read_status' indicates the message read state
->count(),
];
})->values();
if($chatLists->count() > 0){
return self::sentResponse(200, $chatLists, __('digimed_validation.success_response.data_fetch_success'));
}
return self::sentResponse(201, [], __('digimed_validation.error_response.records_not_found'));
} catch (Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
//get user chat history
public function getChatHistory(Request $request)
{
try {
$user_id = auth()->user()->id;
$rules = [
'sender_id' => 'required',
];
$messages = [
'sender_id.required' => __('digimed_validation.form_validation_error.sender_id_req'),
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return self::sentResponse(422, [], $validator->errors()->first());
}
$sender_id = $request->sender_id;
if(isset($request->recipient_id)){
$recipient_id = $request->recipient_id;
}else{
return response()->json([
'code' => 201,
'message' => ['No Records Found'],
'data' => []
]);
}
AdminUserChat::where('sender_id','=',$sender_id)
->where('recipient_id','=',$recipient_id)
->update(['read_status' => 1]);
$messages = AdminUserChat::select('id', 'sender_id', 'recipient_id', 'message', 'status', 'read_status', 'utc_date_time', 'created_at','updated_at')->where(function ($query) use ($sender_id, $recipient_id) {
$query->where(array('sender_id' => $sender_id, 'recipient_id' => $recipient_id));
})->orWhere(function ($query) use ($sender_id, $recipient_id) {
$query->where(array('sender_id' => $recipient_id, 'recipient_id' => $sender_id));
})->with([
'sender:id,first_name,last_name,profile_image',
'recipient:id,first_name,last_name,profile_image'
]) // Load sender data
->latest()
->take(50)
->get();
if($messages->count() > 0){
return self::sentResponse(200, $messages->toArray(), __('digimed_validation.success_response.data_fetch_success'));
}
return response()->json([
'code' => 201,
'message' => ['No Records Found'],
'data' => []
]);
} catch (Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
//send chat
public function sendChat(Request $request)
{
try {
$sender_id = auth()->user()->id;
$chat = new AdminUserChat();
$saveChatId = $chat->create([
'sender_id' => $sender_id ,
'recipient_id' => $request->input('recipient_id'),
'message' => $request->input('message'),
])->id;
if ($saveChatId) {
$chatDataSet = AdminUserChat::find($saveChatId);
$resultSet = new ChatResource($chatDataSet);
event(new SendChat($resultSet));
return self::sentResponse(200, [], __('digimed_validation.success_response.message_sent_success'));
}
} catch (Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
public function chatReadStatus(Request $request)
{
try {
$reciever_id = auth()->user()->id;
$clearChat =AdminUserChat::where('recipient_id','=',$reciever_id)->update(['read_status' => 1]);
return self::sentResponse(200, [], __('digimed_validation.success_response.chat_read_success'));
} catch(Exception | Throwable | QueryException $e) {
return self::sentResponse(500,[],$e->getMessage());
}
}
}