Updated filters

This commit is contained in:
David Bomba 2023-09-06 08:47:48 +10:00
parent 7e8b09b146
commit 25739a912e
4 changed files with 193 additions and 3 deletions

View File

@ -229,6 +229,32 @@ class InvoiceFilters extends QueryFilters
return $this->builder->where('due_date', '>=', $date); return $this->builder->where('due_date', '>=', $date);
} }
public function date_range(string $date_range = ''): Builder
{
$parts = explode(",", $date_range);
if (count($parts) != 3) {
return $this->builder;
}
if(!in_array($parts[0], ['date','due_date'])) {
return $this->builder;
}
try{
$start_date = Carbon::parse($parts[1]);
$end_date = Carbon::parse($parts[2]);
return $this->builder->whereBetween($parts[0], [$start_date, $end_date]);
}
catch(\Exception $e){
return $this->builder;
}
return $this->builder;
}
/** /**
* Sorts the list based on $sort. * Sorts the list based on $sort.

View File

@ -12,8 +12,9 @@
namespace App\Filters; namespace App\Filters;
use App\Models\Payment; use App\Models\Payment;
use Illuminate\Contracts\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\Database\Eloquent\Builder as EloquentBuilder;
/** /**
* PaymentFilters. * PaymentFilters.
@ -177,6 +178,33 @@ class PaymentFilters extends QueryFilters
return $this->builder->orderBy($sort_col[0], $sort_col[1]); return $this->builder->orderBy($sort_col[0], $sort_col[1]);
} }
public function date_range(string $date_range = ''): Builder
{
$parts = explode(",", $date_range);
if (count($parts) != 3) {
return $this->builder;
}
if(!in_array($parts[0], ['date'])) {
return $this->builder;
}
try{
$start_date = Carbon::parse($parts[1]);
$end_date = Carbon::parse($parts[2]);
return $this->builder->whereBetween($parts[0], [$start_date, $end_date]);
}
catch(\Exception $e){
return $this->builder;
}
return $this->builder;
}
/** /**
* Filters the query by the users company ID. * Filters the query by the users company ID.
* *

View File

@ -48,6 +48,70 @@ class InvoiceTest extends TestCase
$this->makeTestData(); $this->makeTestData();
} }
public function testInvoiceGetDatesBetween()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/invoices?date_range=date,2023-01-01,2023-01-01', )
->assertStatus(200);
}
public function testInvoiceGetDatesBetween2()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/invoices?date_range=date', )
->assertStatus(200);
}
public function testInvoiceGetDatesBetween3()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/invoices?date_range=x', )
->assertStatus(200);
}
public function testInvoiceGetDatesBetween4()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/invoices?date_range=date,2023223123,312312321', )
->assertStatus(200);
}
public function testInvoiceGetDatesBetween5()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/invoices?date_range=date,x,23423', )
->assertStatus(200);
}
public function testInvoiceGetDatesBetween6()
{
Invoice::factory()->count(10)->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'date' => '1971-01-02',
]);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/invoices?date_range=date,1971-01-01,1971-01-03', )
->assertStatus(200);
$arr = $response->json();
$this->assertCount(10, $arr['data']);
}
public function testInvoiceGetPaidReversedInvoice() public function testInvoiceGetPaidReversedInvoice()
{ {
@ -66,7 +130,6 @@ class InvoiceTest extends TestCase
$this->assertCount(1, $arr['data']); $this->assertCount(1, $arr['data']);
} }
public function testInvoiceGetPaidInvoices() public function testInvoiceGetPaidInvoices()
{ {
$response = $this->withHeaders([ $response = $this->withHeaders([

View File

@ -60,7 +60,80 @@ class PaymentTest extends TestCase
); );
} }
public function testPatymentGetClientStatus() public function testPaymentGetBetweenQuery1()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/payments?date_range=date,2023-01-01,2023-02-01');
$response->assertStatus(200);
}
public function testPaymentGetBetweenQuery2()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/payments?date_range=');
$response->assertStatus(200);
}
public function testPaymentGetBetweenQuery3()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/payments?date_range=1,1,1,1,1');
$response->assertStatus(200);
}
public function testPaymentGetBetweenQuery4()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/payments?date_range=date,34343,34343434343');
$response->assertStatus(200);
}
public function testPaymentGetBetweenQuery5()
{
Payment::factory()->count(10)->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_id' => $this->client->id,
'date' => '2023-01-02',
]);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/payments?date_range=date,2023-01-01,2023-01-03');
$response->assertStatus(200);
$arr = $response->json();
$this->assertCount(10, $arr['data']);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/payments?date_range=date,2053-10-01,2053-10-03');
$response->assertStatus(200);
$arr = $response->json();
$this->assertCount(0, $arr['data']);
}
public function testPaymentGetClientStatus()
{ {
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),