Core modules: - Laws: CRUD, search, AI-powered QA - Analysis: legal research and case management - Contracts: lifecycle management with templates - Signatures: electronic signature workflow Infrastructure: - FastAPI + SQLite + async SQLAlchemy - Docker deployment support - 54 unit tests passing Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
181 lines
5.1 KiB
Python
181 lines
5.1 KiB
Python
"""Contract API endpoints."""
|
|
from typing import Optional
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_db
|
|
from app.models.contract import ContractStatus
|
|
from app.schemas.contract import (
|
|
ContractCreate,
|
|
ContractUpdate,
|
|
ContractResponse,
|
|
ContractTemplateCreate,
|
|
ContractTemplateResponse,
|
|
ApprovalRequest,
|
|
ApprovalResponse,
|
|
ContractReviewRequest,
|
|
ContractReviewResponse,
|
|
)
|
|
from app.services.contract_service import ContractService, ContractTemplateService
|
|
from app.services.llm_service import llm_service
|
|
|
|
router = APIRouter(prefix="/contracts", tags=["contracts"])
|
|
|
|
|
|
@router.post("", response_model=ContractResponse)
|
|
async def create_contract(
|
|
contract_data: ContractCreate,
|
|
user_id: int = 1, # TODO: Get from auth
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""Create a new contract."""
|
|
service = ContractService(db)
|
|
contract = await service.create_contract(
|
|
created_by=user_id,
|
|
**contract_data.model_dump(exclude={"template_id"}),
|
|
template_id=contract_data.template_id,
|
|
)
|
|
return contract
|
|
|
|
|
|
@router.get("", response_model=list[ContractResponse])
|
|
async def list_contracts(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(20, ge=1, le=100),
|
|
status: Optional[str] = Query(None),
|
|
user_id: int = 1, # TODO: Get from auth
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""List contracts."""
|
|
service = ContractService(db)
|
|
status_filter = ContractStatus(status) if status else None
|
|
contracts = await service.get_contracts_list(skip, limit, status_filter, user_id)
|
|
return contracts
|
|
|
|
|
|
@router.get("/{contract_id}", response_model=ContractResponse)
|
|
async def get_contract(
|
|
contract_id: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""Get a contract by ID."""
|
|
service = ContractService(db)
|
|
contract = await service.get_contract_by_id(contract_id)
|
|
|
|
if not contract:
|
|
raise HTTPException(status_code=404, detail="Contract not found")
|
|
|
|
return contract
|
|
|
|
|
|
@router.put("/{contract_id}", response_model=ContractResponse)
|
|
async def update_contract(
|
|
contract_id: int,
|
|
contract_data: ContractUpdate,
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""Update a contract."""
|
|
service = ContractService(db)
|
|
contract = await service.update_contract(
|
|
contract_id,
|
|
**contract_data.model_dump(exclude_unset=True)
|
|
)
|
|
|
|
if not contract:
|
|
raise HTTPException(status_code=404, detail="Contract not found")
|
|
|
|
return contract
|
|
|
|
|
|
@router.post("/{contract_id}/submit", response_model=ContractResponse)
|
|
async def submit_contract(
|
|
contract_id: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""Submit a contract for approval."""
|
|
service = ContractService(db)
|
|
contract = await service.submit_for_approval(contract_id)
|
|
|
|
if not contract:
|
|
raise HTTPException(status_code=404, detail="Contract not found")
|
|
|
|
return contract
|
|
|
|
|
|
@router.post("/{contract_id}/approve", response_model=ApprovalResponse)
|
|
async def approve_contract(
|
|
contract_id: int,
|
|
approval_data: ApprovalRequest,
|
|
user_id: int = 1, # TODO: Get from auth
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""Approve a contract."""
|
|
service = ContractService(db)
|
|
approval = await service.approve_contract(
|
|
contract_id,
|
|
user_id,
|
|
approval_data.comment,
|
|
)
|
|
return approval
|
|
|
|
|
|
@router.post("/{contract_id}/reject", response_model=ApprovalResponse)
|
|
async def reject_contract(
|
|
contract_id: int,
|
|
approval_data: ApprovalRequest,
|
|
user_id: int = 1, # TODO: Get from auth
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""Reject a contract."""
|
|
service = ContractService(db)
|
|
approval = await service.reject_contract(
|
|
contract_id,
|
|
user_id,
|
|
approval_data.comment,
|
|
)
|
|
return approval
|
|
|
|
|
|
@router.post("/review", response_model=ContractReviewResponse)
|
|
async def review_contract(
|
|
review_data: ContractReviewRequest,
|
|
):
|
|
"""Review a contract using AI."""
|
|
result = await llm_service.review_contract(
|
|
contract_content=review_data.contract_content,
|
|
contract_type=review_data.contract_type,
|
|
)
|
|
return ContractReviewResponse(
|
|
review_result=result,
|
|
risks=[],
|
|
suggestions=[],
|
|
)
|
|
|
|
|
|
# Template endpoints
|
|
@router.post("/templates", response_model=ContractTemplateResponse)
|
|
async def create_template(
|
|
template_data: ContractTemplateCreate,
|
|
user_id: int = 1, # TODO: Get from auth
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""Create a contract template."""
|
|
service = ContractTemplateService(db)
|
|
template = await service.create_template(
|
|
created_by=user_id,
|
|
**template_data.model_dump(),
|
|
)
|
|
return template
|
|
|
|
|
|
@router.get("/templates", response_model=list[ContractTemplateResponse])
|
|
async def list_templates(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(20, ge=1, le=100),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""List contract templates."""
|
|
service = ContractTemplateService(db)
|
|
templates = await service.get_templates_list(skip, limit)
|
|
return templates
|