diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1145462bc..446e06a05 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
## [Unreleased]
+### [Mailer]
+
+- Added the soft delete of mail type variants. remp/crm#2721
+
## [1.1.0] - 2023-01-27
### Project
diff --git a/Mailer/extensions/mailer-module/src/Api/v1/Handlers/Mailers/MailJobCreateApiHandler.php b/Mailer/extensions/mailer-module/src/Api/v1/Handlers/Mailers/MailJobCreateApiHandler.php
index 31da37cd3..349b3b781 100644
--- a/Mailer/extensions/mailer-module/src/Api/v1/Handlers/Mailers/MailJobCreateApiHandler.php
+++ b/Mailer/extensions/mailer-module/src/Api/v1/Handlers/Mailers/MailJobCreateApiHandler.php
@@ -81,7 +81,7 @@ public function handle(array $params): ResponseInterface
$mailTypeVariant = null;
if (isset($params['mail_type_variant_code'])) {
- $mailTypeVariant = $this->listVariantsRepository->findBy('code', ($params['mail_type_variant_code']));
+ $mailTypeVariant = $this->listVariantsRepository->findByCode($params['mail_type_variant_code']);
if (!$mailTypeVariant) {
return new JsonApiResponse(IResponse::S404_NotFound, [
'status' => 'error',
diff --git a/Mailer/extensions/mailer-module/src/Api/v1/Handlers/Mailers/MailTypesListingHandler.php b/Mailer/extensions/mailer-module/src/Api/v1/Handlers/Mailers/MailTypesListingHandler.php
index 25334fd08..e6c5197fd 100644
--- a/Mailer/extensions/mailer-module/src/Api/v1/Handlers/Mailers/MailTypesListingHandler.php
+++ b/Mailer/extensions/mailer-module/src/Api/v1/Handlers/Mailers/MailTypesListingHandler.php
@@ -6,6 +6,7 @@
use Nette\Http\IResponse;
use Remp\MailerModule\Api\JsonValidationTrait;
use Remp\MailerModule\Repositories\ListsRepository;
+use Remp\MailerModule\Repositories\ListVariantsRepository;
use Tomaj\NetteApi\Handlers\BaseHandler;
use Tomaj\NetteApi\Params\GetInputParam;
use Tomaj\NetteApi\Response\JsonApiResponse;
@@ -13,14 +14,12 @@
class MailTypesListingHandler extends BaseHandler
{
- private $listsRepository;
use JsonValidationTrait;
public function __construct(
- ListsRepository $listsRepository
+ private ListsRepository $listsRepository,
+ private ListVariantsRepository $listVariantsRepository
) {
- parent::__construct();
- $this->listsRepository = $listsRepository;
}
public function params(): array
@@ -61,7 +60,7 @@ public function handle(array $params): ResponseInterface
$item->locked = (bool) $row->locked;
$item->is_multi_variant = (bool) $row->is_multi_variant;
$item->sorting = $row->sorting;
- $item->variants = $row->related('mail_type_variants.mail_type_id')->order('sorting')->fetchPairs('id', 'title');
+ $item->variants = $this->listVariantsRepository->getVariantsForType($row)->order('sorting')->fetchPairs('id', 'title');
$output[] = $item;
}
diff --git a/Mailer/extensions/mailer-module/src/Api/v2/Handlers/Mailers/MailTypesListingHandler.php b/Mailer/extensions/mailer-module/src/Api/v2/Handlers/Mailers/MailTypesListingHandler.php
index eab8f143e..df1c164f9 100644
--- a/Mailer/extensions/mailer-module/src/Api/v2/Handlers/Mailers/MailTypesListingHandler.php
+++ b/Mailer/extensions/mailer-module/src/Api/v2/Handlers/Mailers/MailTypesListingHandler.php
@@ -6,6 +6,7 @@
use Nette\Http\IResponse;
use Remp\MailerModule\Api\JsonValidationTrait;
use Remp\MailerModule\Repositories\ListsRepository;
+use Remp\MailerModule\Repositories\ListVariantsRepository;
use Tomaj\NetteApi\Handlers\BaseHandler;
use Tomaj\NetteApi\Params\GetInputParam;
use Tomaj\NetteApi\Response\JsonApiResponse;
@@ -13,14 +14,12 @@
class MailTypesListingHandler extends BaseHandler
{
- private $listsRepository;
use JsonValidationTrait;
public function __construct(
- ListsRepository $listsRepository
+ private ListsRepository $listsRepository,
+ private ListVariantsRepository $listVariantsRepository
) {
- parent::__construct();
- $this->listsRepository = $listsRepository;
}
public function params(): array
@@ -65,7 +64,7 @@ public function handle(array $params): ResponseInterface
$item->locked = (bool) $row->locked;
$item->is_multi_variant = (bool) $row->is_multi_variant;
$item->sorting = $row->sorting;
- $item->variants = $row->related('mail_type_variants.mail_type_id')->order('sorting')->fetchPairs('id', 'title');
+ $item->variants = $this->listVariantsRepository->getVariantsForType($row)->order('sorting')->fetchPairs('id', 'title');
$output[] = $item;
}
diff --git a/Mailer/extensions/mailer-module/src/Presenters/templates/List/show.latte b/Mailer/extensions/mailer-module/src/Presenters/templates/List/show.latte
index 370f4ea0d..89df6eac9 100644
--- a/Mailer/extensions/mailer-module/src/Presenters/templates/List/show.latte
+++ b/Mailer/extensions/mailer-module/src/Presenters/templates/List/show.latte
@@ -136,7 +136,7 @@
-{var $hasVariantsTab = $list->related('mail_type_variants')->count('*')}
+{var $hasVariantsTab = $list->related('mail_type_variants')->where('deleted_at', null)->count('*')}
diff --git a/Mailer/extensions/mailer-module/src/Repositories/ListVariantsRepository.php b/Mailer/extensions/mailer-module/src/Repositories/ListVariantsRepository.php
index 6862299d2..72169226c 100644
--- a/Mailer/extensions/mailer-module/src/Repositories/ListVariantsRepository.php
+++ b/Mailer/extensions/mailer-module/src/Repositories/ListVariantsRepository.php
@@ -28,7 +28,10 @@ public function add(ActiveRow $mailType, string $title, string $code, int $sorti
private function getNextSorting(ActiveRow $mailType): int
{
- $row = $this->getTable()->where(['mail_type_id' => $mailType->id])->order('sorting DESC')->limit(1)->fetch();
+ $row = $this->getTable()->where([
+ 'mail_type_id' => $mailType->id,
+ 'deleted_at' => null
+ ])->order('sorting DESC')->limit(1)->fetch();
if ($row) {
return $row->sorting + 100;
}
@@ -37,18 +40,29 @@ private function getNextSorting(ActiveRow $mailType): int
public function findByIdAndMailTypeId(int $id, int $mailTypeId): ?ActiveRow
{
- return $this->getTable()->where(['id' => $id, 'mail_type_id' => $mailTypeId])->fetch();
+ return $this->getTable()->where(['id' => $id, 'mail_type_id' => $mailTypeId, 'deleted_at' => null])->fetch();
+ }
+
+ public function findByCode(string $code): ?ActiveRow
+ {
+ return $this->getTable()->where(['code' => $code, 'deleted_at' => null])->fetch();
}
public function findByCodeAndMailTypeId(string $code, int $mailTypeId): ?ActiveRow
{
- return $this->getTable()->where(['code' => $code, 'mail_type_id' => $mailTypeId])->fetch();
+ return $this->getTable()->where(['code' => $code, 'mail_type_id' => $mailTypeId, 'deleted_at' => null])->fetch();
+ }
+
+ public function getVariantsForType(ActiveRow $mailType): Selection
+ {
+ return $this->getTable()->where('mail_type_id', $mailType->id)->where('deleted_at', null);
}
public function tableFilter(string $query, string $order, string $orderDirection, ?array $listIds = null, ?int $limit = null, ?int $offset = null): Selection
{
$selection = $this->getTable()
->select('mail_type_variants.*, COUNT(:mail_user_subscription_variants.id) AS count')
+ ->where('deleted_at', null)
->group('mail_type_variants.id');
if ($order === 'count') {
@@ -77,4 +91,9 @@ public function tableFilter(string $query, string $order, string $orderDirection
return $selection;
}
+
+ public function softDelete(ActiveRow $mailTypeVariant): bool
+ {
+ return parent::update($mailTypeVariant, ['deleted_at' => new DateTime()]);
+ }
}
diff --git a/Mailer/extensions/mailer-module/src/Tests/Feature/Api/v1/Handlers/Mailers/MailJobCreateApiHandlerTest.php b/Mailer/extensions/mailer-module/src/Tests/Feature/Api/v1/Handlers/Mailers/MailJobCreateApiHandlerTest.php
index 21aace834..11f052d9c 100644
--- a/Mailer/extensions/mailer-module/src/Tests/Feature/Api/v1/Handlers/Mailers/MailJobCreateApiHandlerTest.php
+++ b/Mailer/extensions/mailer-module/src/Tests/Feature/Api/v1/Handlers/Mailers/MailJobCreateApiHandlerTest.php
@@ -106,6 +106,22 @@ public function testInvalidMailTypeVariant()
$this->assertEquals('error', $response->getPayload()['status']);
}
+ public function testDeletedMailTypeVariant()
+ {
+ $params = $this->getDefaultParams();
+
+ $mailTypeVariantCode = $params['mail_type_variant_code'];
+ $mailTypeVariant = $this->listVariantsRepository->findByCode($mailTypeVariantCode);
+
+ $this->listVariantsRepository->softDelete($mailTypeVariant);
+
+ /** @var JsonApiResponse $response */
+ $response = $this->handler->handle($params);
+ $this->assertInstanceOf(JsonApiResponse::class, $response);
+ $this->assertEquals(404, $response->getCode());
+ $this->assertEquals('error', $response->getPayload()['status']);
+ }
+
private function getDefaultParams()
{
$mailType = $this->createMailTypeWithCategory(
diff --git a/Mailer/extensions/mailer-module/src/Tests/Feature/Api/v1/Handlers/Mailers/MailTypesListingHandlerTest.php b/Mailer/extensions/mailer-module/src/Tests/Feature/Api/v1/Handlers/Mailers/MailTypesListingHandlerTest.php
index 87f348c6b..9ceb119e7 100644
--- a/Mailer/extensions/mailer-module/src/Tests/Feature/Api/v1/Handlers/Mailers/MailTypesListingHandlerTest.php
+++ b/Mailer/extensions/mailer-module/src/Tests/Feature/Api/v1/Handlers/Mailers/MailTypesListingHandlerTest.php
@@ -52,4 +52,31 @@ public function testListWithUnknownCode()
$this->assertInstanceOf(\Tomaj\NetteApi\Response\JsonApiResponse::class, $response);
$this->assertCount(0, $response->getPayload()['data']);
}
+
+ public function testListWithVariants()
+ {
+ $mailType = $this->createMailTypeWithCategory("category1", "code1", "name1");
+
+ $mailTypeVariant1 = $this->createMailTypeVariant($mailType, 'test1');
+ $mailTypeVariant2 = $this->createMailTypeVariant($mailType, 'test2');
+
+ $params = [];
+ $handler = $this->getHandler(MailTypesListingHandler::class);
+ $response = $handler->handle($params);
+
+ $this->assertInstanceOf(\Tomaj\NetteApi\Response\JsonApiResponse::class, $response);
+ $this->assertCount(2, $response->getPayload()['data'][0]->variants);
+
+ $this->listVariantsRepository->softDelete($mailTypeVariant1);
+
+ $handler = $this->getHandler(MailTypesListingHandler::class);
+ /** @var \Tomaj\NetteApi\Response\JsonApiResponse $response */
+ $response = $handler->handle($params);
+
+ $data = $response->getPayload()['data'];
+
+ $this->assertInstanceOf(\Tomaj\NetteApi\Response\JsonApiResponse::class, $response);
+ $this->assertCount(1, $data[0]->variants);
+ $this->assertEquals([$mailTypeVariant2->id => $mailTypeVariant2->title], $data[0]->variants);
+ }
}
diff --git a/Mailer/extensions/mailer-module/src/Tests/Feature/Api/v2/Mailers/MailTypesListingHandlerTest.php b/Mailer/extensions/mailer-module/src/Tests/Feature/Api/v2/Mailers/MailTypesListingHandlerTest.php
index 5dff4119f..f1be1ff00 100644
--- a/Mailer/extensions/mailer-module/src/Tests/Feature/Api/v2/Mailers/MailTypesListingHandlerTest.php
+++ b/Mailer/extensions/mailer-module/src/Tests/Feature/Api/v2/Mailers/MailTypesListingHandlerTest.php
@@ -72,6 +72,33 @@ public function testListPublicByCategoryCode()
$this->assertCount(0, $response->getPayload()['data']);
}
+ public function testListWithVariants()
+ {
+ $mailType = $this->createMailTypeWithCategory("category1", "code1", "name1");
+
+ $mailTypeVariant1 = $this->createMailTypeVariant($mailType, 'test1');
+ $mailTypeVariant2 = $this->createMailTypeVariant($mailType, 'test2');
+
+ $params = [];
+ $handler = $this->getHandler(\Remp\MailerModule\Api\v1\Handlers\Mailers\MailTypesListingHandler::class);
+ $response = $handler->handle($params);
+
+ $this->assertInstanceOf(\Tomaj\NetteApi\Response\JsonApiResponse::class, $response);
+ $this->assertCount(2, $response->getPayload()['data'][0]->variants);
+
+ $this->listVariantsRepository->softDelete($mailTypeVariant1);
+
+ $handler = $this->getHandler(MailTypesListingHandler::class);
+ /** @var \Tomaj\NetteApi\Response\JsonApiResponse $response */
+ $response = $handler->handle($params);
+
+ $data = $response->getPayload()['data'];
+
+ $this->assertInstanceOf(\Tomaj\NetteApi\Response\JsonApiResponse::class, $response);
+ $this->assertCount(1, $data[0]->variants);
+ $this->assertEquals([$mailTypeVariant2->id => $mailTypeVariant2->title], $data[0]->variants);
+ }
+
private function createMailTypes()
{
$this->createMailTypeWithCategory("category1", "code1", "name1", true);
diff --git a/Mailer/extensions/mailer-module/src/migrations/20230123135856_add_deleted_at_into_mail_type_variants.php b/Mailer/extensions/mailer-module/src/migrations/20230123135856_add_deleted_at_into_mail_type_variants.php
new file mode 100644
index 000000000..69a5a2e71
--- /dev/null
+++ b/Mailer/extensions/mailer-module/src/migrations/20230123135856_add_deleted_at_into_mail_type_variants.php
@@ -0,0 +1,14 @@
+table('mail_type_variants')
+ ->addColumn('deleted_at', 'datetime', ['default' => null, 'null' => true, 'after' => 'created_at'])
+ ->update();
+ }
+}
\ No newline at end of file