Skip to content

Commit

Permalink
feat: Automatically closed tasks when conversation closed (dvsa/olcs-…
Browse files Browse the repository at this point in the history
  • Loading branch information
jerotire authored Mar 19, 2024
1 parent b4938e9 commit 6a2a640
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
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);
}
}

0 comments on commit 6a2a640

Please sign in to comment.