70 lines
2.7 KiB
Python
70 lines
2.7 KiB
Python
import uuid
|
|
from fastapi import Depends, HTTPException
|
|
|
|
from firm.core.routes import get_crud_router
|
|
from firm.core.depends import get_authed_tenant_registry
|
|
|
|
from firm.contract.models import Contract, ContractDraft, ContractDraftStatus, replace_variables_in_value, ContractFilters
|
|
from firm.contract.schemas import ContractCreate, ContractRead, ContractUpdate, ContractInit
|
|
|
|
from firm.entity.models import Entity
|
|
from firm.template.models import ProvisionTemplate
|
|
|
|
|
|
contract_router = get_crud_router(Contract, ContractCreate, ContractRead, ContractUpdate, ContractFilters)
|
|
del(contract_router.routes[4]) #delete
|
|
del(contract_router.routes[3]) #update
|
|
del(contract_router.routes[1]) #create
|
|
|
|
@contract_router.post("/", response_description="Contract Successfully created")
|
|
async def create(schema: ContractCreate, reg=Depends(get_authed_tenant_registry)) -> ContractRead:
|
|
await schema.validate_foreign_key(reg.db)
|
|
|
|
draft = await ContractDraft.get(reg.db, schema.draft_id)
|
|
if not draft:
|
|
raise HTTPException(status_code=404, detail=f"Contract draft not found!")
|
|
|
|
for v in draft.variables:
|
|
if not v.key or not v.value:
|
|
raise HTTPException(status_code=400, detail="Variable {} is invalid".format(v))
|
|
|
|
contract_dict = schema.model_dump()
|
|
del(contract_dict['draft_id'])
|
|
|
|
contract_dict['lawyer'] = reg.partner.model_dump()
|
|
|
|
contract_dict['name'] = draft.name
|
|
contract_dict['title'] = draft.title
|
|
parties = []
|
|
for p in draft.parties:
|
|
parties.append({
|
|
'entity': await Entity.get(reg.db, p.entity_id),
|
|
'part': p.part,
|
|
'representative': await Entity.get(reg.db, p.representative_id) if p.representative_id else None,
|
|
'signature_uuid': str(uuid.uuid4())
|
|
})
|
|
|
|
contract_dict['parties'] = parties
|
|
|
|
provisions = []
|
|
for p in draft.provisions:
|
|
p = p.provision
|
|
provision = await ProvisionTemplate.get(reg.db, p.provision_template_id) if p.type == "template" \
|
|
else p
|
|
|
|
provisions.append({
|
|
'title': replace_variables_in_value(draft.variables, provision.title),
|
|
'body': replace_variables_in_value(draft.variables, provision.body)
|
|
})
|
|
|
|
contract_dict['provisions'] = provisions
|
|
|
|
record = await Contract.create(reg.db, ContractInit(**contract_dict))
|
|
await draft.update_status(reg.db, ContractDraftStatus.published)
|
|
|
|
return ContractRead.from_model(record)
|
|
|
|
@contract_router.put("/{record_id}", response_description="")
|
|
async def update(record_id: str, contract_form: ContractUpdate, reg=Depends(get_authed_tenant_registry)) -> ContractRead:
|
|
raise HTTPException(status_code=400, detail="No modification on contract")
|