Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
feat: Messsaging new conversation form (#53)
Browse files Browse the repository at this point in the history
* chore: so far

* chore: so far

* chore: so far

* olcs-transfer bump

* feat: use GOVUK-SELECT class

* chore: code clean-up

* bump: olcs-common
  • Loading branch information
jerotire authored Feb 8, 2024
1 parent de32091 commit 6399942
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 11 deletions.
18 changes: 9 additions & 9 deletions composer.lock

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

9 changes: 8 additions & 1 deletion module/Olcs/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Olcs\Controller\Factory\IndexControllerFactory;
use Olcs\Controller\Factory\MyDetailsControllerFactory;
use Olcs\Controller\IndexController;
use Common\Service\Data as CommonDataService;
use Olcs\Controller\Licence\Vehicle\ListVehicleController;
use Olcs\Controller\Lva\Adapters\ApplicationPeopleAdapter;
use Olcs\Controller\Lva\Adapters\LicencePeopleAdapter;
Expand Down Expand Up @@ -45,6 +46,7 @@
use Olcs\Logging\Log\Processor\CorrelationId;
use Olcs\Logging\Log\Processor\CorrelationIdFactory;
use Olcs\Service\Cookie as CookieService;
use Olcs\Service\Data as DataService;
use Olcs\Service\Processing as ProcessingService;
use Olcs\Service\Qa as QaService;
use Olcs\Session\LicenceVehicleManagement;
Expand Down Expand Up @@ -1462,7 +1464,7 @@
VariationTransportManagerAdapter::class => VariationTransportManagerAdapterFactory::class,
VariationPeopleAdapter::class => VariationPeopleAdapterFactory::class,
\Olcs\Logging\Log\Processor\CorrelationId::class => \Olcs\Logging\Log\Processor\CorrelationIdFactory::class,
]
],
),
'log_processors' => [
'factories' => [
Expand Down Expand Up @@ -1504,6 +1506,11 @@
'simple_date_format' => array(
'default' => 'd-m-Y'
),
'data_services' => [
'factories' => [
DataService\MessagingAppOrLicNo::class => CommonDataService\AbstractListDataServiceFactory::class,
],
],
'view_helpers' => array(
'factories' => [
\Olcs\View\Helper\SessionTimeoutWarning\SessionTimeoutWarning::class => \Olcs\View\Helper\SessionTimeoutWarning\SessionTimeoutWarningFactory::class,
Expand Down
66 changes: 65 additions & 1 deletion module/Olcs/src/Controller/ConversationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Common\Service\Helper\FlashMessengerHelperService;
use Common\Service\Helper\FormHelperService;
use Common\Service\Table\TableFactory;
use Dvsa\Olcs\Transfer\Command\Messaging\Conversation\Create;
use Dvsa\Olcs\Transfer\Command\Messaging\Message\Create as CreateMessageCommand;
use Dvsa\Olcs\Transfer\Query\Messaging\Messages\ByConversation as ByConversationQuery;
use Dvsa\Olcs\Transfer\Query\Messaging\Conversations\ByOrganisation as ByOrganisationQuery;
Expand All @@ -19,6 +20,8 @@
use Laminas\View\Model\ViewModel;
use LmcRbacMvc\Service\AuthorizationService;
use Olcs\Form\Model\Form\Message\Reply as ReplyForm;
use Olcs\Form\Model\Form\Message\Create as CreateForm;
use Olcs\Service\Data\MessagingAppOrLicNo;

class ConversationsController extends AbstractController implements ToggleAwareInterface
{
Expand Down Expand Up @@ -75,14 +78,75 @@ public function indexAction(): ViewModel
return $view;
}

/**
* @throws \Exception
*/
public function addAction(): ViewModel
{
$view = new ViewModel([]);
$form = $this->formHelperService->createForm(CreateForm::class, true, false);
if ($this->getRequest()->isPost()) {
$form->setData($this->getRequest()->getPost());
if ($form->isValid()) {
return $this->submitConversation($form);
}
}

$view = new ViewModel();
$view->setVariable('form', $form);
$view->setTemplate('messages-new');

return $view;
}

/**
* @return Response|ViewModel
* @throws \Exception
*/
private function submitConversation(\Laminas\Form\Form $form)
{
$response = $this->handleCommand($this->mapFormDataToCommand($form));
if (!$response->isOk()) {
$this->flashMessengerHelper->addErrorMessage('There was an server error when submitting your conversation; please try later');
return $this->addAction();
}

$conversationId = $response->getResult()['id']['conversation'] ?? null;
if (empty($conversationId)) {
$this->flashMessengerHelper->addErrorMessage('There was an server error when submitting your conversation; please try later');
return $this->addAction();
}

$this->flashMessengerHelper->addSuccessMessage('Conversation was created successfully');
return $this->redirect()->toRoute('conversations/view', ['conversationId' => $conversationId]);
}

/**
* @throws \Exception
*/
private function mapFormDataToCommand(\Laminas\Form\Form $form): Create
{
$data = $form->getData();
$processedData = [
'messageSubject' => $data['form-actions']['messageSubject'],
'messageContent' => $data['form-actions']['messageContent'],
];

$appOrLicNoPrefix = substr($data['form-actions']['appOrLicNo'], 0, 1);
$appOrLicNoSuffix = substr($data['form-actions']['appOrLicNo'], 1);
switch($appOrLicNoPrefix) {
case MessagingAppOrLicNo::PREFIX_LICENCE:
$processedData['licence'] = $appOrLicNoSuffix;
break;
case MessagingAppOrLicNo::PREFIX_APPLICATION:
$processedData['application'] = $appOrLicNoSuffix;
break;
default:
throw new \Exception('Invalid prefix on appOrLicNo');
}

return Create::create($processedData);
}

/** @return ViewModel|Response */
public function viewAction()
{
Expand Down
84 changes: 84 additions & 0 deletions module/Olcs/src/Form/Model/Fieldset/Message/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Olcs\Form\Model\Fieldset\Message;

use Common\Form\Element\DynamicSelect;
use Common\Form\Elements\InputFilters\ActionButton;
use Common\Form\Elements\Types\GuidanceTranslated;
use Laminas\Form\Annotation as Form;
use Laminas\Form\Element\Textarea;

class Create
{
/**
* @Form\Options({
* "label": "messaging.subject",
* "empty_option": "Please select",
* "disable_inarray_validator": false,
* "service_name": \Common\Service\Data\MessagingSubject::class
* })
* @Form\Attributes({
* "class": "govuk-select"
* })
* @Form\Type(\Common\Form\Element\DynamicSelect::class)
* @Form\Required(true)
*/
public ?DynamicSelect $messageSubject;

/**
* @Form\Options({
* "label": "messaging.app-or-lic-no",
* "empty_option": "Please select",
* "disable_inarray_validator": false,
* "service_name": \Olcs\Service\Data\MessagingAppOrLicNo::class,
* "use_groups": true
* })
* @Form\Attributes({
* "class": "govuk-select"
* })
* @Form\Type(\Common\Form\Element\DynamicSelect::class)
* @Form\Required(true)
*/
public ?DynamicSelect $appOrLicNo;

/**
* @Form\Attributes({
* "class": "extra-long",
* "maxlength": 1000
* })
* @Form\Options({
* "label": "",
* "hint": "You can enter up to 1000 characters"
* })
* @Form\Required(true)
* @Form\Type(\Laminas\Form\Element\Textarea::class)
* @Form\Filter({"name": \Laminas\Filter\StringTrim::class})
* @Form\Validator({
* "name": \Laminas\Validator\StringLength::class,
* "options": {"min": 5, "max" :1000}
* })
*/
public ?TextArea $messageContent = null;

/**
* @Form\Attributes({"value": "markup-messaging-new-conversation-timeframe"})
* @Form\Type(\Common\Form\Elements\Types\GuidanceTranslated::class)
*/
public ?GuidanceTranslated $guidance;

/**
* @Form\Attributes({
* "type": "submit",
* "data-module": "govuk-button",
* "class": "govuk-button govuk-button--default",
* "id": "send"
* })
* @Form\Options({
* "label": "Send message",
* })
* @Form\Type(\Common\Form\Elements\InputFilters\ActionButton::class)
*/
public ?ActionButton $create = null;
}
17 changes: 17 additions & 0 deletions module/Olcs/src/Form/Model/Form/Message/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Olcs\Form\Model\Form\Message;

use Laminas\Form\Annotation as Form;
use Olcs\Form\Model\Fieldset\Message\Reply as ReplyFieldset;

class Create
{
/**
* @Form\Name("form-actions")
* @Form\ComposedObject(\Olcs\Form\Model\Fieldset\Message\Create::class)
*/
public ?ReplyFieldset $formActions = null;
}
72 changes: 72 additions & 0 deletions module/Olcs/src/Service/Data/MessagingAppOrLicNo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Olcs\Service\Data;

use Common\Exception\DataServiceException;
use Common\Service\Data\AbstractListDataService;
use Dvsa\Olcs\Transfer\Query as TransferQry;

class MessagingAppOrLicNo extends AbstractListDataService
{
public const PREFIX_APPLICATION = 'A';
public const PREFIX_LICENCE = 'L';

/**
* @throws DataServiceException
*/
public function fetchListData($context = null): array
{
$data = (array)$this->getData('licapp');

if (count($data) !== 0) {
return $data;
}

$response = $this->handleQuery(
TransferQry\Messaging\ApplicationLicenceList\ByOrganisation::create([])
);

if (!$response->isOk()) {
throw new DataServiceException('Unknown Error - ' . json_encode($response));
}

$result = $response->getResult();

$this->setData('licapp', ($result['results'] ?? null));

return $this->getData('licapp');
}

public function formatDataForGroups(array $data): array
{
$optionData = [
[
'label' => 'Licence',
'options' => []
],
[
'label' => 'Application',
'options' => []
]
];

$optionData[0]['options'] = $data['licences'];
$optionData[1]['options'] = $data['applications'];

$this->prefixArrayKey($optionData[0]['options'], static::PREFIX_LICENCE);
$this->prefixArrayKey($optionData[1]['options'], static::PREFIX_APPLICATION);

return $optionData;
}

private function prefixArrayKey(array &$array, string $prefix): void
{
foreach ($array as $k => $v)
{
$array[$prefix . $k] = $v;
unset($array[$k]);
}
}
}
8 changes: 8 additions & 0 deletions module/Olcs/view/messages-new.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ echo $this->partial(
->setMaxDepth(0)
->setPartial('partials/tabs-nav');
?>

<div>
<?php
echo $this->formErrors($this->form);
echo $this->form($this->form);
?>
</div>

</div>

<?php echo $this->partial('partials/conversations-right-column'); ?>
Expand Down

0 comments on commit 6399942

Please sign in to comment.