Allow broadcast messages to be created with API key

When we have a public API there will be no human creating the broadcast
message. Instead it will be created by an API integration, authenticated
by a key (just like for emails or texts).

This updates the database to:
- add a new foreign key from BroadcastMessages to API keys
- add a `reference` column

It doesn’t change the model yet, because the model is used by previous
migrations, so would cause them to fail when run before the new columns
exist. We can make this change in later pull requests.
This commit is contained in:
Chris Hill-Scott
2021-01-13 11:21:42 +00:00
parent 8d86d70739
commit fe420448e1
2 changed files with 35 additions and 1 deletions

View File

@@ -2245,7 +2245,7 @@ class BroadcastMessage(db.Model):
cancelled_at = db.Column(db.DateTime, nullable=True)
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), nullable=False)
created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), nullable=True)
approved_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), nullable=True)
cancelled_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), nullable=True)
@@ -2253,6 +2253,13 @@ class BroadcastMessage(db.Model):
approved_by = db.relationship('User', foreign_keys=[approved_by_id])
cancelled_by = db.relationship('User', foreign_keys=[cancelled_by_id])
api_key_id = db.Column(UUID(as_uuid=True), db.ForeignKey('api_keys.id'), nullable=True)
api_key = db.relationship('ApiKey')
reference = db.Column(db.String(255), nullable=True)
CheckConstraint("created_by_id is not null or api_key_id is not null")
@property
def personalisation(self):
if self._personalisation:
@@ -2266,6 +2273,7 @@ class BroadcastMessage(db.Model):
def serialize(self):
return {
'id': str(self.id),
'reference': self.reference,
'service_id': str(self.service_id),