2018-06-07 10:30:50 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from flask import Blueprint, jsonify, request
|
2018-06-05 14:25:24 +01:00
|
|
|
from sqlalchemy import desc
|
|
|
|
|
|
2018-06-07 10:30:50 +01:00
|
|
|
from app.complaint.complaint_schema import complaint_count_request
|
|
|
|
|
from app.dao.complaint_dao import fetch_count_of_complaints
|
2018-06-05 14:25:24 +01:00
|
|
|
from app.errors import register_errors
|
|
|
|
|
from app.models import Complaint
|
2018-06-07 10:30:50 +01:00
|
|
|
from app.schema_validation import validate
|
2018-06-05 14:25:24 +01:00
|
|
|
|
|
|
|
|
complaint_blueprint = Blueprint('complaint', __name__, url_prefix='/complaint')
|
|
|
|
|
|
|
|
|
|
register_errors(complaint_blueprint)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@complaint_blueprint.route('', methods=['GET'])
|
|
|
|
|
def get_all_complaints():
|
|
|
|
|
complaints = Complaint.query.order_by(desc(Complaint.created_at)).all()
|
|
|
|
|
|
|
|
|
|
return jsonify([x.serialize() for x in complaints]), 200
|
2018-06-07 10:30:50 +01:00
|
|
|
|
|
|
|
|
|
2018-06-07 16:01:41 +01:00
|
|
|
@complaint_blueprint.route('/count-by-date-range', methods=['GET'])
|
2018-06-07 10:30:50 +01:00
|
|
|
def get_complaint_count():
|
|
|
|
|
request_json = request.args.to_dict
|
|
|
|
|
start_date = None
|
|
|
|
|
end_date = None
|
2018-06-07 16:01:41 +01:00
|
|
|
# TODO: unit tests have yet to be written, need to test setting start and end date
|
2018-06-07 10:30:50 +01:00
|
|
|
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
|