Medisync / models /preorder.py
anique-1's picture
Deploy MediSync AI Backend with APK to Hugging Face
903c699
Raw
History Blame Contribute Delete
1.97 kB
from pydantic import BaseModel, EmailStr, Field
from typing import Optional
from motor.motor_asyncio import AsyncIOMotorDatabase
from utils.email_utils import send_preorder_notification_to_owner
class PreOrderRequest(BaseModel):
name: str = Field(..., max_length=100)
phone: str = Field(..., max_length=20)
gmail: EmailStr = Field(..., description="Gmail address")
message: str | None = None
import datetime
async def create_preorder(db: AsyncIOMotorDatabase, data: PreOrderRequest):
preorder = data.dict()
preorder["created_at"] = datetime.datetime.utcnow().isoformat()
result = await db["preorders"].insert_one(preorder)
# Send notification email to owner
await send_preorder_notification_to_owner(
name=preorder["name"],
phone=preorder["phone"],
gmail=preorder["gmail"],
message=preorder.get("message", "")
)
return str(result.inserted_id)
async def get_all_preorders(db: AsyncIOMotorDatabase):
preorders = []
cursor = db["preorders"].find({})
async for doc in cursor:
doc["_id"] = str(doc["_id"])
if "created_at" not in doc:
doc["created_at"] = ""
preorders.append(doc)
return preorders
async def get_preorder_count(db: AsyncIOMotorDatabase):
return await db["preorders"].count_documents({})
async def delete_preorder(db: AsyncIOMotorDatabase, preorder_id: str):
from bson import ObjectId
result = await db["preorders"].delete_one({"_id": ObjectId(preorder_id)})
return result.deleted_count
async def update_preorder_status(db: AsyncIOMotorDatabase, preorder_id: str, status: str):
from bson import ObjectId
# Only allow valid statuses
if status not in ["pending", "contacted", "completed"]:
raise ValueError("Invalid status")
result = await db["preorders"].update_one(
{"_id": ObjectId(preorder_id)},
{"$set": {"status": status}}
)
return result.modified_count