diff --git a/app/Filters/UserFilters.php b/app/Filters/UserFilters.php index bded4c02eb9e..775103f1afa2 100644 --- a/app/Filters/UserFilters.php +++ b/app/Filters/UserFilters.php @@ -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); + } + } diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index e424640f4885..39d4dafefc1b 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -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([