diff --git a/app/__init__.py b/app/__init__.py index 83ddcef847..b761b766e5 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -199,7 +199,8 @@ def register_blueprint(application): letter_branding_blueprint.before_request(requires_admin_auth) application.register_blueprint(letter_branding_blueprint) - support_blueprint.before_request(requires_admin_auth) + # support_blueprint.before_request(requires_admin_auth) + support_blueprint.before_request(requires_no_auth) application.register_blueprint(support_blueprint, url_prefix="/support") diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index f0598b7b97..c851e62c82 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -192,6 +192,18 @@ def get_notification_by_id(notification_id, service_id=None, _raise=False): return query.one() if _raise else query.first() +@statsd(namespace="dao") +def get_notification_history_by_id(notification_history_id, service_id=None, _raise=False): + filters = [NotificationHistory.id == notification_history_id] + + if service_id: + filters.append(NotificationHistory.service_id == service_id) + + query = db.on_reader().query(NotificationHistory).filter(*filters) + + return query.one() if _raise else query.first() + + def get_notifications(filter_dict=None): return _filter_query(Notification.query, filter_dict=filter_dict) diff --git a/app/support/rest.py b/app/support/rest.py index a361d164a8..19d42e0ec5 100644 --- a/app/support/rest.py +++ b/app/support/rest.py @@ -2,73 +2,127 @@ from sqlalchemy.orm.exc import NoResultFound from app.dao.jobs_dao import dao_get_job_by_id +from app.dao.notifications_dao import get_notification_by_id, get_notification_history_by_id from app.dao.services_dao import dao_fetch_service_by_id from app.dao.templates_dao import dao_get_template_by_id from app.dao.users_dao import get_user_by_id from app.errors import register_errors +from flask import current_app support_blueprint = Blueprint("support", __name__) register_errors(support_blueprint) -@support_blueprint.route("/", methods=["GET"]) -def get_id_info(id): +def notification_query(id): try: - template = dao_get_template_by_id(id) + notification = get_notification_by_id(id) + if notification: + return { + "type": "notification", + "id": notification.id, + "notification_type": notification.notification_type, + "status": notification.status, + "created_at": notification.created_at, + "sent_at": notification.sent_at, + "to": notification.to, + "service_id": notification.service_id, + "service_name": notification.service.name, + "job_id": notification.job_id, + "job_row_number": notification.job_row_number, + "api_key_id": notification.api_key_id, + } except NoResultFound: pass - else: - return jsonify( - { + +def notification_history_query(id): + try: + notification = get_notification_history_by_id(id) + if notification: + return { + "type": "notification_history", + "id": notification.id, + "notification_type": notification.notification_type, + "status": notification.status, + "created_at": notification.created_at, + "sent_at": notification.sent_at, + "to": "expired", + "service_id": notification.service_id, + "service_name": notification.service.name, + "job_id": notification.job_id, + "job_row_number": notification.job_row_number, + "api_key_id": notification.api_key_id, + } + except NoResultFound: + pass + + +def template_query(id): + try: + template = dao_get_template_by_id(id) + if template: + + return { "type": "template", "id": template.id, "name": template.name, "service_id": template.service_id, "service_name": template.service.name, } - ) + except NoResultFound: + pass + +def service_query(id): try: service = dao_fetch_service_by_id(id) + if service: + return {"type": "service", "id": service.id, "name": service.name} except NoResultFound: pass - else: - return jsonify({"type": "service", "id": service.id, "name": service.name}) + +def job_query(id): try: job = dao_get_job_by_id(id) + return { + "type": "job", + "id": job.id, + "original_file_name": job.original_file_name, + "created_at": job.created_at, + "created_by_id": job.created_by_id, + "created_by_name": job.created_by.name, + "processing_started": job.processing_started, + "processing_finished": job.processing_finished, + "notification_count": job.notification_count, + "job_status": job.job_status, + "service_id": job.service_id, + "service_name": job.service.name, + } except NoResultFound: pass - else: - return jsonify( - { - "type": "job", - "id": job.id, - "original_file_name": job.original_file_name, - "created_at": job.created_at, - "created_by_id": job.created_by_id, - "created_by_name": job.created_by.name, - "processing_started": job.processing_started, - "processing_finished": job.processing_finished, - "notification_count": job.notification_count, - "job_status": job.job_status, - "service_id": job.service_id, - "service_name": job.service.name, - } - ) + +def user_query(id): try: user = get_user_by_id(id) - except NoResultFound: - pass - else: - return jsonify( - { + if user: + return { "type": "user", "id": user.id, "name": user.name, } - ) + + except NoResultFound: + pass + + +@support_blueprint.route("/", methods=["GET"]) +def get_id_info(id): + + for query_func in [user_query, service_query, template_query, job_query, notification_query, notification_history_query]: + results = query_func(id) + if results: + return jsonify(results) return jsonify(data={"type": "no result found"})