Skip to content

Commit

Permalink
feat: Active TM search only (dvsa/olcs-backend#154)
Browse files Browse the repository at this point in the history
* feat: Filter by active TM's only

* fix: Unit test

* chore: PHPCS

* fix: Remove common dependency

* chore: Tidy up index class with abstract

* fix: Missing extends
  • Loading branch information
wadedvsa authored Apr 23, 2024
1 parent 4f9c6c2 commit 9428533
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/api/config/application.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
'Olcs\XmlTools',
'Dvsa\Olcs\GdsVerify',
'Dvsa\Olcs\AwsSdk',
'Dvsa\Olcs\Queue'
'Dvsa\Olcs\Queue',
],
// These are various options for the listeners attached to the ModuleManager
'module_listener_options' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Olcs\Db\Service\Search\Indices;

use InvalidArgumentException;
use Olcs\Db\Service\Search\Indices\Terms\ComplexTermInterface;
use Olcs\Db\Service\Search\Indices\Terms\TransportManagerLicenceStatus;

abstract class AbstractIndex
{
protected array $filters;

/** @return ComplexTermInterface[] */
abstract public function getFilters(): array;
}
23 changes: 23 additions & 0 deletions app/api/module/Olcs/Db/src/Service/Search/Indices/Person.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Olcs\Db\Service\Search\Indices;

use Olcs\Db\Service\Search\Indices\Terms\ComplexTermInterface;
use Olcs\Db\Service\Search\Indices\Terms\TransportManagerLicenceStatus;

class Person extends AbstractIndex
{
/** @return ComplexTermInterface[] */
public function getFilters(): array
{
if (!isset($this->filters)) {
$this->filters = [
new TransportManagerLicenceStatus(),
];
}

return $this->filters;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Olcs\Db\Service\Search\Indices\Terms;

interface ComplexTermInterface
{
public function applySearch(array &$params): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Olcs\Db\Service\Search\Indices\Terms;

class TransportManagerLicenceStatus implements ComplexTermInterface
{
public function applySearch(array &$params): void
{
$params['must_not'][] = [
'terms' => [
'app_status_id' => [
'apsts_refused',
'apsts_valid',
'apsts_curtailed',
'apsts_withdrawn',
'apsts_cancelled',
'apsts_not_submitted',
],
],
];
$params['must_not'][] = [
'terms' => [
'lic_status' => [
'lsts_cancelled',
'lsts_terminated',
'lsts_withdrawn',
],
],
];
$params['must_not'][] = [
'exists' => [
'field' => 'date_removed',
],
];
}
}
23 changes: 22 additions & 1 deletion app/api/module/Olcs/Db/src/Service/Search/QueryTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use DomainException;
use Elastica\Query;
use InvalidArgumentException;
use Olcs\Db\Service\Search\Indices\Terms\ComplexTermInterface;
use RuntimeException;

/**
Expand All @@ -15,14 +17,16 @@ class QueryTemplate extends Query
{
public const FILTER_TYPE_DYNAMIC = 'DYNAMIC';
public const FILTER_TYPE_FIXED = 'FIXED';
public const FILTER_TYPE_COMPLEX = 'COMPLEX';
public const FILTER_TYPE_BOOLEAN = 'BOOLEAN';

public function __construct(
string $filename,
string $searchTerm,
array $filters = [],
array $filterTypes = [],
$dateRanges = []
array $dateRanges = [],
protected array $searchTypes = [],
) {
if (!file_exists($filename)) {
throw new RuntimeException("Query template file '" . $filename . "' is missing");
Expand Down Expand Up @@ -70,6 +74,23 @@ private function applyFilters(array $filters, array $filterTypes): self
}

switch ($filterTypes[$field]) {
case self::FILTER_TYPE_COMPLEX:
$filter = null;
foreach ($this->searchTypes as $searchType) {
try {
$filter = $searchType->getFilter($field);
} catch (InvalidArgumentException) {
continue;
}

if (!($filter instanceof ComplexTermInterface)) {
continue;
}

$filter->applySearch($this->_params['query']['bool']);
}
break;

case self::FILTER_TYPE_FIXED:
$fields = explode('|', $field);
foreach ($fields as $subField) {
Expand Down
54 changes: 35 additions & 19 deletions app/api/module/Olcs/Db/src/Service/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Dvsa\Olcs\Api\Domain\AuthAwareInterface;
use Dvsa\Olcs\Api\Domain\AuthAwareTrait;
use LmcRbacMvc\Service\AuthorizationService;
use Olcs\Db\Service\Search\Indices\AbstractIndex;

/**
* Class Search
Expand All @@ -29,16 +30,6 @@ class Search implements AuthAwareInterface

public const MAX_NUMBER_OF_RESULTS = 10000;

/**
* @var Client
*/
protected $client;

/**
* @var SysParamRepo
*/
protected $sysParamRepo;

protected array $filters = [];

protected array $filterTypes = [];
Expand All @@ -58,15 +49,12 @@ class Search implements AuthAwareInterface
*/
protected $order = '';

/**
* Search constructor.
*
*/
public function __construct(Client $client, AuthorizationService $authService, SysParamRepo $sysParamRepo)
{
$this->client = $client;
public function __construct(
protected Client $client,
AuthorizationService $authService,
protected SysParamRepo $sysParamRepo,
) {
$this->authService = $authService;
$this->sysParamRepo = $sysParamRepo;
}

/**
Expand Down Expand Up @@ -139,7 +127,23 @@ public function search($query, $indexes = [], $page = 1, $limit = 10)
if ($queryTemplate === false) {
throw new \RuntimeException('Cannot generate an elasticsearch query, is the template missing');
}
$elasticaQuery = new QueryTemplate($queryTemplate, $query, $this->getFilters(), $this->getFilterTypes(), $this->getDateRanges());

$searchTypes = array_filter(
array_map(
fn($index) => $this->getSearchType($index),
$indexes,
),
fn($item) => $item !== null,
);

$elasticaQuery = new QueryTemplate(
$queryTemplate,
$query,
$this->getFilters(),
$this->getFilterTypes(),
$this->getDateRanges(),
$searchTypes,
);

if (!empty($this->getSort()) && !empty($this->getOrder())) {
$elasticaQuery->setSort([$this->getSort() => strtolower($this->getOrder())]);
Expand Down Expand Up @@ -409,4 +413,16 @@ protected function getInternalUserTAPostFilter($searchIndex)

return $postFilter;
}

protected function getSearchType(string $index): ?AbstractIndex
{
$index = ucwords($index);
$class = '\\Olcs\\Db\\Service\\Search\\Indices\\' . $index;

if (!class_exists($class)) {
return null;
}

return new $class();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
return new Search(
$container->get(Client::class),
$container->get(AuthorizationService::class),
$container->get('RepositoryServiceManager')->get('SystemParameter')
$container->get('RepositoryServiceManager')->get('SystemParameter'),
);
}
}
Loading

0 comments on commit 9428533

Please sign in to comment.