From 91f5be835a9f1a47b447d96c52ff3b8b0d44437e Mon Sep 17 00:00:00 2001 From: David McDonald Date: Thu, 28 Jan 2021 13:57:33 +0000 Subject: [PATCH] Add DB table for service broadcast settings This will allow us to store details of which channel a service should be sending to. See the comment about how all broadcast services can have a row in the table but may not at the moment. This has been done for speed as it's the quickest way to let us set up different services to send to different channels for some needed testing with the mobile handset providers in the coming week. --- app/models.py | 34 +++++++++++++++ app/schemas.py | 1 + .../0342_service_broadcast_settings.py | 43 +++++++++++++++++++ tests/app/service/test_rest.py | 1 - tests/conftest.py | 3 +- 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/0342_service_broadcast_settings.py diff --git a/app/models.py b/app/models.py index 91e2f35b9..64b6e4575 100644 --- a/app/models.py +++ b/app/models.py @@ -505,6 +505,7 @@ class Service(db.Model, Versioned): backref=db.backref('services', lazy='dynamic')) allowed_broadcast_provider = association_proxy('service_broadcast_provider_restriction', 'provider') + broadcast_channel = association_proxy('service_broadcast_settings', 'channel') @classmethod def from_json(cls, data): @@ -2519,6 +2520,39 @@ class BroadcastProviderMessageNumber(db.Model): ) +class ServiceBroadcastSettings(db.Model): + """ + For the moment, broadcasts services CAN have a row in this table which will configure which broadcast + channel they will send to. If they don't then we will assume they should send to the test channel. + + There should only be one row per service in this table, and this is enforced by + the service_id being a primary key. + + TODO: We should enforce that every broadcast service will have a row in this table. We will need to do + this when the admin turns a service into a broadcast service, it inserts a row into this table and adds + the service permission for broadcasts for the service. Once that is up and running, we then should write + a DB migration to create rows for all broadcast services that do not have one yet in this table. + + TODO: Move functionality on the ServiceBroadcastProviderRestriction into this table and remove the + ServiceBroadcastProviderRestriction table + """ + __tablename__ = "service_broadcast_settings" + + service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), primary_key=True, nullable=False) + service = db.relationship(Service, backref=db.backref("service_broadcast_settings", uselist=False)) + channel = db.Column( + db.String(255), db.ForeignKey('broadcast_channel_types.name'), nullable=False + ) + created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) + updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow) + + +class BroadcastChannelTypes(db.Model): + __tablename__ = 'broadcast_channel_types' + + name = db.Column(db.String(255), primary_key=True) + + class ServiceBroadcastProviderRestriction(db.Model): """ Most services don't send broadcasts. Of those that do, most send to all broadcast providers. diff --git a/app/schemas.py b/app/schemas.py index 4451471b8..7d9a59d06 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -271,6 +271,7 @@ class ServiceSchema(BaseSchema, UUIDsAsStringsMixin): 'reply_to_email_addresses', 'returned_letters', 'service_broadcast_provider_restriction', + 'service_broadcast_settings', 'service_notification_stats', 'service_provider_stats', 'service_sms_senders', diff --git a/migrations/versions/0342_service_broadcast_settings.py b/migrations/versions/0342_service_broadcast_settings.py new file mode 100644 index 000000000..ba706f562 --- /dev/null +++ b/migrations/versions/0342_service_broadcast_settings.py @@ -0,0 +1,43 @@ +""" + +Revision ID: 0342_service_broadcast_settings +Revises: 0341_new_letter_rates +Create Date: 2021-01-28 21:30:23.102340 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +revision = '0342_service_broadcast_settings' +down_revision = '0341_new_letter_rates' + +CHANNEL_TYPES = ["test", "severe"] + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('broadcast_channel_types', + sa.Column('name', sa.String(length=255), nullable=False), + sa.PrimaryKeyConstraint('name') + ) + op.create_table('service_broadcast_settings', + sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('channel', sa.String(length=255), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.ForeignKeyConstraint(['channel'], ['broadcast_channel_types.name'], ), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), + sa.PrimaryKeyConstraint('service_id') + ) + # ### end Alembic commands ### + + for channel in CHANNEL_TYPES: + op.execute(f"INSERT INTO broadcast_channel_types VALUES ('{channel}')") + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('service_broadcast_settings') + op.drop_table('broadcast_channel_types') + # ### end Alembic commands ### diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 6aac914ea..937518a2a 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -36,7 +36,6 @@ from app.models import ( INBOUND_SMS_TYPE, NOTIFICATION_RETURNED_LETTER, UPLOAD_LETTERS, - ) from tests import create_authorization_header from tests.app.db import ( diff --git a/tests/conftest.py b/tests/conftest.py index 420f40afd..fa7e3d17d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -119,7 +119,8 @@ def notify_db_session(notify_db, sms_providers): "auth_type", "broadcast_status_type", "invite_status_type", - "service_callback_type"]: + "service_callback_type", + "broadcast_channel_types"]: notify_db.engine.execute(tbl.delete()) notify_db.session.commit()