messages chat view

This commit is contained in:
amazuecos
2024-11-28 16:07:11 +01:00
parent 093aa42e67
commit c1aecb43f2
16 changed files with 661 additions and 396 deletions

View File

@ -49,8 +49,6 @@ class ChatController extends BaseController
$this->clienteModel = model(ClienteModel::class);
$this->chatUserModel = model(ChatUser::class);
$this->chatNotificationModel = model(ChatNotification::class);
}
public function index() {}
public function get_chat_departments()
@ -89,7 +87,6 @@ class ChatController extends BaseController
$data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id);
$this->chatMessageModel->set_chat_department_messages_as_read($chat->id);
$data["count"] = count($data["messages"]);
}
$data["chat"] = $chat;
return $this->response->setJSON($data);
@ -111,13 +108,17 @@ class ChatController extends BaseController
$data["chat"] = $chat;
return $this->response->setJSON($data);
}
public function get_chat_direct_view($chat_id){
public function get_chat_direct_view($chat_id)
{
$chat = $this->chatModel->find($chat_id);
$this->viewData['breadcrumb'] = [
['title' => lang("Chat.chat"), 'route' => route_to("mensajeriaView"), 'active' => false],
['title' => $chat->title, 'route' => 'javascript:void(0);', 'active' => true]
];
$this->viewData["chatId"] = $chat_id;
$auth_user = auth()->user();
$this->chatModel->setAsViewedChatUserNotifications($chat_id, $auth_user->id);
$this->chatModel->setAsUnviewedChatUserMessages($chat_id, $auth_user->id);
return view(static::$viewPath . 'messageChat', $this->viewData);
}
@ -141,6 +142,12 @@ class ChatController extends BaseController
}
$chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]);
$dataResponse = $this->chatMessageModel->find($chat_message_id);
$chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]);
foreach ($chatDepartmentUsers as $user) {
if($user->id != auth()->user()->id){
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id,"user_id"=> $user->id]);
}
}
return $this->response->setJSON($dataResponse);
}
public function store_chat_message_pedido()
@ -156,6 +163,12 @@ class ChatController extends BaseController
}
$chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]);
$dataResponse = $this->chatMessageModel->find($chat_message_id);
$chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]);
foreach ($chatDepartmentUsers as $user) {
if($user->id != auth()->user()->id){
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id,"user_id"=> $user->id]);
}
}
return $this->response->setJSON($dataResponse);
}
public function store_chat_message_factura()
@ -171,6 +184,12 @@ class ChatController extends BaseController
}
$chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]);
$dataResponse = $this->chatMessageModel->find($chat_message_id);
$chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]);
foreach ($chatDepartmentUsers as $user) {
if($user->id != auth()->user()->id){
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id,"user_id"=> $user->id]);
}
}
return $this->response->setJSON($dataResponse);
}
public function store_chat_message_single()
@ -191,7 +210,7 @@ class ChatController extends BaseController
"receiver_id" => $data["receiver_id"],
]
);
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id,"user_id" =>$data["receiver_id"]]);
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $data["receiver_id"]]);
$dataResponse = $this->chatMessageModel->find($chat_message_id);
return $this->response->setJSON($dataResponse);
}
@ -200,11 +219,11 @@ class ChatController extends BaseController
$auth_user = auth()->user();
if ($auth_user->cliente_id) {
$users = $this->chatModel->getOpenChatCliente($auth_user->id);
}else{
} else {
$users = $this->userModel->builder()
->whereNotIn("id", [$auth_user->id])
->where("deleted_at", null)
->get()->getResultObject();
->whereNotIn("id", [$auth_user->id])
->where("deleted_at", null)
->get()->getResultObject();
}
foreach ($users as $user) {
$user->unreadMessages = $this->chatMessageModel->get_chat_messages_count($user->id);
@ -288,7 +307,26 @@ class ChatController extends BaseController
]
)->where("cliente_id", null)
->where("deleted_at", null)
->whereNotIn("id",[auth()->user()->id]);
->whereNotIn("id", [auth()->user()->id]);
if ($this->request->getGet("q")) {
$query->groupStart()
->orLike("users.username", $this->request->getGet("q"))
->orLike("CONCAT(first_name,' ',last_name)", $this->request->getGet("q"))
->groupEnd();
}
return $this->response->setJSON($query->get()->getResultObject());
}
public function get_chat_users_all()
{
$query = $this->userModel->builder()->select(
[
"id",
"CONCAT(first_name,' ',last_name,'(',username,')') as name"
]
)
->where("deleted_at", null)
->whereNotIn("id", [auth()->user()->id]);
if ($this->request->getGet("q")) {
$query->groupStart()
->orLike("users.username", $this->request->getGet("q"))
@ -310,15 +348,16 @@ class ChatController extends BaseController
"message" => $bodyData["message"],
"sender_id" => auth()->user()->id
]);
if(isset($bodyData["users"])){
$chatUserData = array_map(fn($x) => ["user_id" => $x,"chat_id" => $chat_id],$bodyData["users"]);
if (isset($bodyData["users"])) {
$chatUserData = array_map(fn($x) => ["user_id" => $x, "chat_id" => $chat_id], $bodyData["users"]);
$this->chatUserModel->insertBatch($chatUserData);
foreach ($bodyData["users"] as $userId) {
$this->chatNotificationModel->insert(
["chat_message_id" => $chatMessageId,"user_id" => $userId]);
["chat_message_id" => $chatMessageId, "user_id" => $userId]
);
}
}
return $this->response->setJSON(["message" => "Hebra creada correctamente","status" => true]);
return $this->response->setJSON(["message" => "Hebra creada correctamente", "status" => true]);
}
public function store_hebra_pedido()
@ -334,15 +373,16 @@ class ChatController extends BaseController
"message" => $bodyData["message"],
"sender_id" => auth()->user()->id
]);
if(isset($bodyData["users"])){
$chatUserData = array_map(fn($x) => ["user_id" => $x,"chat_id" => $chat_id],$bodyData["users"]);
if (isset($bodyData["users"])) {
$chatUserData = array_map(fn($x) => ["user_id" => $x, "chat_id" => $chat_id], $bodyData["users"]);
$this->chatUserModel->insertBatch($chatUserData);
foreach ($bodyData["users"] as $userId) {
$this->chatNotificationModel->insert(
["chat_message_id" => $chatMessageId,"user_id" => $userId]);
["chat_message_id" => $chatMessageId, "user_id" => $userId]
);
}
}
return $this->response->setJSON(["message" => "Hebra creada correctamente","status" => true]);
return $this->response->setJSON(["message" => "Hebra creada correctamente", "status" => true]);
}
public function store_hebra_factura()
@ -357,101 +397,105 @@ class ChatController extends BaseController
"message" => $bodyData["message"],
"sender_id" => auth()->user()->id
]);
if(isset($bodyData["users"])){
$chatUserData = array_map(fn($x) => ["user_id" => $x,"chat_id" => $chat_id],$bodyData["users"]);
if (isset($bodyData["users"])) {
$chatUserData = array_map(fn($x) => ["user_id" => $x, "chat_id" => $chat_id], $bodyData["users"]);
$this->chatUserModel->insertBatch($chatUserData);
foreach ($bodyData["users"] as $userId) {
$this->chatNotificationModel->insert(
["chat_message_id" => $chatMessageId,"user_id" => $userId]);
["chat_message_id" => $chatMessageId, "user_id" => $userId]
);
}
}
return $this->response->setJSON(["message" => "Hebra creada correctamente","status" => true]);
return $this->response->setJSON(["message" => "Hebra creada correctamente", "status" => true]);
}
public function update_hebra($chat_id)
{
$bodyData = $this->request->getPost();
$chatMessageId = $this->chatMessageModel->insert([
$chatMessageId = $this->chatMessageModel->insert([
"chat_id" => $chat_id,
"message" => $bodyData["message"],
"sender_id" => auth()->user()->id
]);
$actualUsers = $this->chatUserModel->builder()->select("user_id")->where("chat_id",$chat_id)->get()->getResultArray();
$actualUsersArray = array_map(fn($x) => $x["user_id"],$actualUsers);
$actualUsers = $this->chatUserModel->builder()->select("user_id")->where("chat_id", $chat_id)->get()->getResultArray();
$actualUsersArray = array_map(fn($x) => $x["user_id"], $actualUsers);
foreach ($actualUsersArray as $key => $user_id) {
$this->chatNotificationModel->insert(
["chat_message_id" => $chatMessageId,"user_id" => $user_id]);
["chat_message_id" => $chatMessageId, "user_id" => $user_id]
);
}
if(isset($bodyData["users"])){
if (isset($bodyData["users"])) {
foreach ($bodyData["users"] as $userId) {
if(in_array($userId,$actualUsersArray) == false){
$chatUserData = ["user_id" => $userId,"chat_id" => $chat_id];
if (in_array($userId, $actualUsersArray) == false) {
$chatUserData = ["user_id" => $userId, "chat_id" => $chat_id];
$this->chatUserModel->insert($chatUserData);
}
$this->chatNotificationModel->insert(
["chat_message_id" => $chatMessageId,"user_id" => $userId]);
["chat_message_id" => $chatMessageId, "user_id" => $userId]
);
}
}
return $this->response->setJSON(["message" => "Hebra actualizada correctamente","status" => true]);
return $this->response->setJSON(["message" => "Hebra actualizada correctamente", "status" => true]);
}
public function get_hebra_presupuesto($presupuesto_id){
public function get_hebra_presupuesto($presupuesto_id)
{
$data = $this->chatModel->getPresupuestoHebras($presupuesto_id);
$notifications = $this->chatModel->builder()->select([
"chat_notifications.id"
])
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left")
->where("chats.presupuesto_id",$presupuesto_id)
->where("chat_notifications.user_id",auth()->user()->id)
->get()->getResultArray();
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", "left")
->where("chats.presupuesto_id", $presupuesto_id)
->where("chat_notifications.user_id", auth()->user()->id)
->get()->getResultArray();
foreach ($notifications as $notification) {
$this->chatNotificationModel->update($notification["id"],["viewed" => true]);
$this->chatNotificationModel->update($notification["id"], ["viewed" => true]);
}
return $this->response->setJSON($data);
}
public function get_hebra_pedido($pedido_id){
public function get_hebra_pedido($pedido_id)
{
$data = $this->chatModel->getPedidoHebras($pedido_id);
$notifications = $this->chatModel->builder()->select([
"chat_notifications.id"
])
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left")
->where("chats.pedido_id",$pedido_id)
->where("chat_notifications.user_id",auth()->user()->id)
->get()->getResultArray();
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", "left")
->where("chats.pedido_id", $pedido_id)
->where("chat_notifications.user_id", auth()->user()->id)
->get()->getResultArray();
foreach ($notifications as $notification) {
$this->chatNotificationModel->update($notification["id"],["viewed" => true]);
$this->chatNotificationModel->update($notification["id"], ["viewed" => true]);
}
return $this->response->setJSON($data);
}
public function get_hebra_factura($factura_id){
public function get_hebra_factura($factura_id)
{
$data = $this->chatModel->getFacturaHebras($factura_id);
$notifications = $this->chatModel->builder()->select([
"chat_notifications.id"
])
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left")
->where("chats.factura_id",$factura_id)
->where("chat_notifications.user_id",auth()->user()->id)
->get()->getResultArray();
->join("chat_messages", "chat_messages.chat_id = chats.id", "left")
->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", "left")
->where("chats.factura_id", $factura_id)
->where("chat_notifications.user_id", auth()->user()->id)
->get()->getResultArray();
foreach ($notifications as $notification) {
$this->chatNotificationModel->update($notification["id"],["viewed" => true]);
$this->chatNotificationModel->update($notification["id"], ["viewed" => true]);
}
return $this->response->setJSON($data);
}
public function datatable_messages()
{
$query = $this->chatModel->getQueryDatatable();
$auth_user_id = auth()->user()->id;
$query = $this->chatModel->getQueryDatatable($auth_user_id);
return DataTable::of($query)
->edit('created_at',fn($q) => Time::createFromFormat('Y-m-d H:i:s',$q->created_at)->format("d/m/Y H:i"))
->edit('updated_at',fn($q) => Time::createFromFormat('Y-m-d H:i:s',$q->updated_at)->format("d/m/Y H:i"))
->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName)
->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id,$auth_user_id))
->add("action", fn($q) => $q->id)
->toJson(true);
->edit('created_at', fn($q) => Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i"))
->edit('updated_at', fn($q) => Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i"))
->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName)
->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id))
->add("action", fn($q) => $q->id)
->toJson(true);
}
public function store_new_direct_message()
{
@ -462,7 +506,7 @@ class ChatController extends BaseController
"users" => "required",
];
if(!$this->validate($rules)){
if (!$this->validate($rules)) {
return $this->response->setStatusCode(400)->setJSON([
'message' => lang('App.global_alert_save_error'),
'status' => 'error',
@ -470,14 +514,15 @@ class ChatController extends BaseController
]);
}
$this->chatModel->createNewDirectChat(...$bodyData);
return $this->response->setJSON(["message" => lang("Chat.new_message_ok"),"status" => true]);
return $this->response->setJSON(["message" => lang("Chat.new_message_ok"), "status" => true]);
}
public function get_chat_direct($chat_id){
public function get_chat_direct($chat_id)
{
$chatData = $this->chatModel->getChatDirect($chat_id);
return $this->response->setJSON($chatData);
}
public function get_chat_direct_select_users($chat_id){
public function get_chat_direct_select_users($chat_id)
{
$chat_users_id = $this->chatUserModel->getChatUserArrayId($chat_id);
$query = $this->userModel->builder()->select(
@ -485,9 +530,9 @@ class ChatController extends BaseController
"id",
"CONCAT(first_name,' ',last_name,'(',username,')') as name"
]
)->where("cliente_id", null)
)
->where("deleted_at", null)
->whereNotIn("id",$chat_users_id);
->whereNotIn("id", $chat_users_id);
if ($this->request->getGet("q")) {
$query->groupStart()
->orLike("users.username", $this->request->getGet("q"))
@ -497,19 +542,37 @@ class ChatController extends BaseController
return $this->response->setJSON($query->get()->getResultObject());
}
public function store_chat_direct_users($chat_id){
public function store_chat_direct_users($chat_id)
{
$bodyData = $this->request->getPost();
$chat_users = array_map(fn($q) => ["chat_id" => $chat_id,"user_id" => $q],$bodyData["users"]);
$chat_users = [];
foreach ($bodyData["users"] as $user_id) {
$chat_users[] = ["chat_id" => $chat_id, "user_id" => $user_id];
if ($bodyData["notification"]) {
$this->chatModel->createNotificationsToNewChatUser($chat_id, $user_id);
}
}
$this->chatUserModel->insertBatch($chat_users);
return $this->response->setJSON(["message" => "ok","status" => true]);
return $this->response->setJSON(["message" => "ok", "status" => true]);
}
public function store_chat_direct_message(int $chat_id){
public function store_chat_direct_message(int $chat_id)
{
$bodyData = $this->request->getPost();
$bodyData["sender_id"] = auth()->user()->id;
$auth_user = auth()->user();
$bodyData["sender_id"] = $auth_user->id;
$chat_message_id = $this->chatMessageModel->insert($bodyData);
$users_id = $this->chatUserModel->getChatUserArrayId($chat_id);
foreach ($users_id as $user_id) {
if ($user_id != $auth_user->id) {
$this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user_id]);
}
};
$message = $this->chatMessageModel->get_chat_message($chat_message_id);
return $this->response->setJSON($message);
return $this->response->setJSON($message);
}
public function update_chat_direct_message_unread($chat_id){
$this->chatModel->setAsUnviewedChatUserNotifications($chat_id,auth()->user()->id);
return $this->response->setJSON(["message" => "ok", "status" => true]);
}
}