From 94ae518ceee10c1d78cf18d2699cd1902c1b8ef7 Mon Sep 17 00:00:00 2001
From: ousid <imo.ossama1@gmail.com>
Date: Sun, 25 Dec 2022 11:18:14 +0100
Subject: [PATCH 1/4] remove  column

---
 README.md                                         | 2 +-
 database/migrations/create_tickets_table.php.stub | 1 -
 tests/Feature/TicketTest.php                      | 2 +-
 3 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index 3454fa3..c3c6716 100644
--- a/README.md
+++ b/README.md
@@ -152,7 +152,7 @@ public function createCategory()
 |  status |`string` | `open`  |
 |  is_resolved |`boolean` | `false`  |
 |  is_locked |`boolean` | `false`  |
-| assigned_to_user_id | `integer` | `NULL` |
+| assigned_to | `integer` | `NULL` |
 |  created_at |`timestamp` | `NULL`  |
 |  updated_at |`timestamp` | `NULL`  |
 
diff --git a/database/migrations/create_tickets_table.php.stub b/database/migrations/create_tickets_table.php.stub
index 5064a5d..6a8cad0 100644
--- a/database/migrations/create_tickets_table.php.stub
+++ b/database/migrations/create_tickets_table.php.stub
@@ -22,7 +22,6 @@ return new class extends Migration
             $table->string('status')->default('open');
             $table->boolean('is_resolved')->default(false);
             $table->boolean('is_locked')->default(false);
-            $table->foreignId('assigned_to_user_id')->nullable();
             $table->timestamps();
         });
     }
diff --git a/tests/Feature/TicketTest.php b/tests/Feature/TicketTest.php
index b11b627..75e04fa 100644
--- a/tests/Feature/TicketTest.php
+++ b/tests/Feature/TicketTest.php
@@ -231,7 +231,7 @@
     $this->assertTrue($lockedTicket->isLocked());
 });
 
-it('ensures ticket methods as chainable', function () {
+it('ensures ticket methods are chainable', function () {
     $ticket = Ticket::factory()->create([
         'status' => 'open',
         'is_locked' => false,

From a739c93cd2bdcc064d2424c0d4949d6647ea1376 Mon Sep 17 00:00:00 2001
From: ousid <imo.ossama1@gmail.com>
Date: Sun, 25 Dec 2022 11:35:06 +0100
Subject: [PATCH 2/4] add user relationship into messages model

---
 config/laravel_ticket.php                         |  1 +
 .../migrations/create_messages_table.php.stub     |  2 +-
 src/Models/Message.php                            | 15 +++++++++++++++
 tests/Feature/MessageTest.php                     | 13 +++++++++++++
 4 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/config/laravel_ticket.php b/config/laravel_ticket.php
index cc63408..4f5c6ac 100644
--- a/config/laravel_ticket.php
+++ b/config/laravel_ticket.php
@@ -39,6 +39,7 @@
              * @see https://laravel.com/docs/9.x/eloquent-relationships#one-to-many
              */
             'columns' => [
+                'user_foreing_id' => 'user_id',
                 'ticket_foreing_id' => 'ticket_id',
             ],
         ],
diff --git a/database/migrations/create_messages_table.php.stub b/database/migrations/create_messages_table.php.stub
index 51a1907..0a75417 100644
--- a/database/migrations/create_messages_table.php.stub
+++ b/database/migrations/create_messages_table.php.stub
@@ -15,7 +15,7 @@ return new class extends Migration
 
         Schema::create($tableName['table'], function (Blueprint $table) use ($tableName) {
             $table->id();
-            $table->foreignId('user_id');
+            $table->foreignId($tableName['columns']['user_foreing_id']);
             $table->foreignId($tableName['columns']['ticket_foreing_id']);
             $table->text('message');
             $table->timestamps();
diff --git a/src/Models/Message.php b/src/Models/Message.php
index c6a0989..3056c68 100644
--- a/src/Models/Message.php
+++ b/src/Models/Message.php
@@ -38,6 +38,21 @@ public function ticket(): BelongsTo
         );
     }
 
+    /**
+     * Get Message Relationship
+     * 
+     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
+     */
+    public function user(): BelongsTo
+    {
+        $tableName = config('laravel_ticket.table_names.messages', 'message');
+
+        return $this->belongsTo(
+            config('auth.providers.users.model'),
+            $tableName['columns']['user_foreing_id']
+        );
+    }
+
     /**
      * Get the table associated with the model.
      *
diff --git a/tests/Feature/MessageTest.php b/tests/Feature/MessageTest.php
index 22ab8f5..8969dfd 100644
--- a/tests/Feature/MessageTest.php
+++ b/tests/Feature/MessageTest.php
@@ -2,6 +2,7 @@
 
 use Coderflex\LaravelTicket\Models\Message;
 use Coderflex\LaravelTicket\Models\Ticket;
+use Coderflex\LaravelTicket\Tests\Models\User;
 
 it('can attach message to a ticket', function () {
     $message = Message::factory()->create();
@@ -13,3 +14,15 @@
 
     $this->assertEquals($message->ticket->title, 'Can you create a message?');
 });
+
+it('message can be associated to a user', function () {
+    $user = User::factory()->create([
+        'name' => 'Oussama',
+    ]);
+    
+    $message = Message::factory()->create();
+
+    $message->user()->associate($user);
+
+    $this->assertEquals($message->user->name, 'Oussama');
+});

From 76bfd6d3b1c47d1095ea96970a0a34a3220ab203 Mon Sep 17 00:00:00 2001
From: ousid <ousid@users.noreply.github.com>
Date: Sun, 25 Dec 2022 10:35:44 +0000
Subject: [PATCH 3/4] Fix styling

---
 src/Models/Message.php        | 2 +-
 tests/Feature/MessageTest.php | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/Models/Message.php b/src/Models/Message.php
index 3056c68..fc78b62 100644
--- a/src/Models/Message.php
+++ b/src/Models/Message.php
@@ -40,7 +40,7 @@ public function ticket(): BelongsTo
 
     /**
      * Get Message Relationship
-     * 
+     *
      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
      */
     public function user(): BelongsTo
diff --git a/tests/Feature/MessageTest.php b/tests/Feature/MessageTest.php
index 8969dfd..c94b7c6 100644
--- a/tests/Feature/MessageTest.php
+++ b/tests/Feature/MessageTest.php
@@ -19,7 +19,7 @@
     $user = User::factory()->create([
         'name' => 'Oussama',
     ]);
-    
+
     $message = Message::factory()->create();
 
     $message->user()->associate($user);

From ca1ad418b3c90c9b4c03ecd1099dfc268df83e6e Mon Sep 17 00:00:00 2001
From: ousid <imo.ossama1@gmail.com>
Date: Sun, 25 Dec 2022 11:42:07 +0100
Subject: [PATCH 4/4] fix styling

---
 src/Models/Message.php        | 2 +-
 tests/Feature/MessageTest.php | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/Models/Message.php b/src/Models/Message.php
index 3056c68..fc78b62 100644
--- a/src/Models/Message.php
+++ b/src/Models/Message.php
@@ -40,7 +40,7 @@ public function ticket(): BelongsTo
 
     /**
      * Get Message Relationship
-     * 
+     *
      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
      */
     public function user(): BelongsTo
diff --git a/tests/Feature/MessageTest.php b/tests/Feature/MessageTest.php
index 8969dfd..c94b7c6 100644
--- a/tests/Feature/MessageTest.php
+++ b/tests/Feature/MessageTest.php
@@ -19,7 +19,7 @@
     $user = User::factory()->create([
         'name' => 'Oussama',
     ]);
-    
+
     $message = Message::factory()->create();
 
     $message->user()->associate($user);
