Add convert lead action

- Add record action handler
- Add action configuration
- Add unit test
This commit is contained in:
Clemente Raposo 2021-03-18 18:01:25 +00:00 committed by Dillon-Brown
parent 29ac297ab2
commit 4e78b1b8a6
3 changed files with 205 additions and 0 deletions

View file

@ -0,0 +1,29 @@
<?php
use Symfony\Component\DependencyInjection\Container;
if (!isset($container)) {
return;
}
/** @var Container $container */
$actions = $container->getParameter('module.recordview.actions');
if (!isset($actions['modules']['leads'])) {
$actions['modules']['leads'] = [];
}
if (!isset($actions['modules']['leads']['actions'])) {
$actions['modules']['leads']['actions'] = [];
}
$actions['modules']['leads']['actions']['convert-lead'] = [
'key' => 'convert-lead',
'labelKey' => 'LBL_CONVERTLEAD',
'asyncProcess' => true,
'params' => [],
'modes' => ['detail'],
'acl' => ['edit'],
];
$container->setParameter('module.recordview.actions', $actions);

View file

@ -0,0 +1,97 @@
<?php
namespace App\Service\RecordActions;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use App\Entity\Process;
use App\Service\ModuleNameMapperInterface;
use App\Service\ProcessHandlerInterface;
class ConvertLeadAction implements ProcessHandlerInterface
{
protected const MSG_OPTIONS_NOT_FOUND = 'Process options are not defined';
protected const PROCESS_TYPE = 'record-convert-lead';
/**
* @var ModuleNameMapperInterface
*/
private $moduleNameMapper;
/**
* MergeRecordsBulkAction constructor.
* @param ModuleNameMapperInterface $moduleNameMapper
*/
public function __construct(ModuleNameMapperInterface $moduleNameMapper)
{
$this->moduleNameMapper = $moduleNameMapper;
}
/**
* @inheritDoc
*/
public function getProcessType(): string
{
return self::PROCESS_TYPE;
}
/**
* @inheritDoc
*/
public function requiredAuthRole(): string
{
return 'ROLE_USER';
}
/**
* @inheritDoc
*/
public function configure(Process $process): void
{
//This process is synchronous
//We aren't going to store a record on db
//thus we will use process type as the id
$process->setId(self::PROCESS_TYPE);
$process->setAsync(false);
}
/**
* @inheritDoc
*/
public function validate(Process $process): void
{
if (empty($process->getOptions())) {
throw new InvalidArgumentException(self::MSG_OPTIONS_NOT_FOUND);
}
$options = $process->getOptions();
if (empty($options['module']) || empty($options['action'])) {
throw new InvalidArgumentException(self::MSG_OPTIONS_NOT_FOUND);
}
if (empty($options['id'])) {
throw new InvalidArgumentException(self::MSG_OPTIONS_NOT_FOUND);
}
}
/**
* @inheritDoc
*/
public function run(Process $process)
{
$options = $process->getOptions();
$responseData = [
'handler' => 'redirect',
'params' => [
'route' => $options['module'] . '/convert-lead/' . $options['id'],
'queryParams' => [
]
]
];
$process->setStatus('success');
$process->setMessages([]);
$process->setData($responseData);
}
}

View file

@ -0,0 +1,79 @@
<?php
namespace App\Tests\unit\core\src\Service\RecordActions;
use App\Entity\Process;
use App\Legacy\ModuleNameMapperHandler;
use App\Service\RecordActions\ConvertLeadAction;
use App\Tests\UnitTester;
use Codeception\Test\Unit;
use Exception;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
/**
* Class ConvertLeadActionTest
* @package App\Tests
*/
class ConvertLeadActionTest extends Unit
{
/**
* @var UnitTester
*/
protected $tester;
/**
* @var ConvertLeadAction
*/
protected $service;
/**
* Test Convert redirect Info
*/
public function testConvertRedirectInfo(): void
{
$process = new Process();
$process->setType('record-convert-lead');
$process->setOptions([
'action' => 'record-convert-lead',
'id' => 'e24be4e0-4424-9728-9294-5ea315eef53e',
'module' => 'leads'
]);
$this->service->run($process);
static::assertSame([
'handler' => 'redirect',
'params' => [
'route' => 'leads/convert-lead/e24be4e0-4424-9728-9294-5ea315eef53e',
'queryParams' => [
]
]
], $process->getData());
static::assertEquals('success', $process->getStatus());
static::assertSame([], $process->getMessages());
}
/**
* @throws Exception
*/
protected function _before(): void
{
$session = new Session(new MockArraySessionStorage('PHPSESSID'));
$session->start();
$moduleNameMapper = new ModuleNameMapperHandler(
$this->tester->getProjectDir(),
$this->tester->getLegacyDir(),
$this->tester->getLegacySessionName(),
$this->tester->getDefaultSessionName(),
$this->tester->getLegacyScope(),
$session
);
$this->service = new ConvertLeadAction(
$moduleNameMapper
);
}
}