Skip to content

Commit

Permalink
Added preview link generation to MailTemplateListApiHandler
Browse files Browse the repository at this point in the history
remp/crm#1626
+ Changed mailer template proxy redirect to use `code` instead of `id`
  • Loading branch information
Adam Zoldak committed Jan 11, 2021
1 parent 381fed6 commit 3686ab0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
16 changes: 14 additions & 2 deletions src/Api/MailTemplateListApiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
use Crm\ApiModule\Api\JsonResponse;
use Crm\ApiModule\Authorization\ApiAuthorizationInterface;
use Crm\RempMailerModule\Repositories\MailTemplatesRepository;
use Nette\Application\LinkGenerator;
use Nette\Http\Response;

class MailTemplateListApiHandler extends ApiHandler
{
private $mailTemplatesRepository;

private $linkGenerator;

private $allowedMailTypeCodes = [];

public function __construct(MailTemplatesRepository $mailTemplatesRepository)
{
public function __construct(
MailTemplatesRepository $mailTemplatesRepository,
LinkGenerator $linkGenerator
) {
$this->mailTemplatesRepository = $mailTemplatesRepository;
$this->linkGenerator = $linkGenerator;
}

public function params()
Expand All @@ -40,6 +46,12 @@ public function handle(ApiAuthorizationInterface $authorization)
$results[] = [
'code' => $mailTemplate->code,
'name' => $mailTemplate->name,
'link' => $this->linkGenerator->link(
'RempMailer:MailTemplatesAdmin:show',
[
'code' => $mailTemplate->code,
]
),
'description' => $mailTemplate->description ?? "",
'mail_type' => [
'code' => $mailTemplate->mail_type->code,
Expand Down
4 changes: 2 additions & 2 deletions src/Components/MailLogs/mail_logs.latte
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@
<small class="text-muted">{$email->sent_at|userDate}</small>
</td>
<td>
<a href="{plink :RempMailer:MailTemplatesAdmin:show $email->mail_template->id}">{$email->subject}</a>
<a href="{plink :RempMailer:MailTemplatesAdmin:show $email->mail_template->code}">{$email->subject}</a>
<span n:if="$email->attachment_size" class="badge badge-default">{$email->attachment_size|bytes} attachment</span>
</td>
<td><a href="{plink :Users:UsersAdmin:default text => $email->email}">{$email->email}</a></td>
<td>
<a n:if="$email->mail_template->id" href="{plink :RempMailer:MailTemplatesAdmin:Show $email->mail_template->id}">{$email->mail_template->name}</a>
<a n:if="$email->mail_template->id" href="{plink :RempMailer:MailTemplatesAdmin:Show $email->mail_template->code}">{$email->mail_template->name}</a>
<code class="muted" n:if="$email->mail_template->id"><small>{$email->mail_template->code}</small></code>
</td>
<td>
Expand Down
4 changes: 2 additions & 2 deletions src/Presenters/MailTemplatesAdminPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function __construct(Config $config)
$this->config = $config;
}

public function actionShow($id)
public function actionShow($code)
{
$this->redirectUrl("{$this->config->getHost()}/template/show/{$id}");
$this->redirectUrl("{$this->config->getHost()}/template/show-by-code/{$code}");
}
}
30 changes: 27 additions & 3 deletions src/Tests/MailTemplateListApiHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,35 @@
namespace Crm\RempMailerModule\Tests;

use Crm\ApiModule\Authorization\NoAuthorization;
use Crm\ApplicationModule\Tests\DatabaseTestCase;
use Crm\RempMailerModule\Api\MailTemplateListApiHandler;
use Crm\RempMailerModule\Models\Api\Client;
use Crm\RempMailerModule\Repositories\MailTemplatesRepository;
use Nette\Application\LinkGenerator;
use Nette\Http\Response;
use PHPUnit\Framework\TestCase;

class MailTemplateListApiHandlerTest extends TestCase
class MailTemplateListApiHandlerTest extends DatabaseTestCase
{
/** @var LinkGenerator */
private $linkGenerator;

protected function requiredRepositories(): array
{
return [];
}

protected function requiredSeeders(): array
{
return [];
}

public function setUp(): void
{
parent::setUp();

$this->linkGenerator = $this->inject(LinkGenerator::class);
}

public function testListing()
{
$mailTemplate = (object)[
Expand All @@ -30,7 +51,10 @@ public function testListing()
->andReturn([$mailTemplate])
->getMock();

$mailTemplateListApiHandler = new MailTemplateListApiHandler(new MailTemplatesRepository($client));
$mailTemplateListApiHandler = new MailTemplateListApiHandler(
new MailTemplatesRepository($client),
$this->linkGenerator
);
$mailTemplateListApiHandler->addAllowedMailTypeCodes('test_templates');
$response = $mailTemplateListApiHandler->handle(new NoAuthorization());
$this->assertEquals(Response::S200_OK, $response->getHttpCode());
Expand Down

0 comments on commit 3686ab0

Please sign in to comment.