mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-09 17:45:41 -04:00
Merge pull request #1910 from alphagov/summary-complaints-api
Add endpoint to show number of complaints
This commit is contained in:
@@ -1,8 +1,13 @@
|
|||||||
from flask import Blueprint, jsonify
|
from datetime import datetime
|
||||||
|
|
||||||
|
from flask import Blueprint, jsonify, request
|
||||||
from sqlalchemy import desc
|
from sqlalchemy import desc
|
||||||
|
|
||||||
|
from app.complaint.complaint_schema import complaint_count_request
|
||||||
|
from app.dao.complaint_dao import fetch_count_of_complaints
|
||||||
from app.errors import register_errors
|
from app.errors import register_errors
|
||||||
from app.models import Complaint
|
from app.models import Complaint
|
||||||
|
from app.schema_validation import validate
|
||||||
|
|
||||||
complaint_blueprint = Blueprint('complaint', __name__, url_prefix='/complaint')
|
complaint_blueprint = Blueprint('complaint', __name__, url_prefix='/complaint')
|
||||||
|
|
||||||
@@ -14,3 +19,18 @@ def get_all_complaints():
|
|||||||
complaints = Complaint.query.order_by(desc(Complaint.created_at)).all()
|
complaints = Complaint.query.order_by(desc(Complaint.created_at)).all()
|
||||||
|
|
||||||
return jsonify([x.serialize() for x in complaints]), 200
|
return jsonify([x.serialize() for x in complaints]), 200
|
||||||
|
|
||||||
|
|
||||||
|
@complaint_blueprint.route('/count-by-date-range', methods=['GET'])
|
||||||
|
def get_complaint_count():
|
||||||
|
if request.args:
|
||||||
|
validate(request.args, complaint_count_request)
|
||||||
|
|
||||||
|
# If start and end date are not set, we are expecting today's stats.
|
||||||
|
today = str(datetime.utcnow().date())
|
||||||
|
|
||||||
|
start_date = datetime.strptime(request.args.get('start_date', today), '%Y-%m-%d').date()
|
||||||
|
end_date = datetime.strptime(request.args.get('end_date', today), '%Y-%m-%d').date()
|
||||||
|
count_of_complaints = fetch_count_of_complaints(start_date=start_date, end_date=end_date)
|
||||||
|
|
||||||
|
return jsonify(count_of_complaints), 200
|
||||||
|
|||||||
11
app/complaint/complaint_schema.py
Normal file
11
app/complaint/complaint_schema.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
complaint_count_request = {
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"description": "complaint count request schema",
|
||||||
|
"type": "object",
|
||||||
|
"title": "Complaint count request",
|
||||||
|
"properties": {
|
||||||
|
"start_date": {"type": ["string", "null"], "format": "date"},
|
||||||
|
"end_date": {"type": ["string", "null"], "format": "date"},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,11 @@
|
|||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from sqlalchemy import desc
|
||||||
|
|
||||||
from app import db
|
from app import db
|
||||||
from app.dao.dao_utils import transactional
|
from app.dao.dao_utils import transactional
|
||||||
from app.models import Complaint
|
from app.models import Complaint
|
||||||
|
from app.utils import get_london_midnight_in_utc
|
||||||
|
|
||||||
|
|
||||||
@transactional
|
@transactional
|
||||||
@@ -9,4 +14,11 @@ def save_complaint(complaint):
|
|||||||
|
|
||||||
|
|
||||||
def fetch_complaints_by_service(service_id):
|
def fetch_complaints_by_service(service_id):
|
||||||
return Complaint.query.filter_by(service_id=service_id).all()
|
return Complaint.query.filter_by(service_id=service_id).order_by(desc(Complaint.created_at)).all()
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_count_of_complaints(start_date, end_date):
|
||||||
|
start_date = get_london_midnight_in_utc(start_date)
|
||||||
|
end_date = get_london_midnight_in_utc(end_date + timedelta(days=1))
|
||||||
|
|
||||||
|
return Complaint.query.filter(Complaint.created_at >= start_date, Complaint.created_at < end_date).count()
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ def validate(json_to_validate, schema):
|
|||||||
validate_email_address(instance)
|
validate_email_address(instance)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@format_checker.checks('datetime', raises=ValidationError)
|
@format_checker.checks('datetime_within_next_day', raises=ValidationError)
|
||||||
def validate_schema_date_with_hour(instance):
|
def validate_schema_date_with_hour(instance):
|
||||||
if isinstance(instance, str):
|
if isinstance(instance, str):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ post_sms_request = {
|
|||||||
"phone_number": {"type": "string", "format": "phone_number"},
|
"phone_number": {"type": "string", "format": "phone_number"},
|
||||||
"template_id": uuid,
|
"template_id": uuid,
|
||||||
"personalisation": personalisation,
|
"personalisation": personalisation,
|
||||||
"scheduled_for": {"type": ["string", "null"], "format": "datetime"},
|
"scheduled_for": {"type": ["string", "null"], "format": "datetime_within_next_day"},
|
||||||
"sms_sender_id": uuid
|
"sms_sender_id": uuid
|
||||||
},
|
},
|
||||||
"required": ["phone_number", "template_id"],
|
"required": ["phone_number", "template_id"],
|
||||||
@@ -182,7 +182,7 @@ post_email_request = {
|
|||||||
"email_address": {"type": "string", "format": "email_address"},
|
"email_address": {"type": "string", "format": "email_address"},
|
||||||
"template_id": uuid,
|
"template_id": uuid,
|
||||||
"personalisation": personalisation,
|
"personalisation": personalisation,
|
||||||
"scheduled_for": {"type": ["string", "null"], "format": "datetime"},
|
"scheduled_for": {"type": ["string", "null"], "format": "datetime_within_next_day"},
|
||||||
"email_reply_to_id": uuid
|
"email_reply_to_id": uuid
|
||||||
},
|
},
|
||||||
"required": ["email_address", "template_id"],
|
"required": ["email_address", "template_id"],
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
|
from datetime import date
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from flask import url_for
|
||||||
|
from freezegun import freeze_time
|
||||||
|
|
||||||
from tests import create_authorization_header
|
from tests import create_authorization_header
|
||||||
from tests.app.db import create_complaint, create_service, create_template, create_notification
|
from tests.app.db import create_complaint, create_service, create_template, create_notification
|
||||||
|
|
||||||
@@ -22,3 +26,39 @@ def test_get_all_complaints_returns_empty_list(client):
|
|||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert json.loads(response.get_data(as_text=True)) == []
|
assert json.loads(response.get_data(as_text=True)) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_complaint_with_start_and_end_date_passes_these_to_dao_function(mocker, client):
|
||||||
|
start_date = date(2018, 6, 11)
|
||||||
|
end_date = date(2018, 6, 11)
|
||||||
|
dao_mock = mocker.patch('app.complaint.complaint_rest.fetch_count_of_complaints', return_value=3)
|
||||||
|
response = client.get(
|
||||||
|
url_for('complaint.get_complaint_count', start_date=start_date, end_date=end_date),
|
||||||
|
headers=[create_authorization_header()]
|
||||||
|
)
|
||||||
|
|
||||||
|
dao_mock.assert_called_once_with(start_date=start_date, end_date=end_date)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert json.loads(response.get_data(as_text=True)) == 3
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time("2018-06-01 11:00:00")
|
||||||
|
def test_get_complaint_sets_start_and_end_date_to_today_if_not_specified(mocker, client):
|
||||||
|
dao_mock = mocker.patch('app.complaint.complaint_rest.fetch_count_of_complaints', return_value=5)
|
||||||
|
response = client.get(url_for('complaint.get_complaint_count'), headers=[create_authorization_header()])
|
||||||
|
|
||||||
|
dao_mock.assert_called_once_with(start_date=date.today(), end_date=date.today())
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert json.loads(response.get_data(as_text=True)) == 5
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_complaint_with_invalid_data_returns_400_status_code(client):
|
||||||
|
start_date = '1234-56-78'
|
||||||
|
response = client.get(
|
||||||
|
url_for('complaint.get_complaint_count', start_date=start_date),
|
||||||
|
headers=[create_authorization_header()]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert response.json['errors'][0]['message'] == 'start_date time data {} does not match format %Y-%m-%d'.format(
|
||||||
|
start_date)
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from app.dao.complaint_dao import save_complaint, fetch_complaints_by_service
|
from app.dao.complaint_dao import save_complaint, fetch_complaints_by_service, fetch_count_of_complaints
|
||||||
from app.models import Complaint
|
from app.models import Complaint
|
||||||
from tests.app.db import create_service, create_template, create_notification
|
from tests.app.db import create_service, create_template, create_notification, create_complaint
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_complaint_by_service_returns_one(sample_service, sample_email_notification):
|
def test_fetch_complaint_by_service_returns_one(sample_service, sample_email_notification):
|
||||||
@@ -51,7 +51,8 @@ def test_fetch_complaint_by_service_return_many(notify_db_session):
|
|||||||
service_id=service_2.id,
|
service_id=service_2.id,
|
||||||
ses_feedback_id=str(uuid.uuid4()),
|
ses_feedback_id=str(uuid.uuid4()),
|
||||||
complaint_type='abuse',
|
complaint_type='abuse',
|
||||||
complaint_date=datetime.utcnow()
|
complaint_date=datetime.utcnow(),
|
||||||
|
created_at=datetime.utcnow() + timedelta(minutes=1)
|
||||||
)
|
)
|
||||||
|
|
||||||
save_complaint(complaint_1)
|
save_complaint(complaint_1)
|
||||||
@@ -60,5 +61,33 @@ def test_fetch_complaint_by_service_return_many(notify_db_session):
|
|||||||
|
|
||||||
complaints = fetch_complaints_by_service(service_id=service_2.id)
|
complaints = fetch_complaints_by_service(service_id=service_2.id)
|
||||||
assert len(complaints) == 2
|
assert len(complaints) == 2
|
||||||
assert complaints[0] == complaint_2
|
assert complaints[0] == complaint_3
|
||||||
assert complaints[1] == complaint_3
|
assert complaints[1] == complaint_2
|
||||||
|
|
||||||
|
|
||||||
|
def test_fetch_count_of_complaints(sample_email_notification):
|
||||||
|
create_complaint(service=sample_email_notification.service,
|
||||||
|
notification=sample_email_notification,
|
||||||
|
created_at=datetime(2018, 6, 6, 22, 00, 00))
|
||||||
|
create_complaint(service=sample_email_notification.service,
|
||||||
|
notification=sample_email_notification,
|
||||||
|
created_at=datetime(2018, 6, 6, 23, 00, 00))
|
||||||
|
create_complaint(service=sample_email_notification.service,
|
||||||
|
notification=sample_email_notification,
|
||||||
|
created_at=datetime(2018, 6, 7, 00, 00, 00))
|
||||||
|
create_complaint(service=sample_email_notification.service,
|
||||||
|
notification=sample_email_notification,
|
||||||
|
created_at=datetime(2018, 6, 7, 13, 00, 00))
|
||||||
|
create_complaint(service=sample_email_notification.service,
|
||||||
|
notification=sample_email_notification,
|
||||||
|
created_at=datetime(2018, 6, 7, 23))
|
||||||
|
|
||||||
|
count_of_complaints = fetch_count_of_complaints(start_date=datetime(2018, 6, 7),
|
||||||
|
end_date=datetime(2018, 6, 7))
|
||||||
|
assert count_of_complaints == 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_fetch_count_of_complaints_returns_zero(notify_db):
|
||||||
|
count_of_complaints = fetch_count_of_complaints(start_date=datetime(2018, 6, 7),
|
||||||
|
end_date=datetime(2018, 6, 7))
|
||||||
|
assert count_of_complaints == 0
|
||||||
|
|||||||
@@ -566,7 +566,8 @@ def create_ft_billing(bst_date,
|
|||||||
|
|
||||||
|
|
||||||
def create_complaint(service=None,
|
def create_complaint(service=None,
|
||||||
notification=None):
|
notification=None,
|
||||||
|
created_at=None):
|
||||||
if not service:
|
if not service:
|
||||||
service = create_service()
|
service = create_service()
|
||||||
if not notification:
|
if not notification:
|
||||||
@@ -577,7 +578,8 @@ def create_complaint(service=None,
|
|||||||
service_id=service.id,
|
service_id=service.id,
|
||||||
ses_feedback_id=str(uuid.uuid4()),
|
ses_feedback_id=str(uuid.uuid4()),
|
||||||
complaint_type='abuse',
|
complaint_type='abuse',
|
||||||
complaint_date=datetime.utcnow()
|
complaint_date=datetime.utcnow(),
|
||||||
|
created_at=created_at if created_at else datetime.now()
|
||||||
)
|
)
|
||||||
db.session.add(complaint)
|
db.session.add(complaint)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|||||||
Reference in New Issue
Block a user