Added a new rates table.

We only need one rate per channel. This reflects that. The provider_rates has been left for now, it is still not being used.
New dao has been added to select the right rate for the given notification_type and date of notificaiton.
This commit is contained in:
Rebecca Law
2017-04-24 16:20:03 +01:00
parent 028cedbb10
commit 43226dc932
5 changed files with 106 additions and 0 deletions

11
app/dao/rates_dao.py Normal file
View File

@@ -0,0 +1,11 @@
from sqlalchemy import desc
from app import db
from app.models import Rates
def get_rate_for_type_and_date(notification_type, date_sent):
return db.session.query(Rates).filter(Rates.notification_type == notification_type,
Rates.valid_from <= date_sent
).order_by(Rates.valid_from.desc()
).limit(1).first()

View File

@@ -944,3 +944,12 @@ class Event(db.Model):
nullable=False,
default=datetime.datetime.utcnow)
data = db.Column(JSON, nullable=False)
class Rates(db.Model):
__tablename__ = 'rates'
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
valid_from = db.Column(db.DateTime, nullable=False)
rate = db.Column(db.Numeric(), nullable=False)
notification_type = db.Column(notification_types, index=True, nullable=False)

View File

@@ -0,0 +1,28 @@
"""empty message
Revision ID: 0074_update_sms_rate
Revises: 0072_add_dvla_orgs
Create Date: 2017-04-24 12:10:02.116278
"""
import uuid
revision = '0074_update_sms_rate'
down_revision = '0072_add_dvla_orgs'
from alembic import op
def upgrade():
op.get_bind()
op.execute("INSERT INTO provider_rates (id, valid_from, rate, provider_id) "
"VALUES ('{}', '2017-04-01 00:00:00', 1.58, "
"(SELECT id FROM provider_details WHERE identifier = 'mmg'))".format(uuid.uuid4())
)
def downgrade():
op.get_bind()
op.execute("DELETE FROM provider_rates where valid_from = '2017-04-01 00:00:00' "
"and provider_id = (SELECT id FROM provider_details WHERE identifier = 'mmg')")

View File

@@ -0,0 +1,40 @@
"""empty message
Revision ID: 0075_create_rates_table
Revises: 0074_update_sms_rate
Create Date: 2017-04-24 15:12:18.907629
"""
# revision identifiers, used by Alembic.
import uuid
revision = '0075_create_rates_table'
down_revision = '0074_update_sms_rate'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
def upgrade():
notification_types = postgresql.ENUM('email', 'sms', 'letter', name='notification_type', create_type=False)
op.create_table('rates',
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('valid_from', sa.DateTime(), nullable=False),
sa.Column('rate', sa.Numeric(), nullable=False),
sa.Column('notification_type', notification_types, nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_rates_notification_type'), 'rates', ['notification_type'], unique=False)
op.get_bind()
op.execute("INSERT INTO rates(id, valid_from, rate, notification_type) "
"VALUES('{}', '2016-05-18 00:00:00', 1.65, 'sms')".format(uuid.uuid4()))
op.execute("INSERT INTO rates(id, valid_from, rate, notification_type) "
"VALUES('{}', '2017-04-01 00:00:00', 1.58, 'sms')".format(uuid.uuid4()))
def downgrade():
op.drop_index(op.f('ix_rates_notification_type'), table_name='rates')
op.drop_table('rates')

View File

@@ -0,0 +1,18 @@
from datetime import datetime
from decimal import Decimal
from app.dao.rates_dao import get_rate_for_type_and_date
def test_get_rate_for_type_and_date(notify_db):
rate = get_rate_for_type_and_date('sms', datetime.utcnow())
assert rate.rate == Decimal("1.58")
rate = get_rate_for_type_and_date('sms', datetime(2016, 6, 1))
assert rate.rate == Decimal("1.65")
def test_get_rate_for_type_and_date_early_date(notify_db):
rate = get_rate_for_type_and_date('sms', datetime(2014, 6, 1))
assert not rate