feat: chat module

This commit is contained in:
amazuecos
2024-09-23 09:19:45 +02:00
parent bfea4aa67c
commit 766347ad81
28 changed files with 1598 additions and 801 deletions

View File

@ -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;
}
}