-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Automatically closed tasks when conversation closed (dvsa/olcs-…
- Loading branch information
Showing
2 changed files
with
96 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
*/ | ||
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( | ||
|
80 changes: 80 additions & 0 deletions
80
app/api/test/module/Api/src/Domain/CommandHandler/Messaging/Conversation/Close.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dvsa\OlcsTest\Api\Domain\CommandHandler\Messaging\Conversation; | ||
|
||
use Dvsa\Olcs\Api\Domain\Command\Email\CreateCorrespondenceRecord; | ||
use Dvsa\Olcs\Api\Domain\Command\Messaging\Conversation\StoreSnapshot; | ||
use Dvsa\Olcs\Api\Domain\Command\Result; | ||
use Dvsa\Olcs\Api\Domain\Command\Task\CreateTask; | ||
use Dvsa\Olcs\Api\Domain\CommandHandler\Messaging\Conversation\Close as CloseConversationHandler; | ||
use Dvsa\Olcs\Api\Domain\Exception\Exception; | ||
use Dvsa\Olcs\Api\Domain\Repository; | ||
use Dvsa\Olcs\Api\Entity; | ||
use Dvsa\Olcs\Transfer\Command\Messaging\Conversation\Close as CloseConversationCommand; | ||
use Dvsa\Olcs\Transfer\Command\Task\CloseTasks; | ||
use Dvsa\OlcsTest\Api\Domain\CommandHandler\CommandHandlerTestCase; | ||
use LmcRbacMvc\Service\AuthorizationService; | ||
use Mockery as m; | ||
|
||
class Close extends CommandHandlerTestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
$this->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); | ||
} | ||
} |