Skip to content
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

Adding New Scopes & Eloquent Methods. #23

Merged
merged 5 commits into from
Mar 22, 2023
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ The `ticket` model came with handy methods to use, to make your building process
| `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)` | ✓
| `makePriorityAsLow` |`void` | make ticket priority as low | `$ticket->makePriorityAsLow()` | ✓
| `makePriorityAsNormal`|`void`| make ticket priority as normal | `$ticket->makePriorityAsNormal()` | ✓
| `makePriorityAsHigh` |`void` | make ticket priority as high | `$ticket->makePriorityAsHigh()` | ✓

The __Chainable__ column, is showing the state for the method, that if it can be chained or not, something like
```php
Expand Down Expand Up @@ -244,6 +247,8 @@ The `ticket` model has also a list of scopes to begin filter with.
|---|---|---|---|
| `closed` |`void` | get the closed tickets | `Ticket::closed()->get()` |
| `opened` |`void` | get the opened tickets | `Ticket::opened()->get()` |
| `archived` |`void` | get the archived tickets | `Ticket::archived()->get()` |
| `unArchived` |`void` | get the unArchived tickets | `Ticket::unArchived()->get()` |
| `resolved` |`void` | get the resolved tickets | `Ticket::resolved()->get()` |
| `locked` |`void` | get the locked tickets | `Ticket::locked()->get()` |
| `unlocked` |`void` | get the unlocked tickets | `Ticket::unlocked()->get()` |
Expand Down
28 changes: 28 additions & 0 deletions src/Concerns/InteractsWithTickets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

trait InteractsWithTickets
{
Expand Down Expand Up @@ -195,4 +196,31 @@ public function assignTo(Model|int $user): self

return $this;
}

/**
* make ticket priority as low
*/
public function makePriorityAsLow(): self
{
$this->update(['priority' => Priority::LOW->value]);
return $this;
}

/**
* make ticket priority as normal
*/
public function makePriorityAsNormal(): self
{
$this->update(['priority' => Priority::NORMAL->value]);
return $this;
}

/**
* make ticket priority as high
*/
public function makePriorityAsHigh(): self
{
$this->update(['priority' => Priority::HIGH->value]);
return $this;
}
}
16 changes: 16 additions & 0 deletions src/Scopes/TicketScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,20 @@ public function scopeWithHighPriority(Builder $builder): Builder
{
return $builder->where('priority', Priority::HIGH->value);
}

/**
* Get archived tickets
*/
public function scopeArchived(Builder $builder): Builder
{
return $builder->where('status', Status::ARCHIVED->value);
}

/**
* Get unarchived tickets
*/
public function scopeUnArchived(Builder $builder): Builder
{
return $builder->whereNot('status', Status::ARCHIVED->value);
}
}
40 changes: 39 additions & 1 deletion tests/Feature/TicketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@
'status' => 'closed',
]);

$this->assertEquals(Ticket::count(), 10);
Ticket::factory()
->times(6)
->create([
'status' => 'archived',
]);

$this->assertEquals(Ticket::count(), 16);
$this->assertEquals(Ticket::opened()->count(), 3);
$this->assertEquals(Ticket::closed()->count(), 7);
$this->assertEquals(Ticket::archived()->count(), 6);
$this->assertEquals(Ticket::unArchived()->count(), 10);
});

it('filters tickets by resolved status', function () {
Expand Down Expand Up @@ -271,3 +279,33 @@
expect($ticket->assigned_to)
->toBe($agentUser->id);
});

it('can mark a ticket priority as low', function () {
$ticket = Ticket::factory()->create([
'priority' => 'high',
]);

$ticket->makePriorityAsLow();

$this->assertEquals($ticket->priority, 'low');
});

it('can mark a ticket priority as normal', function () {
$ticket = Ticket::factory()->create([
'priority' => 'high',
]);

$ticket->makePriorityAsNormal();

$this->assertEquals($ticket->priority, 'normal');
});

it('can mark a ticket priority as high', function () {
$ticket = Ticket::factory()->create([
'priority' => 'low',
]);

$ticket->makePriorityAsHigh();

$this->assertEquals($ticket->priority, 'high');
});