Skip to content

Commit

Permalink
feat: Remove app/licence select for new conversations. (dvsa/olcs-int…
Browse files Browse the repository at this point in the history
…ernal#116)

Inherit from the current location.
  • Loading branch information
wadedvsa authored Mar 14, 2024
1 parent 9b67afa commit 073ed01
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ class AbstractCreateConversationController extends AbstractInternalController im

protected $toggleConfig = [
'default' => [
FeatureToggle::MESSAGING
FeatureToggle::MESSAGING,
],
];

protected $inlineScripts = [
'addAction' => ['forms/message-categories']
'addAction' => ['forms/message-categories'],
];

protected string $application = '';
protected string $licence = '';

public function getLeftView(): ViewModel
{
$view = new ViewModel(['navigationId' => $this->navigationId]);
Expand All @@ -43,89 +46,18 @@ public function getLeftView(): ViewModel
return $view;
}

public function alterFormForAdd($form)
{
$appLicNoSelect = $form->get('fields')->get('appOrLicNo');

if ($this->params()->fromRoute('licence')){
$licenceId = $this->params()->fromRoute('licence');
$data = $this->handleQuery(
ByLicenceToOrganisation::create(['licence' => $licenceId])
);
} elseif ($this->params()->fromRoute('application')) {
$applicationId = $this->params()->fromRoute('application');
$data = $this->handleQuery(
ByApplicationToOrganisation::create(['application' => $applicationId])
);
} elseif ($this->params()->fromRoute('case')) {
$caseId = $this->params()->fromRoute('case');
$data = $this->handleQuery(
ByCaseToOrganisation::create(['case' => $caseId])
);
} else {
throw new RuntimeException('Error: licence or application required');
}

$applicationLicenceArray = json_decode($data->getHttpResponse()->getBody(), true);

$this->prefixArrayKey($applicationLicenceArray['results']['licences'], 'L');
$this->prefixArrayKey($applicationLicenceArray['results']['applications'], 'A');

$options = [];

if($applicationLicenceArray['results']['licences']){
$options['licence'] = [
'label' => 'Licences',
'options' => $applicationLicenceArray['results']['licences'],
];
}

if($applicationLicenceArray['results']['applications']){
$options['application'] = [
'label' => 'Applications',
'options' => $applicationLicenceArray['results']['applications'],
];
}

$appLicNoSelect->setValueOptions($options);

return $form;
}

public function onDispatch(MvcEvent $e)
{
if ($this->getRequest()->isPost()) {
$postData = $this->getRequest()->getPost();
$postFields = $postData->get('fields');
$appOrLicNo = $postFields['appOrLicNo'] ?? null;
if (!empty($appOrLicNo)) {
switch ( str_split($appOrLicNo, 1)[0] )
{
case 'A':
$postFields['application'] = substr_replace($appOrLicNo, '', 0, 1);
$postFields['licence'] = '';
break;
case 'L':
$postFields['licence'] = substr_replace($appOrLicNo, '', 0, 1);
$postFields['application'] = '';
break;
default:
throw new \Laminas\Validator\Exception\RuntimeException('Unexpected prefix in appOrLicNo');
}
$postData->set('fields', $postFields);
$this->getRequest()->setPost($postData);
}
$params = $e->getRouteMatch()->getParams();
if (isset($params['licence'])) {
$this->licence = $params['licence'];
}
if (isset($params['application'])) {
$this->application = $params['application'];
}
$this->defaultData['application'] = $this->application;
$this->defaultData['licence'] = $this->licence;

return parent::onDispatch($e);
}

private function prefixArrayKey(array &$array, string $prefix): void
{
foreach ($array as $k => $v)
{
$array[$prefix . $k] = $v;
unset($array[$k]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Olcs\Controller\Messages;

use Dvsa\Olcs\Transfer\Query\Cases\Cases;
use Exception;
use Laminas\Mvc\MvcEvent;
use Olcs\Controller\Interfaces\CaseControllerInterface;
use Olcs\Controller\Interfaces\LicenceControllerInterface;

class CaseCreateConversationController extends AbstractCreateConversationController implements CaseControllerInterface
{
Expand All @@ -22,4 +24,26 @@ class CaseCreateConversationController extends AbstractCreateConversationControl
'reUseParams' => true
]
];

public function onDispatch(MvcEvent $e)
{
if ($this->getRequest()->isPost()) {
$caseId = $e->getRouteMatch()->getParam('case');
$queryResponse = $this->handleQuery(Cases::create(['id' => $caseId]));
if (!$queryResponse->isOk()) {
throw new Exception(
sprintf(
'Unexpected error when loading case. Response: HTTP %s :: %s',
$queryResponse->getStatusCode(),
$queryResponse->getBody(),
),
);
}

$queryResult = $queryResponse->getResult();
$this->licence = (string)$queryResult['licence']['id'];
}

return parent::onDispatch($e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Common\Form\Element\DynamicSelect;
use Laminas\Form\Annotation as Form;
use Laminas\Form\Element\Hidden;
use Laminas\Form\Element\Select;
use Laminas\Form\Element\Textarea;
use Laminas\Filter\StringTrim;
use Laminas\Validator\StringLength;
Expand All @@ -30,34 +28,12 @@ class Conversation
*/
public ?DynamicSelect $messageSubject = null;

/**
* @Form\Attributes({"id": "appOrLicNo","placeholder": ""})
* @Form\Options({
* "label": "Application or licence ID",
* "empty_option": "Please Select",
* })
* @Form\Type(Select::class)
*/
public ?Select $appOrLicNo = null;

/**
* @Form\Attributes({"class": "extra-long","id": ""})
* @Form\Attributes({"class": "extra-long", "id": ""})
* @Form\Options({"label": "Message"})
* @Form\Type(Textarea::class)
* @Form\Filter(StringTrim::class)
* @Form\Validator(StringLength::class, options={"min": 5, "max": 1000})
*/
public ?Textarea $messageContent = null;

/**
* @Form\Type(Hidden::class)
* @Form\Options({"value": ""})
*/
public ?Hidden $licence = null;

/**
* @Form\Type(Hidden::class)
* @Form\Options({"value": ""})
*/
public ?Hidden $application = null;
}
14 changes: 9 additions & 5 deletions app/internal/module/Olcs/src/Form/Model/Form/Conversation.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
<?php

declare(strict_types=1);

namespace Olcs\Form\Model\Form;

use Common\Form\Model\Fieldset\CreateConversationFormActions;
use Laminas\Form\Annotation as Form;
use Olcs\Form\Model\Fieldset\Conversation as ConversationFieldset;

/**
* @codeCoverageIgnore Auto-generated file with no methods
* @Form\Name("Conversation")
* @Form\Attributes({"method":"post"})
* @Form\Attributes({"method": "post"})
* @Form\Type("Common\Form\Form")
* @Form\Options({"prefer_form_input_filter": true})
*/
class Conversation
{
/**
* @Form\Name("fields")
* @Form\ComposedObject("Olcs\Form\Model\Fieldset\Conversation")
* @Form\ComposedObject(ConversationFieldset::Class)
*/
public $fields = null;
public ?ConversationFieldset $fields = null;

/**
* @Form\Name("form-actions")
* @Form\Attributes({"class":"govuk-button-group"})
* @Form\ComposedObject("Common\Form\Model\Fieldset\CreateConversationFormActions")
* @Form\ComposedObject(CreateConversationFormActions::class)
*/
public $formActions = null;
public ?CreateConversationFormActions $formActions = null;
}

0 comments on commit 073ed01

Please sign in to comment.