""" Database models for organisation module Models: - Organisation: id[pk], name, status, intake_questionnaire, billing_contact_id[fk], security_contact_id[fk], owner_contact_id[fk] - OrgUsers: org_id[fk][cpk], user_id[fk][cpk], is_admin """ from sqlalchemy import Column, Integer, String, ForeignKey, JSON from sqlalchemy.orm import relationship from src.database import Base class Organisation(Base): __tablename__ = "organisation" id = Column(Integer, primary_key=True) name = Column(String, unique=True) status = Column(String, default="partial") intake_questionnaire = Column(JSON) root_user_id = Column(Integer, ForeignKey("user.id")) billing_contact_id = Column(Integer, ForeignKey("contact.id")) security_contact_id = Column(Integer, ForeignKey("contact.id")) owner_contact_id = Column(Integer, ForeignKey("contact.id")) user_rel = relationship( "User", secondary="orgusers", back_populates="organisation_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): __tablename__ = "orgusers" org_id = Column(Integer, ForeignKey("organisation.id", ondelete="CASCADE"), primary_key=True) user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE"), primary_key=True)