forked from sr2/cloud-api
feat: condensed org get endpoints
The process also added improved ORM relationships for multiple models.
This commit is contained in:
parent
a80767d870
commit
707482adc2
3 changed files with 8 additions and 12 deletions
|
|
@ -33,6 +33,11 @@ class Organisation(Base):
|
|||
)
|
||||
|
||||
group_rel = relationship("Group", back_populates="org_rel")
|
||||
root_user_rel = relationship("User", foreign_keys=[root_user_id])
|
||||
|
||||
@property
|
||||
def root_user_email(self):
|
||||
return self.root_user_rel.email if self.root_user_rel else None
|
||||
|
||||
|
||||
class OrgUsers(Base):
|
||||
|
|
|
|||
|
|
@ -38,17 +38,14 @@ router = APIRouter(
|
|||
|
||||
|
||||
@router.get("/id/{org_id}", response_model=OrgOrgGetResponse)
|
||||
async def get_org_by_id(db: db_dependency, org_id: Annotated[int, Path(gt=0)]):
|
||||
org_model = (db.query(Org).filter(Org.id == org_id).first())
|
||||
if org_model is None:
|
||||
raise HTTPException(status_code=404, detail="Organisation not found")
|
||||
|
||||
async def get_org_by_id(db: db_dependency, org_model: org_model_dependency, org_id: Annotated[int, Path(gt=0)]):
|
||||
response = {
|
||||
"name": org_model.name,
|
||||
"status": org_model.status,
|
||||
"owner_contact": (db.query(Contact).filter(Contact.id == org_model.owner_contact_id).first()),
|
||||
"billing_contact": (db.query(Contact).filter(Contact.id == org_model.billing_contact_id).first()),
|
||||
"security_contact": (db.query(Contact).filter(Contact.id == org_model.security_contact_id).first()),
|
||||
"root_user": org_model.root_user_email
|
||||
}
|
||||
|
||||
return response
|
||||
|
|
@ -173,13 +170,6 @@ async def get_contact(db: db_dependency, contact_type: ContactType, org_id: Anno
|
|||
return contact_model
|
||||
|
||||
|
||||
@router.get("/{org_id}/root_user")
|
||||
async def get_org_root_user(db: db_dependency, org_model: org_model_dependency, org_id: Annotated[int, Path(gt=0)]):
|
||||
root_user = org_model.root_user_id
|
||||
|
||||
return {"root_user": root_user}
|
||||
|
||||
|
||||
@router.patch("/{org_id}/root_user")
|
||||
async def update_root_user(db: db_dependency, org_model: org_model_dependency, org_id: Annotated[int, Path(gt=0)], root_user: Annotated[int, Query(gt=0)]):
|
||||
# TODO: Request model, ditch query
|
||||
|
|
|
|||
|
|
@ -52,6 +52,7 @@ class OrgContactGetResponse(CustomBaseModel):
|
|||
class OrgOrgGetResponse(CustomBaseModel):
|
||||
name: str
|
||||
status: Status
|
||||
root_user: Optional[str] = None
|
||||
owner_contact: Optional[OrgContactGetResponse] = None
|
||||
billing_contact: Optional[OrgContactGetResponse] = None
|
||||
security_contact: Optional[OrgContactGetResponse] = None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue