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

@@ -13,6 +13,7 @@ from app.models import (
Template,
User,
)
from app.utils import midnight_n_days_ago
def _get_notification_ids_for_references(references):
@@ -50,6 +51,25 @@ def insert_or_update_returned_letters(references):
db.session.connection().execute(stmt)
def fetch_returned_letter_count(service_id):
return db.session.query(
func.count(ReturnedLetter.notification_id).label('returned_letter_count'),
).filter(
ReturnedLetter.service_id == service_id,
ReturnedLetter.reported_at > midnight_n_days_ago(7),
).one()
def fetch_most_recent_returned_letter(service_id):
return db.session.query(
ReturnedLetter.reported_at,
).filter(
ReturnedLetter.service_id == service_id,
).order_by(
desc(ReturnedLetter.reported_at)
).first()
def fetch_returned_letter_summary(service_id):
return db.session.query(
func.count(ReturnedLetter.notification_id).label('returned_letter_count'),