From 6a2a6402d6382c3475532b7232ae35a493fdaebf Mon Sep 17 00:00:00 2001 From: James Wragg <5068769+jerotire@users.noreply.github.com> Date: Tue, 19 Mar 2024 09:30:40 +0000 Subject: [PATCH] feat: Automatically closed tasks when conversation closed (dvsa/olcs-backend#115) --- .../Messaging/Conversation/Close.php | 26 +++--- .../Messaging/Conversation/Close.php | 80 +++++++++++++++++++ 2 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 app/api/test/module/Api/src/Domain/CommandHandler/Messaging/Conversation/Close.php diff --git a/app/api/module/Api/src/Domain/CommandHandler/Messaging/Conversation/Close.php b/app/api/module/Api/src/Domain/CommandHandler/Messaging/Conversation/Close.php index bcbf6deae3..f64890f209 100644 --- a/app/api/module/Api/src/Domain/CommandHandler/Messaging/Conversation/Close.php +++ b/app/api/module/Api/src/Domain/CommandHandler/Messaging/Conversation/Close.php @@ -8,41 +8,47 @@ use Dvsa\Olcs\Api\Domain\Command\Messaging\Conversation\StoreSnapshot; use Dvsa\Olcs\Api\Domain\Command\Result; use Dvsa\Olcs\Api\Domain\CommandHandler\AbstractUserCommandHandler; +use Dvsa\Olcs\Api\Domain\Exception\RuntimeException; use Dvsa\Olcs\Api\Domain\ToggleAwareTrait; use Dvsa\Olcs\Api\Domain\ToggleRequiredInterface; +use Dvsa\Olcs\Api\Domain\Repository; use Dvsa\Olcs\Api\Entity\Messaging\MessagingConversation; use Dvsa\Olcs\Api\Entity\System\FeatureToggle; use Dvsa\Olcs\Transfer\Command\CommandInterface; +use Dvsa\Olcs\Transfer\Command\Task\CloseTasks; -/** - * Close a conversation - * - * @author Wade Womersley - */ final class Close extends AbstractUserCommandHandler implements ToggleRequiredInterface { use ToggleAwareTrait; - protected $repoServiceName = 'Conversation'; - protected $toggleConfig = [FeatureToggle::MESSAGING]; + protected $toggleConfig = [ + FeatureToggle::MESSAGING, + ]; + protected $extraRepos = [ + Repository\Conversation::class, + ]; /** * Close Command Handler Abstract + * @throws RuntimeException */ public function handleCommand(CommandInterface $command): Result { /** @var MessagingConversation $conversation */ - $conversation = $this->getRepo()->fetchUsingId($command); + $conversation = $this->getRepo(Repository\Conversation::class)->fetchUsingId($command); $conversation->setIsClosed(true); - $this->getRepo()->save($conversation); + $this->getRepo(Repository\Conversation::class)->save($conversation); $result = new Result(); $result->addId('conversation', $conversation->getId()); $result->addMessage('Conversation closed'); $documentResult = $this->handleSideEffect(StoreSnapshot::create(['id' => $conversation->getId()])); - $result->merge($documentResult); + + $taskResult = $this->handleSideEffect(CloseTasks::create(['ids' => [$conversation->getTask()->getId()]])); + $result->merge($taskResult); + $result->merge( $this->handleSideEffect( CreateCorrespondenceRecord::create( diff --git a/app/api/test/module/Api/src/Domain/CommandHandler/Messaging/Conversation/Close.php b/app/api/test/module/Api/src/Domain/CommandHandler/Messaging/Conversation/Close.php new file mode 100644 index 0000000000..59a82b96e1 --- /dev/null +++ b/app/api/test/module/Api/src/Domain/CommandHandler/Messaging/Conversation/Close.php @@ -0,0 +1,80 @@ +sut = new CloseConversationHandler(); + $this->mockRepo(Repository\Conversation::class, Repository\Conversation::class); + + $this->mockedSmServices = [ + AuthorizationService::class => m::mock(AuthorizationService::class), + ]; + + $defaultMockTask = m::mock(Entity\Task\Task::class)->makePartial()->allows('getId')->getMock(); + $defaultMockConversation = m::mock(Entity\Messaging\MessagingConversation::class)->makePartial()->allows('getTask')->andReturn($defaultMockTask)->getMock()->allows('getRelatedLicence')->getMock(); + $this->repoMap[Repository\Conversation::class]->allows('fetchUsingId')->andReturn($defaultMockConversation)->byDefault(); + $this->repoMap[Repository\Conversation::class]->allows('save')->byDefault(); + + parent::setUp(); + + $this->commandHandler->allows('handleCommand')->andReturn(new Result())->byDefault(); + } + + public function testHandleMarksConversationAsClosed() + { + $command = CloseConversationCommand::create($commandParameters = ['id' => 1]); + + $this->repoMap[Repository\Conversation::class]->expects('save')->with(m::on(function ($conversation) { + $this->assertTrue($conversation->getIsClosed()); + return true; + })); + + $this->sut->handleCommand($command); + } + + public function testHandleMarksTaskAsClosed() + { + $command = CloseConversationCommand::create($commandParameters = ['id' => 1]); + + $this->expectedSideEffect(CloseTasks::class, [], new Result(), 1); + + $this->sut->handleCommand($command); + } + + public function testHandleGeneratesAndStoresSnapshot() + { + $command = CloseConversationCommand::create($commandParameters = ['id' => 1]); + + $this->expectedSideEffect(StoreSnapshot::class, [], new Result(), 1); + + $this->sut->handleCommand($command); + } + + public function testHandleCreatesCorrespondenceRecord() + { + $command = CloseConversationCommand::create($commandParameters = ['id' => 1]); + + $this->expectedSideEffect(CreateCorrespondenceRecord::class, [], new Result(), 1); + + $this->sut->handleCommand($command); + } +}