feat: condensed org get endpoints

The process also added improved ORM relationships for multiple models.
This commit is contained in:
Chris Milne 2026-05-25 12:40:28 +01:00
parent a80767d870
commit 707482adc2
3 changed files with 8 additions and 12 deletions

View file

@ -33,6 +33,11 @@ class Organisation(Base):
) )
group_rel = relationship("Group", back_populates="org_rel") 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): class OrgUsers(Base):

View file

@ -38,17 +38,14 @@ router = APIRouter(
@router.get("/id/{org_id}", response_model=OrgOrgGetResponse) @router.get("/id/{org_id}", response_model=OrgOrgGetResponse)
async def get_org_by_id(db: db_dependency, org_id: Annotated[int, Path(gt=0)]): async def get_org_by_id(db: db_dependency, org_model: org_model_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")
response = { response = {
"name": org_model.name, "name": org_model.name,
"status": org_model.status, "status": org_model.status,
"owner_contact": (db.query(Contact).filter(Contact.id == org_model.owner_contact_id).first()), "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()), "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()), "security_contact": (db.query(Contact).filter(Contact.id == org_model.security_contact_id).first()),
"root_user": org_model.root_user_email
} }
return response return response
@ -173,13 +170,6 @@ async def get_contact(db: db_dependency, contact_type: ContactType, org_id: Anno
return contact_model 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") @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)]): 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 # TODO: Request model, ditch query

View file

@ -52,6 +52,7 @@ class OrgContactGetResponse(CustomBaseModel):
class OrgOrgGetResponse(CustomBaseModel): class OrgOrgGetResponse(CustomBaseModel):
name: str name: str
status: Status status: Status
root_user: Optional[str] = None
owner_contact: Optional[OrgContactGetResponse] = None owner_contact: Optional[OrgContactGetResponse] = None
billing_contact: Optional[OrgContactGetResponse] = None billing_contact: Optional[OrgContactGetResponse] = None
security_contact: Optional[OrgContactGetResponse] = None security_contact: Optional[OrgContactGetResponse] = None