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

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