Fixes for UserFilters as no company() scope is possible

This commit is contained in:
David Bomba 2023-01-19 10:31:48 +11:00
parent 7b40340047
commit 295a435762
2 changed files with 34 additions and 2 deletions

View File

@ -61,10 +61,29 @@ class UserFilters extends QueryFilters
*/
public function entityFilter()
{
//return $this->builder->user_companies()->whereCompanyId(auth()->user()->company()->id);
//return $this->builder->whereCompanyId(auth()->user()->company()->id);
return $this->builder->whereHas('company_users', function ($q) {
$q->where('company_id', '=', auth()->user()->company()->id);
});
}
/**
* Overrides the base with() function as no company ID
* exists on the user table
*
* @param string $value Hashed ID of the user to return back in the dataset
*
* @return Builder
*/
public function with(string $value = ''): Builder
{
if(strlen($value) == 0)
return $this->builder;
return $this->builder
->orWhere($this->with_property, $value)
->orderByRaw("{$this->with_property} = ? DESC", [$value])
->where('account_id', auth()->user()->account_id);
}
}

View File

@ -58,6 +58,19 @@ class UserTest extends TestCase
}
public function testUserFiltersWith()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
])->get('/api/v1/users?with='.$this->user->hashed_id);
$response->assertStatus(200);
}
public function testUserList()
{
$response = $this->withHeaders([