Skip to content

Commit 254107c

Browse files
authored
LYNX-568: Request order token for guest order cancellation
1 parent 9bc71c8 commit 254107c

File tree

16 files changed

+496
-374
lines changed

16 files changed

+496
-374
lines changed

app/code/Magento/OrderCancellationGraphQl/Model/CancelOrderGuest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,25 @@
1717
namespace Magento\OrderCancellationGraphQl\Model;
1818

1919
use Magento\Framework\Exception\LocalizedException;
20-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
2120
use Magento\OrderCancellation\Model\Email\ConfirmationKeySender;
2221
use Magento\OrderCancellation\Model\GetConfirmationKey;
23-
use Magento\OrderCancellationGraphQl\Model\Validator\GuestOrder\ValidateRequest;
2422
use Magento\Sales\Api\OrderRepositoryInterface;
2523
use Magento\Sales\Model\Order;
2624
use Magento\SalesGraphQl\Model\Formatter\Order as OrderFormatter;
2725

28-
/**
29-
* Class for Guest order cancellation
30-
*/
3126
class CancelOrderGuest
3227
{
3328
/**
3429
* CancelOrderGuest Constructor
3530
*
3631
* @param OrderFormatter $orderFormatter
3732
* @param OrderRepositoryInterface $orderRepository
38-
* @param ValidateRequest $validateRequest
3933
* @param ConfirmationKeySender $confirmationKeySender
4034
* @param GetConfirmationKey $confirmationKey
4135
*/
4236
public function __construct(
4337
private readonly OrderFormatter $orderFormatter,
4438
private readonly OrderRepositoryInterface $orderRepository,
45-
private readonly ValidateRequest $validateRequest,
4639
private readonly ConfirmationKeySender $confirmationKeySender,
4740
private readonly GetConfirmationKey $confirmationKey,
4841
) {
@@ -54,13 +47,9 @@ public function __construct(
5447
* @param Order $order
5548
* @param array $input
5649
* @return array
57-
* @throws GraphQlInputException
58-
* @throws LocalizedException
5950
*/
6051
public function execute(Order $order, array $input): array
6152
{
62-
$this->validateRequest->validateCancelGuestOrderInput($input);
63-
6453
try {
6554
// send confirmation key and order id
6655
$this->sendConfirmationKeyEmail($order, $input['reason']);

app/code/Magento/OrderCancellationGraphQl/Model/ConfirmCancelOrderGuest.php renamed to app/code/Magento/OrderCancellationGraphQl/Model/ConfirmCancelOrder.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@
1919
use Magento\Framework\Exception\LocalizedException;
2020
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
2121
use Magento\OrderCancellation\Model\CancelOrder as CancelOrderAction;
22-
use Magento\OrderCancellation\Model\ResourceModel\SalesOrderConfirmCancel
23-
as SalesOrderConfirmCancelResourceModel;
22+
use Magento\OrderCancellation\Model\ResourceModel\SalesOrderConfirmCancel as SalesOrderConfirmCancelResourceModel;
2423
use Magento\Sales\Model\Order;
2524
use Magento\SalesGraphQl\Model\Formatter\Order as OrderFormatter;
2625

2726
/**
2827
* Class for Guest order cancellation confirmation
2928
*/
30-
class ConfirmCancelOrderGuest
29+
class ConfirmCancelOrder
3130
{
3231
/**
33-
* ConfirmCancelOrderGuest Constructor
32+
* ConfirmCancelOrder Constructor
3433
*
3534
* @param OrderFormatter $orderFormatter
3635
* @param CancelOrderAction $cancelOrderAction

app/code/Magento/OrderCancellationGraphQl/Model/Resolver/CancelOrder.php

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@
1818

1919
use Magento\Framework\Exception\LocalizedException;
2020
use Magento\Framework\GraphQl\Config\Element\Field;
21+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
2122
use Magento\Framework\GraphQl\Query\ResolverInterface;
2223
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2324
use Magento\OrderCancellation\Model\CancelOrder as CancelOrderAction;
24-
use Magento\OrderCancellation\Model\Config\Config;
25-
use Magento\OrderCancellationGraphQl\Model\CancelOrderGuest;
26-
use Magento\OrderCancellationGraphQl\Model\Validator\ValidateCustomer;
2725
use Magento\OrderCancellationGraphQl\Model\Validator\ValidateOrder;
2826
use Magento\OrderCancellationGraphQl\Model\Validator\ValidateRequest;
29-
use Magento\Sales\Api\Data\OrderInterface;
3027
use Magento\Sales\Api\OrderRepositoryInterface;
3128
use Magento\SalesGraphQl\Model\Formatter\Order as OrderFormatter;
3229

@@ -42,20 +39,14 @@ class CancelOrder implements ResolverInterface
4239
* @param OrderFormatter $orderFormatter
4340
* @param OrderRepositoryInterface $orderRepository
4441
* @param CancelOrderAction $cancelOrderAction
45-
* @param CancelOrderGuest $cancelOrderGuest
4642
* @param ValidateOrder $validateOrder
47-
* @param ValidateCustomer $validateCustomer
48-
* @param Config $config
4943
*/
5044
public function __construct(
5145
private readonly ValidateRequest $validateRequest,
5246
private readonly OrderFormatter $orderFormatter,
5347
private readonly OrderRepositoryInterface $orderRepository,
5448
private readonly CancelOrderAction $cancelOrderAction,
55-
private readonly CancelOrderGuest $cancelOrderGuest,
56-
private readonly ValidateOrder $validateOrder,
57-
private readonly ValidateCustomer $validateCustomer,
58-
private readonly Config $config
49+
private readonly ValidateOrder $validateOrder
5950
) {
6051
}
6152

@@ -69,61 +60,30 @@ public function resolve(
6960
array $value = null,
7061
array $args = null
7162
) {
72-
$this->validateRequest->execute($args['input'] ?? []);
63+
$this->validateRequest->execute($context, $args['input'] ?? []);
7364

7465
try {
7566
$order = $this->orderRepository->get($args['input']['order_id']);
76-
if (!$this->isOrderCancellationEnabled($order)) {
77-
return $this->createErrorResponse('Order cancellation is not enabled for requested store.');
67+
68+
if ((int)$order->getCustomerId() !== $context->getUserId()) {
69+
throw new GraphQlAuthorizationException(__('Current user is not authorized to cancel this order'));
7870
}
7971

8072
$errors = $this->validateOrder->execute($order);
8173
if ($errors) {
8274
return $errors;
8375
}
8476

85-
if ($order->getCustomerIsGuest()) {
86-
return $this->cancelOrderGuest->execute($order, $args['input']);
87-
}
88-
89-
$this->validateCustomer->execute($order, $context);
90-
9177
$order = $this->cancelOrderAction->execute($order, $args['input']['reason']);
9278

9379
return [
9480
'order' => $this->orderFormatter->format($order)
9581
];
96-
} catch (LocalizedException $e) {
97-
return $this->createErrorResponse($e->getMessage());
98-
}
99-
}
10082

101-
/**
102-
* Create error response
103-
*
104-
* @param string $message
105-
* @param OrderInterface|null $order
106-
* @return array
107-
* @throws LocalizedException
108-
*/
109-
private function createErrorResponse(string $message, OrderInterface $order = null): array
110-
{
111-
$response = ['error' => __($message)];
112-
if ($order) {
113-
$response['order'] = $this->orderFormatter->format($order);
83+
} catch (LocalizedException $e) {
84+
return [
85+
'error' => __($e->getMessage())
86+
];
11487
}
115-
116-
return $response;
117-
}
118-
119-
/**
120-
* Check if order cancellation is enabled in config
121-
*
122-
* @param OrderInterface $order
123-
* @return bool
124-
*/
125-
private function isOrderCancellationEnabled(OrderInterface $order): bool
126-
{
127-
return $this->config->isOrderCancellationEnabledForStore((int)$order->getStoreId());
12888
}
12989
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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
13+
* from Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\OrderCancellationGraphQl\Model\Resolver;
18+
19+
use Magento\Framework\Exception\LocalizedException;
20+
use Magento\Framework\GraphQl\Config\Element\Field;
21+
use Magento\Framework\GraphQl\Query\ResolverInterface;
22+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
23+
use Magento\OrderCancellationGraphQl\Model\ConfirmCancelOrder as ConfirmCancelOrderGuest;
24+
use Magento\OrderCancellationGraphQl\Model\Validator\ValidateOrder;
25+
use Magento\OrderCancellationGraphQl\Model\Validator\ValidateConfirmRequest;
26+
use Magento\Sales\Api\OrderRepositoryInterface;
27+
28+
/**
29+
* Cancels a guest order on confirmation
30+
*/
31+
class ConfirmCancelOrder implements ResolverInterface
32+
{
33+
/**
34+
* ConfirmCancelOrder Constructor
35+
*
36+
* @param ValidateConfirmRequest $validateRequest
37+
* @param OrderRepositoryInterface $orderRepository
38+
* @param ConfirmCancelOrderGuest $confirmCancelOrder
39+
* @param ValidateOrder $validateOrder
40+
*/
41+
public function __construct(
42+
private readonly ValidateConfirmRequest $validateRequest,
43+
private readonly OrderRepositoryInterface $orderRepository,
44+
private readonly ConfirmCancelOrderGuest $confirmCancelOrder,
45+
private readonly ValidateOrder $validateOrder
46+
) {
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function resolve(
53+
Field $field,
54+
$context,
55+
ResolveInfo $info,
56+
array $value = null,
57+
array $args = null
58+
): array {
59+
$this->validateRequest->execute($args['input'] ?? []);
60+
61+
try {
62+
$order = $this->orderRepository->get($args['input']['order_id']);
63+
64+
if (!$order->getCustomerIsGuest()) {
65+
return [
66+
'error' => __('Current user is not authorized to cancel this order')
67+
];
68+
}
69+
70+
$errors = $this->validateOrder->execute($order);
71+
if (!empty($errors)) {
72+
return $errors;
73+
}
74+
75+
return $this->confirmCancelOrder->execute($order, $args['input']);
76+
} catch (LocalizedException $e) {
77+
return [
78+
'error' => __($e->getMessage())
79+
];
80+
}
81+
}
82+
}

app/code/Magento/OrderCancellationGraphQl/Model/Resolver/ConfirmCancelOrderGuest.php

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)