From bfea4aa67cb97ca36efcfac1c6e52f9936f846c3 Mon Sep 17 00:00:00 2001 From: Alvaro Mazuecos Nogales Date: Fri, 20 Sep 2024 11:25:34 +0200 Subject: [PATCH] feat : migration and chat models --- .../2024-09-17-151435_ChatDepartments.php | 15 ++-- ...e.php => 2024-09-17-151440_ChatsTable.php} | 23 +++-- .../2024-09-17-151450_ChatMessages.php | 21 ++--- .../2024-09-17-151500_ChatDepartmentUsers.php | 18 ++-- ci4/app/Database/Seeds/ChatSeeder.php | 83 +++++++++++++++++++ ci4/app/Models/Chat/ChatDeparmentModel.php | 9 +- .../Models/Chat/ChatDeparmentUserModel.php | 10 ++- ci4/app/Models/Chat/ChatMessageModel.php | 3 +- ci4/app/Models/Chat/ChatModel.php | 35 ++++---- 9 files changed, 158 insertions(+), 59 deletions(-) rename ci4/app/Database/Migrations/{2024-09-14-193027_ChatsTable.php => 2024-09-17-151440_ChatsTable.php} (70%) create mode 100644 ci4/app/Database/Seeds/ChatSeeder.php diff --git a/ci4/app/Database/Migrations/2024-09-17-151435_ChatDepartments.php b/ci4/app/Database/Migrations/2024-09-17-151435_ChatDepartments.php index f537b37e..52bc6437 100644 --- a/ci4/app/Database/Migrations/2024-09-17-151435_ChatDepartments.php +++ b/ci4/app/Database/Migrations/2024-09-17-151435_ChatDepartments.php @@ -11,13 +11,17 @@ class ChatDepartments extends Migration "id" => [ "type" => "INT", "unsigned" => true, - "autoincrement" => true + "auto_increment" => true ], "name" => [ "type" => "VARCHAR", "constraint" => '45', "unique" => true, ], + "display" => [ + "type" => "VARCHAR", + "constraint" => '255', + ], ]; public function up() @@ -28,22 +32,21 @@ class ChatDepartments extends Migration "created_at" => [ "type" => "TIMESTAMP", "default" => $currenttime, - + ], "updated_at" => [ "type" => "TIMESTAMP", - "nullable" => true, + "null" => true, ], "deleted_at" => [ "type" => "TIMESTAMP", - "nullable" => true, + "null" => true, ], ]); - $this->forge->createTable("chat_departments",true); $this->forge->addPrimaryKey('id'); - + $this->forge->createTable("chat_departments", true); } public function down() diff --git a/ci4/app/Database/Migrations/2024-09-14-193027_ChatsTable.php b/ci4/app/Database/Migrations/2024-09-17-151440_ChatsTable.php similarity index 70% rename from ci4/app/Database/Migrations/2024-09-14-193027_ChatsTable.php rename to ci4/app/Database/Migrations/2024-09-17-151440_ChatsTable.php index 1be8479d..25ac44de 100644 --- a/ci4/app/Database/Migrations/2024-09-14-193027_ChatsTable.php +++ b/ci4/app/Database/Migrations/2024-09-17-151440_ChatsTable.php @@ -11,7 +11,7 @@ class ChatsTable extends Migration "id" => [ "type" => "INT", "unsigned" => true, - "autoincrement" => true + "auto_increment" => true ], "chat_department_id" => [ "type" => "INT", @@ -23,14 +23,14 @@ class ChatsTable extends Migration "unsigned" => true, ], - + ]; public function up() { - $this->forge->dropTable("chat_conversaciones",true); - $this->forge->dropTable("chat_mensajes",true); - $this->forge->dropTable("chat_participantes",true); - + $this->forge->dropTable("chat_conversaciones", true); + $this->forge->dropTable("chat_mensajes", true); + $this->forge->dropTable("chat_participantes", true); + $this->forge->addField($this->COLUMNS); $currenttime = new RawSql("CURRENT_TIMESTAMP"); $this->forge->addField([ @@ -40,24 +40,23 @@ class ChatsTable extends Migration ], "updated_at" => [ "type" => "TIMESTAMP", - "nullable" => true, + "null" => true, ], "deleted_at" => [ "type" => "TIMESTAMP", - "nullable" => true, + "null" => true, ], ]); $this->forge->addPrimaryKey('id'); $this->forge->addForeignKey('pedido_id', 'pedidos', 'id'); - // $this->forge->addForeignKey('chat_department_id', 'chat_departments', 'id'); - $this->forge->createTable("chats",true); - + $this->forge->addForeignKey('chat_department_id', 'chat_departments', 'id'); + $this->forge->createTable("chats", true); } public function down() { // - $this->forge->dropTable("chats",true); + $this->forge->dropTable("chats", true); } } diff --git a/ci4/app/Database/Migrations/2024-09-17-151450_ChatMessages.php b/ci4/app/Database/Migrations/2024-09-17-151450_ChatMessages.php index 14ea7d1c..6ce55b86 100644 --- a/ci4/app/Database/Migrations/2024-09-17-151450_ChatMessages.php +++ b/ci4/app/Database/Migrations/2024-09-17-151450_ChatMessages.php @@ -10,7 +10,7 @@ class ChatMessages extends Migration protected array $COLUMNS = [ "id" => [ "type" => "INT", - "autoincrement" => true, + "auto_increment" => true, ], "chat_id" => [ "type" => "INT", @@ -20,7 +20,7 @@ class ChatMessages extends Migration "user_id" => [ "type" => "INT", "unsigned" => true, - + ], "message" => [ "type" => "TEXT", @@ -31,32 +31,33 @@ class ChatMessages extends Migration public function up() { $this->forge->addField($this->COLUMNS); - $this->forge->addForeignKey(['user_id'], 'users', ['id']); - $this->forge->addForeignKey(['chat_id'], 'chats', ['id']); + $currenttime = new RawSql("CURRENT_TIMESTAMP"); $this->forge->addField([ "created_at" => [ "type" => "TIMESTAMP", "default" => $currenttime, - + ], "updated_at" => [ "type" => "TIMESTAMP", - "nullable" => true, + "null" => true, ], "deleted_at" => [ "type" => "TIMESTAMP", - "nullable" => true, + "null" => true, ], ]); - $this->forge->createTable("chat_messages",true); - + $this->forge->addPrimaryKey('id'); + $this->forge->addForeignKey(['user_id'], 'users', ['id']); + $this->forge->addForeignKey(['chat_id'], 'chats', ['id']); + $this->forge->createTable("chat_messages", true); } public function down() { - $this->forge->dropTable("chat_messages",true); + $this->forge->dropTable("chat_messages", true); } } diff --git a/ci4/app/Database/Migrations/2024-09-17-151500_ChatDepartmentUsers.php b/ci4/app/Database/Migrations/2024-09-17-151500_ChatDepartmentUsers.php index 386af400..aa1e9ec3 100644 --- a/ci4/app/Database/Migrations/2024-09-17-151500_ChatDepartmentUsers.php +++ b/ci4/app/Database/Migrations/2024-09-17-151500_ChatDepartmentUsers.php @@ -8,8 +8,9 @@ use CodeIgniter\Database\RawSql; class ChatDepartmentUsers extends Migration { protected array $COLUMNS = [ - "chat_deparment_id" => [ + "chat_department_id" => [ "type" => "INT", + "unsigned" => true, ], "user_id" => [ "type" => "INT", @@ -19,34 +20,31 @@ class ChatDepartmentUsers extends Migration public function up() { $this->forge->addField($this->COLUMNS); - $this->forge->addForeignKey(["user_id"],"users",["id"]); $currenttime = new RawSql("CURRENT_TIMESTAMP"); $this->forge->addField([ "created_at" => [ "type" => "TIMESTAMP", "default" => $currenttime, - + ], "updated_at" => [ "type" => "TIMESTAMP", - "nullable" => true, + "null" => true, ], "deleted_at" => [ "type" => "TIMESTAMP", - "nullable" => true, + "null" => true, ], ]); - $this->forge->createTable("chat_department_users",true); - - - + $this->forge->addForeignKey(["user_id"], "users", ["id"]); + $this->forge->addForeignKey(["chat_department_id"], "chat_departments", ["id"]); + $this->forge->createTable("chat_department_users", true); } public function down() { $this->forge->dropTable("chat_department_users"); - } } diff --git a/ci4/app/Database/Seeds/ChatSeeder.php b/ci4/app/Database/Seeds/ChatSeeder.php new file mode 100644 index 00000000..a6fd6c2e --- /dev/null +++ b/ci4/app/Database/Seeds/ChatSeeder.php @@ -0,0 +1,83 @@ + "_produccion", + "display" => "Producción", + "users" => [ + "mbalbaci@safekat.com", + "mari.cano@safekat.com", + "beatriz@safekat.com" + ], + ], + [ + "name" => "_pod", + "display" => "POD", + "users" => [ + "pod@safekat.com", + ], + ], + [ + "name" => "_maquetacion", + "display" => "Maquetación", + "users" => [ + "maquetacion@safekat.com", + ], + ], + // [ + // "name" => "_comercial", + // "display" => "Comercial", + // "users" => [ + // "incidencias@safekat.com", + // ], + // ], + [ + "name" => "_incidencias", + "display" => "Incidencias", + "users" => [ + "incidencias@safekat.com", + ], + ], + [ + "name" => "_logistica", + "display" => "Logística", + "users" => [ + "logistica@safekat.com", + ], + ], + [ + "name" => "_admin", + "display" => "Administración", + "users" => [ + "contabilidad@safekat.com", + ], + ], + ]; + $chatDeparmentModel = model(ChatDeparmentModel::class); + $chatDeparmentUsersModel = model(ChatDeparmentUserModel::class); + $userModel = model(UserModel::class); + + foreach ($data as $row) { + $chatDeparmentId = $chatDeparmentModel->insert(["name" => $row["name"], "display" => $row["display"]]); + if (count($row["users"]) > 0) { + foreach ($row["users"] as $mail) { + $user = $userModel->like("username", $mail)->first(); + if ($user) { + $chatDeparmentUsersModel->insert(['user_id' => $user->id, "chat_department_id" => $chatDeparmentId]); + } + } + } + }; + } +} diff --git a/ci4/app/Models/Chat/ChatDeparmentModel.php b/ci4/app/Models/Chat/ChatDeparmentModel.php index 9b297c95..16a94953 100644 --- a/ci4/app/Models/Chat/ChatDeparmentModel.php +++ b/ci4/app/Models/Chat/ChatDeparmentModel.php @@ -1,18 +1,21 @@ db->table('chats') - ->select([ - "chats.id as chatId", - "users.id as userId", - "users.email", - "chat_messages.created_at as messageCreatedAt", - "chat_messages.message as messageText", - ] - ) - ->join("users","users.id == chat_messages.user_id","left") - ->join("chat_messages","chats.id == chat_messages.chat_id","left"); - + ->select( + [ + "chats.id as chatId", + "users.id as userId", + "chats.pedido_id as pedidoId", + "users.email", + "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(); } }