64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
"""Calendar API routes for VoIdea."""
|
|
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.dependencies import get_current_user, get_db
|
|
from app.models.user import User
|
|
from app.services.calendar_service import CALENDAR_PROVIDERS, CalendarService
|
|
from app.services.idea_service import IdeaService
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/ideas/{idea_id}/calendar")
|
|
async def add_idea_to_calendar(
|
|
idea_id: str,
|
|
user: Annotated[User, Depends(get_current_user)],
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""Create a calendar event from an idea."""
|
|
idea_svc = IdeaService(db)
|
|
idea = await idea_svc.get_by_id(idea_id)
|
|
if not idea or str(idea.user_id) != str(user.id):
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Idea not found")
|
|
|
|
cal_svc = CalendarService(db)
|
|
success = await cal_svc.create_idea_event(user, idea.title, idea.content)
|
|
if not success:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Calendar not configured. Connect a calendar in Settings > Integrations.",
|
|
)
|
|
return {"success": True, "message": "Event created in calendar"}
|
|
|
|
|
|
@router.get("/calendar/providers")
|
|
async def list_calendar_providers():
|
|
return {"providers": list(CALENDAR_PROVIDERS)}
|
|
|
|
|
|
@router.get("/calendar/status")
|
|
async def calendar_status(
|
|
user: Annotated[User, Depends(get_current_user)],
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
cal_svc = CalendarService(db)
|
|
creds = await cal_svc.get_calendar_credentials(user)
|
|
return {
|
|
"connected": creds is not None,
|
|
"provider": user.calendar_provider if hasattr(user, "calendar_provider") else None,
|
|
}
|
|
|
|
|
|
@router.post("/calendar/disconnect")
|
|
async def disconnect_calendar(
|
|
user: Annotated[User, Depends(get_current_user)],
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
cal_svc = CalendarService(db)
|
|
await cal_svc.disconnect_calendar(user)
|
|
return {"success": True}
|