Skip to content

Commit

Permalink
Merge branch 'crm#2721-add-soft-delete-of-mail-type-variants' into 'm…
Browse files Browse the repository at this point in the history
…aster'

Add the soft delete of mail type variants.

See merge request remp/remp!737
  • Loading branch information
lubos-michalik committed Jan 31, 2023
2 parents 247f846 + eebd49a commit e19b29c
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
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;
use Tomaj\NetteApi\Response\ResponseInterface;

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
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
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;
use Tomaj\NetteApi\Response\ResponseInterface;

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
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
</div>
</div>

{var $hasVariantsTab = $list->related('mail_type_variants')->count('*')}
{var $hasVariantsTab = $list->related('mail_type_variants')->where('deleted_at', null)->count('*')}

<div class="row">
<div class="col-md-12">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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') {
Expand Down Expand Up @@ -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()]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class AddDeletedAtIntoMailTypeVariants extends AbstractMigration
{
public function change(): void
{
$this->table('mail_type_variants')
->addColumn('deleted_at', 'datetime', ['default' => null, 'null' => true, 'after' => 'created_at'])
->update();
}
}

0 comments on commit e19b29c

Please sign in to comment.