mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-09 23:02:13 -05:00
When we get complaints we'd like to know how many we get in a day or other date range, so if there is a spike in complaints we can act on it.
Add new endpoint to return the number of complaints in a date range. Unit tests to follow in the next commit.
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 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.models import Complaint
|
||||
from app.schema_validation import validate
|
||||
|
||||
complaint_blueprint = Blueprint('complaint', __name__, url_prefix='/complaint')
|
||||
|
||||
@@ -14,3 +19,22 @@ def get_all_complaints():
|
||||
complaints = Complaint.query.order_by(desc(Complaint.created_at)).all()
|
||||
|
||||
return jsonify([x.serialize() for x in complaints]), 200
|
||||
|
||||
|
||||
@complaint_blueprint.route('/total-per-day', methods=['GET'])
|
||||
def get_complaint_count():
|
||||
request_json = request.args.to_dict
|
||||
start_date = None
|
||||
end_date = None
|
||||
if request_json:
|
||||
validate(request_json, complaint_count_request)
|
||||
start_date = request_json.get('start_date', None)
|
||||
end_date = request_json.get('end_date', None)
|
||||
if not start_date:
|
||||
start_date = datetime.utcnow().date()
|
||||
if not end_date:
|
||||
end_date = datetime.utcnow().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": "datetime"},
|
||||
"end_date": {"type": ["string", "null"], "format": "datetime"},
|
||||
}
|
||||
}
|
||||
@@ -10,3 +10,7 @@ def save_complaint(complaint):
|
||||
|
||||
def fetch_complaints_by_service(service_id):
|
||||
return Complaint.query.filter_by(service_id=service_id).all()
|
||||
|
||||
|
||||
def fetch_count_of_complaints(start_date, end_date):
|
||||
return Complaint.count.filter(Complaint.created_at >= start_date, Complaint.created_at < end_date)
|
||||
|
||||
Reference in New Issue
Block a user