HR-ATS-Portal/backend/talent/app.py

142 lines
4.7 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, Query
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from sqlalchemy.ext.asyncio import AsyncSession
from db_setup import get_session
from talent.views import Talent
from users.permissions import PermissionTag, require_permission
router = APIRouter()
class TalentRunStart(BaseModel):
max_results: int | None = None
location: str | None = None
keywords: str | None = None
class OutreachStatusUpdate(BaseModel):
outreach_status: str
@router.post("/talent/runs/start")
async def start_talent_run(
payload: TalentRunStart,
job_post_id: str = Query(...),
current_user: dict = Depends(require_permission(PermissionTag.TALENT_CREATE)),
session: AsyncSession = Depends(get_session),
):
try:
service = Talent(session=session)
data = await service.start_run(
job_post_id, payload.model_dump(exclude_unset=True), current_user
)
return JSONResponse(content={"data": data, "status_code": 200})
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/talent/runs/status")
async def talent_run_status(
run_id: str = Query(...),
current_user: dict = Depends(require_permission(PermissionTag.TALENT_VIEW)),
session: AsyncSession = Depends(get_session),
):
try:
service = Talent(session=session)
data = await service.run_status(run_id)
return JSONResponse(content={"data": data, "status_code": 200})
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/talent/runs/fetch")
async def fetch_talent_runs(
job_post_id: str = Query(...),
current_user: dict = Depends(require_permission(PermissionTag.TALENT_VIEW)),
session: AsyncSession = Depends(get_session),
):
try:
service = Talent(session=session)
data, total = await service.fetch_runs(job_post_id)
return JSONResponse(content={"data": data, "total": total, "status_code": 200})
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/talent/profiles/fetch")
async def fetch_talent_profiles(
job_post_id: str = Query(...),
search: str | None = Query(None),
top: int | None = Query(None, ge=1, le=500),
skip: int = Query(0, ge=0),
current_user: dict = Depends(require_permission(PermissionTag.TALENT_VIEW)),
session: AsyncSession = Depends(get_session),
):
try:
service = Talent(session=session)
data, total = await service.fetch_profiles(job_post_id, search=search, top=top, skip=skip)
return JSONResponse(content={"data": data, "total": total, "status_code": 200})
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/talent/profiles/fetch_by_id")
async def fetch_talent_profile(
profile_id: str = Query(...),
current_user: dict = Depends(require_permission(PermissionTag.TALENT_VIEW)),
session: AsyncSession = Depends(get_session),
):
try:
service = Talent(session=session)
data = await service.get_profile(profile_id)
return JSONResponse(content={"data": data, "status_code": 200})
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.patch("/talent/profiles/outreach")
async def set_talent_outreach_status(
payload: OutreachStatusUpdate,
profile_id: str = Query(...),
current_user: dict = Depends(require_permission(PermissionTag.TALENT_EDIT)),
session: AsyncSession = Depends(get_session),
):
try:
service = Talent(session=session)
data = await service.set_outreach_status(
profile_id, payload.model_dump(exclude_unset=True), current_user
)
return JSONResponse(content={"data": data, "status_code": 200})
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.delete("/talent/profiles/delete")
async def delete_talent_profile(
profile_id: str = Query(...),
current_user: dict = Depends(require_permission(PermissionTag.TALENT_DELETE)),
session: AsyncSession = Depends(get_session),
):
try:
service = Talent(session=session)
data = await service.delete_profile(profile_id)
return JSONResponse(content={"data": data, "status_code": 200})
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))