Files
armonia-api/app/routers/services.py
T
kiskaadee effa1ee86c feat: scaffold FastAPI backend with Turso and anti-ghosting booking state machine
- Initialize project with uv, FastAPI, and libsql-client for Turso persistence
- Implement local SQLite fallback (`file:local.db`) for zero-friction development and testing
- Create schema and seeder with 9 therapy services from Armonía del Ser flyer
- Implement POST /bookings with automatic WhatsApp deep-link generation and SPEI instructions
- Implement POST /payments/verify 1-tap admin acknowledgment flow
- Add automated test suite with 5 passing tests and verification gates (ruff, pyright, pytest)
2026-10-18 09:01:34 -05:00

82 lines
3.0 KiB
Python

from __future__ import annotations
import uuid
from fastapi import APIRouter, HTTPException, status
from app.db.database import get_db, result_to_dicts, row_to_dict
from app.schemas.service import ServiceCreate, ServiceRead
router = APIRouter(prefix="/services", tags=["Services"])
@router.get("", response_model=list[ServiceRead])
async def list_services(active_only: bool = True):
"""Retrieve catalog of services offered by Armonía del Ser."""
query = "SELECT * FROM services"
if active_only:
query += " WHERE is_active = 1"
query += " ORDER BY service_type ASC, name ASC;"
async with get_db() as client:
res = await client.execute(query)
rows = result_to_dicts(res)
# convert integer is_active to bool
for r in rows:
r["is_active"] = bool(r.get("is_active", 1))
return rows
@router.get("/{service_id_or_slug}", response_model=ServiceRead)
async def get_service(service_id_or_slug: str):
"""Retrieve details for a specific service by ID or slug."""
query = "SELECT * FROM services WHERE id = ? OR slug = ? LIMIT 1;"
async with get_db() as client:
res = await client.execute(query, [service_id_or_slug, service_id_or_slug])
if not res.rows:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Service not found")
item = row_to_dict(res, res.rows[0])
item["is_active"] = bool(item.get("is_active", 1))
return item
@router.post("", response_model=ServiceRead, status_code=status.HTTP_201_CREATED)
async def create_service(payload: ServiceCreate):
"""Add a new service to the catalog (Admin action)."""
service_id = str(uuid.uuid4())
insert_query = """
INSERT INTO services (
id, slug, name, description, service_type, modality,
default_schedule, price_mxn, max_capacity, duration_minutes, is_active
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
"""
async with get_db() as client:
try:
await client.execute(
insert_query,
[
service_id,
payload.slug,
payload.name,
payload.description,
payload.service_type,
payload.modality,
payload.default_schedule,
payload.price_mxn,
payload.max_capacity,
payload.duration_minutes,
1 if payload.is_active else 0,
],
)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Failed to create service: {e}",
) from e
fetch_query = "SELECT * FROM services WHERE id = ? LIMIT 1;"
res = await client.execute(fetch_query, [service_id])
item = row_to_dict(res, res.rows[0])
item["is_active"] = bool(item.get("is_active", 1))
return item