merge feat/mensajes-internos

This commit is contained in:
amazuecos
2024-10-14 13:33:09 +00:00
28 changed files with 1426 additions and 412 deletions

View File

@ -53,13 +53,17 @@ class ChatDeparmentModel extends Model
public function getChatDepartments(string $type = "general"): array
{
$userModel = model(UserModel::class);
$chatMessageModel = model(ChatMessageModel::class);
$query = $this->db->table('chat_departments')
->select(
[
'chat_departments.id',
'chat_departments.name',
'chat_departments.display',
'chat_department_users.user_id',
'chats.id as chatId',
]
)
->join(
@ -67,22 +71,28 @@ class ChatDeparmentModel extends Model
"chat_department_users.chat_department_id = chat_departments.id",
'left'
)
->join("chats", "chats.chat_department_id = chat_departments.id", "left")
->join(
"users",
"chat_department_users.user_id = users.id",
'left'
)->where("chat_departments.type",$type);
if(auth()->user()->cliente_id == null){
$query->where("chat_department_users.user_id",auth()->user()->id);
}
)->join(
"chat_messages",
"chat_messages.chat_id = chats.id",
"left"
)
->where("chat_departments.type", $type);
if (auth()->user()->cliente_id == null) {
$query->where("chat_department_users.user_id", auth()->user()->id);
}
$results = $query->get()->getResultArray();
// Create the desired structure
$departments = [];
foreach ($results as $row) {
$departmentName = $row['name'];
// If the department is not yet added to the array, initialize it
if (!isset($departments[$departmentName])) {
$departments[$departmentName] = [
@ -117,9 +127,8 @@ class ChatDeparmentModel extends Model
"users",
"chat_department_users.user_id = users.id",
'left'
)->where("chat_departments.id",$chat_deparment_id)
)->where("chat_departments.id", $chat_deparment_id)
->get()->getResultObject();
return $result;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Models\Chat;
use App\Models\ChatNotification;
use App\Models\Facturas\FacturaModel;
use App\Models\Pedidos\PedidoModel;
use App\Models\Presupuestos\PresupuestoModel;
@ -22,6 +23,7 @@ class ChatModel extends Model
"chat_department_id",
"presupuesto_id",
"factura_id",
"title"
];
protected bool $allowEmptyInserts = false;
@ -279,6 +281,7 @@ class ChatModel extends Model
}
public function getChatDepartmentNotifications()
{
$chatDeparmentModel = model(ChatDeparmentModel::class);
$chatMessageModel = model(ChatMessageModel::class);
$presupuestoModel = model(PresupuestoModel::class);
$facturaModel = model(FacturaModel::class);
@ -299,11 +302,14 @@ class ChatModel extends Model
->where("chat_department_users.user_id",auth()->user()->id);
$rows = $q->get()->getResultObject();
foreach ($rows as $row) {
$messages = $chatMessageModel->get_chat_messages($row->chatId);
$count = 0;
$chatDeparmentUsers = $chatDeparmentModel->getChatDepartmentUsers($row->chatDepartmentId);
$chatDeparmentUsersId = array_map(fn($x) => $x->id,$chatDeparmentUsers);
foreach ($messages as $m) {
if($m->viewed == false && $m->sender_id != auth()->user()->id)
if($m->viewed == false && $m->sender_id != auth()->user()->id && in_array($m->sender_id,$chatDeparmentUsersId) == false)
$count++;
}
if($row->presupuestoId){
@ -335,25 +341,184 @@ class ChatModel extends Model
public function getChatInternalNotifications()
{
$chatMessageModel = model(ChatMessageModel::class);
$userModel = model(UserModel::class);
$internalMessages = $chatMessageModel->builder()
$presupuestoModel = model(PresupuestoModel::class);
$facturaModel = model(FacturaModel::class);
$pedidoModel = model(PedidoModel::class);
$chatNotificationModel = model(ChatNotification::class);
$q = $this->db->table("chats")
->select([
"chat_id as chatId",
"sender_id",
"COUNT(viewed) as unreadMessages",
"chats.id as chatId",
"chats.pedido_id as pedidoId",
"chats.presupuesto_id as presupuestoId",
"chats.factura_id as facturaId",
"chats.presupuesto_id as presupuestoId",
"chats.title as chatDisplay"
])
->where("receiver_id",auth()->user()->id)
->where("viewed",false)->get()->getResultObject();
foreach ($internalMessages as $m) {
if($m->sender_id){
$sender = $userModel->find($m->sender_id);
$m->model = $sender;
$m->title = $sender->username;
$m->chatDisplay = ($sender->first_name ?? "")." ".($sender->last_name ?? "");
$m->avatar = strtoupper(mb_substr($sender->first_name, 0, 1).mb_substr($sender->last_name, 0, 1));
$m->uri = "#";
->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();
foreach ($rows as $row) {
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);
}
elseif($row->pedidoId){
$row->model = $pedidoModel->find($row->pedidoId);
$row->uri = "/pedidos/edit/".$row->pedidoId;
$row->title = $row->pedidoId;
$row->avatar = "P";
$row->unreadMessages = $this->countUnreadMessagePedido($row->pedidoId);
}
elseif($row->facturaId){
$row->model = $facturaModel->find($row->facturaId);
$row->uri = "/facturas/edit/".$row->facturaId;
$row->avatar = "F";
$row->title = $row->facturaId;
$row->unreadMessages = $this->countUnreadMessageFactura($row->facturaId);
}
}
return $internalMessages;
return $rows;
}
public function getChatInternalHebraPresupuesto(int $chat_id,int $presupuesto_id) : array
{
$data = [];
$query = $this->builder()->select([
"chats.id as chatId",
"chat_messages.message",
"users.username as senderUserName",
"chat_messages.created_at",
"CONCAT(users.first_name,' ',users.last_name) as senderFullName",
])
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("users","users.id = chat_messages.sender_id","left")
->where("chats.id",$chat_id)
->where("chats.presupuesto_id",$presupuesto_id);
$data["chatId"] = $chat_id;
$data["messages"] = $query->get()->getResultObject();
$data["chatTitle"] = $this->find($chat_id)->title;
$data["users"] = $this->getChatUsers($chat_id);
return $data;
}
public function getChatInternalHebraPedido($chat_id,$pedido_id){
$data = [];
$query = $this->builder()->select([
"chats.id as chatId",
"chat_messages.message",
"users.username as senderUserName",
"chat_messages.created_at",
"CONCAT(users.first_name,' ',users.last_name) as senderFullName",
])
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("users","users.id = chat_messages.sender_id","left")
->where("chats.id",$chat_id)
->where("chats.pedido_id",$pedido_id);
$data["chatId"] = $chat_id;
$data["messages"] = $query->get()->getResultObject();
$data["chatTitle"] = $this->find($chat_id)->title;
$data["users"] = $this->getChatUsers($chat_id);
return $data;
}
public function getChatInternalHebraFactura($chat_id,$factura_id){
$data = [];
$query = $this->builder()->select([
"chats.id as chatId",
"chat_messages.message",
"users.username as senderUserName",
"chat_messages.created_at",
"CONCAT(users.first_name,' ',users.last_name) as senderFullName",
])
->join("chat_messages","chat_messages.chat_id = chats.id","left")
->join("users","users.id = chat_messages.sender_id","left")
->where("chats.id",$chat_id)
->where("chats.factura_id",$factura_id);
$data["chatId"] = $chat_id;
$data["messages"] = $query->get()->getResultObject();
$data["chatTitle"] = $this->find($chat_id)->title;
$data["users"] = $this->getChatUsers($chat_id);
return $data;
}
public function getChatUsers(int $chat_id)
{
$query = $this->builder()->select([
"users.username",
"CONCAT(users.first_name,' ',users.last_name) as userFullName",
])
->join("chat_users","chat_users.chat_id = chats.id")
->join("users","users.id = chat_users.user_id","left")
->where("chats.id",$chat_id);
return $query->get()->getResultObject();
}
public function getPresupuestoHebras($presupuesto_id) : array
{
$data = [];
$chats = $this->builder()->select("chats.id as chatId")
->where("chats.chat_department_id",null)
->where("chats.presupuesto_id",$presupuesto_id)->get()->getResultObject();
foreach ($chats as $chat) {
$data[$chat->chatId] = $this->getChatInternalHebraPresupuesto($chat->chatId,$presupuesto_id);
}
return $data;
}
public function getPedidoHebras($pedido_id) : array
{
$data = [];
$chats = $this->builder()->select("chats.id as chatId")
->where("chats.chat_department_id",null)
->where("chats.pedido_id",$pedido_id)->get()->getResultObject();
foreach ($chats as $chat) {
$data[$chat->chatId] = $this->getChatInternalHebraPedido($chat->chatId,$pedido_id);
}
return $data;
}
public function getFacturaHebras($factura_id) : array
{
$data = [];
$chats = $this->builder()->select("chats.id as chatId")
->where("chats.chat_department_id",null)
->where("chats.factura_id",$factura_id)->get()->getResultObject();
foreach ($chats as $chat) {
$data[$chat->chatId] = $this->getChatInternalHebraFactura($chat->chatId,$factura_id);
}
return $data;
}
public function countUnreadMessagePresupuesto($presupuesto_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",$presupuesto_id)
->where("chat_notifications.viewed",false)
->where("chat_notifications.user_id",auth()->user()->id)
->countAllResults();
}
public function countUnreadMessagePedido($pedido_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.pedido_id",$pedido_id)
->where("chat_notifications.viewed",false)
->where("chat_notifications.user_id",auth()->user()->id)
->countAllResults();
}
public function countUnreadMessageFactura($factura_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.factura_id",$factura_id)
->where("chat_notifications.viewed",false)
->where("chat_notifications.user_id",auth()->user()->id)->countAllResults();
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Models;
use CodeIgniter\Model;
class ChatNotification extends Model
{
protected $table = 'chat_notifications';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
"chat_message_id",
"user_id",
"viewed"
];
protected bool $allowEmptyInserts = false;
protected bool $updateOnlyChanged = true;
protected array $casts = [];
protected array $castHandlers = [];
// Dates
protected $useTimestamps = false;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
}

View File

@ -0,0 +1,49 @@
<?php
namespace App\Models;
use CodeIgniter\Model;
class ChatUser extends Model
{
protected $table = 'chat_users';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
"user_id",
"chat_id"
];
protected bool $allowEmptyInserts = false;
protected bool $updateOnlyChanged = true;
protected array $casts = [];
protected array $castHandlers = [];
// Dates
protected $useTimestamps = false;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
}