Adding chtlawfirm to the api
This commit is contained in:
69
api/rpk-api/firm/contract/routes_contract.py
Normal file
69
api/rpk-api/firm/contract/routes_contract.py
Normal file
@@ -0,0 +1,69 @@
|
||||
import uuid
|
||||
from fastapi import Depends, HTTPException
|
||||
|
||||
from firm.core.routes import get_crud_router, get_logged_tenant_db_cursor
|
||||
|
||||
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, db=Depends(get_logged_tenant_db_cursor)) -> ContractRead:
|
||||
await schema.validate_foreign_key(db)
|
||||
|
||||
draft = await ContractDraft.get(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'])
|
||||
|
||||
lawyer = await Entity.get(db, db.user.entity_id)
|
||||
contract_dict['lawyer'] = lawyer.model_dump()
|
||||
|
||||
contract_dict['name'] = draft.name
|
||||
contract_dict['title'] = draft.title
|
||||
parties = []
|
||||
for p in draft.parties:
|
||||
parties.append({
|
||||
'entity': await Entity.get(db, p.entity_id),
|
||||
'part': p.part,
|
||||
'representative': await Entity.get(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(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(db, ContractInit(**contract_dict))
|
||||
await draft.update_status(db, ContractDraftStatus.published)
|
||||
|
||||
return ContractRead.from_model(record)
|
||||
|
||||
@contract_router.put("/{record_id}", response_description="")
|
||||
async def update(record_id: str, contract_form: ContractUpdate, db=Depends(get_logged_tenant_db_cursor)) -> ContractRead:
|
||||
raise HTTPException(status_code=400, detail="No modification on contract")
|
||||
Reference in New Issue
Block a user