message refactor

This commit is contained in:
amazuecos
2025-03-24 08:12:06 +01:00
parent 61af547135
commit c0d54e28b7
26 changed files with 994 additions and 677 deletions

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