text
stringlengths
0
59.1k
customer_address: deliveryAddress,
total_amount: totalAmount,
status: "preparing",
created_at: new Date().toISOString(),
})
.select()
.single();
if (orderError) {
throw new Error(`Failed to create order: ${orderError.message}`);
}
// Create order items in order_items table
const orderItems = items.map((item) => ({
order_id: orderData.id,
menu_item_id: item.menuItemId,
quantity: item.quantity,
price: item.price,
}));
const { error: itemsError } = await supabase.from("order_items").insert(orderItems);
if (itemsError) {
// If order items fail, we should ideally rollback the order
// For now, just log the error
console.error("Order items could not be created:", itemsError);
throw new Error(`Failed to save order items: ${itemsError.message}`);
}
return {
success: true,
orderId: orderData.id,
message: `Your order has been successfully created! Order number: ${orderData.id}`,
estimatedDeliveryTime: "30-45 minutes",
totalAmount: totalAmount,
customerPhone: customerPhone,
items: items,
deliveryAddress: deliveryAddress,
};
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : "An error occurred while creating order",
message: "Sorry, we cannot process your order right now. Please try again later.",
};
}
},
});
```
</details>
![create-order tool](https://cdn.voltagent.dev/examples/with-whatsapp/create-order.png)
**What this tool does:** Takes the items collected in working memory, derives totals, creates the `orders` and `order_items` records, and returns a confirmation message, letting the agent wrap up an order as soon as the customer shares an address.
**Working memory integration:** This tool demonstrates how VoltAgent's working memory maintains conversation context across messages - the AI agent remembers cart items and delivery details without requiring the customer to repeat information.
## Check Order Status Tool
This tool allows customers to check the status of their orders:
<details>
<summary>View code</summary>
```typescript
import { createTool } from "@voltagent/core";
import { z } from "zod";
import { supabase } from "../../lib/supabase";
export const checkOrderStatusTool = createTool({
name: "checkOrderStatus",
description: "Checks the status of a customer's order(s) from the database",
parameters: z.object({
orderId: z.number().optional().describe("Specific order ID to check"),
}),
execute: async ({ orderId }, context) => {
try {
// Always get customer phone from context
const customerPhone = context?.userId;
if (!customerPhone) {
return {
success: false,
message: "Customer phone number not found. Please login to the system.",
};
}
let query = supabase
.from("orders")
.select(
`
id,
customer_phone,
customer_address,
total_amount,
status,
created_at,
order_items (
id,