Spaces:
Sleeping
Sleeping
File size: 25,979 Bytes
aa6cac1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 | """
Database Models and Operations
===============================
This module contains all database operations for the e-commerce support system.
It uses Supabase (PostgreSQL) for data persistence.
Classes:
- OrderDatabase: Main database operations class
- CustomerOperations: Customer-related operations
- InventoryOperations: Inventory-related operations
- OrderOperations: Order-related operations
Usage:
from src.database.models import OrderDatabase
db = OrderDatabase()
result = db.get_product("PROD001")
"""
import os
from supabase import create_client, Client
from dotenv import load_dotenv
from datetime import datetime, timedelta
import json
from typing import Dict, List, Optional, Any
# Load environment variables
load_dotenv()
# Initialize Supabase client
supabase_url = os.getenv("SUPABASE_URL")
supabase_key = os.getenv("SUPABASE_KEY")
if not supabase_url or not supabase_key:
raise ValueError(
"Missing Supabase credentials. Please set SUPABASE_URL and SUPABASE_KEY in .env file"
)
supabase: Client = create_client(supabase_url, supabase_key)
# ============================================
# MAIN DATABASE CLASS
# ============================================
class OrderDatabase:
"""
Main database operations class for the e-commerce system.
This class handles all interactions with the Supabase PostgreSQL database,
including customers, orders, inventory, refunds, and conversations.
Attributes:
supabase (Client): Supabase client instance
Example:
>>> db = OrderDatabase()
>>> product = db.get_product("PROD001")
>>> print(product["name"])
'MacBook Pro 14-inch'
"""
def __init__(self):
"""Initialize database connection"""
self.supabase = supabase
# ============================================
# CUSTOMER OPERATIONS
# ============================================
def create_customer(
self,
customer_id: str,
email: str,
name: str,
phone: Optional[str] = None,
address: Optional[str] = None
) -> Dict[str, Any]:
"""
Create a new customer in the database.
Args:
customer_id: Unique customer identifier (e.g., "CUST001")
email: Customer email address (must be unique)
name: Customer full name
phone: Optional phone number
address: Optional street address
Returns:
Dict with 'success' bool and 'customer' data or 'error' message
Example:
>>> db.create_customer("CUST001", "john@example.com", "John Doe")
{'success': True, 'customer': {...}}
"""
try:
data = {
"customer_id": customer_id,
"email": email,
"name": name,
"phone": phone,
"address": address,
"created_at": datetime.now().isoformat()
}
result = self.supabase.table("customers").insert(data).execute()
return {"success": True, "customer": result.data[0]}
except Exception as e:
return {"success": False, "error": str(e)}
def get_customer(self, customer_id: str) -> Dict[str, Any]:
"""
Get customer information by customer ID.
Args:
customer_id: The customer identifier
Returns:
Dict with customer information or error
"""
try:
result = self.supabase.table("customers")\
.select("*")\
.eq("customer_id", customer_id)\
.execute()
if result.data:
return {"success": True, "customer": result.data[0]}
else:
return {"success": False, "error": "Customer not found"}
except Exception as e:
return {"success": False, "error": str(e)}
def get_customer_orders(self, customer_id: str) -> Dict[str, Any]:
"""
Get all orders for a specific customer.
Args:
customer_id: The customer identifier
Returns:
Dict with list of orders or error
"""
try:
result = self.supabase.table("orders")\
.select("*")\
.eq("customer_id", customer_id)\
.order("created_at", desc=True)\
.execute()
return {"success": True, "orders": result.data}
except Exception as e:
return {"success": False, "error": str(e)}
# ============================================
# ORDER OPERATIONS
# ============================================
def create_order(
self,
order_id: str,
customer_id: str,
items: List[Dict],
total: float
) -> Dict[str, Any]:
"""
Create a new order in the database.
Args:
order_id: Unique order identifier (e.g., "ORD1299")
customer_id: Customer who placed the order
items: List of order items with product details
total: Total order amount
Returns:
Dict with success status and order data
Example:
>>> items = [{"product_id": "PROD001", "quantity": 1, "price": 999.99}]
>>> db.create_order("ORD001", "CUST001", items, 999.99)
"""
try:
data = {
"order_id": order_id,
"customer_id": customer_id,
"items": json.dumps(items),
"total": total,
"status": "pending",
"created_at": datetime.now().isoformat(),
"tracking_number": None,
"expected_delivery": None
}
result = self.supabase.table("orders").insert(data).execute()
return {"success": True, "order": result.data[0]}
except Exception as e:
return {"success": False, "error": str(e)}
def get_order_status(self, order_id: str) -> Dict[str, Any]:
"""
Retrieve order status and details.
Args:
order_id: The order identifier
Returns:
Dict with order information including status, tracking, items
"""
try:
result = self.supabase.table("orders")\
.select("*")\
.eq("order_id", order_id)\
.execute()
if result.data:
order = result.data[0]
return {
"success": True,
"order_id": order["order_id"],
"status": order["status"],
"tracking_number": order.get("tracking_number"),
"expected_delivery": order.get("expected_delivery"),
"total": order["total"],
"items": json.loads(order["items"])
}
else:
return {"success": False, "error": "Order not found"}
except Exception as e:
return {"success": False, "error": str(e)}
def update_order_status(
self,
order_id: str,
status: str,
tracking_number: Optional[str] = None,
expected_delivery: Optional[str] = None
) -> Dict[str, Any]:
"""
Update order status and tracking information.
Args:
order_id: Order to update
status: New status (pending, processing, shipped, delivered, etc.)
tracking_number: Optional shipping tracking number
expected_delivery: Optional expected delivery date
Returns:
Dict with success status and updated order
"""
try:
update_data = {"status": status}
if tracking_number:
update_data["tracking_number"] = tracking_number
if expected_delivery:
update_data["expected_delivery"] = expected_delivery
result = self.supabase.table("orders")\
.update(update_data)\
.eq("order_id", order_id)\
.execute()
return {"success": True, "order": result.data[0]}
except Exception as e:
return {"success": False, "error": str(e)}
# ============================================
# REFUND OPERATIONS
# ============================================
def create_refund(self, order_id: str, reason: str, amount: float) -> Dict[str, Any]:
"""
Initiate a refund for an order.
Args:
order_id: Order to refund
reason: Reason for refund
amount: Refund amount
Returns:
Dict with refund details or error
"""
try:
# First check if order exists
order_result = self.get_order_status(order_id)
if not order_result["success"]:
return {"success": False, "error": "Order not found"}
# Create refund record
refund_data = {
"order_id": order_id,
"reason": reason,
"amount": amount,
"status": "pending",
"created_at": datetime.now().isoformat()
}
result = self.supabase.table("refunds").insert(refund_data).execute()
# Update order status to refunded
self.update_order_status(order_id, "refunded")
return {
"success": True,
"refund_id": result.data[0]["id"],
"amount": amount,
"status": "pending"
}
except Exception as e:
return {"success": False, "error": str(e)}
def get_refund_status(self, refund_id: int) -> Dict[str, Any]:
"""
Check refund status.
Args:
refund_id: The refund identifier
Returns:
Dict with refund information
"""
try:
result = self.supabase.table("refunds")\
.select("*")\
.eq("id", refund_id)\
.execute()
if result.data:
return {"success": True, "refund": result.data[0]}
else:
return {"success": False, "error": "Refund not found"}
except Exception as e:
return {"success": False, "error": str(e)}
# ============================================
# INVENTORY OPERATIONS
# ============================================
def add_product(
self,
product_id: str,
name: str,
description: str,
price: float,
category: str,
stock_quantity: int,
low_stock_threshold: int = 10
) -> Dict[str, Any]:
"""
Add a new product to inventory.
Args:
product_id: Unique product identifier
name: Product name
description: Product description
price: Product price
category: Product category
stock_quantity: Initial stock quantity
low_stock_threshold: When to alert for low stock
Returns:
Dict with success status and product data
"""
try:
data = {
"product_id": product_id,
"name": name,
"description": description,
"price": price,
"category": category,
"stock_quantity": stock_quantity,
"reserved_quantity": 0,
"low_stock_threshold": low_stock_threshold,
"is_active": True,
"created_at": datetime.now().isoformat()
}
result = self.supabase.table("inventory").insert(data).execute()
return {"success": True, "product": result.data[0]}
except Exception as e:
return {"success": False, "error": str(e)}
def get_product(self, product_id: str) -> Dict[str, Any]:
"""
Get product details and availability.
Args:
product_id: The product identifier
Returns:
Dict with complete product information including:
- Basic info (name, price, description)
- Stock levels (total, reserved, available)
- Availability status (in stock, low stock)
"""
try:
result = self.supabase.table("inventory")\
.select("*")\
.eq("product_id", product_id)\
.execute()
if result.data:
product = result.data[0]
available = product["stock_quantity"] - product["reserved_quantity"]
return {
"success": True,
"product_id": product["product_id"],
"name": product["name"],
"description": product["description"],
"price": product["price"],
"category": product["category"],
"stock_quantity": product["stock_quantity"],
"reserved_quantity": product["reserved_quantity"],
"available_quantity": available,
"is_in_stock": available > 0,
"is_low_stock": available <= product["low_stock_threshold"],
"low_stock_threshold": product["low_stock_threshold"],
"is_active": product["is_active"]
}
else:
return {"success": False, "error": "Product not found"}
except Exception as e:
return {"success": False, "error": str(e)}
def search_products(
self,
search_term: Optional[str] = None,
category: Optional[str] = None,
in_stock_only: bool = False
) -> Dict[str, Any]:
"""
Search for products by name or category.
Args:
search_term: Product name or keyword to search
category: Filter by category
in_stock_only: If True, only return products with stock > 0
Returns:
Dict with list of matching products
"""
try:
query = self.supabase.table("inventory").select("*")
if search_term:
query = query.ilike("name", f"%{search_term}%")
if category:
query = query.eq("category", category)
if in_stock_only:
query = query.gt("stock_quantity", 0)
query = query.eq("is_active", True)
result = query.execute()
products = []
for product in result.data:
available = product["stock_quantity"] - product["reserved_quantity"]
products.append({
"product_id": product["product_id"],
"name": product["name"],
"price": product["price"],
"category": product["category"],
"available_quantity": available,
"is_in_stock": available > 0
})
return {"success": True, "products": products}
except Exception as e:
return {"success": False, "error": str(e)}
def update_stock(
self,
product_id: str,
quantity_change: int,
reason: str = "manual_update"
) -> Dict[str, Any]:
"""
Update stock quantity (positive to add, negative to remove).
Args:
product_id: Product to update
quantity_change: Amount to change (+ to add, - to remove)
reason: Reason for stock change
Returns:
Dict with previous stock, new stock, and change amount
"""
try:
# Get current product
product_result = self.get_product(product_id)
if not product_result["success"]:
return {"success": False, "error": "Product not found"}
current_stock = product_result["stock_quantity"]
new_stock = current_stock + quantity_change
if new_stock < 0:
return {"success": False, "error": "Insufficient stock"}
# Update stock
result = self.supabase.table("inventory")\
.update({"stock_quantity": new_stock})\
.eq("product_id", product_id)\
.execute()
# Log the stock movement
self.log_stock_movement(
product_id,
quantity_change,
reason,
current_stock,
new_stock
)
return {
"success": True,
"product_id": product_id,
"previous_stock": current_stock,
"new_stock": new_stock,
"change": quantity_change
}
except Exception as e:
return {"success": False, "error": str(e)}
def reserve_stock(
self,
product_id: str,
quantity: int,
customer_id: str
) -> Dict[str, Any]:
"""
Reserve stock for a customer (during checkout).
Reservation expires after 15 minutes.
Args:
product_id: Product to reserve
quantity: Number of units to reserve
customer_id: Customer making the reservation
Returns:
Dict with reservation details including expiry time
"""
try:
# Get current product
product_result = self.get_product(product_id)
if not product_result["success"]:
return {"success": False, "error": "Product not found"}
available = product_result["available_quantity"]
if available < quantity:
return {
"success": False,
"error": f"Insufficient stock. Available: {available}, Requested: {quantity}"
}
# Update reserved quantity
new_reserved = product_result["reserved_quantity"] + quantity
result = self.supabase.table("inventory")\
.update({"reserved_quantity": new_reserved})\
.eq("product_id", product_id)\
.execute()
# Create reservation record
reservation_data = {
"product_id": product_id,
"customer_id": customer_id,
"quantity": quantity,
"status": "active",
"expires_at": (datetime.now() + timedelta(minutes=15)).isoformat(),
"created_at": datetime.now().isoformat()
}
reservation = self.supabase.table("reservations")\
.insert(reservation_data)\
.execute()
return {
"success": True,
"reservation_id": reservation.data[0]["id"],
"product_id": product_id,
"quantity": quantity,
"expires_at": reservation_data["expires_at"]
}
except Exception as e:
return {"success": False, "error": str(e)}
def release_reservation(self, reservation_id: int) -> Dict[str, Any]:
"""
Release a reservation (if order is cancelled or expired).
Args:
reservation_id: The reservation to release
Returns:
Dict with success status
"""
try:
# Get reservation details
reservation = self.supabase.table("reservations")\
.select("*")\
.eq("id", reservation_id)\
.execute()
if not reservation.data:
return {"success": False, "error": "Reservation not found"}
res_data = reservation.data[0]
if res_data["status"] != "active":
return {"success": False, "error": "Reservation already released"}
# Get current product
product = self.supabase.table("inventory")\
.select("*")\
.eq("product_id", res_data["product_id"])\
.execute()
if not product.data:
return {"success": False, "error": "Product not found"}
# Update reserved quantity
new_reserved = max(0, product.data[0]["reserved_quantity"] - res_data["quantity"])
self.supabase.table("inventory")\
.update({"reserved_quantity": new_reserved})\
.eq("product_id", res_data["product_id"])\
.execute()
# Mark reservation as released
self.supabase.table("reservations")\
.update({"status": "released"})\
.eq("id", reservation_id)\
.execute()
return {"success": True, "message": "Reservation released successfully"}
except Exception as e:
return {"success": False, "error": str(e)}
def get_low_stock_products(self) -> Dict[str, Any]:
"""
Get products with stock below threshold.
Returns:
Dict with list of low stock products
"""
try:
result = self.supabase.table("inventory")\
.select("*")\
.eq("is_active", True)\
.execute()
low_stock = []
for product in result.data:
available = product["stock_quantity"] - product["reserved_quantity"]
if available <= product["low_stock_threshold"]:
low_stock.append({
"product_id": product["product_id"],
"name": product["name"],
"available_quantity": available,
"threshold": product["low_stock_threshold"]
})
return {"success": True, "products": low_stock}
except Exception as e:
return {"success": False, "error": str(e)}
def log_stock_movement(
self,
product_id: str,
quantity_change: int,
reason: str,
previous_stock: int,
new_stock: int
) -> Dict[str, Any]:
"""
Log stock movements for audit trail.
Args:
product_id: Product that changed
quantity_change: Amount of change
reason: Reason for change
previous_stock: Stock before change
new_stock: Stock after change
Returns:
Dict with success status
"""
try:
data = {
"product_id": product_id,
"quantity_change": quantity_change,
"reason": reason,
"previous_stock": previous_stock,
"new_stock": new_stock,
"timestamp": datetime.now().isoformat()
}
self.supabase.table("stock_movements").insert(data).execute()
return {"success": True}
except Exception as e:
return {"success": False, "error": str(e)}
# ============================================
# CONVERSATION OPERATIONS
# ============================================
def save_conversation(
self,
customer_id: str,
message: str,
role: str
) -> Dict[str, Any]:
"""
Save conversation messages.
Args:
customer_id: Customer in conversation
message: Message content
role: Message role (user, assistant, agent name)
Returns:
Dict with success status
"""
try:
data = {
"customer_id": customer_id,
"message": message,
"role": role,
"timestamp": datetime.now().isoformat()
}
result = self.supabase.table("conversations").insert(data).execute()
return {"success": True}
except Exception as e:
return {"success": False, "error": str(e)}
def get_conversation_history(
self,
customer_id: str,
limit: int = 50
) -> Dict[str, Any]:
"""
Retrieve conversation history for a customer.
Args:
customer_id: Customer identifier
limit: Maximum number of messages to retrieve
Returns:
Dict with list of messages
"""
try:
result = self.supabase.table("conversations")\
.select("*")\
.eq("customer_id", customer_id)\
.order("timestamp", desc=True)\
.limit(limit)\
.execute()
return {"success": True, "messages": result.data}
except Exception as e:
return {"success": False, "error": str(e)} |