Skip to content

Commit 750dab4

Browse files
authored
LYNX-519 : Error codes for order cancellation
1 parent 2261e6c commit 750dab4

File tree

4 files changed

+534
-0
lines changed

4 files changed

+534
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Sales\Model\Order;
20+
21+
/**
22+
* Identify error code based on the error message
23+
*/
24+
class CancelOrderErrorCodes
25+
{
26+
private const MESSAGE_CODES_MAPPER = [
27+
"order cancellation is not enabled for requested store." => 'ORDER_CANCELLATION_DISABLED',
28+
"current user is not authorized to cancel this order" => 'UNAUTHORISED',
29+
"the entity that was requested doesn't exist. verify the entity and try again." => 'ORDER_NOT_FOUND',
30+
"order with one or more items shipped cannot be cancelled" => 'PARTIAL_ORDER_ITEM_SHIPPED',
31+
"order already closed, complete, cancelled or on hold" => 'INVALID_ORDER_STATUS',
32+
];
33+
34+
/**
35+
* Get message error code.
36+
*
37+
* @param string $messageCode
38+
* @return string
39+
*/
40+
public function getErrorCodeFromMapper(string $messageCode): string
41+
{
42+
return self::MESSAGE_CODES_MAPPER[strtolower($messageCode)] ?? 'UNDEFINED';
43+
}
44+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Resolver;
18+
19+
use Magento\Framework\GraphQl\Config\Element\Field;
20+
use Magento\Framework\GraphQl\Query\ResolverInterface;
21+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
22+
use Magento\OrderCancellationGraphQl\Model\CancelOrderErrorCodes;
23+
24+
/**
25+
* Resolver to return the description of a CancellationReason with error code
26+
*/
27+
class CancelOrderError implements ResolverInterface
28+
{
29+
/**
30+
* @param CancelOrderErrorCodes $cancelOrderErrorCodes
31+
*/
32+
public function __construct(
33+
private readonly CancelOrderErrorCodes $cancelOrderErrorCodes
34+
) {
35+
}
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
public function resolve(
41+
Field $field,
42+
$context,
43+
ResolveInfo $info,
44+
array $value = null,
45+
array $args = null
46+
): ?array {
47+
if (empty($value['error'])) {
48+
return null;
49+
}
50+
51+
return [
52+
'message' => $value['error'],
53+
'code' => $this->cancelOrderErrorCodes->getErrorCodeFromMapper((string) $value['error']),
54+
];
55+
}
56+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ input ConfirmCancelOrderInput {
2828
type CancelOrderOutput @doc(description: "Contains the updated customer order and error message if any.") {
2929
error: String @doc(description: "Error encountered while cancelling the order.")
3030
order: CustomerOrder @doc(description: "Updated customer order.")
31+
errorV2: CancelOrderError @resolver(class: "Magento\\OrderCancellationGraphQl\\Model\\Resolver\\CancelOrderError")
32+
}
33+
34+
type CancelOrderError {
35+
message: String! @doc(description: "A localized error message.")
36+
code: CancelOrderErrorCode! @doc(description: "An error code that is specific to cancel order.")
37+
}
38+
39+
enum CancelOrderErrorCode {
40+
ORDER_CANCELLATION_DISABLED
41+
UNDEFINED
42+
UNAUTHORISED
43+
ORDER_NOT_FOUND
44+
PARTIAL_ORDER_ITEM_SHIPPED
45+
INVALID_ORDER_STATUS
3146
}
3247

3348
enum OrderActionType @doc(description: "The list of available order actions.") {

0 commit comments

Comments
 (0)