From 49b2cdc57b7535a5ec02a7f7071a9304f81dc101 Mon Sep 17 00:00:00 2001
From: Wade Womersley <155439365+wadedvsa@users.noreply.github.com>
Date: Mon, 19 Feb 2024 13:01:11 +0000
Subject: [PATCH] fix: Disable reply on closed conversations (#61)

* feat: OLCS composer packages to dev-project/messaging (#19)

* feat: Messaging conversation list (#22)

* feat: Messaging conversation list

* fix: Missing route from branch re-create

* fix: Rename messages to conversations

* fix: Unused null check

* feat: Update olcs-comon lock

* feat: List messages in a conversation. (#23)

* feat: Conversation messages list

* feat: Controller view test

* fix: Unused null check

* fix: Test PR changes (case and unused var)

* fix: Just added whitespace!

* feat: Messaging tab visibility (#32)

* Update README.md

* WIP

* Tab WIP

* Messaging tab view based on feature toggle only

* Messaging tab logic without extra query

* Change routing to conversations

* Clean unused

* Review improvement

* Revert whitespace from bad resolution

---------

Co-authored-by: Joshua License <joshua.license@dvsa.gov.uk>
Co-authored-by: Saul Wilcox <saul.wilcox@dvsa.gov.uk>

* feat: Project branch change for composer.lock

* feat: Composer update due to common and transfer merges from main

* fix: Navigation unit test (#36)

* Add messaging feature in NavigationTest

* fix: Add once() assertions to shouldReceive()

* Fix test to match CI error_reporting

* Formatting improvements

---------

Co-authored-by: Saul Wilcox <saul.wilcox@dvsa.gov.uk>

* feat: Message list split in to two columns (#41)

* feat: Message list split in to two columns

* feat: composer.lock

* feat: Start conversation link (#45)

* feat: Start a new conversation link (and empty page)

* fix: PR: Rename newAction to addAction

* feat: Add header to new sidebar for start link (#50)

* feat: Reply to conversation (#49)

* feat: Reply to a conversation

* fix: Strong types on form

* feat: Messsaging new conversation form (#53)

* chore: so far

* chore: so far

* chore: so far

* olcs-transfer bump

* feat: use GOVUK-SELECT class

* chore: code clean-up

* bump: olcs-common

* chore: bump olcs-common and olcs-transfer

* Add "Messaging not disabled" to tab display conditions (#57)

* Add condition for messaging disabled for organisation

* Adjust to match call count

* Add full return to tests

* Whitespace

---------

Co-authored-by: Saul Wilcox <saul.wilcox@dvsa.gov.uk>

* fix: Trying to access array key on null in navigation (#60)

* fix: Disable reply on closed conversations

* fix: Test fix

* fix: Pull composer.json from main

---------

Co-authored-by: Saul Wilcox <1481742+hobbyhacker0@users.noreply.github.com>
Co-authored-by: Joshua License <joshua.license@dvsa.gov.uk>
Co-authored-by: Saul Wilcox <saul.wilcox@dvsa.gov.uk>
Co-authored-by: James Wragg <5068769+jerotire@users.noreply.github.com>
---
 .../Controller/ConversationsController.php    |  8 +++--
 module/Olcs/view/messages-view.phtml          | 29 ++++++++++---------
 .../ConversationsControllerTest.php           |  8 ++++-
 3 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/module/Olcs/src/Controller/ConversationsController.php b/module/Olcs/src/Controller/ConversationsController.php
index 026c9d3fa..9ea7e4665 100644
--- a/module/Olcs/src/Controller/ConversationsController.php
+++ b/module/Olcs/src/Controller/ConversationsController.php
@@ -158,9 +158,12 @@ public function viewAction()
         ];
 
         $response = $this->handleQuery(ByConversationQuery::create($params));
+        $canReply = false;
 
         if ($response->isOk()) {
             $messages = $response->getResult();
+            $canReply = !$messages['extra']['conversation']['isClosed'];
+            unset($messages['extra']);
         } else {
             $this->flashMessengerHelper->addErrorMessage('unknown-error');
             $messages = [];
@@ -174,8 +177,9 @@ public function viewAction()
 
         $view = new ViewModel(
             [
-                'table' => $table,
-                'form'  => $form,
+                'table'    => $table,
+                'form'     => $form,
+                'canReply' => $canReply,
             ],
         );
         $view->setTemplate('messages-view');
diff --git a/module/Olcs/view/messages-view.phtml b/module/Olcs/view/messages-view.phtml
index db5596f43..62ceeac4c 100644
--- a/module/Olcs/view/messages-view.phtml
+++ b/module/Olcs/view/messages-view.phtml
@@ -19,19 +19,22 @@ echo $this->partial(
               ->setMaxDepth(0)
               ->setPartial('partials/tabs-nav');
     ?>
-    <details class="govuk-details" data-module="govuk-details">
-      <summary class="govuk-details__summary" aria-controls="details-content-operating-centre">
-        <span class="govuk-details__summary-text">
-            Send a reply
-        </span>
-      </summary>
-      <div class="govuk-details__text" id="details-content-operating-centre">
-        <?php
-        echo $this->formErrors($this->form);
-        echo $this->form($this->form);
-        ?>
-      </div>
-    </details>
+
+    <?php if ($this->canReply): ?>
+      <details class="govuk-details" data-module="govuk-details">
+        <summary class="govuk-details__summary" aria-controls="details-content-operating-centre">
+          <span class="govuk-details__summary-text">
+              Send a reply
+          </span>
+        </summary>
+        <div class="govuk-details__text" id="details-content-operating-centre">
+          <?php
+          echo $this->formErrors($this->form);
+          echo $this->form($this->form);
+          ?>
+        </div>
+      </details>
+    <?php endif; ?>
     <?php
     echo $this->table;
     ?>
diff --git a/test/Olcs/src/Controller/ConversationsControllerTest.php b/test/Olcs/src/Controller/ConversationsControllerTest.php
index d37c63e44..34da9f68f 100644
--- a/test/Olcs/src/Controller/ConversationsControllerTest.php
+++ b/test/Olcs/src/Controller/ConversationsControllerTest.php
@@ -78,7 +78,13 @@ public function testViewAction(): void
         $mockResponse->shouldReceive('isOk')
                      ->andReturn(true);
         $mockResponse->shouldReceive('getResult')
-                     ->andReturn([]);
+                     ->andReturn([
+                         'extra' => [
+                             'conversation' => [
+                                 'isClosed' => true,
+                             ],
+                         ],
+                     ]);
 
         $mockHandleQuery = m::mock(HandleQuery::class)
                             ->makePartial();