2017-06-02 12:21:12 +01:00
|
|
|
from datetime import (
|
|
|
|
|
timedelta,
|
2018-04-30 16:58:26 +01:00
|
|
|
datetime,
|
|
|
|
|
date
|
2017-06-02 12:21:12 +01:00
|
|
|
)
|
2017-11-06 18:19:02 +00:00
|
|
|
from flask import current_app
|
2018-02-06 09:35:33 +00:00
|
|
|
from notifications_utils.statsd_decorators import statsd
|
2018-04-04 16:59:48 +01:00
|
|
|
from sqlalchemy import desc, and_
|
|
|
|
|
from sqlalchemy.orm import aliased
|
2017-06-02 12:21:12 +01:00
|
|
|
|
2017-05-22 11:26:47 +01:00
|
|
|
from app import db
|
|
|
|
|
from app.dao.dao_utils import transactional
|
2017-05-31 14:49:14 +01:00
|
|
|
from app.models import InboundSms
|
2018-04-30 16:58:26 +01:00
|
|
|
from app.utils import get_london_midnight_in_utc
|
2017-05-22 11:26:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@transactional
|
|
|
|
|
def dao_create_inbound_sms(inbound_sms):
|
|
|
|
|
db.session.add(inbound_sms)
|
2017-05-31 14:49:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_inbound_sms_for_service(service_id, limit=None, user_number=None):
|
2018-04-30 16:58:26 +01:00
|
|
|
start_date = get_london_midnight_in_utc(date.today() - timedelta(days=6))
|
2017-05-31 14:49:14 +01:00
|
|
|
q = InboundSms.query.filter(
|
2018-04-30 16:58:26 +01:00
|
|
|
InboundSms.service_id == service_id,
|
|
|
|
|
InboundSms.created_at >= start_date
|
2017-05-31 14:49:14 +01:00
|
|
|
).order_by(
|
|
|
|
|
InboundSms.created_at.desc()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if user_number:
|
|
|
|
|
q = q.filter(InboundSms.user_number == user_number)
|
|
|
|
|
|
|
|
|
|
if limit:
|
|
|
|
|
q = q.limit(limit)
|
|
|
|
|
|
|
|
|
|
return q.all()
|
|
|
|
|
|
|
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
def dao_get_paginated_inbound_sms_for_service_for_public_api(
|
2017-11-06 18:19:02 +00:00
|
|
|
service_id,
|
|
|
|
|
older_than=None,
|
|
|
|
|
page_size=None
|
|
|
|
|
):
|
|
|
|
|
if page_size is None:
|
|
|
|
|
page_size = current_app.config['PAGE_SIZE']
|
|
|
|
|
|
|
|
|
|
filters = [InboundSms.service_id == service_id]
|
|
|
|
|
|
2017-11-07 11:55:51 +00:00
|
|
|
if older_than:
|
2017-11-06 18:19:02 +00:00
|
|
|
older_than_created_at = db.session.query(
|
|
|
|
|
InboundSms.created_at).filter(InboundSms.id == older_than).as_scalar()
|
|
|
|
|
filters.append(InboundSms.created_at < older_than_created_at)
|
|
|
|
|
|
|
|
|
|
query = InboundSms.query.filter(*filters)
|
|
|
|
|
|
|
|
|
|
return query.order_by(desc(InboundSms.created_at)).paginate(
|
|
|
|
|
per_page=page_size
|
|
|
|
|
).items
|
|
|
|
|
|
|
|
|
|
|
2017-05-31 14:49:14 +01:00
|
|
|
def dao_count_inbound_sms_for_service(service_id):
|
2018-04-30 16:58:26 +01:00
|
|
|
start_date = get_london_midnight_in_utc(date.today() - timedelta(days=6))
|
2017-05-31 14:49:14 +01:00
|
|
|
return InboundSms.query.filter(
|
2018-04-30 16:58:26 +01:00
|
|
|
InboundSms.service_id == service_id,
|
|
|
|
|
InboundSms.created_at >= start_date
|
2017-05-31 14:49:14 +01:00
|
|
|
).count()
|
2017-06-02 12:21:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@statsd(namespace="dao")
|
|
|
|
|
@transactional
|
|
|
|
|
def delete_inbound_sms_created_more_than_a_week_ago():
|
|
|
|
|
seven_days_ago = datetime.utcnow() - timedelta(days=7)
|
|
|
|
|
|
|
|
|
|
deleted = db.session.query(InboundSms).filter(
|
|
|
|
|
InboundSms.created_at < seven_days_ago
|
|
|
|
|
).delete(synchronize_session='fetch')
|
|
|
|
|
|
|
|
|
|
return deleted
|
2017-06-06 17:11:59 +01:00
|
|
|
|
|
|
|
|
|
2017-06-07 14:23:31 +01:00
|
|
|
def dao_get_inbound_sms_by_id(service_id, inbound_id):
|
|
|
|
|
return InboundSms.query.filter_by(
|
|
|
|
|
id=inbound_id,
|
|
|
|
|
service_id=service_id
|
|
|
|
|
).one()
|
2018-04-04 16:59:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_paginated_most_recent_inbound_sms_by_user_number_for_service(
|
|
|
|
|
service_id,
|
|
|
|
|
page
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
This query starts from inbound_sms and joins on to itself to find the most recent row for each user_number
|
|
|
|
|
|
|
|
|
|
Equivalent sql:
|
|
|
|
|
|
|
|
|
|
SELECT t1.*
|
|
|
|
|
FROM inbound_sms t1
|
|
|
|
|
LEFT OUTER JOIN inbound_sms AS t2 ON (
|
|
|
|
|
-- identifying
|
|
|
|
|
t1.user_number = t2.user_number AND
|
|
|
|
|
t1.service_id = t2.service_id AND
|
|
|
|
|
-- ordering
|
|
|
|
|
t1.created_at < t2.created_at
|
|
|
|
|
)
|
|
|
|
|
WHERE t2.id IS NULL AND t1.service_id = :service_id
|
|
|
|
|
ORDER BY t1.created_at DESC;
|
|
|
|
|
LIMIT 50 OFFSET :page
|
|
|
|
|
"""
|
2018-04-30 16:58:26 +01:00
|
|
|
start_date = get_london_midnight_in_utc(date.today() - timedelta(days=6))
|
2018-04-04 16:59:48 +01:00
|
|
|
t2 = aliased(InboundSms)
|
|
|
|
|
q = db.session.query(
|
|
|
|
|
InboundSms
|
|
|
|
|
).outerjoin(
|
|
|
|
|
t2,
|
|
|
|
|
and_(
|
|
|
|
|
InboundSms.user_number == t2.user_number,
|
|
|
|
|
InboundSms.service_id == t2.service_id,
|
2018-04-30 16:58:26 +01:00
|
|
|
InboundSms.created_at < t2.created_at,
|
2018-04-04 16:59:48 +01:00
|
|
|
)
|
|
|
|
|
).filter(
|
|
|
|
|
t2.id == None, # noqa
|
2018-04-30 16:58:26 +01:00
|
|
|
InboundSms.service_id == service_id,
|
|
|
|
|
InboundSms.created_at >= start_date
|
2018-04-04 16:59:48 +01:00
|
|
|
).order_by(
|
|
|
|
|
InboundSms.created_at.desc()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return q.paginate(
|
|
|
|
|
page=page,
|
|
|
|
|
per_page=current_app.config['PAGE_SIZE']
|
|
|
|
|
)
|