mirror of
https://github.com/temporal-community/temporal-ai-agent.git
synced 2026-03-17 06:58:09 +01:00
food ordering for stevea video
This commit is contained in:
63
tools/food/add_to_cart.py
Normal file
63
tools/food/add_to_cart.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
def add_to_cart(args: dict) -> dict:
|
||||
customer_email = args.get("customer_email")
|
||||
item_id = args.get("item_id")
|
||||
quantity = int(args.get("quantity", 1))
|
||||
restaurant_id = args.get("restaurant_id", "rest_001")
|
||||
|
||||
file_path = Path(__file__).resolve().parent.parent / "data" / "food_ordering_data.json"
|
||||
if not file_path.exists():
|
||||
return {"error": "Data file not found."}
|
||||
|
||||
with open(file_path, "r") as file:
|
||||
data = json.load(file)
|
||||
|
||||
# Find the item to get its price
|
||||
item_price = None
|
||||
item_name = None
|
||||
for restaurant in data["restaurants"]:
|
||||
if restaurant["id"] == restaurant_id:
|
||||
for item in restaurant["menu"]:
|
||||
if item["id"] == item_id:
|
||||
item_price = item["price"]
|
||||
item_name = item["name"]
|
||||
break
|
||||
|
||||
if item_price is None:
|
||||
return {"error": f"Item {item_id} not found."}
|
||||
|
||||
# Initialize cart if it doesn't exist
|
||||
if customer_email not in data["carts"]:
|
||||
data["carts"][customer_email] = {
|
||||
"restaurant_id": restaurant_id,
|
||||
"items": []
|
||||
}
|
||||
|
||||
# Check if item already in cart
|
||||
cart = data["carts"][customer_email]
|
||||
existing_item = None
|
||||
for cart_item in cart["items"]:
|
||||
if cart_item["item_id"] == item_id:
|
||||
existing_item = cart_item
|
||||
break
|
||||
|
||||
if existing_item:
|
||||
existing_item["quantity"] += quantity
|
||||
else:
|
||||
cart["items"].append({
|
||||
"item_id": item_id,
|
||||
"quantity": quantity,
|
||||
"price": item_price
|
||||
})
|
||||
|
||||
# Save back to file
|
||||
with open(file_path, "w") as file:
|
||||
json.dump(data, file, indent=2)
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"message": f"Added {quantity} x {item_name} to cart",
|
||||
"cart": cart
|
||||
}
|
||||
28
tools/food/check_order_status.py
Normal file
28
tools/food/check_order_status.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
def check_order_status(args: dict) -> dict:
|
||||
order_id = args.get("order_id")
|
||||
|
||||
file_path = Path(__file__).resolve().parent.parent / "data" / "food_ordering_data.json"
|
||||
if not file_path.exists():
|
||||
return {"error": "Data file not found."}
|
||||
|
||||
with open(file_path, "r") as file:
|
||||
data = json.load(file)
|
||||
|
||||
orders = data["orders"]
|
||||
|
||||
for order in orders:
|
||||
if order["id"] == order_id:
|
||||
return {
|
||||
"order_id": order["id"],
|
||||
"status": order["status"],
|
||||
"order_date": order["order_date"],
|
||||
"estimated_delivery": order["estimated_delivery"],
|
||||
"actual_delivery": order.get("actual_delivery"),
|
||||
"total": order["total"],
|
||||
"items": order["items"]
|
||||
}
|
||||
|
||||
return {"error": f"Order {order_id} not found."}
|
||||
23
tools/food/get_menu.py
Normal file
23
tools/food/get_menu.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
def get_menu(args: dict) -> dict:
|
||||
restaurant_id = args.get("restaurant_id", "rest_001")
|
||||
|
||||
file_path = Path(__file__).resolve().parent.parent / "data" / "food_ordering_data.json"
|
||||
if not file_path.exists():
|
||||
return {"error": "Data file not found."}
|
||||
|
||||
with open(file_path, "r") as file:
|
||||
data = json.load(file)
|
||||
|
||||
restaurants = data["restaurants"]
|
||||
|
||||
for restaurant in restaurants:
|
||||
if restaurant["id"] == restaurant_id:
|
||||
return {
|
||||
"restaurant_name": restaurant["name"],
|
||||
"menu": restaurant["menu"]
|
||||
}
|
||||
|
||||
return {"error": f"Restaurant {restaurant_id} not found."}
|
||||
23
tools/food/get_menu_item_details.py
Normal file
23
tools/food/get_menu_item_details.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
def get_menu_item_details(args: dict) -> dict:
|
||||
item_id = args.get("item_id")
|
||||
restaurant_id = args.get("restaurant_id", "rest_001")
|
||||
|
||||
file_path = Path(__file__).resolve().parent.parent / "data" / "food_ordering_data.json"
|
||||
if not file_path.exists():
|
||||
return {"error": "Data file not found."}
|
||||
|
||||
with open(file_path, "r") as file:
|
||||
data = json.load(file)
|
||||
|
||||
restaurants = data["restaurants"]
|
||||
|
||||
for restaurant in restaurants:
|
||||
if restaurant["id"] == restaurant_id:
|
||||
for item in restaurant["menu"]:
|
||||
if item["id"] == item_id:
|
||||
return item
|
||||
|
||||
return {"error": f"Menu item {item_id} not found."}
|
||||
57
tools/food/place_order.py
Normal file
57
tools/food/place_order.py
Normal file
@@ -0,0 +1,57 @@
|
||||
from pathlib import Path
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
def place_order(args: dict) -> dict:
|
||||
customer_email = args.get("customer_email")
|
||||
|
||||
file_path = Path(__file__).resolve().parent.parent / "data" / "food_ordering_data.json"
|
||||
if not file_path.exists():
|
||||
return {"error": "Data file not found."}
|
||||
|
||||
with open(file_path, "r") as file:
|
||||
data = json.load(file)
|
||||
|
||||
# Check if cart exists
|
||||
if customer_email not in data["carts"] or not data["carts"][customer_email]["items"]:
|
||||
return {"error": "Cart is empty. Please add items to cart first."}
|
||||
|
||||
cart = data["carts"][customer_email]
|
||||
|
||||
# Calculate total
|
||||
total = sum(item["price"] * item["quantity"] for item in cart["items"])
|
||||
|
||||
# Create order
|
||||
order_id = f"order_{str(uuid.uuid4())[:8]}"
|
||||
order_date = datetime.now().isoformat() + "Z"
|
||||
estimated_delivery = (datetime.now() + timedelta(minutes=30)).isoformat() + "Z"
|
||||
|
||||
new_order = {
|
||||
"id": order_id,
|
||||
"customer_email": customer_email,
|
||||
"restaurant_id": cart["restaurant_id"],
|
||||
"items": cart["items"],
|
||||
"total": round(total, 2),
|
||||
"status": "preparing",
|
||||
"order_date": order_date,
|
||||
"estimated_delivery": estimated_delivery
|
||||
}
|
||||
|
||||
# Add order to data
|
||||
data["orders"].append(new_order)
|
||||
|
||||
# Clear cart
|
||||
data["carts"][customer_email] = {"restaurant_id": cart["restaurant_id"], "items": []}
|
||||
|
||||
# Save back to file
|
||||
with open(file_path, "w") as file:
|
||||
json.dump(data, file, indent=2)
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"order_id": order_id,
|
||||
"total": round(total, 2),
|
||||
"estimated_delivery": estimated_delivery,
|
||||
"message": "Order placed successfully!"
|
||||
}
|
||||
Reference in New Issue
Block a user