Skip to content

Commit 7907416

Browse files
authored
Merge pull request #8 from krekas/feature/assign-ticket-to
Add ability to assign ticket to a user
2 parents c3e2d4e + 8914486 commit 7907416

8 files changed

+91
-19
lines changed

Diff for: README.md

+21-19
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

@@ -193,25 +194,26 @@ public function createCategory()
193194
### Ticket API Methods
194195
The `ticket` model came with handy methods to use, to make your building process easy and fast, and here is the list of the available __API__:
195196

196-
| Method | Arguments | Description | Example | Chainable |
197-
|---|---|---|---|---|
198-
| `archive` |`void` | archive the ticket | `$ticket->archive()` | ✓
199-
| `close` |`void` | close the ticket | `$ticket->close()` | ✓
200-
| `reopen` |`void` | reopen a closed ticket | `$ticket->reopen()` | ✓
201-
| `markAsResolved` |`void` | mark the ticket as resolved | `$ticket->markAsResolved()` | ✓
202-
| `markAsLocked` |`void` | mark the ticket as locked | `$ticket->markAsLocked()` | ✓
203-
| `markAsUnlocked` |`void` | mark the ticket as unlocked | `$ticket->markAsUnlocked()` | ✓
204-
| `markAsArchived` |`void` | mark the ticket as archived | `$ticket->markAsArchived()` | ✓
205-
| `closeAsResolved` |`void` | close the ticket and marked it as resolved | `$ticket->closeAsResolved()` | ✓
206-
| `closeAsUnresolved` |`void` | close the ticket and marked it as unresolved | `$ticket->closeAsUnresolved()` | ✓
207-
| `reopenAsUnresolved` |`void` | reopen the ticket and marked it as unresolved | `$ticket->reopenAsUnresolved()` | ✓
208-
| `isArchived` |`void` | check if the ticket archived | `$ticket->isArchived()` | ✗
209-
| `isOpen` |`void` | check if the ticket open | `$ticket->isOpen()` | ✗
210-
| `isClosed` |`void` | check if the ticket closed | `$ticket->isClosed()` | ✗
211-
| `isResolved` |`void` | check if the ticket has a resolved status | `$ticket->isResolved()` | ✗
212-
| `isUnresolved` |`void` | check if the ticket has an unresolved status | `$ticket->isUnresolved()` | ✗
213-
| `isLocked` |`void` | check if the ticket is locked | `$ticket->isLocked()` | ✗
214-
| `isUnlocked` |`void` | check if the ticket is unlocked | `$ticket->isUnlocked()` | ✗
197+
| Method | Arguments | Description | Example | Chainable |
198+
|----------------------|---|-----------------------------------------------|-----------------------------------------------------|---|
199+
| `archive` |`void` | archive the ticket | `$ticket->archive()` | ✓
200+
| `close` |`void` | close the ticket | `$ticket->close()` | ✓
201+
| `reopen` |`void` | reopen a closed ticket | `$ticket->reopen()` | ✓
202+
| `markAsResolved` |`void` | mark the ticket as resolved | `$ticket->markAsResolved()` | ✓
203+
| `markAsLocked` |`void` | mark the ticket as locked | `$ticket->markAsLocked()` | ✓
204+
| `markAsUnlocked` |`void` | mark the ticket as unlocked | `$ticket->markAsUnlocked()` | ✓
205+
| `markAsArchived` |`void` | mark the ticket as archived | `$ticket->markAsArchived()` | ✓
206+
| `closeAsResolved` |`void` | close the ticket and marked it as resolved | `$ticket->closeAsResolved()` | ✓
207+
| `closeAsUnresolved` |`void` | close the ticket and marked it as unresolved | `$ticket->closeAsUnresolved()` | ✓
208+
| `reopenAsUnresolved` |`void` | reopen the ticket and marked it as unresolved | `$ticket->reopenAsUnresolved()` | ✓
209+
| `isArchived` |`void` | check if the ticket archived | `$ticket->isArchived()` | ✗
210+
| `isOpen` |`void` | check if the ticket open | `$ticket->isOpen()` | ✗
211+
| `isClosed` |`void` | check if the ticket closed | `$ticket->isClosed()` | ✗
212+
| `isResolved` |`void` | check if the ticket has a resolved status | `$ticket->isResolved()` | ✗
213+
| `isUnresolved` |`void` | check if the ticket has an unresolved status | `$ticket->isUnresolved()` | ✗
214+
| `isLocked` |`void` | check if the ticket is locked | `$ticket->isLocked()` | ✗
215+
| `isUnlocked` |`void` | check if the ticket is unlocked | `$ticket->isUnlocked()` | ✗
216+
| `assignTo` |`void` | assign ticket to a user | `$ticket->assignTo($user)` or `$ticket->assignTo(2)` | ✓
215217

216218
The __Chainable__ column, is showing the state for the method, that if it can be chained or not, something like
217219
```php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelTicket\Database\Factories;
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
return new class extends Migration
10+
{
11+
public function up()
12+
{
13+
$tableName = config('laravel_ticket.table_names.tickets', 'tickets');
14+
15+
Schema::table($tableName, function (Blueprint $table) {
16+
$table->unsignedBigInteger('assigned_to')->nullable()->references('id')->on('users')->after('is_locked');
17+
});
18+
}
19+
};

Diff for: 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
}

Diff for: 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,
231+
]);
232+
233+
return $this;
234+
}
219235
}

Diff for: src/LaravelTicketServiceProvider.php

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function configurePackage(Package $package): void
2424
'create_labels_table',
2525
'create_category_ticket_table',
2626
'create_label_ticket_table',
27+
'add_assigned_to_column_into_tickets_table',
2728
);
2829
}
2930
}

Diff for: 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
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 assignedToUser(): BelongsTo
57+
{
58+
return $this->belongsTo(User::class, 'assigned_to');
59+
}
60+
5061
/**
5162
* Get Messages RelationShip
5263
*

Diff for: tests/Feature/TicketTest.php

+21
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,23 @@
250251

251252
$this->assertEquals(Ticket::count(), 0);
252253
});
254+
255+
it('can assign ticket to a user using user model', function () {
256+
$ticket = Ticket::factory()->create();
257+
$agentUser = User::factory()->create();
258+
259+
$ticket->assignTo($agentUser);
260+
261+
expect($ticket->assigned_to)
262+
->toBe($agentUser);
263+
});
264+
265+
it('can assign ticket to a user using user id', function () {
266+
$ticket = Ticket::factory()->create();
267+
$agentUser = User::factory()->create();
268+
269+
$ticket->assignTo($agentUser->id);
270+
271+
expect($ticket->assigned_to)
272+
->toBe($agentUser->id);
273+
});

Diff for: tests/TestCase.php

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function getEnvironmentSetUp($app)
3535
include __DIR__.'/../database/migrations/create_categories_table.php.stub',
3636
include __DIR__.'/../database/migrations/create_messages_table.php.stub',
3737
include __DIR__.'/../database/migrations/create_labels_table.php.stub',
38+
include __DIR__.'/../database/migrations/add_assigned_to_column_into_tickets_table.php.stub',
3839

3940
// Many to Many tables
4041
include __DIR__.'/../database/migrations/create_label_ticket_table.php.stub',

0 commit comments

Comments
 (0)