feat: mensajes directos cliente

This commit is contained in:
amazuecos
2024-11-12 18:26:22 +01:00
parent cc9eba99fb
commit b97e027920
9 changed files with 200 additions and 11768 deletions

View File

@ -16,7 +16,7 @@ class ChatModel extends Model
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'object';
protected $useSoftDeletes = false;
protected $useSoftDeletes = true;
protected $protectFields = true;
protected $allowedFields = [
"pedido_id",
@ -33,7 +33,7 @@ class ChatModel extends Model
protected array $castHandlers = [];
// Dates
protected $useTimestamps = false;
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
@ -313,7 +313,6 @@ class ChatModel extends Model
"chats.pedido_id as pedidoId",
"chats.presupuesto_id as presupuestoId",
"chats.factura_id as facturaId",
"chats.presupuesto_id as presupuestoId",
"chats.chat_department_id as chatDepartmentId",
"chat_departments.display as chatDisplay",
])
@ -372,22 +371,23 @@ class ChatModel extends Model
"chats.pedido_id as pedidoId",
"chats.presupuesto_id as presupuestoId",
"chats.factura_id as facturaId",
"chats.presupuesto_id as presupuestoId",
"chats.title as chatDisplay"
"chats.title as chatDisplay",
])
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left")
->where("chat_notifications.user_id",auth()->user()->id)
->where("chat_notifications.viewed",false);
$rows = $q->get()->getResultObject();
$rows_new = [];
foreach ($rows as $row) {
$row->unreadMessages = 0;
if($row->presupuestoId){
$row->model = $presupuestoModel->find($row->presupuestoId);
$row->uri = "/presupuestos/cosidotapablanda/edit/".$row->presupuestoId;
$row->title = $row->presupuestoId;
$row->avatar = "PRE";
$row->unreadMessages = $this->countUnreadMessagePresupuesto($row->presupuestoId);
$rows_new[] = $row;
}
elseif($row->pedidoId){
$row->model = $pedidoModel->find($row->pedidoId);
@ -395,6 +395,7 @@ class ChatModel extends Model
$row->title = $row->pedidoId;
$row->avatar = "P";
$row->unreadMessages = $this->countUnreadMessagePedido($row->pedidoId);
$rows_new[] = $row;
}
@ -404,10 +405,42 @@ class ChatModel extends Model
$row->avatar = "F";
$row->title = $row->facturaId;
$row->unreadMessages = $this->countUnreadMessageFactura($row->facturaId);
$rows_new[] = $row;
}
}
return $rows;
return $rows_new;
}
public function getChatDirectMessageNotifications(){
$chatMessageModel = model(ChatMessageModel::class);
$chatNotificationModel = model(ChatNotification::class);
$q = $this->db->table("chats")
->select([
"chats.id as chatId",
"chats.title as chatDisplay",
"COUNT(chat_notifications.user_id) as unreadMessages"
])
->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",null)
->where("chats.chat_department_id",null)
->where("chats.pedido_id",null)
->where("chats.factura_id",null)
->where("chat_notifications.viewed",false)
->where("chat_notifications.user_id",auth()->user()->id);
$rows = $q->get()->getResultObject();
$rows_new = [];
foreach ($rows as $row) {
if($row->chatId){
$row->model = [];
$row->uri = "/mensajes/internos";
$row->avatar = "MD";
$row->title = "MD";
$row->chatDisplay = $this->getSenderIdFromChatMessage($row->chatId)?->username ?? "Unknown";
$rows_new[] = $row;
}
}
return $rows_new;
}
public function getChatInternalHebraPresupuesto(int $chat_id,int $presupuesto_id) : array
{
@ -541,4 +574,31 @@ class ChatModel extends Model
->where("chat_notifications.viewed",false)
->where("chat_notifications.user_id",auth()->user()->id)->countAllResults();
}
public function countUnreadMessageDirectos(int $chat_id) : int|string
{
return $this->builder()->select()
->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",null)
->where("chats.pedido_id",null)
->where("chats.factura_id",null)
->where("chats.chat_department_id",null)
->where("chat_messages.chat_id",$chat_id)
->where("chat_notifications.viewed",false)
->where("chat_notifications.user_id",auth()->user()->id)->countAllResults();
}
public function getSenderIdFromChatMessage(int $chat_id)
{
$first_message = $this->builder()->select()
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->where("chats.presupuesto_id",null)
->where("chats.pedido_id",null)
->where("chats.factura_id",null)
->where("chats.id",$chat_id)
->where("chat_messages.receiver_id",auth()->user()->id)->get()->getFirstRow();
$userModel = model(UserModel::class);
return $userModel->find($first_message->sender_id);
}
}