diff --git a/app/models.py b/app/models.py index fd116ad78..4f4683e53 100644 --- a/app/models.py +++ b/app/models.py @@ -57,6 +57,10 @@ SMS_AUTH_TYPE = 'sms_auth' EMAIL_AUTH_TYPE = 'email_auth' USER_AUTH_TYPE = [SMS_AUTH_TYPE, EMAIL_AUTH_TYPE] +DELIVERY_STATUS_CALLBACK_TYPE = 'delivery_status' +COMPLAINT_CALLBACK_TYPE = 'complaint' +SERVICE_CALLBACK_TYPES = [DELIVERY_STATUS_CALLBACK_TYPE, COMPLAINT_CALLBACK_TYPE] + def filter_null_value_fields(obj): return dict( @@ -596,6 +600,7 @@ class ServiceCallbackApi(db.Model, Versioned): service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, nullable=False, unique=True) service = db.relationship('Service', backref='service_callback_api') url = db.Column(db.String(), nullable=False) + callback_type = db.Column(db.String(), db.ForeignKey('service_callback_type.name'), nullable=True) _bearer_token = db.Column("bearer_token", db.String(), nullable=False) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False) updated_at = db.Column(db.DateTime, nullable=True) @@ -624,6 +629,12 @@ class ServiceCallbackApi(db.Model, Versioned): } +class ServiceCallbackType(db.Model): + __tablename__ = 'service_callback_type' + + name = db.Column(db.String, primary_key=True) + + class ApiKey(db.Model, Versioned): __tablename__ = 'api_keys' diff --git a/app/service/callback_rest.py b/app/service/callback_rest.py index 26d0b6e2d..7565d9d8a 100644 --- a/app/service/callback_rest.py +++ b/app/service/callback_rest.py @@ -11,7 +11,8 @@ from app.errors import ( ) from app.models import ( ServiceInboundApi, - ServiceCallbackApi + ServiceCallbackApi, + DELIVERY_STATUS_CALLBACK_TYPE, ) from app.schema_validation import validate from app.service.service_callback_api_schema import ( @@ -88,6 +89,7 @@ def create_service_callback_api(service_id): data = request.get_json() validate(data, create_service_callback_api_schema) data["service_id"] = service_id + data["callback_type"] = DELIVERY_STATUS_CALLBACK_TYPE callback_api = ServiceCallbackApi(**data) try: save_service_callback_api(callback_api) diff --git a/migrations/versions/0205_service_callback_type.py b/migrations/versions/0205_service_callback_type.py new file mode 100644 index 000000000..2ecb45ead --- /dev/null +++ b/migrations/versions/0205_service_callback_type.py @@ -0,0 +1,35 @@ +""" + +Revision ID: 0205_service_callback_type +Revises: 0204_service_data_retention +Create Date: 2018-07-17 15:51:10.776698 + +""" +from alembic import op +import sqlalchemy as sa + + +revision = '0205_service_callback_type' +down_revision = '0204_service_data_retention' + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('service_callback_type', + sa.Column('name', sa.String(), nullable=False), + sa.PrimaryKeyConstraint('name') + ) + op.execute("insert into service_callback_type values ('delivery_status'), ('complaint')") + op.add_column('service_callback_api', sa.Column('callback_type', sa.String(), nullable=True)) + op.create_foreign_key("service_callback_api_type_fk", 'service_callback_api', 'service_callback_type', ['callback_type'], ['name']) + op.add_column('service_callback_api_history', sa.Column('callback_type', sa.String(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('service_callback_api_history', 'callback_type') + op.drop_constraint("service_callback_api_type_fk", 'service_callback_api', type_='foreignkey') + op.drop_column('service_callback_api', 'callback_type') + op.drop_table('service_callback_type') + # ### end Alembic commands ### diff --git a/tests/conftest.py b/tests/conftest.py index 3797ab6d9..ee47643ce 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -106,7 +106,8 @@ def notify_db_session(notify_db): "service_permission_types", "auth_type", "invite_status_type", - "letter_rates"]: + "letter_rates", + "service_callback_type"]: notify_db.engine.execute(tbl.delete()) notify_db.session.commit()