feat : mensajes internos module

This commit is contained in:
amazuecos
2024-10-13 23:10:17 +00:00
parent ba3ec185ef
commit 2012ada27f
28 changed files with 1426 additions and 412 deletions

View File

@ -7,6 +7,8 @@ use App\Models\Chat\ChatDeparmentModel;
use App\Models\Chat\ChatDeparmentUserModel;
use App\Models\Chat\ChatMessageModel;
use App\Models\Chat\ChatModel;
use App\Models\ChatNotification;
use App\Models\ChatUser;
use App\Models\Clientes\ClienteModel;
use App\Models\Usuarios\UserModel;
use CodeIgniter\HTTP\ResponseInterface;
@ -22,6 +24,8 @@ class ChatController extends BaseController
protected ChatMessageModel $chatMessageModel;
protected UserModel $userModel;
protected ClienteModel $clienteModel;
protected ChatUser $chatUserModel;
protected ChatNotification $chatNotificationModel;
@ -40,6 +44,9 @@ class ChatController extends BaseController
$this->chatMessageModel = model(ChatMessageModel::class);
$this->userModel = model(UserModel::class);
$this->clienteModel = model(ClienteModel::class);
$this->chatUserModel = model(ChatUser::class);
$this->chatNotificationModel = model(ChatNotification::class);
}
public function index() {}
@ -55,11 +62,13 @@ class ChatController extends BaseController
$data = [
"chat" => null,
"messages" => null,
"count" => 0,
];
$chat = $this->chatModel->getChatPresupuesto($chat_department_id, $presupuesto_id);
if ($chat) {
$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);
@ -70,11 +79,13 @@ class ChatController extends BaseController
$data = [
"chat" => null,
"messages" => null,
"count" => 0,
];
$chat = $this->chatModel->getChatPedido($chat_department_id, $pedido_id);
if ($chat) {
$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;
@ -86,12 +97,13 @@ class ChatController extends BaseController
$data = [
"chat" => null,
"messages" => null,
"count" => 0,
];
$chat = $this->chatModel->getChatFactura($chat_department_id, $factura_id);
if ($chat) {
$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);
@ -151,7 +163,7 @@ class ChatController extends BaseController
public function store_chat_message_single()
{
$data = $this->request->getPost();
$existChat = $this->chatMessageModel->get_chat_contact_messages($data["receiver_id"]);
if (count($existChat) > 0) {
$chatId = $existChat[0]->chat_id;
@ -171,7 +183,7 @@ class ChatController extends BaseController
}
public function get_chat_internal_contacts()
{
if(auth()->user()->cliente_id){
if (auth()->user()->cliente_id) {
return $this->response->setJSON([]);
}
$users = $this->userModel->builder()
@ -187,7 +199,7 @@ class ChatController extends BaseController
}
public function get_chat_internal_contact(int $user_id)
{
if(auth()->user()->cliente_id){
if (auth()->user()->cliente_id) {
return $this->response->setJSON([]);
}
$users = $this->userModel->builder()
@ -207,7 +219,7 @@ class ChatController extends BaseController
{
$cliente_id = auth()->user()->cliente_id;
$response = [];
if($cliente_id){
if ($cliente_id) {
$data = $this->clienteModel->getClienteDataPresupuestoPedidoFactura($cliente_id);
$response["totalMessages"] = 0;
$response["chatFacturas"] = $this->chatModel->getClienteChatFacturas($data["facturas"]);
@ -223,8 +235,7 @@ class ChatController extends BaseController
$response["totalMessages"] += $value->unreadMessages;
}
$response["data"] = $data;
}else{
} else {
$response["internals"] = $this->chatModel->getChatDepartmentNotifications();
$internal_notifications = $this->chatModel->getChatInternalNotifications();
foreach ($internal_notifications as $value) {
@ -243,4 +254,167 @@ class ChatController extends BaseController
$data = $this->chatDeparmentModel->getChatDepartmentUsers($chat_department_id);
return $this->response->setJSON($data);
}
public function get_chat_users_internal()
{
$query = $this->userModel->builder()->select(
[
"id",
"CONCAT(first_name,' ',last_name,'(',username,')') as name"
]
)->where("cliente_id", null)
->where("deleted_at", null)
->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 store_hebra_presupuesto()
{
$bodyData = $this->request->getPost();
$chat_id = $this->chatModel->insert([
"presupuesto_id" => $bodyData["modelId"],
"title" => $bodyData["title"]
]);
$chatMessageId = $this->chatMessageModel->insert([
"chat_id" => $chat_id,
"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"]);
$this->chatUserModel->insertBatch($chatUserData);
foreach ($bodyData["users"] as $userId) {
$this->chatNotificationModel->insert(
["chat_message_id" => $chatMessageId,"user_id" => $userId]);
}
}
return $this->response->setJSON(["message" => "Hebra creada correctamente","status" => true]);
}
public function store_hebra_pedido()
{
$bodyData = $this->request->getPost();
$chat_id = $this->chatModel->insert([
"pedido_id" => $bodyData["modelId"],
"title" => $bodyData["title"]
]);
$chatMessageId = $this->chatMessageModel->insert([
"chat_id" => $chat_id,
"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"]);
$this->chatUserModel->insertBatch($chatUserData);
foreach ($bodyData["users"] as $userId) {
$this->chatNotificationModel->insert(
["chat_message_id" => $chatMessageId,"user_id" => $userId]);
}
}
return $this->response->setJSON(["message" => "Hebra creada correctamente","status" => true]);
}
public function store_hebra_factura()
{
$bodyData = $this->request->getPost();
$chat_id = $this->chatModel->insert([
"factura_id" => $bodyData["modelId"],
"title" => $bodyData["title"]
]);
$chatMessageId = $this->chatMessageModel->insert([
"chat_id" => $chat_id,
"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"]);
$this->chatUserModel->insertBatch($chatUserData);
foreach ($bodyData["users"] as $userId) {
$this->chatNotificationModel->insert(
["chat_message_id" => $chatMessageId,"user_id" => $userId]);
}
}
return $this->response->setJSON(["message" => "Hebra creada correctamente","status" => true]);
}
public function update_hebra($chat_id)
{
$bodyData = $this->request->getPost();
$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);
foreach ($actualUsersArray as $key => $user_id) {
$this->chatNotificationModel->insert(
["chat_message_id" => $chatMessageId,"user_id" => $user_id]);
}
if(isset($bodyData["users"])){
foreach ($bodyData["users"] as $userId) {
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]);
}
}
return $this->response->setJSON(["message" => "Hebra actualizada correctamente","status" => true]);
}
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();
foreach ($notifications as $notification) {
$this->chatNotificationModel->update($notification["id"],["viewed" => true]);
}
return $this->response->setJSON($data);
}
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();
foreach ($notifications as $notification) {
$this->chatNotificationModel->update($notification["id"],["viewed" => true]);
}
return $this->response->setJSON($data);
}
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();
foreach ($notifications as $notification) {
$this->chatNotificationModel->update($notification["id"],["viewed" => true]);
}
return $this->response->setJSON($data);
}
}