# Order Management

## The `onOrderManagement` callback

When the Assistant detects that the user is trying to view or cancel their orders, it invokes the callback associated with the order management user journey. The callback looks like this:

{% tabs %}
{% tab title="Android Native" %}

```
OrderManagementAppState onOrderManagement(OrderInfo orderInfo, OrderManagementUserJourney userJourney);
```

{% endtab %}

{% tab title="React Native" %}

```
onOrderManagement: async (orderInfo, orderMangementUserJourney)
```

{% endtab %}

{% tab title="Web" %}

```
onManageOrder: async (orderInfo, orderMangementUserJourney)
```

{% endtab %}

{% tab title="Android (Deprecated)" %}

```java
OrderManagementUserJourney.AppState onOrderManagement(OrderInfo orderInfo, OrderManagementUserJourney userJourney);
```

{% endtab %}
{% endtabs %}

When this callback is invoked, the app is expected to:

1. Use the `OrderInfo` parameter to check if the user is interested to view or cancel their past orders
2. Open the corresponding order page using the index field in the `OrderInfo` parameter
3. Return the appropriate App State and pass the relevant condition to it

For  example, for a given `onOrderManagement` callback invocation, if the user asked for viewing a specific order,  and if that order did not exist, the app return OrderViewAppState and pass the  condition ORDER\_*NOT\_*&#x46;OUND to it.&#x20;

{% tabs %}
{% tab title="Android Native" %}

```
public OrderManagementAppState onOrderManagement(OrderInfo orderInfo, OrderManagementUserJourney userJourney) {
   int orderIndex = orderInfo.getOrderIndex();
   
   if (!checkOrder(orderIndex)) { // Order does not exist
      return new OrderViewAppState(ORDER_NOT_FOUND, orderIndex);
   } else {
      return new OrderViewAppState(SUCCESS);   
   }
}
```

{% endtab %}

{% tab title="React Native" %}

```
onOrderManagement: async (orderInfo, orderMangementUserJourney) => {
  var orderIndex = orderInfo.orderIndex;
   
  if (!checkOrder(orderIndex)) { // Order does not exist
    userJourney.setViewOrderNotFound(orderIndex);
  } else {   
    userJourney.setViewSuccess();
  }
  return OrderManagementUserJourney.AppState.VIEW_ORDER;
},
```

{% endtab %}

{% tab title="Web" %}

```javascript
onManageOrder: async (orderInfo, orderMangementUserJourney) => {
  const orderIndex = orderInfo.orderIndex;
   
  if (!checkOrder(orderIndex)) { // Order does not exist
    userJourney.setOrderNotFound(orderIndex);
  } else {   
    userJourney.setViewSuccess();
  }
  return OrderManagementUserJourney.AppStates.VIEW_ORDER;
},
```

{% endtab %}

{% tab title="Android (Deprecated)" %}

```java
public OrderManagementUserJourney.AppState onOrderManagement(OrderInfo orderInfo, OrderManagementUserJourney userJourney) {
   int orderIndex = orderInfo.getOrderIndex();
   
   if (!checkOrder(orderIndex)) { // Order does not exist
      userJourney.setViewOrderNotFound(orderIndex);
   } else {   
      userJourney.setViewSuccess();
   }
   return OrderManagementUserJourney.AppState.VIEW_ORDER;
}
```

{% endtab %}
{% endtabs %}

## Sample Utterances that could trigger Order Management

The following are some examples of commands that could trigger this user journey

* *"show my orders"*
* *"where is my last order"*
* *"cancel my first order"*

## `OrderInfo` Parameter

The parameter`OrderInfo` contains the breakdown of the original order request. It has the following structure:

{% tabs %}
{% tab title="Android Native" %}

```
Class OrderInfo {
	int getOrderIndex(); // The index of the order
	OrderAction getOrderAction();
	ConfirmationStatus getCancelConfirmationStatus();

	enum OrderAction {
		VIEW,
		CANCEL
	},
	
	enum ConfirmationStatus {
	  UNKNOWN,    // When the user requested for cancelation, initially the confirmation status would be UNKNOWN
	  CONFIRMED,  // When the user has explicitly confirmed the cancellation, 
	  DENIED      // When the user has explicitly denied the cancellation 
	}
}
```

{% endtab %}

{% tab title="React Native" %}

```
{
  "action": <string value>,             // One of "VIEW, CANCEL"
  "confirmationStatus": <string value>, // One of "UNKNOWN, CONFIRMED, DENIED"
  "index": <int>                        // one of -1, 0,...
}
```

{% endtab %}

{% tab title="Web" %}

```
{
  "action": <string value>,             // One of "VIEW, CANCEL"
  "confirmationStatus": <string value>, // One of "UNKNOWN, CONFIRMED, DENIED"
  "index": <int>                        // one of -1, 0,...
}
```

{% endtab %}
{% endtabs %}

So when the user speaks *"show my last order",* &#x20;

* `Index` =  `-1`
* `OrderAction` = `VIEW`

When the user speaks *"cancel my order"*

* `Index` = `0`
* `OrderAction` = `CANCEL`
* `ConfirmationStatus` = `UNKNOWN`

{% hint style="info" %}
`index` can take the following values:

* -1      = to indicate last order *("cancel my last order")*
* 0       = when no explicit order is mentioned or when the user says "all"
* 1...n  = The index as mentioned by the user *("show my first order")*
  {% endhint %}

## Supported `AppState`s

The following `AppState`s are supported:

* **ORDER\_VIEW (`OrderViewAppState`):**  To be returned when the app handles the order request and transitions to the orders page.
* **ORDER\_CANCEL (`OrderCancelAppState`):** To be returned when the app handles the cancel request and navigates to the cancel orders page
* **UNSUPPORTED (`UnsupportedAppState`):** To be returned when the app is not ready to handle orders yet. The Assistant will speak out an appropriate prompt to the user.&#x20;

{% hint style="info" %}
The Slang Retail Assistant provides a special `AppState` call **`WAITING (WaitingAppState)`** that is common across all `UserJourney` types for completing asynchronous operations within the callback. Refer to the [Asynchronous Action Handling](broken://pages/-MOf016Tfk_kJdogKS1g#asynchronous-action-handling) section for details of how to deal with asynchronous operations.
{% endhint %}

### Supported `Condition`s

The following `Conditions` are supported for each of the `AppState`s supported by the Assistant

| **App State**                        | **Condition**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `ORDER_VIEW (OrderViewAppState)`     | <ul><li><strong>SUCCESS</strong> - The order could be shown successfully</li><li><strong>ORDER\_HISTORY\_EMPTY</strong> - There are no orders to show</li><li><strong>ORDER\_NOT\_FOUND </strong><em><strong>(index)</strong></em> - The specified order was not found</li><li><strong>FAILURE</strong> - There was a failure while showing orders</li></ul>                                                                                                                                                                                                                                                                                                                               |
| `ORDER_CANCEL (OrderCancelAppState)` | <ul><li><strong>SUCCESS -</strong> The order has been canceled successfully</li><li><strong>ORDER\_CANCEL\_CONFIRMATION\_REQUIRED</strong> - The app wants the user to confirm the cancellation request</li><li><strong>ORDER\_CANCEL\_USER\_CONFIRMED</strong> - The user has confirmed the cancellation and the app was able to cancel the order</li><li><strong>ORDER\_CANCEL\_USER\_DENIED</strong> - The user has denied the cancellation and the app is not going to cancel the order</li><li><strong>ORDER\_NOT\_FOUND </strong><em><strong>(index)</strong></em> - The specified order was not found</li><li><strong>FAILURE</strong> - The order could not be canceled </li></ul> |

## Handling cancellations effectively

When the user is trying to cancel an order, the ideal workflow is that the app would want the user to confirm before actually canceling the order.  Typically this would be done in a multi-modal fashion, i.e. the app would inform the Assistant to ask for confirmation via voice, while the app also shows a dialog box for the user to visually confirm the same. &#x20;

Alternately, the app could choose to not show any confirmation and just directly cancel the order (if the order could be found)

### Handling cancel confirmations

When the user initially requests for a cancellation, the Assistant sets the `ConfirmationStatus` (inside the `OrderInfo` parameter) to UNKNOWN. The app could then do the following:

* If **`ConfirmationStatus`** **== `UNKNOWN`**, return OrderCancelAppState(`ORDER_CANCEL_CONFIRMATION_REQUIRED)`
  * The Assistant will then ask the user to confirm the cancellation of the order and the same user journey callback will be invoked again (with the `ConfirmationStatus` set to `CONFIRMED` or `DENIED`)
* If the **`ConfirmationStatus`** **== `CONFIRMED`**, the app should attempt to cancel the order and if that was:
  * Successful: return OrderCancelAppState`(ORDER_CANCEL_USER_CONFIRMED)`
  * Failure:  return OrderCancelAppState(`FAILURE`)
* If the **`ConfirmationStatus`** **== `DENIED`**,  return OrderCancelAppState`(ORDER_CANCEL_USER_DENIED)`

{% hint style="info" %}
When the cancellation is done via the UI, the app should notify about the status of the operation via the `notifyAppState` API as described in the [Asynchronous Action Handling ](broken://pages/-MOf016Tfk_kJdogKS1g#asynchronous-action-handling)section and use the same app state condition methods described above.
{% endhint %}

## Assistant Prompts

Based on the App State and the Condition that was set, the Assistant will speak out an appropriate message to the user. You can examine the default set of prompts configured for the Assistant through the Console and also customize it to your needs. Refer to the [Customizing the Assistant](broken://pages/-MOf6QJG8KGxusCCCxx9) section for details.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.conva.ai/slang/getting-started/integrating-slang-retail-assistant/code-integration-basic-steps/code-integration-basic-steps-1/supported-user-journeys/order-management.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
