Skip to content

Commit

Permalink
Merge pull request #23 from neazk/main
Browse files Browse the repository at this point in the history
Adding New Scopes & Eloquent Methods.
  • Loading branch information
ousid committed Mar 22, 2023
2 parents c569986 + 1123484 commit 68c1a60
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
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');
});

0 comments on commit 68c1a60

Please sign in to comment.