mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
feat : migration and chat models
This commit is contained in:
@ -11,13 +11,17 @@ class ChatDepartments extends Migration
|
|||||||
"id" => [
|
"id" => [
|
||||||
"type" => "INT",
|
"type" => "INT",
|
||||||
"unsigned" => true,
|
"unsigned" => true,
|
||||||
"autoincrement" => true
|
"auto_increment" => true
|
||||||
],
|
],
|
||||||
"name" => [
|
"name" => [
|
||||||
"type" => "VARCHAR",
|
"type" => "VARCHAR",
|
||||||
"constraint" => '45',
|
"constraint" => '45',
|
||||||
"unique" => true,
|
"unique" => true,
|
||||||
],
|
],
|
||||||
|
"display" => [
|
||||||
|
"type" => "VARCHAR",
|
||||||
|
"constraint" => '255',
|
||||||
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
public function up()
|
public function up()
|
||||||
@ -28,22 +32,21 @@ class ChatDepartments extends Migration
|
|||||||
"created_at" => [
|
"created_at" => [
|
||||||
"type" => "TIMESTAMP",
|
"type" => "TIMESTAMP",
|
||||||
"default" => $currenttime,
|
"default" => $currenttime,
|
||||||
|
|
||||||
],
|
],
|
||||||
"updated_at" => [
|
"updated_at" => [
|
||||||
"type" => "TIMESTAMP",
|
"type" => "TIMESTAMP",
|
||||||
"nullable" => true,
|
"null" => true,
|
||||||
|
|
||||||
],
|
],
|
||||||
"deleted_at" => [
|
"deleted_at" => [
|
||||||
"type" => "TIMESTAMP",
|
"type" => "TIMESTAMP",
|
||||||
"nullable" => true,
|
"null" => true,
|
||||||
|
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
$this->forge->createTable("chat_departments",true);
|
|
||||||
$this->forge->addPrimaryKey('id');
|
$this->forge->addPrimaryKey('id');
|
||||||
|
$this->forge->createTable("chat_departments", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function down()
|
public function down()
|
||||||
|
|||||||
@ -11,7 +11,7 @@ class ChatsTable extends Migration
|
|||||||
"id" => [
|
"id" => [
|
||||||
"type" => "INT",
|
"type" => "INT",
|
||||||
"unsigned" => true,
|
"unsigned" => true,
|
||||||
"autoincrement" => true
|
"auto_increment" => true
|
||||||
],
|
],
|
||||||
"chat_department_id" => [
|
"chat_department_id" => [
|
||||||
"type" => "INT",
|
"type" => "INT",
|
||||||
@ -23,14 +23,14 @@ class ChatsTable extends Migration
|
|||||||
"unsigned" => true,
|
"unsigned" => true,
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
||||||
];
|
];
|
||||||
public function up()
|
public function up()
|
||||||
{
|
{
|
||||||
$this->forge->dropTable("chat_conversaciones",true);
|
$this->forge->dropTable("chat_conversaciones", true);
|
||||||
$this->forge->dropTable("chat_mensajes",true);
|
$this->forge->dropTable("chat_mensajes", true);
|
||||||
$this->forge->dropTable("chat_participantes",true);
|
$this->forge->dropTable("chat_participantes", true);
|
||||||
|
|
||||||
$this->forge->addField($this->COLUMNS);
|
$this->forge->addField($this->COLUMNS);
|
||||||
$currenttime = new RawSql("CURRENT_TIMESTAMP");
|
$currenttime = new RawSql("CURRENT_TIMESTAMP");
|
||||||
$this->forge->addField([
|
$this->forge->addField([
|
||||||
@ -40,24 +40,23 @@ class ChatsTable extends Migration
|
|||||||
],
|
],
|
||||||
"updated_at" => [
|
"updated_at" => [
|
||||||
"type" => "TIMESTAMP",
|
"type" => "TIMESTAMP",
|
||||||
"nullable" => true,
|
"null" => true,
|
||||||
],
|
],
|
||||||
"deleted_at" => [
|
"deleted_at" => [
|
||||||
"type" => "TIMESTAMP",
|
"type" => "TIMESTAMP",
|
||||||
"nullable" => true,
|
"null" => true,
|
||||||
|
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
$this->forge->addPrimaryKey('id');
|
$this->forge->addPrimaryKey('id');
|
||||||
$this->forge->addForeignKey('pedido_id', 'pedidos', 'id');
|
$this->forge->addForeignKey('pedido_id', 'pedidos', 'id');
|
||||||
// $this->forge->addForeignKey('chat_department_id', 'chat_departments', 'id');
|
$this->forge->addForeignKey('chat_department_id', 'chat_departments', 'id');
|
||||||
$this->forge->createTable("chats",true);
|
$this->forge->createTable("chats", true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function down()
|
public function down()
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
$this->forge->dropTable("chats",true);
|
$this->forge->dropTable("chats", true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -10,7 +10,7 @@ class ChatMessages extends Migration
|
|||||||
protected array $COLUMNS = [
|
protected array $COLUMNS = [
|
||||||
"id" => [
|
"id" => [
|
||||||
"type" => "INT",
|
"type" => "INT",
|
||||||
"autoincrement" => true,
|
"auto_increment" => true,
|
||||||
],
|
],
|
||||||
"chat_id" => [
|
"chat_id" => [
|
||||||
"type" => "INT",
|
"type" => "INT",
|
||||||
@ -20,7 +20,7 @@ class ChatMessages extends Migration
|
|||||||
"user_id" => [
|
"user_id" => [
|
||||||
"type" => "INT",
|
"type" => "INT",
|
||||||
"unsigned" => true,
|
"unsigned" => true,
|
||||||
|
|
||||||
],
|
],
|
||||||
"message" => [
|
"message" => [
|
||||||
"type" => "TEXT",
|
"type" => "TEXT",
|
||||||
@ -31,32 +31,33 @@ class ChatMessages extends Migration
|
|||||||
public function up()
|
public function up()
|
||||||
{
|
{
|
||||||
$this->forge->addField($this->COLUMNS);
|
$this->forge->addField($this->COLUMNS);
|
||||||
$this->forge->addForeignKey(['user_id'], 'users', ['id']);
|
|
||||||
$this->forge->addForeignKey(['chat_id'], 'chats', ['id']);
|
|
||||||
$currenttime = new RawSql("CURRENT_TIMESTAMP");
|
$currenttime = new RawSql("CURRENT_TIMESTAMP");
|
||||||
$this->forge->addField([
|
$this->forge->addField([
|
||||||
"created_at" => [
|
"created_at" => [
|
||||||
"type" => "TIMESTAMP",
|
"type" => "TIMESTAMP",
|
||||||
"default" => $currenttime,
|
"default" => $currenttime,
|
||||||
|
|
||||||
],
|
],
|
||||||
"updated_at" => [
|
"updated_at" => [
|
||||||
"type" => "TIMESTAMP",
|
"type" => "TIMESTAMP",
|
||||||
"nullable" => true,
|
"null" => true,
|
||||||
|
|
||||||
],
|
],
|
||||||
"deleted_at" => [
|
"deleted_at" => [
|
||||||
"type" => "TIMESTAMP",
|
"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()
|
public function down()
|
||||||
{
|
{
|
||||||
$this->forge->dropTable("chat_messages",true);
|
$this->forge->dropTable("chat_messages", true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,8 +8,9 @@ use CodeIgniter\Database\RawSql;
|
|||||||
class ChatDepartmentUsers extends Migration
|
class ChatDepartmentUsers extends Migration
|
||||||
{
|
{
|
||||||
protected array $COLUMNS = [
|
protected array $COLUMNS = [
|
||||||
"chat_deparment_id" => [
|
"chat_department_id" => [
|
||||||
"type" => "INT",
|
"type" => "INT",
|
||||||
|
"unsigned" => true,
|
||||||
],
|
],
|
||||||
"user_id" => [
|
"user_id" => [
|
||||||
"type" => "INT",
|
"type" => "INT",
|
||||||
@ -19,34 +20,31 @@ class ChatDepartmentUsers extends Migration
|
|||||||
public function up()
|
public function up()
|
||||||
{
|
{
|
||||||
$this->forge->addField($this->COLUMNS);
|
$this->forge->addField($this->COLUMNS);
|
||||||
$this->forge->addForeignKey(["user_id"],"users",["id"]);
|
|
||||||
$currenttime = new RawSql("CURRENT_TIMESTAMP");
|
$currenttime = new RawSql("CURRENT_TIMESTAMP");
|
||||||
$this->forge->addField([
|
$this->forge->addField([
|
||||||
"created_at" => [
|
"created_at" => [
|
||||||
"type" => "TIMESTAMP",
|
"type" => "TIMESTAMP",
|
||||||
"default" => $currenttime,
|
"default" => $currenttime,
|
||||||
|
|
||||||
],
|
],
|
||||||
"updated_at" => [
|
"updated_at" => [
|
||||||
"type" => "TIMESTAMP",
|
"type" => "TIMESTAMP",
|
||||||
"nullable" => true,
|
"null" => true,
|
||||||
|
|
||||||
],
|
],
|
||||||
"deleted_at" => [
|
"deleted_at" => [
|
||||||
"type" => "TIMESTAMP",
|
"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()
|
public function down()
|
||||||
{
|
{
|
||||||
$this->forge->dropTable("chat_department_users");
|
$this->forge->dropTable("chat_department_users");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
83
ci4/app/Database/Seeds/ChatSeeder.php
Normal file
83
ci4/app/Database/Seeds/ChatSeeder.php
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Database\Seeds;
|
||||||
|
|
||||||
|
use App\Models\Chat\ChatDeparmentModel;
|
||||||
|
use App\Models\Chat\ChatDeparmentUserModel;
|
||||||
|
use CodeIgniter\Database\Seeder;
|
||||||
|
use App\Models\Usuarios\UserModel;
|
||||||
|
|
||||||
|
class ChatSeeder extends Seeder
|
||||||
|
{
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
[
|
||||||
|
"name" => "_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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,18 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models\Chat;
|
||||||
|
|
||||||
use CodeIgniter\Model;
|
use CodeIgniter\Model;
|
||||||
|
|
||||||
class ChatDeparmentModel extends Model
|
class ChatDeparmentModel extends Model
|
||||||
{
|
{
|
||||||
protected $table = 'chat_deparments';
|
protected $table = 'chat_departments';
|
||||||
protected $primaryKey = 'id';
|
protected $primaryKey = 'id';
|
||||||
protected $useAutoIncrement = true;
|
protected $useAutoIncrement = true;
|
||||||
protected $returnType = 'array';
|
protected $returnType = 'array';
|
||||||
protected $useSoftDeletes = false;
|
protected $useSoftDeletes = false;
|
||||||
protected $protectFields = true;
|
protected $protectFields = true;
|
||||||
protected $allowedFields = [];
|
protected $allowedFields = [
|
||||||
|
"name",
|
||||||
|
"display"
|
||||||
|
];
|
||||||
|
|
||||||
protected bool $allowEmptyInserts = false;
|
protected bool $allowEmptyInserts = false;
|
||||||
protected bool $updateOnlyChanged = true;
|
protected bool $updateOnlyChanged = true;
|
||||||
|
|||||||
@ -1,18 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models\Chat;
|
||||||
|
|
||||||
|
|
||||||
use CodeIgniter\Model;
|
use CodeIgniter\Model;
|
||||||
|
|
||||||
class ChatDeparmentUserModel extends Model
|
class ChatDeparmentUserModel extends Model
|
||||||
{
|
{
|
||||||
protected $table = 'chat_deparment_users';
|
protected $table = 'chat_department_users';
|
||||||
protected $primaryKey = 'id';
|
protected $primaryKey = 'id';
|
||||||
protected $useAutoIncrement = true;
|
protected $useAutoIncrement = true;
|
||||||
protected $returnType = 'array';
|
protected $returnType = 'array';
|
||||||
protected $useSoftDeletes = false;
|
protected $useSoftDeletes = false;
|
||||||
protected $protectFields = true;
|
protected $protectFields = true;
|
||||||
protected $allowedFields = [];
|
protected $allowedFields = [
|
||||||
|
"chat_department_id",
|
||||||
|
"user_id"
|
||||||
|
];
|
||||||
|
|
||||||
protected bool $allowEmptyInserts = false;
|
protected bool $allowEmptyInserts = false;
|
||||||
protected bool $updateOnlyChanged = true;
|
protected bool $updateOnlyChanged = true;
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models\Chat;
|
||||||
|
|
||||||
|
|
||||||
use CodeIgniter\Model;
|
use CodeIgniter\Model;
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models\Chat;
|
||||||
|
|
||||||
|
|
||||||
use CodeIgniter\Model;
|
use CodeIgniter\Model;
|
||||||
|
|
||||||
@ -12,7 +13,10 @@ class ChatModel extends Model
|
|||||||
protected $returnType = 'array';
|
protected $returnType = 'array';
|
||||||
protected $useSoftDeletes = false;
|
protected $useSoftDeletes = false;
|
||||||
protected $protectFields = true;
|
protected $protectFields = true;
|
||||||
protected $allowedFields = [];
|
protected $allowedFields = [
|
||||||
|
"pedido_id",
|
||||||
|
"chat_department_id"
|
||||||
|
];
|
||||||
|
|
||||||
protected bool $allowEmptyInserts = false;
|
protected bool $allowEmptyInserts = false;
|
||||||
protected bool $updateOnlyChanged = true;
|
protected bool $updateOnlyChanged = true;
|
||||||
@ -44,20 +48,23 @@ class ChatModel extends Model
|
|||||||
protected $beforeDelete = [];
|
protected $beforeDelete = [];
|
||||||
protected $afterDelete = [];
|
protected $afterDelete = [];
|
||||||
|
|
||||||
|
|
||||||
public function getChat(int $chat_id)
|
public function getChat(int $chat_id)
|
||||||
{
|
{
|
||||||
$this->db->table('chats')
|
$this->db->table('chats')
|
||||||
->select([
|
->select(
|
||||||
"chats.id as chatId",
|
[
|
||||||
"users.id as userId",
|
"chats.id as chatId",
|
||||||
"users.email",
|
"users.id as userId",
|
||||||
"chat_messages.created_at as messageCreatedAt",
|
"chats.pedido_id as pedidoId",
|
||||||
"chat_messages.message as messageText",
|
"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");
|
)
|
||||||
|
->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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user