Count recently-returned letters on the dashboard

Currently you have no way of getting to the returned letter page. This
commit adds a link to it from the dashboard, following the pattern of
the new received text messages banner.
This commit is contained in:
Chris Hill-Scott
2020-02-12 14:19:21 +00:00
parent 74e70ed8bc
commit f369f76ae4
7 changed files with 218 additions and 8 deletions

View File

@@ -356,6 +356,22 @@ def format_delta(date):
)
def format_delta_days(date):
now = datetime.now(timezone.utc)
date = utc_string_to_aware_gmt_datetime(date)
delta = now - date
if date.strftime('%Y-%M-%D') == now.strftime('%Y-%M-%D'):
return "today"
if date.strftime('%Y-%M-%D') == (now - timedelta(days=1)).strftime('%Y-%M-%D'):
return "yesterday"
return ago.human(
delta,
precision=1,
future_tense='{} from now',
past_tense='{} ago',
)
def valid_phone_number(phone_number):
try:
validate_phone_number(phone_number)
@@ -744,6 +760,7 @@ def add_template_filters(application):
format_date_short,
format_datetime_relative,
format_delta,
format_delta_days,
format_notification_status,
format_notification_type,
format_notification_status_as_time,

View File

@@ -1,4 +1,8 @@
from datetime import datetime, timedelta
from dateutil.parser import parse
from flask import abort, current_app
from notifications_utils.timezones import local_timezone
from werkzeug.utils import cached_property
from app.models import JSONModel
@@ -670,3 +674,24 @@ class Service(JSONModel):
@cached_property
def returned_letter_summary(self):
return service_api_client.get_returned_letter_summary(self.id)
@property
def most_recent_returned_letter_report(self):
if not self.returned_letter_summary:
return None
return parse(
self.returned_letter_summary[0]['reported_at'] + " 00:00:00"
).replace(tzinfo=local_timezone)
@property
def count_of_returned_letters_in_last_7_days(self):
seven_days_ago = (
datetime.now() - timedelta(days=7)
).replace(
hour=0, minute=0, second=0
)
return sum(
report['returned_letter_count']
for report in self.returned_letter_summary
if parse(report['reported_at'] + " 00:00:00") >= seven_days_ago
)

View File

@@ -17,4 +17,17 @@
{% endif %}
</a>
{% endif %}
{% if current_service.returned_letter_summary %}
<a id="total-returned-letters" class="banner-dashboard" href="{{ url_for('main.returned_letter_summary', service_id=current_service.id) }}">
<span class="banner-dashboard-count">
{{ current_service.count_of_returned_letters_in_last_7_days|format_thousands }}
</span>
<span class="banner-dashboard-count-label">
returned {{ message_count_label(current_service.count_of_returned_letters_in_last_7_days, 'letter', suffix='') }}
</span>
<span class="banner-dashboard-meta">
latest report {{ current_service.most_recent_returned_letter_report|format_delta_days }}
</span>
</a>
{% endif %}
</div>