from fastapi import FastAPI
from typing import Literal, TypedDict
from upstash_workflow.fastapi import Serve
from upstash_workflow import AsyncWorkflowContext
app = FastAPI()
serve = Serve(app)
UserState = Literal["non-active", "active"]
class InitialData(TypedDict):
email: str
async def send_email(message: str, email: str) -> None:
# Implement email sending logic here
print(f"Sending {message} email to {email}")
async def get_user_state() -> UserState:
# Implement user state logic here
return "non-active"
@serve.post("/customer-onboarding")
async def customer_onboarding(context: AsyncWorkflowContext[InitialData]) -> None:
email = context.request_payload["email"]
async def _new_signup() -> None:
await send_email("Welcome to the platform", email)
await context.run("new-signup", _new_signup)
await context.sleep("wait-for-3-days", 60 * 60 * 24 * 3)
while True:
async def _check_user_state() -> UserState:
return await get_user_state()
state: UserState = await context.run("check-user-state", _check_user_state)
if state == "non-active":
async def _send_email_non_active() -> None:
await send_email("Email to non-active users", email)
await context.run("send-email-non-active", _send_email_non_active)
else:
async def _send_email_active() -> None:
await send_email("Send newsletter to active users", email)
await context.run("send-email-active", _send_email_active)
await context.sleep("wait-for-1-month", 60 * 60 * 24 * 30)