mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
message refactor
This commit is contained in:
79
ci4/app/Entities/Chat/ChatDepartmentEntity.php
Normal file
79
ci4/app/Entities/Chat/ChatDepartmentEntity.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entities\Chat;
|
||||
|
||||
use App\Models\Chat\ChatDeparmentUserModel;
|
||||
use App\Models\Chat\ChatModel;
|
||||
use CodeIgniter\Entity\Entity;
|
||||
|
||||
|
||||
class ChatDepartmentEntity extends Entity
|
||||
{
|
||||
protected $attributes = [
|
||||
"id" => null,
|
||||
"name" => null,
|
||||
"display" => null,
|
||||
"description" => null,
|
||||
"type" => null,
|
||||
];
|
||||
|
||||
|
||||
protected $casts = [
|
||||
"name" => "string",
|
||||
"display" => "string",
|
||||
"description" => "?string",
|
||||
"type" => "string",
|
||||
];
|
||||
|
||||
/**
|
||||
* Chat department users that are in department as administrador. These users belongs to department in all chats in any model associated and
|
||||
* will receive notifications always
|
||||
*
|
||||
* @return array<ChatDepartmentEntity>
|
||||
*/
|
||||
public function chatDepartmentAdminUsers(){
|
||||
$m = model(ChatDeparmentUserModel::class);
|
||||
$chatDepartmentUsers = $m->where('chat_department_id',$this->attributes['id'])
|
||||
->where('pedido_id',null)
|
||||
->where('factura_id',null)
|
||||
->where('presupuesto_id',null)->findAll();
|
||||
return $chatDepartmentUsers;
|
||||
}
|
||||
/**
|
||||
* Chat department users that has been associated to the department by sending a
|
||||
* message and users that are not as admin of the department that has written a message.
|
||||
*
|
||||
* @param integer $modelFkId Id from model associated
|
||||
* @param string $model Name of the model associated `['presupuesto','pedido','factura']`
|
||||
* @return array<ChatDepartmentEntity>
|
||||
*/
|
||||
public function chatDepartmentExternalUsers(int $modelFkId,string $model = "presupuesto") : array
|
||||
{
|
||||
$m = model(ChatDeparmentUserModel::class);
|
||||
$m->where('chat_department_id',$this->attributes['id']);
|
||||
|
||||
switch ($model) {
|
||||
case 'presupuesto':
|
||||
$m->where('presupuesto_id',$modelFkId);
|
||||
break;
|
||||
case 'pedido':
|
||||
$m->where('pedido_id',$modelFkId);
|
||||
break;
|
||||
case 'factura':
|
||||
$m->where('pedido_id',$modelFkId);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return $m->findAll() ?? [];
|
||||
|
||||
}
|
||||
public function withUsers(int $modelFkId,string $model = "presupuesto") : self
|
||||
{
|
||||
$externalUsers = $this->chatDepartmentExternalUsers($modelFkId,$model);
|
||||
$this->attributes["adminUsers"] = array_map(fn(ChatDepartmentUserEntity $du) => $du->user()->withAvatar() , $this->chatDepartmentAdminUsers());
|
||||
$this->attributes["externalUsers"] = array_map(fn(ChatDepartmentUserEntity $du) => $du->user()->withAvatar() , $externalUsers);
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
60
ci4/app/Entities/Chat/ChatDepartmentUserEntity.php
Normal file
60
ci4/app/Entities/Chat/ChatDepartmentUserEntity.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entities\Chat;
|
||||
|
||||
use App\Entities\Facturas\FacturaEntity;
|
||||
use App\Entities\Pedidos\PedidoEntity;
|
||||
use App\Entities\Presupuestos\PresupuestoEntity;
|
||||
use App\Entities\Usuarios\UserEntity;
|
||||
use App\Models\Chat\ChatDeparmentModel;
|
||||
use App\Models\Pedidos\PedidoModel;
|
||||
use App\Models\Presupuestos\PresupuestoModel;
|
||||
use App\Models\Usuarios\UserModel;
|
||||
use CodeIgniter\Entity\Entity;
|
||||
|
||||
class ChatDepartmentUserEntity extends Entity
|
||||
{
|
||||
protected $attributes = [
|
||||
"chat_department_id" => null,
|
||||
"user_id" => null,
|
||||
"pedido_id" => null,
|
||||
"factura_id" => null,
|
||||
"presupuesto_id" => null,
|
||||
];
|
||||
|
||||
|
||||
protected $casts = [
|
||||
"chat_department_id" => "integer",
|
||||
"user_id" => "integer",
|
||||
"pedido_id" => "?integer",
|
||||
"factura_id" => "?integer",
|
||||
"presupuesto_id" => "?integer",
|
||||
];
|
||||
|
||||
public function user() : ?UserEntity
|
||||
{
|
||||
$m = model(UserModel::class);
|
||||
return $m->find($this->attributes['user_id']);
|
||||
}
|
||||
public function department() : ?ChatDepartmentEntity
|
||||
{
|
||||
$m = model(ChatDeparmentModel::class);
|
||||
return $m->find($this->attributes['chat_department_id']);
|
||||
}
|
||||
public function presupuesto() : ?PresupuestoEntity
|
||||
{
|
||||
$m = model(PresupuestoModel::class);
|
||||
return $m->find($this->attributes['presupuesto_id']);
|
||||
}
|
||||
public function pedido() : ?PedidoEntity
|
||||
{
|
||||
$m = model(PedidoModel::class);
|
||||
return $m->find($this->attributes['pedido_id']);
|
||||
}
|
||||
public function factura() : ?FacturaEntity
|
||||
{
|
||||
$m = model(FacturaEntity::class);
|
||||
return $m->find($this->attributes['factura_id']);
|
||||
}
|
||||
|
||||
}
|
||||
148
ci4/app/Entities/Chat/ChatEntity.php
Normal file
148
ci4/app/Entities/Chat/ChatEntity.php
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entities\Chat;
|
||||
|
||||
use App\Entities\Facturas\FacturaEntity;
|
||||
use App\Entities\Pedidos\PedidoEntity;
|
||||
use App\Entities\Presupuestos\PresupuestoEntity;
|
||||
use App\Entities\Usuarios\UserEntity;
|
||||
use App\Models\Chat\ChatDeparmentModel;
|
||||
use App\Models\Chat\ChatMessageModel;
|
||||
use App\Models\ChatUser;
|
||||
use App\Models\Facturas\FacturaModel;
|
||||
use App\Models\Pedidos\PedidoModel;
|
||||
use App\Models\Presupuestos\PresupuestoModel;
|
||||
use App\Models\Usuarios\UserModel;
|
||||
use CodeIgniter\Entity\Entity;
|
||||
use CodeIgniter\Model;
|
||||
use stdClass;
|
||||
|
||||
class ChatEntity extends Entity
|
||||
{
|
||||
protected $attributes = [
|
||||
"id" => null,
|
||||
"chat_department_id" => null,
|
||||
"pedido_id" => null,
|
||||
"presupuesto_id" => null,
|
||||
"factura_id" => null,
|
||||
"title" => null
|
||||
];
|
||||
|
||||
|
||||
protected $casts = [
|
||||
"chat_department_id" => "?integer",
|
||||
"pedido_id" => "?integer",
|
||||
"presupuesto_id" => "?integer",
|
||||
"factura_id" => "?integer",
|
||||
"title" => "string"
|
||||
];
|
||||
|
||||
public function withMessages(): self
|
||||
{
|
||||
$auth_user = auth()->user();
|
||||
$messages = $this->messages();
|
||||
foreach ($messages as $key => $message) {
|
||||
if ($message->sender_id == $auth_user->id) {
|
||||
$message->pos = 'right';
|
||||
} else {
|
||||
$message->pos = 'left';
|
||||
}
|
||||
}
|
||||
$this->attributes["messages"] = $messages;
|
||||
return $this;
|
||||
}
|
||||
public function department(): ?ChatDepartmentEntity
|
||||
{
|
||||
$m = model(ChatDeparmentModel::class);
|
||||
return $m->find($this->attributes['chat_department_id']);
|
||||
}
|
||||
public function presupuesto(): ?PresupuestoEntity
|
||||
{
|
||||
$m = model(PresupuestoModel::class);
|
||||
return $m->find($this->attributes['presupuesto_id']);
|
||||
}
|
||||
public function pedido(): ?PedidoEntity
|
||||
{
|
||||
$m = model(PedidoModel::class);
|
||||
return $m->find($this->attributes['pedido_id']);
|
||||
}
|
||||
public function factura(): ?FacturaEntity
|
||||
{
|
||||
$m = model(FacturaEntity::class);
|
||||
return $m->find($this->attributes['factura_id']);
|
||||
}
|
||||
public function messages(): ?array
|
||||
{
|
||||
$m = model(ChatMessageModel::class);
|
||||
|
||||
$messages = $m->where('chat_id', $this->attributes["id"])->findAll();
|
||||
return array_map(fn(ChatMessageEntity $m) => $m->withUser(), $messages);
|
||||
}
|
||||
public function withHebra(): self
|
||||
{
|
||||
$this->attributes["messages"] = array_map(fn(ChatMessageEntity $m) => $m->withUser(), $this->messages());
|
||||
$this->attributes["internalUsers"] = array_map(fn(ChatUserEntity $u) => $u->user(), $this->internalUsers());
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array<ChatUserEntity>
|
||||
*/
|
||||
public function internalUsers(): array
|
||||
{
|
||||
$m = model(ChatUser::class);
|
||||
return $m->where('chat_id', $this->attributes["id"])->findAll() ?? [];
|
||||
}
|
||||
public function getModel(): ?Model
|
||||
{
|
||||
$model = null;
|
||||
$models = [
|
||||
"presupuesto_id" => model(PresupuestoModel::class),
|
||||
"pedido_id" => model(PedidoModel::class),
|
||||
"factura_id" => model(FacturaModel::class)
|
||||
];
|
||||
$fks = [
|
||||
"presupuesto_id" => $this->attributes["presupuesto_id"],
|
||||
"pedido_id" => $this->attributes["pedido_id"],
|
||||
"factura_id" => $this->attributes["factura_id"],
|
||||
];
|
||||
foreach ($fks as $key => $fk) {
|
||||
if ($fk) {
|
||||
$model = $models[$key];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
public function relatedFk(): array
|
||||
{
|
||||
$relatedFk = null;
|
||||
$relatedFkValue = null;
|
||||
$fks = [
|
||||
"presupuesto" => $this->attributes["presupuesto_id"],
|
||||
"pedido" => $this->attributes["pedido_id"],
|
||||
"factura" => $this->attributes["factura_id"],
|
||||
];
|
||||
foreach ($fks as $key => $fk) {
|
||||
if ($fk) {
|
||||
$relatedFk = $key;
|
||||
$relatedFkValue = $fk;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return [
|
||||
"relatedModel" => $relatedFk,
|
||||
"relatedFkValue" => $relatedFkValue
|
||||
];
|
||||
}
|
||||
public function withModel() : self
|
||||
{
|
||||
if($this->getModel()){
|
||||
$this->attributes["modelData"] = $this->getModel()->find($this->relatedFk()['relatedFkValue']);
|
||||
}else{
|
||||
$this->attributes["modelData"] = null;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
65
ci4/app/Entities/Chat/ChatMessageEntity.php
Normal file
65
ci4/app/Entities/Chat/ChatMessageEntity.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entities\Chat;
|
||||
|
||||
use App\Entities\Usuarios\UserEntity;
|
||||
use App\Models\Chat\ChatModel;
|
||||
use App\Models\ChatNotification;
|
||||
use App\Models\Usuarios\UserModel;
|
||||
use CodeIgniter\Entity\Entity;
|
||||
use CodeIgniter\I18n\Time;
|
||||
|
||||
class ChatMessageEntity extends Entity
|
||||
{
|
||||
protected $attributes = [
|
||||
"id" => null,
|
||||
"message" => null,
|
||||
"chat_id" => null,
|
||||
"sender_id" => null,
|
||||
"receiver_id" => null,
|
||||
"viewed" => null,
|
||||
];
|
||||
|
||||
|
||||
protected $casts = [
|
||||
"message" => "string",
|
||||
"chat_id" => "integer",
|
||||
"sender_id" => "?integer",
|
||||
"receiver_id" => "?integer",
|
||||
"viewed" => "boolean",
|
||||
];
|
||||
protected $dates = [];
|
||||
|
||||
public function chat() : ?ChatEntity
|
||||
{
|
||||
$m = model(ChatModel::class);
|
||||
return $m->find($this->attributes['chat_id']);
|
||||
|
||||
}
|
||||
/**
|
||||
* Notifications related with this chat entity.
|
||||
*
|
||||
* @return array<ChatNotificationEntity>
|
||||
*/
|
||||
public function notifications() : array
|
||||
{
|
||||
$m = model(ChatNotification::class);
|
||||
return $m->asArray()->where('chat_id',$this->attributes['chat_id'])->findAll() ?? [];
|
||||
}
|
||||
public function sentBy() : ?UserEntity
|
||||
{
|
||||
$m = model(UserModel::class);
|
||||
return $m->find($this->attributes["sender_id"])->withAvatar();
|
||||
|
||||
}
|
||||
public function withUser() : self
|
||||
{
|
||||
$this->attributes["user"] = $this->sentBy();
|
||||
return $this;
|
||||
}
|
||||
public function getCreatedAt()
|
||||
{
|
||||
return Time::createFromFormat("Y-m-d H:i:s",$this->attributes['created_at'])->format('d/m/Y H:i');
|
||||
|
||||
}
|
||||
}
|
||||
42
ci4/app/Entities/Chat/ChatNotificationEntity.php
Normal file
42
ci4/app/Entities/Chat/ChatNotificationEntity.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entities\Chat;
|
||||
|
||||
use App\Entities\Usuarios\UserEntity;
|
||||
use App\Models\Chat\ChatMessageModel;
|
||||
use App\Models\Usuarios\UserModel;
|
||||
use CodeIgniter\Entity\Entity;
|
||||
use CodeIgniter\HTTP\Message;
|
||||
|
||||
class ChatNotificationEntity extends Entity
|
||||
{
|
||||
protected $attributes = [
|
||||
"id" => null,
|
||||
"chat_message_id" => null,
|
||||
"user_id" => null,
|
||||
"viewed" => null,
|
||||
];
|
||||
|
||||
|
||||
protected $casts = [
|
||||
"chat_message_id" => "integer",
|
||||
"user_id" => "integer",
|
||||
"viewed" => "boolean",
|
||||
];
|
||||
|
||||
public function message() : ?ChatMessageEntity
|
||||
{
|
||||
$m = model(ChatMessageModel::class);
|
||||
return $m->find($this->attributes['chat_message_id']);
|
||||
}
|
||||
public function user() : ?UserEntity
|
||||
{
|
||||
$m = model(UserModel::class);
|
||||
return $m->find($this->attributes['user_id']);
|
||||
}
|
||||
public function chat() : ?ChatEntity
|
||||
{
|
||||
return $this->message()->chat();
|
||||
}
|
||||
|
||||
}
|
||||
36
ci4/app/Entities/Chat/ChatUserEntity.php
Normal file
36
ci4/app/Entities/Chat/ChatUserEntity.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entities\Chat;
|
||||
|
||||
use App\Entities\Usuarios\UserEntity;
|
||||
use App\Models\Chat\ChatModel;
|
||||
use App\Models\Usuarios\UserModel;
|
||||
use CodeIgniter\Entity\Entity;
|
||||
|
||||
class ChatUserEntity extends Entity
|
||||
{
|
||||
protected $attributes = [
|
||||
"id" => null,
|
||||
"user_id" => null,
|
||||
"chat_id" => null,
|
||||
];
|
||||
|
||||
|
||||
protected $casts = [
|
||||
"user_id" => "integer",
|
||||
"chat_id" => "integer",
|
||||
];
|
||||
|
||||
public function chat() : ?ChatEntity
|
||||
{
|
||||
$m = model(ChatModel::class);
|
||||
return $m->find($this->attributes['chat_id']);
|
||||
|
||||
}
|
||||
public function user() : ?UserEntity
|
||||
{
|
||||
$m = model(UserModel::class);
|
||||
return $m->find($this->attributes['user_id'])->withAvatar();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user