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

feat: Enable/Disable Messaging File Upload #72

Merged
merged 12 commits into from
Mar 1, 2024
77 changes: 65 additions & 12 deletions module/Olcs/src/Controller/ConversationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

namespace Olcs\Controller;

use Common\Category;
use Common\Controller\Interfaces\ToggleAwareInterface;
use Common\Controller\Lva\AbstractController;
use Common\FeatureToggle;
use Common\Form\Form;
use Common\Service\Helper\FileUploadHelperService;
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\Documents;
use Dvsa\Olcs\Transfer\Query\Messaging\Messages\ByConversation as ByConversationQuery;
use Dvsa\Olcs\Transfer\Query\Messaging\Conversations\ByOrganisation as ByOrganisationQuery;
use Dvsa\Olcs\Utils\Translation\NiTextTranslation;
Expand All @@ -36,19 +39,22 @@ class ConversationsController extends AbstractController implements ToggleAwareI
protected TableFactory $tableFactory;
protected FormHelperService $formHelperService;
protected Navigation $navigationService;
protected FileUploadHelperService $uploadHelper;

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

parent::__construct($niTextTranslationUtil, $authService);
}
Expand Down Expand Up @@ -110,13 +116,17 @@ 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');
$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');
$this->flashMessengerHelper->addErrorMessage(
'There was an server error when submitting your conversation; please try later',
);
return $this->addAction();
}

Expand All @@ -137,7 +147,7 @@ private function mapFormDataToCommand(\Laminas\Form\Form $form): Create

$appOrLicNoPrefix = substr($data['form-actions']['appOrLicNo'], 0, 1);
$appOrLicNoSuffix = substr($data['form-actions']['appOrLicNo'], 1);
switch($appOrLicNoPrefix) {
switch ($appOrLicNoPrefix) {
case MessagingAppOrLicNo::PREFIX_LICENCE:
$processedData['licence'] = $appOrLicNoSuffix;
break;
Expand Down Expand Up @@ -176,21 +186,25 @@ public function viewAction()
}

$form = $this->formHelperService->createForm(ReplyForm::class, true, false);
$form->get('correlationId')->setValue(sha1(microtime()));
$this->formHelperService->setFormActionFromRequest($form, $this->getRequest());

$table = $this->tableFactory
->buildTable('messages-view', $messages, $params);
$table = $this->tableFactory->buildTable('messages-view', $messages, $params);

$canUploadFiles = $this->getCurrentOrganisation()['isMessagingFileUploadEnabled'];

$view = new ViewModel(
[
'table' => $table,
'form' => $form,
'canReply' => $canReply,
'table' => $table,
'form' => $form,
'canReply' => $canReply,
'openReply' => false,
'canUploadFiles' => $canUploadFiles,
],
);
$view->setTemplate('messages-view');

if ($this->getRequest()->isPost() && $this->params()->fromPost('action') === 'reply') {
if ($this->getRequest()->isPost()) {
return $this->parseReply($view, $form);
}

Expand All @@ -200,17 +214,29 @@ public function viewAction()
/** @return Response|ViewModel */
protected function parseReply(ViewModel $view, Form $form)
{
$form->setData((array)$this->params()->fromPost());
$form->setData((array)$this->getRequest()->getPost());
$form->get('id')->setValue($this->params()->fromRoute('conversation'));

if (!$form->isValid()) {
$hasProcessedFiles = $this->processFiles(
$form,
'form-actions->file',
[$this, 'processFileUpload'],
[$this, 'deleteFile'],
[$this, 'getUploadedFiles'],
'form-actions->file->fileCount',
);

$view->setVariable('openReply', $hasProcessedFiles);

if ($hasProcessedFiles || $this->params()->fromPost('action') !== 'reply' || !$form->isValid()) {
return $view;
}

$response = $this->handleCommand(
CreateMessageCommand::create(
[
'conversation' => $this->params()->fromRoute('conversationId'),
'correlationId' => $this->getRequest()->getPost('correlationId'),
'messageContent' => $form->get('form-actions')->get('reply')->getValue(),
],
),
Expand All @@ -225,4 +251,31 @@ protected function parseReply(ViewModel $view, Form $form)

return parent::indexAction();
}

public function processFileUpload($file)
{
$dtoData = [
'category' => Category::CATEGORY_LICENSING,
'subCategory' => Category::DOC_SUB_CATEGORY_MESSAGING,
'description' => $file['name'],
'isExternal' => true,
'messagingConversation' => $this->params()->fromRoute('conversationId'),
'correlationId' => $this->getRequest()->getPost('correlationId'),
];

$this->uploadFile($file, $dtoData);
}

public function getUploadedFiles()
{
$params = [
'category' => Category::CATEGORY_LICENSING,
'subCategory' => Category::DOC_SUB_CATEGORY_MESSAGING,
'correlationId' => $this->getRequest()->getPost('correlationId'),
];

$response = $this->handleQuery(Documents::create($params));

return $response->getResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Olcs\Controller\Factory;

use Common\Service\Helper\FileUploadHelperService;
use Common\Service\Helper\FlashMessengerHelperService;
use Common\Service\Helper\FormHelperService;
use Common\Service\Table\TableFactory;
Expand All @@ -27,14 +28,16 @@ public function __invoke(
$tableFactory = $container->get(TableFactory::class);
$formHelperService = $container->get(FormHelperService::class);
$navigationService = $container->get(Navigation::class);
$uploadHolder = $container->get(FileUploadHelperService::class);

return new ConversationsController(
$niTextTranslationUtil,
$authService,
$flashMessengerHelper,
$tableFactory,
$formHelperService,
$navigationService
$navigationService,
$uploadHolder
);
}
}
19 changes: 14 additions & 5 deletions module/Olcs/src/Form/Model/Fieldset/Message/Reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Olcs\Form\Model\Fieldset\Message;

use Common\Form\Elements\InputFilters\ActionButton;
use Common\Form\Model\Fieldset\MultipleFileUpload;
use Laminas\Form\Annotation as Form;
use Laminas\Form\Element\File;
use Laminas\Form\Element\Textarea;
use Laminas\Filter\StringTrim;
use Laminas\Validator\StringLength;

class Reply
{
Expand All @@ -18,12 +20,19 @@ class Reply
* })
* @Form\Options({"label": "You can enter up to 1000 characters"})
* @Form\Required(true)
* @Form\Type(\Laminas\Form\Element\Textarea::class)
* @Form\Filter(\Laminas\Filter\StringTrim::class)
* @Form\Validator(\Laminas\Validator\StringLength::class, options={"min": 5, "max": 1000})
* @Form\Type(Textarea::class)
* @Form\Filter(StringTrim::class)
* @Form\Validator(StringLength::class, options={"min": 5, "max": 1000})
*/
public ?TextArea $reply = null;

/**
* @Form\Name("file")
* @Form\Attributes({"id": "file"})
* @Form\ComposedObject(MultipleFileUpload::class)
*/
public ?MultipleFileUpload $file = null;

/**
* @Form\Attributes({
* "type": "submit",
Expand All @@ -34,7 +43,7 @@ class Reply
* @Form\Options({
* "label": "Send message"
* })
* @Form\Type(\Common\Form\Elements\InputFilters\ActionButton::class)
* @Form\Type(ActionButton::class)
*/
public ?ActionButton $send = null;
}
7 changes: 7 additions & 0 deletions module/Olcs/src/Form/Model/Form/Message/Reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Olcs\Form\Model\Form\Message;

use Common\Form\Model\Fieldset\MultipleFileUpload;
use Laminas\Form\Annotation as Form;
use Laminas\Form\Element\Hidden;
use Olcs\Form\Model\Fieldset\Message\Reply as ReplyFieldset;
Expand All @@ -23,6 +24,12 @@ class Reply
*/
public ?Hidden $id = null;

/**
* @Form\Attributes({"value": ""})
* @Form\Type(\Laminas\Form\Element\Hidden::class)
*/
public ?Hidden $correlationId = null;

/**
* @Form\Attributes({"value": "reply"})
* @Form\Type(\Laminas\Form\Element\Hidden::class)
Expand Down
2 changes: 1 addition & 1 deletion module/Olcs/view/messages-view.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ echo $this->partial(
?>

<?php if ($this->canReply): ?>
<details class="govuk-details" data-module="govuk-details">
<details class="govuk-details" data-module="govuk-details" <?php if ($this->openReply): ?>open<?php endif; ?>>
<summary class="govuk-details__summary" aria-controls="details-content-operating-centre">
<span class="govuk-details__summary-text">
Send a reply
Expand Down
Loading
Loading