Skip to content

Commit

Permalink
Merge pull request #4686 from GCTC-NTGC/bug/4557-search-page-queries
Browse files Browse the repository at this point in the history
Bug - Search page queries
  • Loading branch information
yonikid15 authored Nov 10, 2022
2 parents 1d8d8a4 + 80c9007 commit e04a110
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 124 deletions.
9 changes: 8 additions & 1 deletion api/app/GraphQL/Queries/CountPoolCandidatesByPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Models\PoolCandidate;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use App\Models\Pool;
use Database\Helpers\ApiEnums;

final class CountPoolCandidatesByPool
{
Expand All @@ -31,6 +31,13 @@ public function __invoke($_, array $args)
PoolCandidate::filterByPools($queryBuilder, $pools);
}

// available candidates scope (scope CANDIDATE_STATUS_QUALIFIED_AVAILABLE or CANDIDATE_STATUS_PLACED_CASUAL)
PoolCandidate::scopeAvailable($queryBuilder);

// expiry status filter (filter active pool candidates)
PoolCandidate::scopeExpiryFilter($queryBuilder, [ 'expiryStatus' => ApiEnums::CANDIDATE_EXPIRY_FILTER_ACTIVE ]);


$queryBuilder->whereHas('user', function (Builder $userQuery) use ($filters) {
// user filters go here

Expand Down
10 changes: 6 additions & 4 deletions api/app/Models/PoolCandidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public function scopePoolCandidateStatuses(Builder $query, ?array $poolCandidate
return $query;
}

public function scopeAvailable(Builder $query): Builder
public static function scopeAvailable(Builder $query): Builder
{
return $query->whereIn('pool_candidate_status', [ApiEnums::CANDIDATE_STATUS_QUALIFIED_AVAILABLE, ApiEnums::CANDIDATE_STATUS_PLACED_CASUAL]);
}
Expand All @@ -357,12 +357,14 @@ public function scopeHasDiploma(Builder $query, bool $hasDiploma): Builder
return $query;
}

public function scopeExpiryFilter(Builder $query, ?array $args)
public static function scopeExpiryFilter(Builder $query, ?array $args)
{
$expiryStatus = isset($args['expiryStatus']) ? $args['expiryStatus'] : ApiEnums::CANDIDATE_EXPIRY_FILTER_ACTIVE;
if ($expiryStatus == ApiEnums::CANDIDATE_EXPIRY_FILTER_ACTIVE) {
$query->whereDate('expiry_date', '>=', date("Y-m-d"))
->orWhereNull('expiry_date');
$query->where(function ($query) {
$query->whereDate('expiry_date', '>=', date("Y-m-d"))
->orWhereNull('expiry_date');
});
} else if ($expiryStatus == ApiEnums::CANDIDATE_EXPIRY_FILTER_EXPIRED) {
$query->whereDate('expiry_date', '<', date("Y-m-d"));
}
Expand Down
226 changes: 114 additions & 112 deletions api/tests/Feature/CountPoolCandidatesByPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ protected function setUp(): void
$newUser->save();
}

public function poolCandidateData(Pool $pool, User $user, ?bool $available = true, ?bool $futureDate = true) {
return [
'pool_id' => $pool,
'user_id' => $user,
'pool_candidate_status' => $available ? ApiEnums::CANDIDATE_STATUS_QUALIFIED_AVAILABLE : ApiEnums::CANDIDATE_STATUS_SCREENED_OUT_APPLICATION,
'expiry_date' => $futureDate ? config('constants.far_future_date') : config('constants.past_date') ,
];
}

// user (admin) not returned if no candidates
// the admin has no candidates so should get no results
public function testThatEmptyDoesNotReturnTheAdmin()
Expand Down Expand Up @@ -66,10 +75,7 @@ public function testThatEmptyReturnsACandidate()
$user = User::factory()->create([
'job_looking_status' => ApiEnums::USER_STATUS_ACTIVELY_LOOKING
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user->id
]);
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user));

$this->graphQL(
/** @lang GraphQL */
Expand Down Expand Up @@ -109,10 +115,7 @@ public function testThatPoolPropertiesCanBeReturned()
$user = User::factory()->create([
'job_looking_status' => ApiEnums::USER_STATUS_ACTIVELY_LOOKING
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user->id
]);
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user));

$this->graphQL(
/** @lang GraphQL */
Expand Down Expand Up @@ -157,14 +160,8 @@ public function testThatEmptyReturnsTwoCandidatesForOneUser()
$user = User::factory()->create([
'job_looking_status' => ApiEnums::USER_STATUS_ACTIVELY_LOOKING
]);
PoolCandidate::factory()->create([
'pool_id' => $pool1->id,
'user_id' => $user->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool2->id,
'user_id' => $user->id
]);
PoolCandidate::factory()->create($this->poolCandidateData($pool1, $user));
PoolCandidate::factory()->create($this->poolCandidateData($pool2, $user));

$this->graphQL(
/** @lang GraphQL */
Expand Down Expand Up @@ -204,10 +201,7 @@ public function testAvailableForOpportunities()
$user = User::factory()->create([
'job_looking_status' => $status
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user->id
]);
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user));
}

$this->graphQL(
Expand Down Expand Up @@ -252,18 +246,9 @@ public function testHasDiploma()
'job_looking_status' => ApiEnums::USER_STATUS_ACTIVELY_LOOKING,
'has_diploma' => null
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user1->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user2->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user3->id
]);
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user1));
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user2));
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user3));

$this->graphQL(
/** @lang GraphQL */
Expand Down Expand Up @@ -309,18 +294,9 @@ public function testEquityIsWoman()
'job_looking_status' => ApiEnums::USER_STATUS_ACTIVELY_LOOKING,
'is_woman' => null
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user1->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user2->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user3->id
]);
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user1));
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user2));
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user3));

$this->graphQL(
/** @lang GraphQL */
Expand Down Expand Up @@ -370,18 +346,9 @@ public function testLanguageAbility()
'job_looking_status' => ApiEnums::USER_STATUS_ACTIVELY_LOOKING,
'language_ability' => ApiEnums::LANGUAGE_ABILITY_FRENCH
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user1->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user2->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user3->id
]);
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user1));
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user2));
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user3));

$this->graphQL(
/** @lang GraphQL */
Expand Down Expand Up @@ -436,18 +403,9 @@ public function testOperationalRequirements()
ApiEnums::OPERATIONAL_REQUIREMENT_OVERTIME_OCCASIONAL
]
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user1->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user2->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user3->id
]);
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user1));
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user2));
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user3));

$this->graphQL(
/** @lang GraphQL */
Expand Down Expand Up @@ -505,18 +463,9 @@ public function testLocationPreferences()
ApiEnums::WORK_REGION_NATIONAL_CAPITAL
]
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user1->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user2->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user3->id
]);
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user1));
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user2));
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user3));

$this->graphQL(
/** @lang GraphQL */
Expand Down Expand Up @@ -565,18 +514,9 @@ public function testWouldAcceptTemporary()
'job_looking_status' => ApiEnums::USER_STATUS_ACTIVELY_LOOKING,
'would_accept_temporary' => null
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user1->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user2->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user3->id
]);
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user1));
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user2));
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user3));

$this->graphQL(
/** @lang GraphQL */
Expand Down Expand Up @@ -616,10 +556,7 @@ public function testClassifications()
);
$users = User::factory(3)
->afterCreating(function ($user) use ($pool) {
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user->id,
]);
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user));
})
->create([
'job_looking_status' => ApiEnums::USER_STATUS_ACTIVELY_LOOKING
Expand Down Expand Up @@ -681,10 +618,7 @@ public function testSkills()
$users = User::factory(3)
->afterCreating(function ($user) use ($pool) {
$exp = AwardExperience::factory()->create(['user_id' => $user->id]);
PoolCandidate::factory()->create([
'pool_id' => $pool->id,
'user_id' => $user->id,
]);
PoolCandidate::factory()->create($this->poolCandidateData($pool, $user));
})
->create(['job_looking_status' => ApiEnums::USER_STATUS_ACTIVELY_LOOKING]);

Expand Down Expand Up @@ -741,18 +675,10 @@ public function testPoolFilter()
$user = User::factory()->create([
'job_looking_status' => ApiEnums::USER_STATUS_ACTIVELY_LOOKING
]);
PoolCandidate::factory()->create([
'pool_id' => $pool1->id,
'user_id' => $user->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool2->id,
'user_id' => $user->id
]);
PoolCandidate::factory()->create([
'pool_id' => $pool3->id,
'user_id' => $user->id
]);

PoolCandidate::factory()->create($this->poolCandidateData($pool1, $user));
PoolCandidate::factory()->create($this->poolCandidateData($pool2, $user));
PoolCandidate::factory()->create($this->poolCandidateData($pool3, $user));

$this->graphQL(
/** @lang GraphQL */
Expand Down Expand Up @@ -787,4 +713,80 @@ public function testPoolFilter()
]
]);
}

public function testAvailableScope()
{
$pool = Pool::factory()->create();
foreach (ApiEnums::candidateStatuses() as $status) {
$user = User::factory()->create([
'job_looking_status' => ApiEnums::USER_STATUS_ACTIVELY_LOOKING
]);
PoolCandidate::factory()->create([
'pool_id' => $pool,
'user_id' => $user,
'pool_candidate_status' => $status,
'expiry_date' => config('constants.far_future_date')
]);
}

$this->graphQL(
/** @lang GraphQL */
'
query ($where: ApplicantFilterInput) {
countPoolCandidatesByPool(where: $where) {
pool { id }
candidateCount
}
}
',
[
'where' => []
]
)->assertSimilarJson([
'data' => [
'countPoolCandidatesByPool' => [
[
'pool' => ['id' => $pool->id],
'candidateCount' => 2
]
]
]
]);
}

public function testExpiryFilter()
{
$pool1 = Pool::factory()->create();
$pool2 = Pool::factory()->create();
$user = User::factory()->create([
'job_looking_status' => ApiEnums::USER_STATUS_ACTIVELY_LOOKING
]);

PoolCandidate::factory()->create($this->poolCandidateData($pool1, $user, true, true)); // future expiry date
PoolCandidate::factory()->create($this->poolCandidateData($pool2, $user, true, false)); // past expiry date

$this->graphQL(
/** @lang GraphQL */
'
query ($where: ApplicantFilterInput) {
countPoolCandidatesByPool(where: $where) {
pool { id }
candidateCount
}
}
',
[
'where' => []
]
)->assertSimilarJson([
'data' => [
'countPoolCandidatesByPool' => [
[
'pool' => ['id' => $pool1->id],
'candidateCount' => 1
]
]
]
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ const applicantFilterToQueryArgs = (
return {
where: {
...filter,
// TODO: does recreating the equity object serve any purpose?
equity: {
hasDisability: filter?.equity?.hasDisability,
isIndigenous: filter?.equity?.isIndigenous,
isVisibleMinority: filter?.equity?.isVisibleMinority,
isWoman: filter?.equity?.isWoman,
},
equity: { ...filter?.equity },
expectedClassifications: filter?.expectedClassifications
? pickMap(filter.expectedClassifications, ["group", "level"])
: [],
Expand Down

0 comments on commit e04a110

Please sign in to comment.