Skip to content

Commit 71d43a9

Browse files
authored
LYNX-523: GraphQL: Provide available order actions
1 parent 5eb555d commit 71d43a9

File tree

10 files changed

+441
-0
lines changed

10 files changed

+441
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\OrderCancellationGraphQl\Model;
18+
19+
use Magento\OrderCancellation\Model\Config\Config;
20+
use Magento\OrderCancellation\Model\CustomerCanCancel;
21+
use Magento\SalesGraphQl\Api\OrderAvailableActionProviderInterface;
22+
use Magento\Sales\Model\Order;
23+
24+
class GetOrderCancellationAvailableActions implements OrderAvailableActionProviderInterface
25+
{
26+
/**
27+
* @param CustomerCanCancel $customerCanCancel
28+
* @param Config $config
29+
*/
30+
public function __construct(
31+
private readonly CustomerCanCancel $customerCanCancel,
32+
private readonly Config $config
33+
) {
34+
}
35+
36+
/**
37+
* Get cancel available action
38+
*
39+
* @param Order $order
40+
* @return array|string[]
41+
*/
42+
public function execute(Order $order): array
43+
{
44+
if ($this->config->isOrderCancellationEnabledForStore((int)$order->getStoreId())
45+
&& $this->customerCanCancel->execute($order)
46+
) {
47+
return ['CANCEL'];
48+
}
49+
return [];
50+
}
51+
}

app/code/Magento/OrderCancellationGraphQl/etc/graphql/di.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,13 @@
1414
</argument>
1515
</arguments>
1616
</type>
17+
<preference for="Magento\SalesGraphQl\Api\OrderAvailableActionProviderInterface"
18+
type="Magento\OrderCancellationGraphQl\Model\GetOrderCancellationAvailableActions" />
19+
<type name="Magento\SalesGraphQl\Model\GetOrderAvailableActions">
20+
<arguments>
21+
<argument name="actions" xsi:type="array">
22+
<item name="cancel_action" xsi:type="object">Magento\OrderCancellationGraphQl\Model\GetOrderCancellationAvailableActions</item>
23+
</argument>
24+
</arguments>
25+
</type>
1726
</config>

app/code/Magento/OrderCancellationGraphQl/etc/schema.graphqls

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ type CancelOrderOutput @doc(description: "Contains the updated customer order an
2222
error: String @doc(description: "Error encountered while cancelling the order.")
2323
order: CustomerOrder @doc(description: "Updated customer order.")
2424
}
25+
26+
enum OrderActionType @doc(description: "The list of available order actions.") {
27+
CANCEL
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\SalesGraphQl\Api;
18+
19+
interface OrderAvailableActionProviderInterface
20+
{
21+
/**
22+
* Get available order action
23+
*
24+
* @param \Magento\Sales\Model\Order $order
25+
* @return array
26+
*/
27+
public function execute(\Magento\Sales\Model\Order $order): array;
28+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\SalesGraphQl\Model;
18+
19+
use Magento\SalesGraphQl\Api\OrderAvailableActionProviderInterface;
20+
21+
class GetOrderAvailableActions
22+
{
23+
/**
24+
* @param OrderAvailableActionProviderInterface[] $actions
25+
*/
26+
public function __construct(
27+
private readonly array $actions = []
28+
) {
29+
}
30+
31+
/**
32+
* Get available order action
33+
*
34+
* @param \Magento\Sales\Model\Order $order
35+
* @return array
36+
*/
37+
public function execute(\Magento\Sales\Model\Order $order): array
38+
{
39+
$availableAction = [];
40+
foreach ($this->actions as $action) {
41+
$availableAction[] = $action->execute($order);
42+
}
43+
return array_merge(...$availableAction);
44+
}
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\SalesGraphQl\Model;
18+
19+
use Magento\SalesGraphQl\Api\OrderAvailableActionProviderInterface;
20+
21+
class GetReorderAvailableActions implements OrderAvailableActionProviderInterface
22+
{
23+
/**
24+
* Get reorder available action
25+
*
26+
* @param \Magento\Sales\Model\Order $order
27+
* @return array|string[]
28+
*/
29+
public function execute(\Magento\Sales\Model\Order $order): array
30+
{
31+
if ($order->canReorder()) {
32+
return ['REORDER'];
33+
}
34+
return [];
35+
}
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\SalesGraphQl\Model\Resolver;
18+
19+
use Magento\Framework\Exception\LocalizedException;
20+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
21+
use Magento\Framework\GraphQl\Config\Element\Field;
22+
use Magento\Framework\GraphQl\Query\ResolverInterface;
23+
use Magento\Sales\Model\Order;
24+
use Magento\SalesGraphQl\Model\GetOrderAvailableActions;
25+
26+
/**
27+
* Resolver for the available_actions in Order
28+
*/
29+
class OrderAvailableActions implements ResolverInterface
30+
{
31+
/**
32+
* @param GetOrderAvailableActions $orderAvailableActionProvider
33+
*/
34+
public function __construct(
35+
private readonly GetOrderAvailableActions $orderAvailableActionProvider
36+
) {
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): array
43+
{
44+
if (!isset($value['model']) && !($value['model'] instanceof Order)) {
45+
throw new LocalizedException(__('"model" value should be specified'));
46+
}
47+
/** @var Order $order */
48+
$order = $value['model'];
49+
50+
return $this->orderAvailableActionProvider->execute($order);
51+
}
52+
}

app/code/Magento/SalesGraphQl/etc/graphql/di.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,13 @@
5252
</argument>
5353
</arguments>
5454
</type>
55+
<preference for="Magento\SalesGraphQl\Api\OrderAvailableActionProviderInterface"
56+
type="Magento\SalesGraphQl\Model\GetReorderAvailableActions" />
57+
<type name="Magento\SalesGraphQl\Model\GetOrderAvailableActions">
58+
<arguments>
59+
<argument name="actions" xsi:type="array">
60+
<item name="is_reorder_available_action" xsi:type="object">Magento\SalesGraphQl\Model\GetReorderAvailableActions</item>
61+
</argument>
62+
</arguments>
63+
</type>
5564
</config>

app/code/Magento/SalesGraphQl/etc/schema.graphqls

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type CustomerOrder @doc(description: "Contains details about each of the custome
8080
applied_coupons: [AppliedCoupon!]! @doc(description: "Coupons applied to the order.")
8181
email: String @doc(description: "Order customer email.")
8282
is_virtual: Boolean! @doc(description: "`TRUE` if the order is virtual") @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\OrderIsVirtual")
83+
available_actions: [OrderActionType!]! @doc(description: "List of available order actions.") @resolver(class: "\\Magento\\SalesGraphQl\\Model\\Resolver\\OrderAvailableActions")
8384
}
8485

8586
type OrderAddress @doc(description: "Contains detailed information about an order's billing and shipping addresses."){
@@ -301,3 +302,7 @@ input OrderInformationInput @doc(description: "Input to retrieve an order based
301302
email: String! @doc(description: "Order billing address email.")
302303
postcode: String! @doc(description: "Order billing address postcode.")
303304
}
305+
306+
enum OrderActionType @doc(description: "The list of available order actions.") {
307+
REORDER
308+
}

0 commit comments

Comments
 (0)