# 10 - Complete Request Flow (End-to-End Walkthrough)

Is document me hum ek concrete real example legi aur trace karenge ki **Swagger UI se click karne se leke MongoDB Atlas me document store hone aur return hone tak har millisecond me kya hota hai.**

---

## Real Test Scenario

### Client Input (JSON Request Payload):
```json
POST /api/v1/tenant/register
{
  "domain": "HTTP://MyAwesomeStore.com/shop",
  "name": "Awesome Store"
}
```

---

## Step-by-Step Execution Journey

```text
  [Swagger UI / cURL]
         │ 
         │  1. HTTP POST Request Sent to http://localhost:8000/api/v1/tenant/register
         ▼
  [app/main.py]
         │ 
         │  2. Routes matching request to app/routers/tenant.py
         ▼
  [app/routers/tenant.py -> register_tenant(tenant_in: TenantCreate)]
         │ 
         │  3. Pydantic validation begins on raw JSON
         ▼
  [app/models/tenant.py -> TenantCreate.clean_domain()]
         │ 
         │  4. Field validator executes:
         │     - Lowercases & strips spaces -> "http://myawesomestore.com/shop"
         │     - Removes protocol "http://" -> "myawesomestore.com/shop"
         │     - Splits by '/' and takes host -> "myawesomestore.com"
         │     - TenantCreate instance constructed successfully:
         │       domain = "myawesomestore.com", name = "Awesome Store"
         ▼
  [app/routers/tenant.py]
         │ 
         │  5. Calls get_database() from app/database.py
         ▼
  [app/database.py]
         │ 
         │  6. Returns db instance: client["chatbot_db"]
         │     (Using AsyncMongoClient + certifi TLS CA certificates)
         ▼
  [app/routers/tenant.py]
         │ 
         │  7. Calls get_tenant_by_domain(db, "myawesomestore.com")
         ▼
  [app/repositories/tenant_repository.py]
         │ 
         │  8. Executes db["tenants"].find_one({"domain": "myawesomestore.com"})
         │     - Returns None (Domain does not exist yet)
         ▼
  [app/routers/tenant.py]
         │ 
         │  9. Duplicate check passes -> Calls create_tenant(db, tenant_in)
         ▼
  [app/repositories/tenant_repository.py]
         │ 
         │ 10. Generates siteId: f"site_{uuid.uuid4().hex[:12]}" -> "site_9a2b4c6d8e0f"
         │ 11. Generates timestamp: datetime.now(timezone.utc)
         │ 12. Prepares tenant_doc dictionary:
         │     {
         │       "siteId": "site_9a2b4c6d8e0f",
         │       "domain": "myawesomestore.com",
         │       "name": "Awesome Store",
         │       "createdAt": ISODate("2026-08-12T17:00:00Z")
         │     }
         │ 13. Executes await db["tenants"].insert_one(tenant_doc)
         │ 14. Document physically written to MongoDB Atlas!
         │     MongoDB assigns _id: ObjectId("66b9f1234567890abcdef123")
         ▼
  [app/routers/tenant.py]
         │ 
         │ 15. Receives created tenant_doc dict from repository
         │ 16. Returns tenant_doc to FastAPI engine with status 201 Created
         ▼
  [FastAPI Response Engine]
         │ 
         │ 17. Formats dictionary using TenantResponse schema:
         │     - Includes: siteId, domain, name, createdAt
         │     - Excludes internal _id field
         ▼
  [Client Received Response (HTTP 201 Created)]
```

---

## Final Document in MongoDB Atlas

Database: **`chatbot_db`**  
Collection: **`tenants`**

```json
{
  "_id": ObjectId("66b9f1234567890abcdef123"),
  "siteId": "site_9a2b4c6d8e0f",
  "domain": "myawesomestore.com",
  "name": "Awesome Store",
  "createdAt": ISODate("2026-08-12T17:00:00.000Z")
}
```

---

## Response Returned to Client (`HTTP 201 Created`)

```json
{
  "siteId": "site_9a2b4c6d8e0f",
  "domain": "myawesomestore.com",
  "name": "Awesome Store",
  "createdAt": "2026-08-12T17:00:00.000Z"
}
```

This completes the entire end-to-end request lifecycle!
