mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-14 09:12:06 -05:00
Added a query and endpoint to return the count of complaints for a date-range.
Unit tests for complaints_rest.get_complaint_count have yet to be written. Maybe there is a better name for the new endpoint.
This commit is contained in:
@@ -21,11 +21,12 @@ def get_all_complaints():
|
|||||||
return jsonify([x.serialize() for x in complaints]), 200
|
return jsonify([x.serialize() for x in complaints]), 200
|
||||||
|
|
||||||
|
|
||||||
@complaint_blueprint.route('/total-per-day', methods=['GET'])
|
@complaint_blueprint.route('/count-by-date-range', methods=['GET'])
|
||||||
def get_complaint_count():
|
def get_complaint_count():
|
||||||
request_json = request.args.to_dict
|
request_json = request.args.to_dict
|
||||||
start_date = None
|
start_date = None
|
||||||
end_date = None
|
end_date = None
|
||||||
|
# TODO: unit tests have yet to be written, need to test setting start and end date
|
||||||
if request_json:
|
if request_json:
|
||||||
validate(request_json, complaint_count_request)
|
validate(request_json, complaint_count_request)
|
||||||
start_date = request_json.get('start_date', None)
|
start_date = request_json.get('start_date', None)
|
||||||
|
|||||||
@@ -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,8 +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):
|
def fetch_count_of_complaints(start_date, end_date):
|
||||||
return Complaint.count.filter(Complaint.created_at >= start_date, Complaint.created_at < 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()
|
||||||
|
|||||||
@@ -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