Files
armonia-api/app/routers/services.py
T
kiskaadee 92edb4ac12 feat(packages): add multi-session package endpoints and service buffer schema
- Add `packages` table and indexes in `schema.sql` to track prepaid or
  partially paid multi-session therapy bundles (`total_sessions`,
  `completed_sessions`, `financial_status`).
- Add `buffer_minutes`, `duration_minutes`, and `package_id` columns to
  support mandatory post-consultation buffers and package redemptions.
- Seed 15-minute mandatory buffer on `atencion-psicologica-individual`.
- Implement `/packages` CRUD router (`POST`, `GET`, `PATCH`) and mount it
  in `app/main.py`.

Refs: #1
2026-10-18 12:00:10 -05:00

83 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, buffer_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,
payload.buffer_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