2026-04-06 12:41:49 +01:00
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
|
"""
|
2026-05-25 09:05:17 +01:00
|
|
|
from sqlalchemy import Column, Integer, String, ForeignKey, JSON
|
2026-05-25 10:21:15 +01:00
|
|
|
from sqlalchemy.orm import relationship
|
2026-04-06 12:41:49 +01:00
|
|
|
|
|
|
|
|
from src.database import Base
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Organisation(Base):
|
|
|
|
|
__tablename__ = "organisation"
|
|
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
2026-05-25 09:05:17 +01:00
|
|
|
name = Column(String, unique=True)
|
2026-04-06 12:41:49 +01:00
|
|
|
status = Column(String, default="partial")
|
|
|
|
|
intake_questionnaire = Column(JSON)
|
|
|
|
|
|
2026-05-25 09:05:17 +01:00
|
|
|
root_user_id = Column(Integer, ForeignKey("user.id"))
|
|
|
|
|
|
2026-04-06 12:41:49 +01:00
|
|
|
billing_contact_id = Column(Integer, ForeignKey("contact.id"))
|
|
|
|
|
security_contact_id = Column(Integer, ForeignKey("contact.id"))
|
|
|
|
|
owner_contact_id = Column(Integer, ForeignKey("contact.id"))
|
|
|
|
|
|
2026-05-25 12:06:24 +01:00
|
|
|
user_rel = relationship(
|
2026-05-25 10:21:15 +01:00
|
|
|
"User",
|
|
|
|
|
secondary="orgusers",
|
2026-05-25 12:06:24 +01:00
|
|
|
back_populates="organisation_rel"
|
2026-05-25 10:21:15 +01:00
|
|
|
)
|
|
|
|
|
|
2026-05-25 12:06:24 +01:00
|
|
|
group_rel = relationship("Group", back_populates="org_rel")
|
2026-05-25 12:40:28 +01:00
|
|
|
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
|
2026-05-25 12:06:24 +01:00
|
|
|
|
2026-04-06 12:41:49 +01:00
|
|
|
|
|
|
|
|
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)
|