Added a new endpoint to return the last used date for a template.

The existing endpoint returned a whole notification for the last time the template was used. But this only takes into account data in the last week. This new methods allows us to be specific about when the template was last used if ever but looking into the ft_notification_status table as well.
This commit is contained in:
Rebecca Law
2020-02-05 13:03:54 +00:00
parent f4b137c658
commit 3a32c35dd2
4 changed files with 143 additions and 4 deletions

View File

@@ -30,6 +30,7 @@ from app.dao.dao_utils import transactional
from app.errors import InvalidRequest
from app.letters.utils import get_letter_pdf_filename
from app.models import (
FactNotificationStatus,
Notification,
NotificationHistory,
ProviderDetails,
@@ -70,6 +71,37 @@ def dao_get_last_template_usage(template_id, template_type, service_id):
).first()
@statsd(namespace="dao")
def dao_get_last_date_template_was_used(template_id, template_type, service_id):
last_date = db.session.query(
functions.max(FactNotificationStatus.bst_date)
).filter(
FactNotificationStatus.template_id == template_id,
FactNotificationStatus.key_type != KEY_TYPE_TEST
).scalar()
last_date_from_notifications = db.session.query(
functions.max(Notification.created_at)
).filter(
Notification.service_id == service_id,
Notification.notification_type == template_type,
Notification.template_id == template_id,
Notification.key_type != KEY_TYPE_TEST
).scalar()
if last_date and last_date_from_notifications:
if datetime.combine(last_date, datetime.utcnow().min.time()) >= last_date_from_notifications:
return last_date
else:
return last_date_from_notifications
elif not last_date:
return last_date_from_notifications
elif not last_date_from_notifications:
return last_date
else:
return None
@statsd(namespace="dao")
@transactional
def dao_create_notification(notification):

View File

@@ -1,5 +1,7 @@
from flask import Blueprint, jsonify, request
from app.dao.notifications_dao import dao_get_last_template_usage
from app import DATETIME_FORMAT
from app.dao.notifications_dao import dao_get_last_template_usage, dao_get_last_date_template_was_used
from app.dao.templates_dao import dao_get_template_by_id_and_service_id
from app.dao.fact_notification_status_dao import fetch_notification_status_for_service_for_today_and_7_previous_days
@@ -52,3 +54,14 @@ def get_template_statistics_for_template_id(service_id, template_id):
data = notification_with_template_schema.dump(notification).data
return jsonify(data=data)
@template_statistics.route('/last-used/<uuid:template_id>')
def get_last_used_datetime_for_template(service_id, template_id):
template = dao_get_template_by_id_and_service_id(template_id, service_id)
last_date_used = dao_get_last_date_template_was_used(template_id=template_id,
template_type=template.template_type,
service_id=service_id)
return jsonify(last_date_used=last_date_used.strftime(DATETIME_FORMAT))