mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
feat: chat module
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models\Chat;
|
||||
|
||||
use App\Models\Usuarios\UserModel;
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class ChatDeparmentModel extends Model
|
||||
@ -14,7 +15,9 @@ class ChatDeparmentModel extends Model
|
||||
protected $protectFields = true;
|
||||
protected $allowedFields = [
|
||||
"name",
|
||||
"display"
|
||||
"display",
|
||||
"description",
|
||||
"type"
|
||||
];
|
||||
|
||||
protected bool $allowEmptyInserts = false;
|
||||
@ -46,4 +49,53 @@ class ChatDeparmentModel extends Model
|
||||
protected $afterFind = [];
|
||||
protected $beforeDelete = [];
|
||||
protected $afterDelete = [];
|
||||
|
||||
public function getChatDepartments(string $type = "general"): array
|
||||
{
|
||||
$userModel = model(UserModel::class);
|
||||
$query = $this->db->table('chat_departments')
|
||||
->select(
|
||||
[
|
||||
'chat_departments.id',
|
||||
'chat_departments.name',
|
||||
'chat_departments.display',
|
||||
'chat_department_users.user_id'
|
||||
]
|
||||
)
|
||||
->join(
|
||||
"chat_department_users",
|
||||
"chat_department_users.chat_department_id = chat_departments.id",
|
||||
'left'
|
||||
)
|
||||
->join(
|
||||
"users",
|
||||
"chat_department_users.user_id = users.id",
|
||||
'left'
|
||||
)->where("chat_departments.type",$type)
|
||||
->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] = [
|
||||
'id' => $row['id'],
|
||||
'name' => $row['name'],
|
||||
'display' => $row['display'],
|
||||
'users' => [] // Initialize users as an empty array
|
||||
];
|
||||
}
|
||||
|
||||
// If user_id is not null, add the user to the department's 'users' array
|
||||
if ($row['user_id']) {
|
||||
$departments[$departmentName]['users'][] = $userModel->find($row["user_id"]);
|
||||
}
|
||||
}
|
||||
return $departments;
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Models\Chat;
|
||||
|
||||
|
||||
use App\Models\Usuarios\UserModel;
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class ChatMessageModel extends Model
|
||||
@ -13,7 +13,13 @@ class ChatMessageModel extends Model
|
||||
protected $returnType = 'array';
|
||||
protected $useSoftDeletes = false;
|
||||
protected $protectFields = true;
|
||||
protected $allowedFields = [];
|
||||
protected $allowedFields = [
|
||||
"message",
|
||||
"chat_id",
|
||||
"sender_id",
|
||||
"receiver_id",
|
||||
"viewed"
|
||||
];
|
||||
|
||||
protected bool $allowEmptyInserts = false;
|
||||
protected bool $updateOnlyChanged = true;
|
||||
@ -44,4 +50,72 @@ class ChatMessageModel extends Model
|
||||
protected $afterFind = [];
|
||||
protected $beforeDelete = [];
|
||||
protected $afterDelete = [];
|
||||
|
||||
/**
|
||||
* Devuelve los mensajes del chat
|
||||
*
|
||||
* @param integer $chat_id
|
||||
* @return object
|
||||
*/
|
||||
public function get_chat_messages(int $chat_id): array
|
||||
{
|
||||
$user = model(UserModel::class);
|
||||
$messages = $this->builder()->where("chat_id", $chat_id)->orderBy("created_at", "asc")->get()->getResultObject();
|
||||
foreach ($messages as $message) {
|
||||
$message->pos = auth()->user()->id == $message->sender_id ? "right" : "left";
|
||||
if (auth()->user()->id == $message->sender_id) {
|
||||
$message->user = auth()->user();
|
||||
} else {
|
||||
$message->user = $user->find($message->sender_id);
|
||||
}
|
||||
}
|
||||
return $messages;
|
||||
}
|
||||
public function get_chat_contact_messages(int $receiver_id): array
|
||||
{
|
||||
$conversationArray = [];
|
||||
$userModel = model(UserModel::class);
|
||||
$receiverUser = $userModel->find($receiver_id);
|
||||
$messagesFromClient = $this->builder()
|
||||
->where("sender_id", auth()->user()->id)
|
||||
->where("receiver_id", $receiverUser->id)
|
||||
->get()->getResultObject();
|
||||
$messagesFromReceiver = $this->builder()
|
||||
->where("sender_id", $receiver_id)
|
||||
->where("receiver_id", auth()->user()->id)
|
||||
->get()->getResultObject();
|
||||
foreach ($messagesFromClient as $message) {
|
||||
$message->pos = "right";
|
||||
$message->user = auth()->user();
|
||||
$conversationArray[] = $message;
|
||||
}
|
||||
foreach ($messagesFromReceiver as $message) {
|
||||
$message->pos = "left";
|
||||
$message->user = $receiverUser;
|
||||
$conversationArray[] = $message;
|
||||
}
|
||||
$dates = array();
|
||||
foreach ($conversationArray as $key => $row) {
|
||||
$dates[$key] = strtotime($row->created_at);
|
||||
}
|
||||
array_multisort($dates, SORT_ASC, $conversationArray);
|
||||
return $conversationArray;
|
||||
}
|
||||
|
||||
public function get_chat_unread_messages_count(int $sender_id): int
|
||||
{
|
||||
$messagesFromReceiver = $this->builder()
|
||||
->where("sender_id", $sender_id)
|
||||
->where("viewed", false)
|
||||
->where("receiver_id", auth()->user()->id)->countAllResults();
|
||||
return $messagesFromReceiver;
|
||||
}
|
||||
public function set_chat_messages_as_read(int $sender_id): int
|
||||
{
|
||||
$messagesFromReceiver = $this->builder()
|
||||
->set("viewed", true)
|
||||
->where("sender_id", $sender_id)
|
||||
->where("receiver_id", auth()->user()->id)->update();
|
||||
return $messagesFromReceiver;
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ namespace App\Models\Chat;
|
||||
|
||||
|
||||
use CodeIgniter\Model;
|
||||
use stdClass;
|
||||
|
||||
class ChatModel extends Model
|
||||
{
|
||||
@ -15,7 +16,9 @@ class ChatModel extends Model
|
||||
protected $protectFields = true;
|
||||
protected $allowedFields = [
|
||||
"pedido_id",
|
||||
"chat_department_id"
|
||||
"chat_department_id",
|
||||
"presupuesto_id",
|
||||
"factura_id",
|
||||
];
|
||||
|
||||
protected bool $allowEmptyInserts = false;
|
||||
@ -49,22 +52,145 @@ class ChatModel extends Model
|
||||
protected $afterDelete = [];
|
||||
|
||||
|
||||
public function getChat(int $chat_id)
|
||||
public function getChat(int $chat_id): array
|
||||
{
|
||||
$this->db->table('chats')
|
||||
return $this->db->table('chats')
|
||||
->select(
|
||||
[
|
||||
"chats.id as chatId",
|
||||
"users.id as userId",
|
||||
"chats.pedido_id as pedidoId",
|
||||
"users.email",
|
||||
"chats.*",
|
||||
"chat_messages.created_at as messageCreatedAt",
|
||||
"chat_messages.message as messageText",
|
||||
]
|
||||
)
|
||||
->join("users", "users.id == chat_messages.user_id", "left")
|
||||
->join("chat_deparments", "chat_deparments.id == chats.chat_deparment_id", "left")
|
||||
->join("chat_messages", "chats.id == chat_messages.chat_id", "left")
|
||||
->where("chatId", $chat_id)->get()->getResultObject();
|
||||
->join("chat_messages", "chats.id = chat_messages.chat_id", "left")
|
||||
->orderBy("created_at", "desc")
|
||||
->where("chats.id", $chat_id)
|
||||
->get()->getResultObject();
|
||||
}
|
||||
public function getChatPresupuesto(int $chat_department_id, int $presupuesto_id)
|
||||
{
|
||||
return $this->builder()->where("presupuesto_id", $presupuesto_id)->where("chat_department_id", $chat_department_id)->get()->getFirstRow();
|
||||
}
|
||||
public function getChatPedido(int $chat_department_id, int $pedido_id)
|
||||
{
|
||||
return $this->builder()->where("pedido_id", $pedido_id)->where("chat_department_id", $chat_department_id)->get()->getFirstRow();
|
||||
}
|
||||
public function getChatFactura(int $chat_department_id, int $factura_id)
|
||||
{
|
||||
return $this->builder()->where("factura_id", $factura_id)->where("chat_department_id", $chat_department_id)->get()->getFirstRow();
|
||||
}
|
||||
|
||||
public function createChatPresupuesto(int $chat_department_id, int $presupuesto_id): int
|
||||
{
|
||||
return $this->insert([
|
||||
"presupuesto_id" => $presupuesto_id,
|
||||
"chat_department_id" => $chat_department_id
|
||||
]);
|
||||
}
|
||||
public function createChatPedido(int $chat_department_id, int $pedido_id) : int
|
||||
{
|
||||
|
||||
return $this->insert([
|
||||
"pedido_id" => $pedido_id,
|
||||
"chat_department_id" => $chat_department_id
|
||||
]);
|
||||
}
|
||||
public function createChatFactura(int $chat_department_id, int $factura_id) : int
|
||||
{
|
||||
|
||||
return $this->insert([
|
||||
"factura_id" => $factura_id,
|
||||
"chat_department_id" => $chat_department_id
|
||||
]);
|
||||
}
|
||||
public function createChatSingle() : int
|
||||
{
|
||||
return $this->insert(["chat_department_id" => null]);
|
||||
}
|
||||
public function existChatPresupuesto(int $chat_department_id, int $presupuesto_id): bool
|
||||
{
|
||||
$countChatPresupuesto = $this->builder()
|
||||
->where("presupuesto_id", $presupuesto_id)
|
||||
->where("chat_department_id", $chat_department_id)
|
||||
->countAllResults();
|
||||
return $countChatPresupuesto > 0;
|
||||
}
|
||||
public function existChatPedido(int $chat_department_id, int $pedido_id): bool
|
||||
{
|
||||
$countChatPresupuesto = $this->builder()
|
||||
->where("pedido_id", $pedido_id)
|
||||
->where("chat_department_id", $chat_department_id)
|
||||
->countAllResults();
|
||||
return $countChatPresupuesto > 0;
|
||||
}
|
||||
public function existChatFactura(int $chat_department_id, int $factura_id): bool
|
||||
{
|
||||
$countChatPresupuesto = $this->builder()
|
||||
->where("factura_id", $factura_id)
|
||||
->where("chat_department_id", $chat_department_id)
|
||||
->countAllResults();
|
||||
return $countChatPresupuesto > 0;
|
||||
}
|
||||
|
||||
public function getChatPedidosChat() : array
|
||||
{
|
||||
$query = $this->db->table("chats")
|
||||
->select([
|
||||
"chats.id as chatId",
|
||||
"chats.pedido_id as pedidoId",
|
||||
"chats.chat_department_id as chatDepartmentId",
|
||||
"chat_departments.display as chatDisplay",
|
||||
"pedidos.id as title"
|
||||
])
|
||||
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
|
||||
->join("pedidos","pedidos.id = chats.pedido_id","left")
|
||||
->get()->getResultObject();
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function getChatPresupuestosChat() : array
|
||||
{
|
||||
$query = $this->db->table("chats")
|
||||
->select([
|
||||
"chats.id as chatId",
|
||||
"chats.pedido_id as pedidoId",
|
||||
"chats.chat_department_id as chatDepartmentId",
|
||||
"chat_departments.display as chatDisplay",
|
||||
"presupuestos.titulo as title"
|
||||
])
|
||||
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
|
||||
->join("presupuestos","presupuestos.id = chats.pedido_id","left")
|
||||
->get()->getResultObject();
|
||||
return $query;
|
||||
}
|
||||
public function getChatFacturasChat() : array
|
||||
{
|
||||
$query = $this->db->table("chats")
|
||||
->select([
|
||||
"chats.id as chatId",
|
||||
"chats.pedido_id as pedidoId",
|
||||
"chats.chat_department_id as chatDepartmentId",
|
||||
"chat_departments.display as chatDisplay",
|
||||
"facturas.numero as title"
|
||||
])
|
||||
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
|
||||
->join("facturas","facturas.id = chats.pedido_id","left")
|
||||
->get()->getResultObject();
|
||||
return $query;
|
||||
}
|
||||
public function getChatSingleChat() : array
|
||||
{
|
||||
$query = $this->db->table("chats")
|
||||
->select([
|
||||
"chats.id as chatId",
|
||||
"chats.chat_department_id as chatDepartmentId",
|
||||
"chat_departments.display as chatDisplay",
|
||||
"facturas.numero as title"
|
||||
])
|
||||
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
|
||||
->join("facturas","facturas.id = chats.pedido_id","left")
|
||||
->get()->getResultObject();
|
||||
return $query;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -781,4 +781,7 @@ class PresupuestoModel extends \App\Models\BaseModel
|
||||
|
||||
return $description_interior . $description_cubierta . $description_sobrecubierta . $acabado;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user