Add an endpoint to return returned letter stats

Currently the dashboard in the admin app pull the entire returned letter
summary for a service to calculate how many letters have been returned
in the last seven days.

Adding a separate endpoint for this purpose is better because:
- it’s a more efficient query
- it’s less data to send down the pipe
- it gives us a place to return the complete datetime, so the dashboard
  can be more precise about when the most recent report was
This commit is contained in:
Chris Hill-Scott
2020-03-03 17:12:45 +00:00
parent 223ec1360c
commit 9c03438a53
4 changed files with 121 additions and 2 deletions

View File

@@ -31,8 +31,10 @@ from app.dao.fact_notification_status_dao import (
from app.dao.inbound_numbers_dao import dao_allocate_number_for_service
from app.dao.organisation_dao import dao_get_organisation_by_service_id
from app.dao.returned_letters_dao import (
fetch_most_recent_returned_letter,
fetch_returned_letter_count,
fetch_returned_letter_summary,
fetch_returned_letters
fetch_returned_letters,
)
from app.dao.service_data_retention_dao import (
fetch_service_data_retention,
@@ -946,6 +948,25 @@ def check_if_reply_to_address_already_in_use(service_id, email_address):
)
@service_blueprint.route('/<uuid:service_id>/returned-letter-statistics', methods=['GET'])
def returned_letter_statistics(service_id):
most_recent = fetch_most_recent_returned_letter(service_id)
if not most_recent:
return jsonify({
'returned_letter_count': 0,
'most_recent_report': None,
})
count = fetch_returned_letter_count(service_id)
return jsonify({
'returned_letter_count': count.returned_letter_count,
'most_recent_report': most_recent.reported_at.strftime(DATETIME_FORMAT_NO_TIMEZONE),
})
@service_blueprint.route('/<uuid:service_id>/returned-letter-summary', methods=['GET'])
def returned_letter_summary(service_id):
results = fetch_returned_letter_summary(service_id)