Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class Tablenames(Enum):
TIMED_EXECUTIONS = "timed_executions"
CONVERSATION_SHARE = "conversation_share"
CONVERSATION_GLOBAL_SHARE = "conversation_global_share"
INBOX_MAIL = "inbox_mail"

def snake_case_to_pascal_case(self):
# the type name (written in PascalCase) of a table is needed to create backrefs
Expand Down
130 changes: 130 additions & 0 deletions global_objects/inbox_mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from typing import Dict, List, Optional, Any
from submodules.model.util import sql_alchemy_to_dict

from ..session import session
from sqlalchemy import cast, String, func, desc

from submodules.model.business_objects import general
from submodules.model.models import InboxMail
from sqlalchemy import or_


def get_by_thread(
org_id: str,
user_id: str,
thread_id: str,
) -> List[InboxMail]:

return (
session.query(InboxMail)
.filter(
InboxMail.organization_id == org_id,
InboxMail.thread_id == thread_id,
or_(
InboxMail.recipient_id == user_id,
InboxMail.sender_id == user_id,
),
)
.order_by(desc(InboxMail.created_at))
.all()
)


def get_overview_by_threads(
org_id: str,
user_id: str,
page: int = 1,
limit: int = 10,
) -> Dict[str, Any]:
subquery = (
session.query(
InboxMail.thread_id,
func.max(InboxMail.created_at).label("latest_mail_time"),
)
.filter(
InboxMail.organization_id == org_id,
or_(
InboxMail.recipient_id == user_id,
InboxMail.sender_id == user_id,
),
)
.group_by(InboxMail.thread_id)
.subquery()
)

latest_mails_query = (
session.query(InboxMail)
.join(
subquery,
(InboxMail.thread_id == subquery.c.thread_id)
& (InboxMail.created_at == subquery.c.latest_mail_time),
)
.order_by(desc(InboxMail.created_at))
)

total_threads = latest_mails_query.count()
latest_mails = latest_mails_query.offset((page - 1) * limit).limit(limit).all()

threads = []
for mail in latest_mails:
total_mails = (
session.query(func.count(InboxMail.id))
.filter(
InboxMail.thread_id == mail.thread_id,
InboxMail.organization_id == org_id,
)
.scalar()
)

threads.append(
{
"threadId": mail.thread_id,
"latestMail": sql_alchemy_to_dict(mail),
"totalMails": total_mails,
}
)

return {
"totalThreads": total_threads,
"page": page,
"limit": limit,
"threads": threads,
}


def create_by_thread(
org_id: str,
sender_id: str,
recipient_ids: List[str],
subject: str,
content: str,
meta_data: Optional[Dict] = None,
parent_id: Optional[str] = None,
thread_id: Optional[str] = None,
is_important: bool = False,
with_commit: bool = True,
) -> List[InboxMail]:
mail_entities: List[InboxMail] = []
for rid in recipient_ids:
other_recipient_ids = [r for r in recipient_ids if r != rid]

mail_entity = InboxMail(
organization_id=org_id,
sender_id=sender_id,
recipient_id=rid,
other_recipient_ids=other_recipient_ids,
subject=subject,
content=content,
meta_data=meta_data or {},
thread_id=thread_id,
parent_id=parent_id,
is_important=is_important,
)

mail_entities.append(mail_entity)

general.add_all(mail_entities)
if with_commit:
general.commit()

return mail_entities
37 changes: 37 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2561,3 +2561,40 @@ class ConversationGlobalShare(Base):
index=True,
)
created_at = Column(DateTime, default=sql.func.now())


class InboxMail(Base):
__tablename__ = Tablenames.INBOX_MAIL.value
__table_args__ = {"schema": "global"}
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
organization_id = Column(
UUID(as_uuid=True),
ForeignKey(f"{Tablenames.ORGANIZATION.value}.id", ondelete="CASCADE"),
index=True,
)
created_at = Column(DateTime, default=sql.func.now())
sender_id = Column(
UUID(as_uuid=True),
ForeignKey(f"{Tablenames.USER.value}.id", ondelete="SET NULL"),
index=True,
)
recipient_id = Column(
UUID(as_uuid=True),
ForeignKey(f"{Tablenames.USER.value}.id", ondelete="CASCADE"),
index=True,
)
other_recipient_ids = Column(JSON)
thread_id = Column(UUID(as_uuid=True), index=True, unique=True, default=uuid.uuid4)
parent_id = Column(
UUID(as_uuid=True),
ForeignKey("global.inbox_mail.id", ondelete="SET NULL"),
nullable=True,
index=True,
)
subject = Column(String)
content = Column(String)
meta_data = Column(JSON)

is_seen = Column(Boolean, default=False)
is_important = Column(Boolean, default=False)
being_working_on = Column(Boolean, default=False)