import datetime from uuid import UUID from fastapi import APIRouter, Depends, HTTPException from sqlalchemy import delete, select from sqlalchemy.ext.asyncio import AsyncSession from app.api import deps from app.api.utils import update_object_from_payload from app.models import ( Project, Sms, User, ) from app.schemas.requests import SmsCreateRequest, SmsUpdateRequest from app.schemas.responses import SMSResponse router = APIRouter() @router.get("/project/{id}/sms", response_model=list[SMSResponse]) async def list_project_sms( id: UUID, current_user: User = Depends(deps.get_current_user), session: AsyncSession = Depends(deps.get_session), ): """List sms from project""" p = await session.get(Project, id) if p is None: raise HTTPException(status_code=404, detail="Project not found") results = await session.execute(select(Sms).where(Sms.project_id == id)) return results.scalars() @router.post("/project/{id}/sms", response_model=list[SMSResponse]) async def create_sms( id: UUID, new_sms: SmsCreateRequest, current_user: User = Depends(deps.get_current_user), session: AsyncSession = Depends(deps.get_session), ): """create a new to the project""" p = await session.get(Project, id) if p is None: raise HTTPException(status_code=404, detail="Project not found") sms = Sms(project_id=id, **new_sms.dict()) session.add(sms) await session.commit() return sms @router.post("/project/{project_id}/sms/{sms_id}", response_model=list[SMSResponse]) async def update_sms( project_id: UUID, sms_id: UUID, new_sms: SmsUpdateRequest, current_user: User = Depends(deps.get_current_user), session: AsyncSession = Depends(deps.get_session), ): """Create a new to the project""" sms = await session.get(Sms, sms_id) if (sms is None) or sms.project_id != project_id: raise HTTPException(status_code=404, detail="Sms not found") update_object_from_payload(sms, new_sms.dict()) await session.commit() await session.refresh(sms) return sms @router.delete("/project/{id}/sms/{sms_id}") async def delete_sms( id: UUID, sms_id: UUID, current_user: User = Depends(deps.get_current_user), session: AsyncSession = Depends(deps.get_session), ): """Delete a sms from the project""" await session.execute( delete(Sms).where((Sms.id == sms_id) & (Sms.project_id == id)) ) await session.commit() @router.get("/sms/to-send", response_model=list[SMSResponse]) async def list_sms_to_send( current_user: User = Depends(deps.get_current_user), session: AsyncSession = Depends(deps.get_session), ): """List sms to""" results = await session.execute( select(Sms).where( (Sms.sending_time < datetime.datetime.now()) & Sms.send_time.is_not(None) ) ) return results.scalars() @router.get("/sms/not-send", response_model=list[SMSResponse]) async def list_not_sent( current_user: User = Depends(deps.get_current_user), session: AsyncSession = Depends(deps.get_session), ): """List sms that are not sent""" results = await session.execute(select(Sms).where(Sms.send_time.is_not(None))) return results.scalars() @router.get("/sms/future", response_model=list[SMSResponse]) async def list_future_sms( current_user: User = Depends(deps.get_current_user), session: AsyncSession = Depends(deps.get_session), ): """List sms that should be sent in the future""" results = await session.execute( select(Sms).where(Sms.sending_time > datetime.datetime.now()) ) return results.scalars()