Skip to content

Commit 0c803a2

Browse files
committed
Add ability to assign ticket to a user
1 parent c3e2d4e commit 0c803a2

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public function createCategory()
152152
| status |`string` | `open` |
153153
| is_resolved |`boolean` | `false` |
154154
| is_locked |`boolean` | `false` |
155+
| assigned_to_user_id | `integer` | `NULL` |
155156
| created_at |`timestamp` | `NULL` |
156157
| updated_at |`timestamp` | `NULL` |
157158

database/migrations/create_tickets_table.php.stub

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ return new class extends Migration
2222
$table->string('status')->default('open');
2323
$table->boolean('is_resolved')->default(false);
2424
$table->boolean('is_locked')->default(false);
25+
$table->foreignId('assigned_to_user_id')->nullable();
2526
$table->timestamps();
2627
});
2728
}

src/Concerns/InteractsWithTickets.php

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Coderflex\LaravelTicket\Concerns;
44

55
use Coderflex\LaravelTicket\Enums\Status;
6+
use Illuminate\Database\Eloquent\Model;
67

78
trait InteractsWithTickets
89
{
@@ -216,4 +217,19 @@ public function reopenAsUnresolved(): self
216217

217218
return $this;
218219
}
220+
221+
/**
222+
* Add new message on an existing ticket as a custom user
223+
*
224+
* @param \Illuminate\Database\Eloquent\Model|int $user
225+
* @return self
226+
*/
227+
public function assignTo(Model|int $user): self
228+
{
229+
$this->update([
230+
'assigned_to_user_id' => $user,
231+
]);
232+
233+
return $this;
234+
}
219235
}

src/Models/Ticket.php

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @property string $status
2323
* @property bool $is_resolved
2424
* @property bool $is_locked
25+
* @property int $assigned_to_user_id
2526
*/
2627
class Ticket extends Model
2728
{
@@ -47,6 +48,16 @@ public function user(): BelongsTo
4748
return $this->belongsTo(User::class);
4849
}
4950

51+
/**
52+
* Get Assigned To User RelationShip
53+
*
54+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
55+
*/
56+
public function assigned_to_user(): BelongsTo
57+
{
58+
return $this->belongsTo(User::class, 'assigned_to_user_id');
59+
}
60+
5061
/**
5162
* Get Messages RelationShip
5263
*

tests/Feature/TicketTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Coderflex\LaravelTicket\Models\Ticket;
4+
use Coderflex\LaravelTicket\Tests\Models\User;
45

56
it('filters tickets by status', function () {
67
Ticket::factory()
@@ -250,3 +251,13 @@
250251

251252
$this->assertEquals(Ticket::count(), 0);
252253
});
254+
255+
it('can assign ticket to a user', function () {
256+
$ticket = Ticket::factory()->create();
257+
$agentUser = User::factory()->create();
258+
259+
$ticket->assignTo($agentUser);
260+
261+
expect($ticket->assigned_to_user_id)
262+
->toBe($agentUser);
263+
});

0 commit comments

Comments
 (0)