1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Clean up all filters and add back test:

This commit is contained in:
David Bomba 2023-01-19 11:24:40 +11:00
parent 295a435762
commit fe24cc5eb6
19 changed files with 151 additions and 177 deletions

View File

@ -11,11 +11,7 @@
namespace App\Filters;
use App\Models\BankIntegration;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
/**
* BankIntegrationFilters.

View File

@ -12,10 +12,7 @@
namespace App\Filters;
use App\Models\BankTransaction;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
/**
* BankTransactionFilters.
@ -77,87 +74,49 @@ class BankTransactionFilters extends QueryFilters
$status_parameters = explode(',', $value);
$status_array = [];
$debit_or_withdrawal_array = [];
if (in_array('all', $status_parameters)) {
return $this->builder;
}
if (in_array('unmatched', $status_parameters)) {
$status_array[] = BankTransaction::STATUS_UNMATCHED;
// $this->builder->orWhere('status_id', BankTransaction::STATUS_UNMATCHED);
}
$this->builder->where(function ($query) use ($status_parameters){
if (in_array('matched', $status_parameters)) {
$status_array[] = BankTransaction::STATUS_MATCHED;
// $this->builder->where('status_id', BankTransaction::STATUS_MATCHED);
}
$status_array = [];
$debit_or_withdrawal_array = [];
if (in_array('converted', $status_parameters)) {
$status_array[] = BankTransaction::STATUS_CONVERTED;
// $this->builder->where('status_id', BankTransaction::STATUS_CONVERTED);
}
if (in_array('unmatched', $status_parameters)) {
$status_array[] = BankTransaction::STATUS_UNMATCHED;
}
if (in_array('deposits', $status_parameters)) {
$debit_or_withdrawal_array[] = 'CREDIT';
// $this->builder->where('base_type', 'CREDIT');
}
if (in_array('matched', $status_parameters)) {
$status_array[] = BankTransaction::STATUS_MATCHED;
}
if (in_array('withdrawals', $status_parameters)) {
$debit_or_withdrawal_array[] = 'DEBIT';
// $this->builder->where('base_type', 'DEBIT');
}
if (in_array('converted', $status_parameters)) {
$status_array[] = BankTransaction::STATUS_CONVERTED;
}
if(count($status_array) >=1) {
$this->builder->whereIn('status_id', $status_array);
}
if (in_array('deposits', $status_parameters)) {
$debit_or_withdrawal_array[] = 'CREDIT';
}
if(count($debit_or_withdrawal_array) >=1) {
$this->builder->orWhereIn('base_type', $debit_or_withdrawal_array);
}
if (in_array('withdrawals', $status_parameters)) {
$debit_or_withdrawal_array[] = 'DEBIT';
}
if(count($status_array) >=1) {
$query->whereIn('status_id', $status_array);
}
if(count($debit_or_withdrawal_array) >=1) {
$query->orWhereIn('base_type', $debit_or_withdrawal_array);
}
});
return $this->builder;
}
/**
* Filters the list based on the status
* archived, active, deleted.
*
* @param string filter
* @return Builder
*/
public function status(string $filter = '') : Builder
{
if (strlen($filter) == 0) {
return $this->builder;
}
$filters = explode(',', $filter);
return $this->builder->where(function ($query) use ($filters) {
if (in_array(parent::STATUS_ACTIVE, $filters)) {
$query->orWhereNull('deleted_at');
}
if (in_array(parent::STATUS_ARCHIVED, $filters)) {
$query->orWhere(function ($query) use ($table) {
$query->whereNotNull($table.'.deleted_at');
if (! in_array($table, ['users'])) {
$query->where($table.'.is_deleted', '=', 0);
}
});
}
if (in_array(parent::STATUS_DELETED, $filters)) {
$query->orWhere($table.'.is_deleted', '=', 1);
}
});
}
/**
* Sorts the list based on $sort.
*
@ -186,19 +145,6 @@ class BankTransactionFilters extends QueryFilters
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
}
/**
* Returns the base query.
*
* @param int company_id
* @param User $user
* @return Builder
* @deprecated
*/
public function baseQuery(int $company_id, User $user) : Builder
{
}
/**
* Filters the query by the users company ID.
*
@ -206,7 +152,6 @@ class BankTransactionFilters extends QueryFilters
*/
public function entityFilter()
{
//return $this->builder->whereCompanyId(auth()->user()->company()->id);
return $this->builder->company();
}
}

View File

@ -13,9 +13,7 @@
namespace App\Filters;
use App\Models\Credit;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
class CreditFilters extends QueryFilters
{
@ -44,20 +42,20 @@ class CreditFilters extends QueryFilters
return $this->builder;
}
if (in_array('draft', $status_parameters)) {
$this->builder->where('status_id', Credit::STATUS_DRAFT);
}
if (in_array('partial', $status_parameters)) {
$this->builder->where('status_id', Credit::STATUS_PARTIAL);
}
$credit_filters = [];
if (in_array('applied', $status_parameters)) {
$this->builder->where('status_id', Credit::STATUS_APPLIED);
}
if (in_array('draft', $status_parameters))
$credit_filters[] = Credit::STATUS_DRAFT;
if (in_array('partial', $status_parameters))
$credit_filters[] = Credit::STATUS_PARTIAL;
//->where('due_date', '>', Carbon::now())
//->orWhere('partial_due_date', '>', Carbon::now());
if (in_array('applied', $status_parameters))
$credit_filters[] = Credit::STATUS_APPLIED;
if(count($credit_filters) >=1)
$this->builder->whereIn('status_id', $credit_filters);
return $this->builder;
}

View File

@ -11,11 +11,7 @@
namespace App\Filters;
use App\Models\Design;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
/**
* DesignFilters.
@ -27,9 +23,10 @@ class DesignFilters extends QueryFilters
*
* @param string query filter
* @return Builder
*
* @deprecated
*/
public function filter(string $filter = '') : Builder
public function filter(string $filter = ''): Builder
{
if (strlen($filter) == 0) {
return $this->builder;
@ -44,48 +41,17 @@ class DesignFilters extends QueryFilters
* Sorts the list based on $sort.
*
* @param string sort formatted as column|asc
*
* @return Builder
*/
public function sort(string $sort) : Builder
public function sort(string $sort): Builder
{
$sort_col = explode('|', $sort);
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
}
if(is_array($sort_col))
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
/**
* Returns the base query.
*
* @param int company_id
* @param User $user
* @return Builder
* @deprecated
*/
public function baseQuery(int $company_id, User $user) : Builder
{
$query = DB::table('designs')
->join('companies', 'companies.id', '=', 'designs.company_id')
->where('designs.company_id', '=', $company_id)
->select(
'designs.id',
'designs.name',
'designs.design',
'designs.created_at',
'designs.created_at as design_created_at',
'designs.deleted_at',
'designs.is_deleted',
'designs.user_id',
);
/*
* If the user does not have permissions to view all invoices
* limit the user to only the invoices they have created
*/
if (Gate::denies('view-list', Design::class)) {
$query->where('designs.user_id', '=', $user->id);
}
return $query;
return $this->builder;
}
/**
@ -93,7 +59,7 @@ class DesignFilters extends QueryFilters
*
* @return Illuminate\Database\Query\Builder
*/
public function entityFilter()
public function entityFilter(): Builder
{
//return $this->builder->whereCompanyId(auth()->user()->company()->id);
return $this->builder->where('company_id', auth()->user()->company()->id)->orWhere('company_id', null)->orderBy('id','asc');

View File

@ -12,7 +12,6 @@
namespace App\Filters;
use App\Models\Company;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
/**
@ -27,7 +26,7 @@ class DocumentFilters extends QueryFilters
* @return Builder
* @deprecated
*/
public function filter(string $filter = '') : Builder
public function filter(string $filter = ''): Builder
{
if (strlen($filter) == 0) {
return $this->builder;
@ -36,8 +35,15 @@ class DocumentFilters extends QueryFilters
return $this->builder;
}
/* If client ID passed to this entity, simply return */
public function client_id(string $client_id = '') :Builder
/**
* Overriding method as client_id does
* not exist on this model, just pass
* back the builder
* @param string $client_id The client hashed id.
*
* @return Builder
*/
public function client_id(string $client_id = ''): Builder
{
return $this->builder;
}
@ -48,11 +54,14 @@ class DocumentFilters extends QueryFilters
* @param string sort formatted as column|asc
* @return Builder
*/
public function sort(string $sort) : Builder
public function sort(string $sort = '') : Builder
{
$sort_col = explode('|', $sort);
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
if(is_array($sort_col))
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
return $this->builder;
}

View File

@ -11,11 +11,7 @@
namespace App\Filters;
use App\Models\Expense;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
/**
* ExpenseCategoryFilters.
@ -49,9 +45,9 @@ class ExpenseCategoryFilters extends QueryFilters
{
$sort_col = explode('|', $sort);
if (is_array($sort_col) && in_array($sort_col[1], ['asc', 'desc']) && in_array($sort_col[0], ['name'])) {
if (is_array($sort_col) && in_array($sort_col[1], ['asc', 'desc']) && in_array($sort_col[0], ['name']))
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
}
return $this->builder;
}
@ -63,8 +59,6 @@ class ExpenseCategoryFilters extends QueryFilters
*/
public function entityFilter()
{
//return $this->builder->whereCompanyId(auth()->user()->company()->id);
return $this->builder->company();
}
}

View File

@ -11,11 +11,7 @@
namespace App\Filters;
use App\Models\Expense;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
/**
* ExpenseFilters.

View File

@ -12,7 +12,6 @@
namespace App\Filters;
use App\Models\Invoice;
use App\Models\User;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
@ -80,6 +79,9 @@ class InvoiceFilters extends QueryFilters
public function number(string $number = '') :Builder
{
if(strlen($number) == 0)
return $this->builder;
return $this->builder->where('number', $number);
}

View File

@ -11,7 +11,6 @@
namespace App\Filters;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
/**
@ -49,6 +48,7 @@ class PaymentFilters extends QueryFilters
{
if($value == 'true'){
return $this->builder
->where('is_deleted',0)
->where(function ($query){
@ -72,7 +72,10 @@ class PaymentFilters extends QueryFilters
{
$sort_col = explode('|', $sort);
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
if(is_array($sort_col))
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
return true;
}
public function number(string $number) : Builder

View File

@ -11,11 +11,7 @@
namespace App\Filters;
use App\Models\Design;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
/**
* PaymentTermFilters.
@ -29,7 +25,7 @@ class PaymentTermFilters extends QueryFilters
* @return Builder
* @deprecated
*/
public function filter(string $filter = '') : Builder
public function filter(string $filter = ''): Builder
{
if (strlen($filter) == 0) {
return $this->builder;
@ -46,7 +42,7 @@ class PaymentTermFilters extends QueryFilters
* @param string sort formatted as column|asc
* @return Builder
*/
public function sort(string $sort) : Builder
public function sort(string $sort): Builder
{
$sort_col = explode('|', $sort);
@ -56,12 +52,10 @@ class PaymentTermFilters extends QueryFilters
/**
* Filters the query by the users company ID.
*
* @return Illuminate\Database\Query\Builder
* @return Builder
*/
public function entityFilter()
public function entityFilter(): Builder
{
return $this->builder->company();
//return $this->builder->whereCompanyId(auth()->user()->company()->id);
// return $this->builder->whereCompanyId(auth()->user()->company()->id)->orWhere('company_id', null);
}
}

View File

@ -11,7 +11,6 @@
namespace App\Filters;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
/**

View File

@ -12,7 +12,6 @@
namespace App\Filters;
use App\Models\Quote;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
/**

View File

@ -15,7 +15,6 @@ use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
use Tests\MockAccountData;
use Tests\TestCase;
@ -42,6 +41,17 @@ class BankTransactionApiTest extends TestCase
Model::reguard();
}
public function testBankTransactionGetClientStatus()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/bank_transactions?client_status=unmatched'.$this->encodePrimaryKey($this->bank_transaction->id));
$response->assertStatus(200);
}
public function testBankTransactionGet()
{
$response = $this->withHeaders([

View File

@ -40,6 +40,16 @@ class CreditTest extends TestCase
$this->makeTestData();
}
public function testCreditGetClientStatus()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/credits?client_status=draft'.$this->encodePrimaryKey($this->bank_transaction->id));
$response->assertStatus(200);
}
public function testCreditsList()
{
Client::factory()->count(3)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) {

View File

@ -41,6 +41,18 @@ class ExpenseApiTest extends TestCase
Model::reguard();
}
public function testExpenseGetClientStatus()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/expenses?client_status=paid');
$response->assertStatus(200);
}
public function testExpensePost()
{
$data = [

View File

@ -47,6 +47,16 @@ class InvoiceTest extends TestCase
}
public function testInvoiceGetPaidInvoices()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/invoices?client_status=paid',)
->assertStatus(200);
}
public function testInvoiceArchiveAction()
{

View File

@ -48,6 +48,17 @@ class PaymentTermsApiTest extends TestCase
);
}
public function testPaymentTermsGetWithFilter()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/payment_terms?filter=hey');
$response->assertStatus(200);
}
public function testPaymentTermsGet()
{
$response = $this->withHeaders([

View File

@ -62,6 +62,17 @@ class PaymentTest extends TestCase
);
}
public function testGetPaymentMatchList()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/payments?match_transactions=true');
}
public function testStorePaymentIdempotencyKeyIllegalLength()
{
$client = ClientFactory::create($this->company->id, $this->user->id);

View File

@ -47,6 +47,15 @@ class ProductTest extends TestCase
$this->makeTestData();
}
public function testProductGetProductKeyFilter()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/products?product_key=xx')
->assertStatus(200);
}
public function testProductList()
{
$response = $this->withHeaders([