110 lines
3.4 KiB
Python
110 lines
3.4 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from bson import ObjectId
|
|
from datetime import datetime, timezone
|
|
from typing import List
|
|
|
|
from app.database import get_db
|
|
from app.dependencies import get_current_user
|
|
from app.models.schedule import ScheduleIn, ScheduleOut
|
|
from app.services.email import send_interview_email
|
|
|
|
router = APIRouter(prefix="/api/schedule", tags=["schedule"])
|
|
|
|
@router.post("/", response_model=ScheduleOut, status_code=status.HTTP_201_CREATED)
|
|
async def create_schedule(
|
|
sched: ScheduleIn,
|
|
db=Depends(get_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
now = datetime.now(timezone.utc)
|
|
doc = sched.model_dump()
|
|
doc["user_id"] = current_user["google_id"]
|
|
doc["status"] = "Scheduled"
|
|
doc["created_at"] = now
|
|
doc["updated_at"] = now
|
|
|
|
result = await db["schedules"].insert_one(doc)
|
|
doc["id"] = str(result.inserted_id)
|
|
|
|
# Try sending email
|
|
email_sent = await send_interview_email(
|
|
candidate_email=sched.candidate_email,
|
|
candidate_name=sched.candidate_name,
|
|
date=sched.interview_date,
|
|
time=sched.interview_time,
|
|
meeting_link=sched.meeting_link or "",
|
|
notes=sched.notes or ""
|
|
)
|
|
|
|
# Could log email_sent status if needed
|
|
|
|
return doc
|
|
|
|
@router.get("/", response_model=List[ScheduleOut])
|
|
async def list_schedules(
|
|
db=Depends(get_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
cursor = db["schedules"].find({"user_id": current_user["google_id"]}).sort("interview_date", 1)
|
|
scheds = []
|
|
async for doc in cursor:
|
|
doc["id"] = str(doc.pop("_id"))
|
|
scheds.append(doc)
|
|
return scheds
|
|
|
|
@router.get("/{sched_id}", response_model=ScheduleOut)
|
|
async def get_schedule(
|
|
sched_id: str,
|
|
db=Depends(get_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
try:
|
|
oid = ObjectId(sched_id)
|
|
except:
|
|
raise HTTPException(status_code=400, detail="Invalid ID")
|
|
|
|
doc = await db["schedules"].find_one({"_id": oid, "user_id": current_user["google_id"]})
|
|
if not doc:
|
|
raise HTTPException(status_code=404, detail="Schedule not found")
|
|
|
|
doc["id"] = str(doc.pop("_id"))
|
|
return doc
|
|
|
|
@router.put("/{sched_id}/status", response_model=ScheduleOut)
|
|
async def update_schedule_status(
|
|
sched_id: str,
|
|
status: str,
|
|
db=Depends(get_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
try:
|
|
oid = ObjectId(sched_id)
|
|
except:
|
|
raise HTTPException(status_code=400, detail="Invalid ID")
|
|
|
|
result = await db["schedules"].find_one_and_update(
|
|
{"_id": oid, "user_id": current_user["google_id"]},
|
|
{"$set": {"status": status, "updated_at": datetime.now(timezone.utc)}},
|
|
return_document=True
|
|
)
|
|
|
|
if not result:
|
|
raise HTTPException(status_code=404, detail="Schedule not found")
|
|
|
|
result["id"] = str(result.pop("_id"))
|
|
return result
|
|
|
|
@router.delete("/{sched_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_schedule(
|
|
sched_id: str,
|
|
db=Depends(get_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
try:
|
|
oid = ObjectId(sched_id)
|
|
except:
|
|
raise HTTPException(status_code=400, detail="Invalid ID")
|
|
|
|
result = await db["schedules"].delete_one({"_id": oid, "user_id": current_user["google_id"]})
|
|
if result.deleted_count == 0:
|
|
raise HTTPException(status_code=404, detail="Schedule not found")
|