Skip to content

Commit

Permalink
feat: Internal Unread Message Conversation Counter (dvsa/olcs-interna…
Browse files Browse the repository at this point in the history
…l#104)

* chore: so far

* chore: remove uneeded code

* chore: reformat horizontal-navigation

* chore: olcs-transfer bump

* feat: added robust error checking and default action

* chore: Added docblock to getUnreadConversationCountForLicence to explain extra error checking

* chore: renamed function to better explain action

* chore: tests and refactors

* chore: common bump and use REFDATA for admin roles
  • Loading branch information
jerotire authored Mar 5, 2024
1 parent 28d345d commit b3cd448
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 37 deletions.
24 changes: 12 additions & 12 deletions app/internal/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
use Common\Controller\Traits as CommonTraits;
use Common\Controller\Traits\GenericMethods;
use Common\Controller\Traits\GenericRenderView;
use Common\RefData;
use Common\Service\Helper\FormHelperService;
use Common\Service\Script\ScriptFactory;
use Common\Service\Table\TableFactory;
use Common\Util\FlashMessengerTrait;
use Dvsa\Olcs\Transfer\Query\Messaging\Messages\UnreadCountByLicenceAndRoles;
use Laminas\Mvc\Controller\AbstractActionController as LaminasAbstractActionController;
use Laminas\Mvc\MvcEvent;
use Laminas\View\HelperPluginManager;
use Olcs\Controller\Traits as OlcsTraits;
use Olcs\Logging\Log\Logger;

/**
* Abstract Controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use Common\Controller\Plugin\Redirect;
use Common\Data\Mapper\MapperInterface;
use Common\Form\Form;
use Common\RefData;
use Common\Service\Cqrs\Exception\NotFoundException;
use Common\Service\Cqrs\Response;
use Common\Service\Helper\FlashMessengerHelperService;
use Common\Service\Helper\FormHelperService;
use Common\Service\Helper\TranslationHelperService;
use Common\Service\Table\TableBuilder;
use Dvsa\Olcs\Transfer\Command\CommandInterface;
use Dvsa\Olcs\Transfer\Query\Messaging\Messages\UnreadCountByLicenceAndRoles;
use Dvsa\Olcs\Transfer\Query\QueryInterface;
use Laminas\Http\Request;
use Laminas\Http\Response as HttpResponse;
Expand Down
89 changes: 81 additions & 8 deletions app/internal/module/Olcs/src/Listener/RouteParam/Licence.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use Common\Exception\DataServiceException;
use Common\FeatureToggle;
use Common\RefData;
use Common\Service\Cqrs\Response;
use Common\Service\Data\Surrender;
use Common\View\Helper\PluginManagerAwareTrait as ViewHelperManagerAwareTrait;
use Dvsa\Olcs\Transfer\Query\FeatureToggle\IsEnabled as IsEnabledQry;
use Dvsa\Olcs\Transfer\Query\Messaging\Messages\UnreadCountByLicenceAndRoles;
use Olcs\Logging\Log\Logger;
use Psr\Container\ContainerInterface;
use Laminas\EventManager\EventInterface;
use Laminas\EventManager\EventManagerInterface;
Expand Down Expand Up @@ -155,6 +158,71 @@ public function attach(EventManagerInterface $events, $priority = 1)
);
}

/**
* Gets and applies the count to the 'Messages' navigation tab. This RouteParam is triggered from most other
* RouteParams (Application, Case, etc...).
*
* This has many checks and catches to prevent any errors from affecting the rest of internal as this code will
* be executed on most pages.
*
* As a result, errors will result in the counter dot appearing with a value of "E"; this is due to internal
* users may begin to rely on the visibility of the red counter dot appearing if there are new messages to be read
* and this ensures that they know that something is wrong, and that they should manually check.
*
* Any errors, although caught, displaying counter as "E" and moving on, will also result in a Logger::err().
*
* @param int $licence
* @return void
*/
final public function fetchAndApplyUnreadConversationCountForLicenceToMessageTabs(int $licenceId, Navigation $navigationService): void
{
$query = UnreadCountByLicenceAndRoles::create([
'licence' => $licenceId,
'roles' => [
RefData::ROLE_SYSTEM_ADMIN,
RefData::ROLE_INTERNAL_ADMIN,
RefData::ROLE_INTERNAL_CASE_WORKER,
RefData::ROLE_INTERNAL_IRHP_ADMIN,
RefData::ROLE_INTERNAL_READ_ONLY,
]
]);

try {
/* @var Response $response */
$response = $this->getQueryService()->send($this->getAnnotationBuilderService()->createQuery($query));

if (!$response->isOk()) {
throw new \Exception(
sprintf(
'Received non-OK response: %s -- %s',
$response->getStatusCode(),
$response->getBody()
)
);
}
$count = $response->getResult()['count'];
} catch (\Exception $e) {
$count = 'E';
Logger::err(
'Unable to get getUnreadConversationCountForLicence as non-OK response from UnreadCountByLicenceAndRoles query; defaulting to E',
[
'query' => [
'class' => get_class($query),
'data' => $query->getArrayCopy(),
],
'exception' => [
'class' => get_class($e),
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
],
]
);
}

$navigationService->findById('conversations')->set('unreadLicenceConversationCount', $count);
$navigationService->findById('application_conversations')->set('unreadLicenceConversationCount', $count);
}

public function onLicence(EventInterface $e)
{
$routeParam = $e->getTarget();
Expand Down Expand Up @@ -186,7 +254,7 @@ public function onLicence(EventInterface $e)
$licenceCategoryId = $licence['goodsOrPsv']['id'] ?? null;
$navigationService = $this->getMainNavigationService();

$this->handleMessagingTabVisibility($licence, $navigationService);
$this->handleMessagingTabVisibility($licenceId, $navigationService);

if ($licenceCategoryId === RefData::LICENCE_CATEGORY_GOODS_VEHICLE) {
$navigationService->findOneById('licence_bus')->setVisible(0);
Expand Down Expand Up @@ -559,16 +627,21 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
return $this;
}

private function handleMessagingTabVisibility($licence, $sidebarNav)
private function handleMessagingTabVisibility(int $licenceId, Navigation $navigationService): void
{
if ($this->isMessagingFeatureToggleEnabled()) {
$this->fetchAndApplyUnreadConversationCountForLicenceToMessageTabs($licenceId, $navigationService);
} else {
$navigationService->findById('conversations')->setVisible(0);
$navigationService->findById('application_conversations')->setVisible(0);
}
}

private function isMessagingFeatureToggleEnabled(): bool
{
$query = $this->getAnnotationBuilderService()->createQuery(
IsEnabledQry::create(['ids' => [FeatureToggle::MESSAGING]])
);
$isEnabled = $this->getQueryService()->send($query)->getResult()['isEnabled'];

if (!$isEnabled) {
$sidebarNav->findById('conversations')->setVisible(0);
$sidebarNav->findById('application_conversations')->setVisible(0);
}
return (bool)$this->getQueryService()->send($query)->getResult()['isEnabled'];
}
}
21 changes: 14 additions & 7 deletions app/internal/module/Olcs/view/partials/horizontal-navigation.phtml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<ul class="horizontal-navigation">
<?php foreach ($this->container as $page): ?>
<?php foreach ($this->container as $page): ?>
<?php if ($page->isVisible(true) && $this->navigation()->accept($page, false)): ?>
<li class="horizontal-navigation__item<?php if ($page->isActive(true)) { ?> current<?php } ?>">
<li class="horizontal-navigation__item<?php if ($page->isActive(true)) { ?> current<?php } ?>"
style="display: flex">
<?php if ($page->isActive(true)): ?>
<span><?php echo $this->translate($page->getLabel()); ?></span>
<span><?php echo $this->translate($page->getLabel()); ?></span>
<?php else: ?>
<?php echo $this->navigation()->menu()->htmlify($page) . PHP_EOL; ?>
<?php echo $this->navigation()->menu()->htmlify($page) . PHP_EOL; ?>
<?php endif; ?>
</li>
<?php if ($page->get('unreadLicenceConversationCount')): ?>
<div
class="notification-count"<?php if (!is_numeric($page->get('unreadLicenceConversationCount'))): ?> title="There was an error checking unread conversation count; you should manually check for any potentially missed messages."<?php endif; ?>>
<span class="notification-count__number"><?php echo $page->get('unreadLicenceConversationCount'); ?></span>
</div>
<?php endif; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</ul>
Loading

0 comments on commit b3cd448

Please sign in to comment.