28 lines
1.2 KiB
Python
28 lines
1.2 KiB
Python
# app/models/broadcast.py
|
|
# -----------------------
|
|
# Stores admin-sent broadcast notification records.
|
|
# Each broadcast creates one Notification row per targeted user —
|
|
# the iOS app receives them via its existing poll cycle
|
|
# (GET /api/v1/notifications?since=...) with no new API endpoint required.
|
|
|
|
from app import db
|
|
from app.utils.time_utils import now_eastern
|
|
|
|
|
|
class Broadcast(db.Model):
|
|
__tablename__ = 'broadcasts'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
title = db.Column(db.String(255), nullable=False)
|
|
body = db.Column(db.Text, nullable=False)
|
|
# JSON-encoded list of role strings targeted, e.g. '["inspector","project_manager"]'
|
|
target_roles = db.Column(db.JSON, nullable=False, default=list)
|
|
sent_by_id = db.Column(db.Integer, db.ForeignKey('users.id', ondelete='SET NULL'), nullable=True)
|
|
sent_at = db.Column(db.DateTime, default=now_eastern, nullable=False)
|
|
# Number of Notification rows created (resolved at send time)
|
|
recipient_count = db.Column(db.Integer, default=0, nullable=False)
|
|
|
|
sent_by = db.relationship('User', foreign_keys=[sent_by_id])
|
|
|
|
def __repr__(self):
|
|
return f'<Broadcast {self.id} "{self.title[:30]}" roles={self.target_roles}>' |