Skip to content

Add ability to assign ticket to a user #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Coderflex\LaravelTicket\Database\Factories;

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
$tableName = config('laravel_ticket.table_names.tickets', 'tickets');

Schema::table($tableName, function (Blueprint $table) {
$table->unsignedBigInteger('assigned_to')->nullable()->references('id')->on('users')->after('is_locked');
});
}
};
1 change: 1 addition & 0 deletions database/migrations/create_tickets_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down
16 changes: 16 additions & 0 deletions src/Concerns/InteractsWithTickets.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Coderflex\LaravelTicket\Concerns;

use Coderflex\LaravelTicket\Enums\Status;
use Illuminate\Database\Eloquent\Model;

trait InteractsWithTickets
{
Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions src/LaravelTicketServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
}
}
11 changes: 11 additions & 0 deletions src/Models/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @property string $status
* @property bool $is_resolved
* @property bool $is_locked
* @property int $assigned_to
*/
class Ticket extends Model
{
Expand All @@ -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
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Feature/TicketTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Coderflex\LaravelTicket\Models\Ticket;
use Coderflex\LaravelTicket\Tests\Models\User;

it('filters tickets by status', function () {
Ticket::factory()
Expand Down Expand Up @@ -250,3 +251,23 @@

$this->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);
});
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down