Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Commit

Permalink
bugfix(multimedia) - Relation strategy added for attributes added as …
Browse files Browse the repository at this point in the history
…bindings (#1732)
  • Loading branch information
wiewiurdp authored Nov 17, 2021
1 parent 2c02540 commit 0e7f61e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#### 1.2.0
- performance [#1729](https://github.com/ergonode/backend/issues/1729) Serialization performance upgrade with Closure API (piotrkreft)
- feature [#1724](https://github.com/ergonode/backend/issues/1724) AbstractAggregate isDirty method allowing to check not saved state (piotrkreft)
- bugfix [#1731](https://github.com/ergonode/backend/issues/1731) Relation strategy added for attributes added as bindings (wiewiurdp)
- bugfix [#1699](https://github.com/ergonode/backend/issues/1699) Multimedia stays on storage (wfajczyk)
- bugfix [#1695](https://github.com/ergonode/backend/issues/1695) Bugfix option name validation (wiewiurdp)
- feature [#1687](https://github.com/ergonode/backend/issues/1687) Product availability in segment events (piotrkreft)
Expand Down
4 changes: 4 additions & 0 deletions module/product/features/variable-product-add-from-sku.feature
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ Feature: Variable product
And the JSON node "type" should be equal to "VARIABLE-PRODUCT"
And the JSON node "id" should be equal to "@product_id@"

Scenario: Delete attribute used as a bound attribute
And I send a "DELETE" request to "/api/v1/en_GB/attributes/@attribute_id@"
Then the response status code should be 409

Scenario: Edit product select value in "en_GB" language
When I send a PUT request to "/api/v1/en_GB/products/@simple_product_id@/attribute/@attribute_id@" with body:
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* Copyright © Bold Brand Commerce Sp. z o.o. All rights reserved.
* See LICENSE.txt for license details.
*/

declare(strict_types=1);

namespace Ergonode\Product\Domain\Query;

use Ergonode\SharedKernel\Domain\AggregateId;

interface ProductWithVariantsQueryInterface
{
/**
* @return array
*/
public function findProductIdsWithBoundAttributeByAttributeId(AggregateId $id): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Types\Types;
use Ergonode\Product\Domain\Query\ProductQueryInterface;
use Ergonode\Product\Domain\Query\ProductWithVariantsQueryInterface;
use Ergonode\Product\Domain\ValueObject\Sku;
use Ergonode\SharedKernel\Domain\Aggregate\AttributeId;
use Ergonode\SharedKernel\Domain\Aggregate\CategoryId;
Expand All @@ -22,7 +23,7 @@
use Ramsey\Uuid\Uuid;
use Ergonode\SharedKernel\Domain\Aggregate\TemplateId;

class DbalProductQuery implements ProductQueryInterface
class DbalProductQuery implements ProductQueryInterface, ProductWithVariantsQueryInterface
{
private const PRODUCT_TABLE = 'public.product';
private const VALUE_TRANSLATION_TABLE = 'public.value_translation';
Expand Down Expand Up @@ -453,6 +454,24 @@ public function findAttributeIdsByProductId(ProductId $productId): array
return $result;
}

public function findProductIdsWithBoundAttributeByAttributeId(AggregateId $id): array
{
$qb = $this->connection->createQueryBuilder();
$records = $qb->select('id')
->from(self::PRODUCT_TABLE, 'p')
->join('p', 'public.product_binding', 'ppb', 'p.id = ppb.product_id')
->where($qb->expr()->eq('attribute_id', ':attributeId'))
->setParameter('attributeId', $id->getValue())
->execute()
->fetchAll(\PDO::FETCH_COLUMN);

$result = [];
foreach ($records as $record) {
$result[] = new ProductId($record);
}

return $result;
}
private function getQuery(): QueryBuilder
{
return $this->connection->createQueryBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* Copyright © Bold Brand Commerce Sp. z o.o. All rights reserved.
* See LICENSE.txt for license details.
*/

declare(strict_types=1);

namespace Ergonode\Product\Infrastructure\Strategy\Relationship;

use Ergonode\Core\Infrastructure\Model\RelationshipGroup;
use Ergonode\Core\Infrastructure\Strategy\RelationshipStrategyInterface;
use Ergonode\Product\Domain\Query\ProductWithVariantsQueryInterface;
use Ergonode\SharedKernel\Domain\Aggregate\AttributeId;
use Ergonode\SharedKernel\Domain\AggregateId;
use Webmozart\Assert\Assert;

class ProductWithVariantsAttributeRelationshipStrategy implements RelationshipStrategyInterface
{
private const ONE_MESSAGE = 'This attribute is a binding attribute in product.';
private const MULTIPLE_MESSAGE = 'This attribute is a binding attribute in %count% products.';

private ProductWithVariantsQueryInterface $query;

public function __construct(ProductWithVariantsQueryInterface $query)
{
$this->query = $query;
}

public function supports(AggregateId $id): bool
{
return $id instanceof AttributeId;
}

public function getRelationshipGroup(AggregateId $id): RelationshipGroup
{
Assert::isInstanceOf($id, AttributeId::class);

$relations = $this->query->findProductIdsWithBoundAttributeByAttributeId($id);
$message = count($relations) === 1 ? self::ONE_MESSAGE : self::MULTIPLE_MESSAGE;

return new RelationshipGroup($message, $relations);
}
}

0 comments on commit 0e7f61e

Please sign in to comment.