Fix syntax errors in MapView.vue and improve backend production robustness (CORS, DB URL, auto-migrations, and seeding)

This commit is contained in:
2026-02-22 16:00:52 -05:00
parent 33154169c8
commit 532aad16df
18 changed files with 2460 additions and 2995 deletions

View File

@ -2,6 +2,7 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlmodel import Session, select
from typing import List
from uuid import UUID
from app.core.database import get_session
from app.models.bus_stop import BusStop
@ -20,7 +21,7 @@ async def get_bus_stops(session: Session = Depends(get_session)):
@router.get("/{stop_id}", response_model=BusStopResponse)
async def get_bus_stop(stop_id: str, session: Session = Depends(get_session)):
async def get_bus_stop(stop_id: UUID, session: Session = Depends(get_session)):
"""Get a single bus stop by ID."""
stop = session.get(BusStop, stop_id)
if not stop:
@ -29,7 +30,7 @@ async def get_bus_stop(stop_id: str, session: Session = Depends(get_session)):
@router.get("/{stop_id}/routes")
async def get_bus_stop_routes(stop_id: str, session: Session = Depends(get_session)):
async def get_bus_stop_routes(stop_id: UUID, session: Session = Depends(get_session)):
"""Get all routes passing through a bus stop."""
from app.models.route_stop import RouteStop
from app.models.route import Route
@ -56,7 +57,7 @@ async def create_bus_stop(
@router.put("/{stop_id}", response_model=BusStopResponse)
async def update_bus_stop(
stop_id: str,
stop_id: UUID,
stop_update: BusStopUpdate,
session: Session = Depends(get_session),
_: bool = Depends(get_current_admin)
@ -77,7 +78,7 @@ async def update_bus_stop(
@router.delete("/{stop_id}")
async def delete_bus_stop(
stop_id: str,
stop_id: UUID,
session: Session = Depends(get_session),
_: bool = Depends(get_current_admin)
):

View File

@ -42,5 +42,12 @@ class Settings(BaseSettings):
)
# Global settings instance
# Global settings instance
@property
def get_database_url(self) -> str:
url = self.database_url
if url.startswith("postgres://"):
url = url.replace("postgres://", "postgresql://", 1)
return url
settings = Settings()

View File

@ -8,7 +8,12 @@ from app.core.config import settings
# Create database engine
# Convert asyncpg URL to psycopg2 for synchronous operations
database_url = settings.database_url.replace("+asyncpg", "+psycopg2")
database_url = settings.get_database_url
if "+asyncpg" in database_url:
database_url = database_url.replace("+asyncpg", "+psycopg2")
elif "postgresql://" in database_url and "+psycopg2" not in database_url:
database_url = database_url.replace("postgresql://", "postgresql+psycopg2://")
engine = create_engine(
database_url,
echo=settings.debug,

View File

@ -8,6 +8,9 @@ from app.core.config import settings
from app.core.database import init_db, engine
from sqlmodel import Session, select
from app.models.taxi import Taxi
from app.models.shuttle import Shuttle
from app.models.business import Business
from app.models.coupon import Coupon
from app.api.routes import router as routes_router
from app.api.bus_stops import router as bus_stops_router
from app.api.schedules import router as schedules_router
@ -23,14 +26,29 @@ from app.api.reports import router as reports_router
from app.api.shuttles import router as shuttles_router
from contextlib import asynccontextmanager
from alembic.config import Config
from alembic import command
import random
from datetime import datetime, timedelta
@asynccontextmanager
async def lifespan(app: FastAPI):
# Initialize database
init_db()
# Run migrations
try:
alembic_cfg = Config("alembic.ini")
command.upgrade(alembic_cfg, "head")
print("DEBUG: Database migrations completed successfully")
except Exception as e:
print(f"WARNING: Database migrations failed: {e}")
# Fallback to init_db if alembic fails or isn't configured
try:
init_db()
except:
pass
# Seed sample data if empty
with Session(engine) as session:
# 1. Taxis
taxi_count = session.exec(select(Taxi)).first()
if not taxi_count:
sample_taxis = [
@ -55,6 +73,32 @@ async def lifespan(app: FastAPI):
]
session.add_all(sample_taxis)
session.commit()
# 2. Shuttles
shuttle_count = session.exec(select(Shuttle)).first()
if not shuttle_count:
shuttles_data = [
{'route_name': 'Boquete > Santa Catalina', 'origin': 'Boquete', 'destination': 'Santa Catalina', 'vehicle_type': 'Mini Van', 'company_name': 'Chiriqui Transfers', 'trip_type': 'one_way', 'price_per_person': 35.0, 'estimated_duration': '4.5 horas', 'departure_times': '8:00 AM', 'contact_whatsapp': '+50760000000', 'is_active': True},
{'route_name': 'Boquete > Bocas del Toro', 'origin': 'Boquete', 'destination': 'Bocas del Toro', 'vehicle_type': 'Van + Bote', 'company_name': 'Hello Panama', 'trip_type': 'one_way', 'price_per_person': 30.0, 'estimated_duration': '3.5 horas', 'departure_times': '8:00 AM', 'contact_whatsapp': '+50760000000', 'is_active': True}
]
for data in shuttles_data:
session.add(Shuttle(**data))
session.commit()
# 3. Businesses & Coupons (Tourist spots)
biz_count = session.exec(select(Business)).first()
if not biz_count:
tourist_spots = [
{"name": "Finca El Explorador", "area": "Boquete", "lat": 8.7845, "lng": -82.4350},
{"name": "Cascada San Ramón", "area": "Boquete", "lat": 8.8120, "lng": -82.4650}
]
for spot in tourist_spots:
biz = Business(name=spot["name"], address=f"Sector {spot['area']}", phone="6000-0000", category="Area Turistica", area=spot["area"], latitude=spot["lat"], longitude=spot["lng"])
session.add(biz)
session.flush()
coupon = Coupon(title=f"Oferta en {biz.name}", description=f"Descuento especial en {biz.name}", business_id=biz.id, business_name=biz.name, business_address=biz.address, business_phone=biz.phone, category=biz.category, discount_percentage=15, valid_from=datetime.now().isoformat(), valid_until=(datetime.now() + timedelta(days=30)).isoformat(), is_active=True)
session.add(coupon)
session.commit()
yield
app = FastAPI(
@ -75,7 +119,7 @@ origins = [
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],