43 lines
1.7 KiB
Python
43 lines
1.7 KiB
Python
from beanie import PydanticObjectId
|
|
from fastapi import HTTPException, Depends
|
|
|
|
from firm.core.routes import get_crud_router
|
|
from firm.core.depends import get_authed_tenant_registry
|
|
|
|
from firm.contract.models import ContractDraft, ContractDraftStatus, ContractDraftFilters
|
|
from firm.contract.schemas import ContractDraftCreate, ContractDraftRead, ContractDraftUpdate
|
|
|
|
draft_router = get_crud_router(ContractDraft, ContractDraftCreate, ContractDraftRead, ContractDraftUpdate, ContractDraftFilters)
|
|
|
|
del(draft_router.routes[3]) #update route
|
|
del(draft_router.routes[1]) #post route
|
|
|
|
|
|
@draft_router.post("/", response_description="Contract Draft added to the database")
|
|
async def create(schema: ContractDraftCreate, reg=Depends(get_authed_tenant_registry)) -> ContractDraftRead:
|
|
await schema.validate_foreign_key(reg.db)
|
|
record = await ContractDraft.create(reg.db, schema)
|
|
await record.check_is_ready(reg.db)
|
|
|
|
return ContractDraftRead.from_model(record)
|
|
|
|
|
|
@draft_router.put("/{record_id}", response_description="Contract Draft record updated")
|
|
async def update(record_id: PydanticObjectId, schema: ContractDraftUpdate, reg=Depends(get_authed_tenant_registry)) -> ContractDraftRead:
|
|
record = await ContractDraft.get(reg.db, record_id)
|
|
if not record:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail="Contract Draft record not found!"
|
|
)
|
|
if record.status == ContractDraftStatus.published:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="Contract Draft has already been published"
|
|
)
|
|
|
|
record = await ContractDraft.update(reg.db, record, schema)
|
|
await record.check_is_ready(reg.db)
|
|
|
|
return ContractDraftRead.from_model(record)
|