mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-08 10:27:24 -04: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
|
||||
|
||||
|
||||
@complaint_blueprint.route('/total-per-day', methods=['GET'])
|
||||
@complaint_blueprint.route('/count-by-date-range', methods=['GET'])
|
||||
def get_complaint_count():
|
||||
request_json = request.args.to_dict
|
||||
start_date = None
|
||||
end_date = None
|
||||
# TODO: unit tests have yet to be written, need to test setting start and end date
|
||||
if request_json:
|
||||
validate(request_json, complaint_count_request)
|
||||
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.dao.dao_utils import transactional
|
||||
from app.models import Complaint
|
||||
from app.utils import get_london_midnight_in_utc
|
||||
|
||||
|
||||
@transactional
|
||||
@@ -9,8 +14,11 @@ def save_complaint(complaint):
|
||||
|
||||
|
||||
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):
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user