krz/hutch-notify

Notification server for Hutch.

clone: git clone https://gitbay.org/krz/hutch-notify.git

main: app/models.py · raw

 1from datetime import datetime
 2from sqlalchemy import String, Boolean, Integer, DateTime, ForeignKey, Text, UniqueConstraint
 3from sqlalchemy.orm import Mapped, mapped_column, relationship
 4from app.db import Base
 5
 6
 7class Device(Base):
 8    __tablename__ = "devices"
 9    __table_args__ = (UniqueConstraint("apns_token", "bundle_id", name="uq_device_token_bundle"),)
10
11    id: Mapped[int] = mapped_column(Integer, primary_key=True)
12    user_id: Mapped[str] = mapped_column(String(255), index=True)
13    apns_token: Mapped[str] = mapped_column(Text)
14    apns_env: Mapped[str] = mapped_column(String(32))
15    bundle_id: Mapped[str] = mapped_column(String(255))
16    platform: Mapped[str] = mapped_column(String(32), default="ios")
17    app_version: Mapped[str | None] = mapped_column(String(64), nullable=True)
18    device_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
19    is_enabled: Mapped[bool] = mapped_column(Boolean, default=True)
20    last_seen_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
21    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
22    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
23
24    subscriptions = relationship("Subscription", back_populates="device", cascade="all, delete-orphan")
25
26
27class Subscription(Base):
28    __tablename__ = "subscriptions"
29
30    id: Mapped[int] = mapped_column(Integer, primary_key=True)
31    device_id: Mapped[int] = mapped_column(ForeignKey("devices.id", ondelete="CASCADE"), index=True)
32    source_type: Mapped[str] = mapped_column(String(64), index=True)
33    source_id: Mapped[str] = mapped_column(String(255), index=True)
34    event_type: Mapped[str] = mapped_column(String(64), index=True)
35    is_enabled: Mapped[bool] = mapped_column(Boolean, default=True)
36    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
37    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
38
39    device = relationship("Device", back_populates="subscriptions")
40
41
42class Event(Base):
43    __tablename__ = "events"
44    __table_args__ = (UniqueConstraint("dedupe_key", name="uq_event_dedupe_key"),)
45
46    id: Mapped[int] = mapped_column(Integer, primary_key=True)
47    source_type: Mapped[str] = mapped_column(String(64), index=True)
48    source_id: Mapped[str] = mapped_column(String(255), index=True)
49    event_type: Mapped[str] = mapped_column(String(64), index=True)
50    external_id: Mapped[str] = mapped_column(String(255), index=True)
51    dedupe_key: Mapped[str] = mapped_column(String(255))
52    title: Mapped[str] = mapped_column(String(255))
53    body: Mapped[str] = mapped_column(Text)
54    deep_link: Mapped[str | None] = mapped_column(String(512), nullable=True)
55    payload_json: Mapped[str | None] = mapped_column(Text, nullable=True)
56    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
57
58
59class Delivery(Base):
60    __tablename__ = "deliveries"
61
62    id: Mapped[int] = mapped_column(Integer, primary_key=True)
63    event_id: Mapped[int] = mapped_column(ForeignKey("events.id", ondelete="CASCADE"), index=True)
64    device_id: Mapped[int] = mapped_column(ForeignKey("devices.id", ondelete="CASCADE"), index=True)
65    status: Mapped[str] = mapped_column(String(32), default="pending", index=True)
66    attempt_count: Mapped[int] = mapped_column(Integer, default=0)
67    last_error: Mapped[str | None] = mapped_column(Text, nullable=True)
68    apns_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
69    scheduled_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
70    sent_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
71    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
72    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)