forked from sr2/cloud-api
Blank contacts are now generated on org creation and assigned to each contact type. These contacts are linked to the org, only accessible to the org, and removed when the org is removed. With this all contact endpoints have been removed. Contact manipulation is done via the org only.
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
"""
|
|
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
|
|
|
|
billing_contact_rel = relationship("Contact", foreign_keys=[billing_contact_id])
|
|
security_contact_rel = relationship("Contact", foreign_keys=[security_contact_id])
|
|
owner_contact_rel = relationship("Contact", foreign_keys=[owner_contact_id])
|
|
|
|
|
|
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)
|