Skip to content

Commit b21be30

Browse files
author
Prabhu Ram
committed
- Modified assigncomparelust output to match schema in architecture
- Added validations for list_id of one customer cannot be accessed by other customer - Added item_count - tests pending for above
1 parent 32f251f commit b21be30

File tree

13 files changed

+105
-53
lines changed

13 files changed

+105
-53
lines changed

app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Catalog\Model;
99

1010
use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as CompareListResource;
11+
use Magento\Framework\Exception\LocalizedException;
1112

1213
/**
1314
* CompareListId to MaskedListId resolver
@@ -41,12 +42,17 @@ public function __construct(
4142
*
4243
* @param int $listId
4344
*
45+
* @param int|null $customerId
4446
* @return null|string
47+
* @throws LocalizedException
4548
*/
46-
public function execute(int $listId): ?string
49+
public function execute(int $listId, int $customerId = null): ?string
4750
{
4851
$compareList = $this->compareListFactory->create();
4952
$this->compareListResource->load($compareList, $listId, 'list_id');
53+
if ((int)$compareList->getCustomerId() !== (int)$customerId) {
54+
throw new LocalizedException(__('This customer is not authorized to access this list'));
55+
}
5056
return $compareList->getListIdMask() ?? null;
5157
}
5258
}

app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Catalog\Model;
99

1010
use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as CompareListResource;
11+
use Magento\Framework\Exception\LocalizedException;
1112

1213
/**
1314
* MaskedListId to ListId resolver
@@ -40,14 +41,17 @@ public function __construct(
4041
* Get maskedId by listId
4142
*
4243
* @param string $maskedListId
43-
*
44+
* @param int $customerId
4445
* @return int
46+
* @throws LocalizedException
4547
*/
46-
public function execute(string $maskedListId): int
48+
public function execute(string $maskedListId, int $customerId = null): int
4749
{
4850
$compareList = $this->compareListFactory->create();
4951
$this->compareListResource->load($compareList, $maskedListId, 'list_id_mask');
50-
52+
if ((int)$compareList->getCustomerId() !== (int)$customerId) {
53+
throw new LocalizedException(__('This customer is not authorized to access this list'));
54+
}
5155
return (int)$compareList->getListId();
5256
}
5357
}

app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\CompareListGraphQl\Model\Service\AddToCompareList;
1212
use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId;
1313
use Magento\CompareListGraphQl\Model\Service\GetCompareList;
14+
use Magento\Framework\Exception\LocalizedException;
1415
use Magento\Framework\GraphQl\Config\Element\Field;
1516
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1617
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
@@ -90,23 +91,23 @@ public function resolve(
9091
throw new GraphQlInputException(__('"products" value must be specified.'));
9192
}
9293

93-
$listId = $this->maskedListIdToCompareListId->execute($args['input']['uid']);
94+
try {
95+
$listId = $this->maskedListIdToCompareListId->execute($args['input']['uid'], $context->getUserId());
96+
} catch (LocalizedException $exception) {
97+
throw new GraphQlInputException(__($exception->getMessage()));
98+
}
99+
94100

95101
if (!$listId) {
96102
throw new GraphQlInputException(__('"uid" value does not exist'));
97103
}
98104

99-
if ($userId = $context->getUserId()) {
100-
$customerListId = $this->getListIdByCustomerId->execute($userId);
101-
if ($listId === $customerListId) {
102-
$this->addProductToCompareList->execute($customerListId, $args['input']['products'], $context);
103-
104-
return $this->getCompareList->execute($customerListId, $context);
105-
}
105+
try {
106+
$this->addProductToCompareList->execute($listId, $args['input']['products'], $context);
107+
} catch (\Exception $exception) {
108+
throw new GraphQlInputException(__($exception->getMessage()));
106109
}
107110

108-
$this->addProductToCompareList->execute($listId, $args['input']['products'], $context);
109-
110111
return $this->getCompareList->execute($listId, $context);
111112
}
112113
}

app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Model\MaskedListIdToCompareListId;
1111
use Magento\CompareListGraphQl\Model\Service\Customer\SetCustomerToCompareList;
12+
use Magento\CompareListGraphQl\Model\Service\GetCompareList;
1213
use Magento\Framework\Exception\LocalizedException;
1314
use Magento\Framework\GraphQl\Config\Element\Field;
1415
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -32,16 +33,23 @@ class AssignCompareListToCustomer implements ResolverInterface
3233
*/
3334
private $maskedListIdToCompareListId;
3435

36+
/**
37+
* @var GetCompareList
38+
*/
39+
private $getCompareList;
40+
3541
/**
3642
* @param SetCustomerToCompareList $setCustomerToCompareList
3743
* @param MaskedListIdToCompareListId $maskedListIdToCompareListId
3844
*/
3945
public function __construct(
4046
SetCustomerToCompareList $setCustomerToCompareList,
41-
MaskedListIdToCompareListId $maskedListIdToCompareListId
47+
MaskedListIdToCompareListId $maskedListIdToCompareListId,
48+
GetCompareList $getCompareList
4249
) {
4350
$this->setCustomerToCompareList = $setCustomerToCompareList;
4451
$this->maskedListIdToCompareListId = $maskedListIdToCompareListId;
52+
$this->getCompareList = $getCompareList;
4553
}
4654

4755
/**
@@ -73,19 +81,30 @@ public function resolve(
7381
throw new GraphQlInputException(__('Customer must be logged'));
7482
}
7583

76-
$listId = $this->maskedListIdToCompareListId->execute($args['uid']);
77-
$result = false;
84+
try {
85+
$listId = $this->maskedListIdToCompareListId->execute($args['uid']);
86+
} catch (LocalizedException $exception) {
87+
throw new GraphQlInputException(__($exception->getMessage()));
88+
}
7889

7990
if ($listId) {
8091
try {
8192
$result = $this->setCustomerToCompareList->execute($listId, $context->getUserId());
93+
if ($result) {
94+
return [
95+
'result' => true,
96+
'compare_list' => $this->getCompareList->execute($listId, $context)
97+
];
98+
}
8299
} catch (LocalizedException $exception) {
83100
throw new GraphQlInputException(
84101
__('Something was wrong during assigning customer.')
85102
);
86103
}
87104
}
88105

89-
return $result;
106+
return [
107+
'result' => false
108+
];
90109
}
91110
}

app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Model\MaskedListIdToCompareListId;
1111
use Magento\CompareListGraphQl\Model\Service\GetCompareList;
12+
use Magento\Framework\Exception\LocalizedException;
1213
use Magento\Framework\GraphQl\Config\Element\Field;
1314
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1415
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
@@ -69,7 +70,11 @@ public function resolve(
6970
throw new GraphQlInputException(__('"uid" value must be specified'));
7071
}
7172

72-
$listId = $this->maskedListIdToCompareListId->execute($args['uid']);
73+
try {
74+
$listId = $this->maskedListIdToCompareListId->execute($args['uid'], $context->getUserId());
75+
} catch (LocalizedException $exception) {
76+
throw new GraphQlInputException(__($exception->getMessage()));
77+
}
7378

7479
if (!$listId) {
7580
return null;

app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ public function resolve(
8787
throw new GraphQlInputException(__('"uid" value must be specified'));
8888
}
8989

90-
$listId = $this->maskedListIdToCompareListId->execute($args['uid']);
90+
try {
91+
$listId = $this->maskedListIdToCompareListId->execute($args['uid'], $context->getUserId());
92+
} catch (LocalizedException $exception) {
93+
throw new GraphQlInputException(__($exception->getMessage()));
94+
}
9195
$removed = ['result' => false];
9296

9397
if ($userId = $context->getUserId()) {

app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ public function resolve(
9292
throw new GraphQlInputException(__('"uid" value must be specified.'));
9393
}
9494

95-
$listId = $this->maskedListIdToCompareListId->execute($args['input']['uid']);
95+
try {
96+
$listId = $this->maskedListIdToCompareListId->execute($args['input']['uid'], $context->getUserId());
97+
} catch (LocalizedException $exception) {
98+
throw new GraphQlInputException(__($exception->getMessage()));
99+
}
96100

97101
if (!$listId) {
98102
throw new GraphQlInputException(__('"uid" value does not exist'));

app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function __construct(
5656
* @param ContextInterface $context
5757
*
5858
* @return int
59+
* @throws \Exception
5960
*/
6061
public function execute(int $listId, array $products, ContextInterface $context): int
6162
{

app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,23 @@ public function __construct(
5555
* @param int $listId
5656
* @param int $customerId
5757
*
58-
* @return bool
58+
* @return CompareList
5959
*
6060
* @throws GraphQlAuthenticationException
6161
* @throws GraphQlInputException
6262
* @throws GraphQlNoSuchEntityException
6363
*/
64-
public function execute(int $listId, int $customerId): bool
64+
public function execute(int $listId, int $customerId): ?CompareList
6565
{
6666
if ($this->validateCustomer->execute($customerId)) {
6767
/** @var CompareList $compareListModel */
6868
$compareList = $this->compareListFactory->create();
6969
$this->resourceCompareList->load($compareList, $listId, 'list_id');
7070
$compareList->setCustomerId($customerId);
7171
$this->resourceCompareList->save($compareList);
72-
return true;
72+
return $compareList;
7373
}
7474

75-
return false;
75+
return null;
7676
}
7777
}

app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
namespace Magento\CompareListGraphQl\Model\Service;
99

1010
use Magento\Catalog\Block\Product\Compare\ListCompare;
11-
use Magento\Catalog\Model\CompareListIdToMaskedListId;
1211
use Magento\Catalog\Model\Product;
1312
use Magento\Catalog\Model\ProductRepository;
1413
use Magento\CompareListGraphQl\Model\Service\Collection\GetComparableItemsCollection as ComparableItemsCollection;
1514
use Magento\Framework\Exception\LocalizedException;
1615
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1716
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
18-
use Magento\Store\Api\Data\StoreInterface;
1917

2018
/**
2119
* Get comparable products
@@ -37,27 +35,19 @@ class GetComparableItems
3735
*/
3836
private $productRepository;
3937

40-
/**
41-
* @var CompareListIdToMaskedListId
42-
*/
43-
private $compareListIdToMaskedListId;
44-
4538
/**
4639
* @param ListCompare $listCompare
4740
* @param ComparableItemsCollection $comparableItemsCollection
4841
* @param ProductRepository $productRepository
49-
* @param CompareListIdToMaskedListId $compareListIdToMaskedListId
5042
*/
5143
public function __construct(
5244
ListCompare $listCompare,
5345
ComparableItemsCollection $comparableItemsCollection,
54-
ProductRepository $productRepository,
55-
CompareListIdToMaskedListId $compareListIdToMaskedListId
46+
ProductRepository $productRepository
5647
) {
5748
$this->blockListCompare = $listCompare;
5849
$this->comparableItemsCollection = $comparableItemsCollection;
5950
$this->productRepository = $productRepository;
60-
$this->compareListIdToMaskedListId = $compareListIdToMaskedListId;
6151
}
6252

6353
/**
@@ -72,11 +62,10 @@ public function __construct(
7262
public function execute(int $listId, ContextInterface $context)
7363
{
7464
$items = [];
75-
$maskedListId = $this->compareListIdToMaskedListId->execute($listId);
7665
foreach ($this->comparableItemsCollection->execute($listId, $context) as $item) {
7766
/** @var Product $item */
7867
$items[] = [
79-
'uid' => $maskedListId,
68+
'uid' => $item->getId(),
8069
'product' => $this->getProductData((int)$item->getId()),
8170
'attributes' => $this->getProductComparableAttributes($listId, $item, $context)
8271
];

0 commit comments

Comments
 (0)