And an endpoint to get uploaded letters for a day

Because we won’t be showing uploaded letters individually on the uploads
page any more we need a way of listing them. This should be by printing
day, to match how we’re grouping them on the uploads page.

The response matches a normal `get_notifications` response so we can
reuse the same code in the admin app.
This commit is contained in:
Chris Hill-Scott
2020-05-11 10:51:40 +01:00
parent 421c1aac96
commit 864c6772b3
4 changed files with 185 additions and 2 deletions

View File

@@ -117,3 +117,23 @@ def dao_get_uploads_by_service_id(service_id, limit_days=None, page=1, page_size
).order_by(
desc("processing_started"), desc("created_at")
).paginate(page=page, per_page=page_size)
def dao_get_uploaded_letters_by_print_date(service_id, letter_print_date, page=1, page_size=50):
return db.session.query(
Notification,
).join(
Template, Notification.template_id == Template.id
).filter(
Notification.service_id == service_id,
Notification.notification_type == LETTER_TYPE,
Notification.api_key_id.is_(None),
Notification.status != NOTIFICATION_CANCELLED,
Template.hidden.is_(True),
_get_printing_day(Notification.created_at) == letter_print_date.date(),
).order_by(
desc(Notification.created_at)
).paginate(
page=page,
per_page=page_size,
)

View File

@@ -1,5 +1,7 @@
from datetime import datetime
from flask import (
Blueprint,
abort,
jsonify,
request,
current_app
@@ -9,10 +11,14 @@ from app.dao.fact_notification_status_dao import fetch_notification_statuses_for
from app.dao.jobs_dao import (
dao_get_notification_outcomes_for_job
)
from app.dao.uploads_dao import dao_get_uploads_by_service_id
from app.dao.uploads_dao import (
dao_get_uploaded_letters_by_print_date,
dao_get_uploads_by_service_id,
)
from app.errors import (
register_errors,
)
from app.schemas import notification_with_template_schema
from app.utils import pagination_links, midnight_n_days_ago
upload_blueprint = Blueprint('upload', __name__, url_prefix='/service/<uuid:service_id>/upload')
@@ -74,3 +80,31 @@ def get_paginated_uploads(service_id, limit_days, page):
service_id=service_id
)
}
@upload_blueprint.route('/uploaded-letters/<letter_print_date>', methods=['GET'])
def get_uploaded_letter_by_service_and_print_day(service_id, letter_print_date):
try:
letter_print_datetime = datetime.strptime(letter_print_date, '%Y-%m-%d')
except ValueError:
abort(404)
pagination = dao_get_uploaded_letters_by_print_date(
service_id,
letter_print_date=letter_print_datetime,
page=request.args.get('page', type=int),
page_size=current_app.config['PAGE_SIZE']
)
return jsonify({
'notifications': notification_with_template_schema.dump(
pagination.items,
many=True,
).data,
'page_size': pagination.per_page,
'total': pagination.total,
'links': pagination_links(
pagination,
'.get_uploaded_letter_by_service_and_print_day',
service_id=service_id,
letter_print_date=letter_print_date,
),
})