diff --git a/README.md b/README.md index 62c8cf5..746c3fa 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,7 @@ public function createCategory() | status |`string` | `open` | | is_resolved |`boolean` | `false` | | is_locked |`boolean` | `false` | +| assigned_to_user_id | `integer` | `NULL` | | created_at |`timestamp` | `NULL` | | updated_at |`timestamp` | `NULL` | @@ -193,25 +194,26 @@ public function createCategory() ### Ticket API Methods 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__: -| Method | Arguments | Description | Example | Chainable | -|---|---|---|---|---| -| `archive` |`void` | archive the ticket | `$ticket->archive()` | ✓ -| `close` |`void` | close the ticket | `$ticket->close()` | ✓ -| `reopen` |`void` | reopen a closed ticket | `$ticket->reopen()` | ✓ -| `markAsResolved` |`void` | mark the ticket as resolved | `$ticket->markAsResolved()` | ✓ -| `markAsLocked` |`void` | mark the ticket as locked | `$ticket->markAsLocked()` | ✓ -| `markAsUnlocked` |`void` | mark the ticket as unlocked | `$ticket->markAsUnlocked()` | ✓ -| `markAsArchived` |`void` | mark the ticket as archived | `$ticket->markAsArchived()` | ✓ -| `closeAsResolved` |`void` | close the ticket and marked it as resolved | `$ticket->closeAsResolved()` | ✓ -| `closeAsUnresolved` |`void` | close the ticket and marked it as unresolved | `$ticket->closeAsUnresolved()` | ✓ -| `reopenAsUnresolved` |`void` | reopen the ticket and marked it as unresolved | `$ticket->reopenAsUnresolved()` | ✓ -| `isArchived` |`void` | check if the ticket archived | `$ticket->isArchived()` | ✗ -| `isOpen` |`void` | check if the ticket open | `$ticket->isOpen()` | ✗ -| `isClosed` |`void` | check if the ticket closed | `$ticket->isClosed()` | ✗ -| `isResolved` |`void` | check if the ticket has a resolved status | `$ticket->isResolved()` | ✗ -| `isUnresolved` |`void` | check if the ticket has an unresolved status | `$ticket->isUnresolved()` | ✗ -| `isLocked` |`void` | check if the ticket is locked | `$ticket->isLocked()` | ✗ -| `isUnlocked` |`void` | check if the ticket is unlocked | `$ticket->isUnlocked()` | ✗ +| Method | Arguments | Description | Example | Chainable | +|----------------------|---|-----------------------------------------------|-----------------------------------------------------|---| +| `archive` |`void` | archive the ticket | `$ticket->archive()` | ✓ +| `close` |`void` | close the ticket | `$ticket->close()` | ✓ +| `reopen` |`void` | reopen a closed ticket | `$ticket->reopen()` | ✓ +| `markAsResolved` |`void` | mark the ticket as resolved | `$ticket->markAsResolved()` | ✓ +| `markAsLocked` |`void` | mark the ticket as locked | `$ticket->markAsLocked()` | ✓ +| `markAsUnlocked` |`void` | mark the ticket as unlocked | `$ticket->markAsUnlocked()` | ✓ +| `markAsArchived` |`void` | mark the ticket as archived | `$ticket->markAsArchived()` | ✓ +| `closeAsResolved` |`void` | close the ticket and marked it as resolved | `$ticket->closeAsResolved()` | ✓ +| `closeAsUnresolved` |`void` | close the ticket and marked it as unresolved | `$ticket->closeAsUnresolved()` | ✓ +| `reopenAsUnresolved` |`void` | reopen the ticket and marked it as unresolved | `$ticket->reopenAsUnresolved()` | ✓ +| `isArchived` |`void` | check if the ticket archived | `$ticket->isArchived()` | ✗ +| `isOpen` |`void` | check if the ticket open | `$ticket->isOpen()` | ✗ +| `isClosed` |`void` | check if the ticket closed | `$ticket->isClosed()` | ✗ +| `isResolved` |`void` | check if the ticket has a resolved status | `$ticket->isResolved()` | ✗ +| `isUnresolved` |`void` | check if the ticket has an unresolved status | `$ticket->isUnresolved()` | ✗ +| `isLocked` |`void` | check if the ticket is locked | `$ticket->isLocked()` | ✗ +| `isUnlocked` |`void` | check if the ticket is unlocked | `$ticket->isUnlocked()` | ✗ +| `assignTo` |`void` | assign ticket to a user | `$ticket->assignTo($user)` or `$ticket->assignTo(2)` | ✓ The __Chainable__ column, is showing the state for the method, that if it can be chained or not, something like ```php diff --git a/database/migrations/add_assigned_to_column_into_tickets_table.php.stub b/database/migrations/add_assigned_to_column_into_tickets_table.php.stub new file mode 100644 index 0000000..8f5d5ed --- /dev/null +++ b/database/migrations/add_assigned_to_column_into_tickets_table.php.stub @@ -0,0 +1,19 @@ +unsignedBigInteger('assigned_to')->nullable()->references('id')->on('users')->after('is_locked'); + }); + } +}; diff --git a/database/migrations/create_tickets_table.php.stub b/database/migrations/create_tickets_table.php.stub index 6a8cad0..5064a5d 100644 --- a/database/migrations/create_tickets_table.php.stub +++ b/database/migrations/create_tickets_table.php.stub @@ -22,6 +22,7 @@ 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/src/Concerns/InteractsWithTickets.php b/src/Concerns/InteractsWithTickets.php index 50c7870..039a98f 100644 --- a/src/Concerns/InteractsWithTickets.php +++ b/src/Concerns/InteractsWithTickets.php @@ -3,6 +3,7 @@ namespace Coderflex\LaravelTicket\Concerns; use Coderflex\LaravelTicket\Enums\Status; +use Illuminate\Database\Eloquent\Model; trait InteractsWithTickets { @@ -216,4 +217,19 @@ public function reopenAsUnresolved(): self return $this; } + + /** + * Add new message on an existing ticket as a custom user + * + * @param \Illuminate\Database\Eloquent\Model|int $user + * @return self + */ + public function assignTo(Model|int $user): self + { + $this->update([ + 'assigned_to' => $user, + ]); + + return $this; + } } diff --git a/src/LaravelTicketServiceProvider.php b/src/LaravelTicketServiceProvider.php index 1dc2aa3..f38f764 100644 --- a/src/LaravelTicketServiceProvider.php +++ b/src/LaravelTicketServiceProvider.php @@ -24,6 +24,7 @@ public function configurePackage(Package $package): void 'create_labels_table', 'create_category_ticket_table', 'create_label_ticket_table', + 'add_assigned_to_column_into_tickets_table', ); } } diff --git a/src/Models/Ticket.php b/src/Models/Ticket.php index a8dd6a0..333373c 100644 --- a/src/Models/Ticket.php +++ b/src/Models/Ticket.php @@ -22,6 +22,7 @@ * @property string $status * @property bool $is_resolved * @property bool $is_locked + * @property int $assigned_to */ class Ticket extends Model { @@ -47,6 +48,16 @@ public function user(): BelongsTo return $this->belongsTo(User::class); } + /** + * Get Assigned To User RelationShip + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function assignedToUser(): BelongsTo + { + return $this->belongsTo(User::class, 'assigned_to'); + } + /** * Get Messages RelationShip * diff --git a/tests/Feature/TicketTest.php b/tests/Feature/TicketTest.php index 82321cc..b11b627 100644 --- a/tests/Feature/TicketTest.php +++ b/tests/Feature/TicketTest.php @@ -1,6 +1,7 @@ assertEquals(Ticket::count(), 0); }); + +it('can assign ticket to a user using user model', function () { + $ticket = Ticket::factory()->create(); + $agentUser = User::factory()->create(); + + $ticket->assignTo($agentUser); + + expect($ticket->assigned_to) + ->toBe($agentUser); +}); + +it('can assign ticket to a user using user id', function () { + $ticket = Ticket::factory()->create(); + $agentUser = User::factory()->create(); + + $ticket->assignTo($agentUser->id); + + expect($ticket->assigned_to) + ->toBe($agentUser->id); +}); diff --git a/tests/TestCase.php b/tests/TestCase.php index 32b309f..631faf7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -35,6 +35,7 @@ public function getEnvironmentSetUp($app) include __DIR__.'/../database/migrations/create_categories_table.php.stub', include __DIR__.'/../database/migrations/create_messages_table.php.stub', include __DIR__.'/../database/migrations/create_labels_table.php.stub', + include __DIR__.'/../database/migrations/add_assigned_to_column_into_tickets_table.php.stub', // Many to Many tables include __DIR__.'/../database/migrations/create_label_ticket_table.php.stub',