Skip to content

Commit

Permalink
feat: Unread counter on Messages tab (dvsa/olcs-selfserve#70)
Browse files Browse the repository at this point in the history
* WIP

* Updated navigation and tests

* Fix tests

* Revert phpunit; Restore composer lock from closed branch

* Resolve tab highlighting issues

* chore: Whitespace for CodeSniffer

---------

Co-authored-by: Saul Wilcox <saul.wilcox@dvsa.gov.uk>
  • Loading branch information
hobbyhacker0 and hobbyhacker0 authored Feb 28, 2024
1 parent b696ed9 commit a16cfaa
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 52 deletions.
12 changes: 6 additions & 6 deletions app/selfserve/composer.lock

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

7 changes: 7 additions & 0 deletions app/selfserve/module/Olcs/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,13 @@
'id' => 'dashboard-messaging',
'label' => 'dashboard-nav-messaging',
'route' => 'conversations',
'pages' => array(
array(
'id' => 'messaging-create-conversation',
'label' => 'New Conversation',
'route' => 'conversations/new',
),
),
),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Dvsa\Olcs\Transfer\Query\Messaging\Conversations\ByOrganisation as ByOrganisationQuery;
use Dvsa\Olcs\Utils\Translation\NiTextTranslation;
use Laminas\Http\Response;
use Laminas\Navigation\Navigation;
use Laminas\View\Model\ViewModel;
use LmcRbacMvc\Service\AuthorizationService;
use Olcs\Form\Model\Form\Message\Reply as ReplyForm;
Expand All @@ -34,17 +35,20 @@ class ConversationsController extends AbstractController implements ToggleAwareI
protected FlashMessengerHelperService $flashMessengerHelper;
protected TableFactory $tableFactory;
protected FormHelperService $formHelperService;
protected Navigation $navigationService;

public function __construct(
NiTextTranslation $niTextTranslationUtil,
AuthorizationService $authService,
FlashMessengerHelperService $flashMessengerHelper,
TableFactory $tableFactory,
FormHelperService $formHelperService
FormHelperService $formHelperService,
Navigation $navigationService
) {
$this->flashMessengerHelper = $flashMessengerHelper;
$this->tableFactory = $tableFactory;
$this->formHelperService = $formHelperService;
$this->navigationService = $navigationService;

parent::__construct($niTextTranslationUtil, $authService);
}
Expand Down Expand Up @@ -150,6 +154,8 @@ private function mapFormDataToCommand(\Laminas\Form\Form $form): Create
/** @return ViewModel|Response */
public function viewAction()
{
$this->navigationService->findBy('id', 'dashboard-messaging')->setActive();

$params = [
'page' => $this->params()->fromQuery('page', 1),
'limit' => $this->params()->fromQuery('limit', 10),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Common\Service\Helper\FormHelperService;
use Common\Service\Table\TableFactory;
use Dvsa\Olcs\Utils\Translation\NiTextTranslation;
use Laminas\Navigation\Navigation;
use Psr\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use LmcRbacMvc\Service\AuthorizationService;
Expand All @@ -25,13 +26,15 @@ public function __invoke(
$flashMessengerHelper = $container->get(FlashMessengerHelperService::class);
$tableFactory = $container->get(TableFactory::class);
$formHelperService = $container->get(FormHelperService::class);
$navigationService = $container->get(Navigation::class);

return new ConversationsController(
$niTextTranslationUtil,
$authService,
$flashMessengerHelper,
$tableFactory,
$formHelperService,
$navigationService
);
}
}
25 changes: 25 additions & 0 deletions app/selfserve/module/Olcs/src/Controller/Listener/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Common\FeatureToggle;
use Common\Rbac\User as RbacUser;
use Common\Service\Cqrs\Query\QuerySender;
use Dvsa\Olcs\Transfer\Query\Messaging\Messages\UnreadCountByOrganisationAndUser;
use Laminas\EventManager\EventManagerInterface;
use Laminas\EventManager\ListenerAggregateInterface;
use Laminas\EventManager\ListenerAggregateTrait;
Expand Down Expand Up @@ -168,6 +169,8 @@ private function toggleMessagesTab(bool $shouldShowMessagesTab): void
{
$this->navigation->findBy('id', 'dashboard-messaging')
->setVisible($shouldShowMessagesTab);

$this->addUnreadMessagingCount();
}

private function shouldShowMessagesTab(): bool
Expand Down Expand Up @@ -201,4 +204,26 @@ public function setGovUkReferers(array $govUkReferers): void
{
$this->govUkReferers = $govUkReferers;
}

public function getUnreadMessageCount(): int
{
$userOrganisationId = $this->identity->getUserData()['organisationUsers'][0]['organisation']['id'];

$unreadByOrganisation = $this->querySender->send(
UnreadCountByOrganisationAndUser::create(
[
'organisation' => $userOrganisationId,
'user' => $this->identity->getId(),
]
)
);

return($unreadByOrganisation->getResult()['count'] ?? 0);
}

public function addUnreadMessagingCount(){
$this->navigation->findBy('id', 'dashboard-licences-applications')
->findBy('id','dashboard-messaging')
->set('unreadMessageCount', $this->getUnreadMessageCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public function formatDataForGroups(array $data): array

private function prefixArrayKey(array &$array, string $prefix): void
{
foreach ($array as $k => $v)
{
foreach ($array as $k => $v) {
$array[$prefix . $k] = $v;
unset($array[$k]);
}
Expand Down
9 changes: 9 additions & 0 deletions app/selfserve/module/Olcs/view/partials/tabs-nav.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
<?php echo $this->escapeHtml($this->translate($page->getLabel())); ?>
<?php echo $page->get('count') ? '('.$page->get('count').')' : '';?>
</a>
<?php if ($page->id == "dashboard-messaging" && $page->get('unreadMessageCount') > 0): ?>
<div class="notification-count">
<span class="notification-count__number">
<?php
echo $page->get('unreadMessageCount');
?>
</span>
</div>
<?php endif; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Laminas\Http\Response as HttpResponse;
use Laminas\Mvc\Controller\Plugin\Params;
use Dvsa\Olcs\Transfer\Command\Messaging\Message\Create as CreateMessageCommand;
use Laminas\Navigation\Navigation;
use Laminas\View\Model\ViewModel;
use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryTestCase as TestCase;
Expand All @@ -36,6 +37,7 @@ public function setUp(): void
$this->mockFlashMessengerHelper = m::mock(FlashMessengerHelperService::class)->makePartial();
$this->mockTableFactory = m::mock(TableFactory::class)->makePartial();
$this->mockFormHelperService = m::mock(FormHelperService::class)->makePartial();
$this->mockNavigation = m::mock(Navigation::class)->shouldIgnoreMissing();
$this->mockForm = m::mock(Form::class);
$this->mockParams = m::mock(Params::class);

Expand All @@ -49,6 +51,7 @@ public function setUp(): void
$this->setMockedProperties($reflectionClass, 'flashMessengerHelper', $this->mockFlashMessengerHelper);
$this->setMockedProperties($reflectionClass, 'tableFactory', $this->mockTableFactory);
$this->setMockedProperties($reflectionClass, 'formHelperService', $this->mockFormHelperService);
$this->setMockedProperties($reflectionClass, 'navigationService', $this->mockNavigation);

$this->mockFormHelperService->shouldReceive('createForm')
->once()
Expand Down Expand Up @@ -120,6 +123,10 @@ public function testViewAction(): void
)
->andReturn($table);

$this->mockNavigation
->shouldReceive('findBy->setActive')
->once();

$view = $this->sut->viewAction();
$this->assertInstanceOf(ViewModel::class, $view);
$this->assertEquals($table, $view->getVariable('table'));
Expand Down
Loading

0 comments on commit a16cfaa

Please sign in to comment.