mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-22 00:49:57 -04:00
remove letters from /service
This commit is contained in:
@@ -383,12 +383,11 @@ def associate_services_to_organisations():
|
||||
|
||||
@notify_command(name='populate-service-volume-intentions')
|
||||
@click.option('-f', '--file_name', required=True,
|
||||
help="Pipe delimited file containing service_id, SMS, email, letters")
|
||||
help="Pipe delimited file containing service_id, SMS, email")
|
||||
def populate_service_volume_intentions(file_name):
|
||||
# [0] service_id
|
||||
# [1] SMS:: volume intentions for service
|
||||
# [2] Email:: volume intentions for service
|
||||
# [3] Letters:: volume intentions for service
|
||||
|
||||
with open(file_name, 'r') as f:
|
||||
for line in itertools.islice(f, 1, None):
|
||||
@@ -397,7 +396,6 @@ def populate_service_volume_intentions(file_name):
|
||||
service = dao_fetch_service_by_id(columns[0])
|
||||
service.volume_sms = columns[1]
|
||||
service.volume_email = columns[2]
|
||||
service.volume_letter = columns[3]
|
||||
dao_update_service(service)
|
||||
print("populate-service-volume-intentions complete")
|
||||
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
from sqlalchemy import desc, func
|
||||
|
||||
from app import db
|
||||
from app.models import (
|
||||
Job,
|
||||
Notification,
|
||||
NotificationHistory,
|
||||
ReturnedLetter,
|
||||
Template,
|
||||
User,
|
||||
)
|
||||
from app.utils import midnight_n_days_ago
|
||||
|
||||
|
||||
def fetch_recent_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'),
|
||||
ReturnedLetter.reported_at
|
||||
).filter(
|
||||
ReturnedLetter.service_id == service_id,
|
||||
).group_by(
|
||||
ReturnedLetter.reported_at
|
||||
).order_by(
|
||||
desc(ReturnedLetter.reported_at)
|
||||
).all()
|
||||
|
||||
|
||||
def fetch_returned_letters(service_id, report_date):
|
||||
results = []
|
||||
for table in [Notification, NotificationHistory]:
|
||||
query = db.session.query(
|
||||
ReturnedLetter.notification_id,
|
||||
ReturnedLetter.reported_at,
|
||||
table.client_reference,
|
||||
table.created_at,
|
||||
Template.name.label('template_name'),
|
||||
table.template_id,
|
||||
table.template_version,
|
||||
Template.hidden,
|
||||
table.api_key_id,
|
||||
table.created_by_id,
|
||||
User.name.label('user_name'),
|
||||
User.email_address,
|
||||
Job.original_file_name,
|
||||
(table.job_row_number + 1).label('job_row_number') # row numbers start at 0
|
||||
).outerjoin(
|
||||
User, table.created_by_id == User.id
|
||||
).outerjoin(
|
||||
Job, table.job_id == Job.id
|
||||
).filter(
|
||||
ReturnedLetter.service_id == service_id,
|
||||
ReturnedLetter.reported_at == report_date,
|
||||
ReturnedLetter.notification_id == table.id,
|
||||
table.template_id == Template.id
|
||||
).order_by(
|
||||
desc(ReturnedLetter.reported_at), desc(table.created_at)
|
||||
)
|
||||
results = results + query.all()
|
||||
results = sorted(results, key=lambda i: i.created_at, reverse=True)
|
||||
return results
|
||||
@@ -1,105 +0,0 @@
|
||||
from sqlalchemy import desc
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import autocommit
|
||||
from app.models import ServiceLetterContact, Template
|
||||
|
||||
|
||||
def dao_get_letter_contacts_by_service_id(service_id):
|
||||
letter_contacts = db.session.query(
|
||||
ServiceLetterContact
|
||||
).filter(
|
||||
ServiceLetterContact.service_id == service_id,
|
||||
ServiceLetterContact.archived == False # noqa
|
||||
).order_by(
|
||||
desc(ServiceLetterContact.is_default),
|
||||
desc(ServiceLetterContact.created_at)
|
||||
).all()
|
||||
|
||||
return letter_contacts
|
||||
|
||||
|
||||
def dao_get_letter_contact_by_id(service_id, letter_contact_id):
|
||||
letter_contact = db.session.query(
|
||||
ServiceLetterContact
|
||||
).filter(
|
||||
ServiceLetterContact.service_id == service_id,
|
||||
ServiceLetterContact.id == letter_contact_id,
|
||||
ServiceLetterContact.archived == False # noqa
|
||||
).one()
|
||||
return letter_contact
|
||||
|
||||
|
||||
@autocommit
|
||||
def add_letter_contact_for_service(service_id, contact_block, is_default):
|
||||
old_default = _get_existing_default(service_id)
|
||||
if is_default:
|
||||
_reset_old_default_to_false(old_default)
|
||||
|
||||
new_letter_contact = ServiceLetterContact(
|
||||
service_id=service_id,
|
||||
contact_block=contact_block,
|
||||
is_default=is_default
|
||||
)
|
||||
db.session.add(new_letter_contact)
|
||||
return new_letter_contact
|
||||
|
||||
|
||||
@autocommit
|
||||
def update_letter_contact(service_id, letter_contact_id, contact_block, is_default):
|
||||
old_default = _get_existing_default(service_id)
|
||||
# if we want to make this the default, ensure there are no other existing defaults
|
||||
if is_default:
|
||||
_reset_old_default_to_false(old_default)
|
||||
|
||||
letter_contact_update = ServiceLetterContact.query.get(letter_contact_id)
|
||||
letter_contact_update.contact_block = contact_block
|
||||
letter_contact_update.is_default = is_default
|
||||
db.session.add(letter_contact_update)
|
||||
return letter_contact_update
|
||||
|
||||
|
||||
@autocommit
|
||||
def archive_letter_contact(service_id, letter_contact_id):
|
||||
letter_contact_to_archive = ServiceLetterContact.query.filter_by(
|
||||
id=letter_contact_id,
|
||||
service_id=service_id
|
||||
).one()
|
||||
|
||||
Template.query.filter_by(
|
||||
service_letter_contact_id=letter_contact_id
|
||||
).update({
|
||||
'service_letter_contact_id': None
|
||||
})
|
||||
|
||||
letter_contact_to_archive.archived = True
|
||||
|
||||
db.session.add(letter_contact_to_archive)
|
||||
return letter_contact_to_archive
|
||||
|
||||
|
||||
def _get_existing_default(service_id):
|
||||
old_defaults = [
|
||||
x for x
|
||||
in dao_get_letter_contacts_by_service_id(service_id=service_id)
|
||||
if x.is_default
|
||||
]
|
||||
|
||||
if len(old_defaults) == 0:
|
||||
return None
|
||||
|
||||
if len(old_defaults) == 1:
|
||||
return old_defaults[0]
|
||||
|
||||
raise Exception(
|
||||
"There should only be one default letter contact for each service. Service {} has {}".format(
|
||||
service_id,
|
||||
len(old_defaults)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _reset_old_default_to_false(old_default):
|
||||
if old_default:
|
||||
old_default.is_default = False
|
||||
db.session.add(old_default)
|
||||
@@ -5,13 +5,7 @@ from app import api_user, authenticated_service
|
||||
from app.config import QueueNames
|
||||
from app.dao import notifications_dao
|
||||
from app.errors import InvalidRequest, register_errors
|
||||
from app.models import (
|
||||
EMAIL_TYPE,
|
||||
KEY_TYPE_TEAM,
|
||||
LETTER_TYPE,
|
||||
PRIORITY,
|
||||
SMS_TYPE,
|
||||
)
|
||||
from app.models import EMAIL_TYPE, KEY_TYPE_TEAM, PRIORITY, SMS_TYPE
|
||||
from app.notifications.process_notifications import (
|
||||
persist_notification,
|
||||
send_notification_to_queue,
|
||||
@@ -81,7 +75,6 @@ def send_notification(notification_type):
|
||||
|
||||
if notification_type not in [SMS_TYPE, EMAIL_TYPE]:
|
||||
msg = "{} notification type is not supported".format(notification_type)
|
||||
msg = msg + ", please use the latest version of the client" if notification_type == LETTER_TYPE else msg
|
||||
raise InvalidRequest(msg, 400)
|
||||
|
||||
notification_form = (
|
||||
|
||||
@@ -14,14 +14,12 @@ from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app import redis_store
|
||||
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
|
||||
from app.dao.service_letter_contact_dao import dao_get_letter_contact_by_id
|
||||
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
|
||||
from app.models import (
|
||||
EMAIL_TYPE,
|
||||
INTERNATIONAL_SMS_TYPE,
|
||||
KEY_TYPE_TEAM,
|
||||
KEY_TYPE_TEST,
|
||||
LETTER_TYPE,
|
||||
SMS_TYPE,
|
||||
ServicePermission,
|
||||
)
|
||||
@@ -201,8 +199,6 @@ def check_reply_to(service_id, reply_to_id, type_):
|
||||
return check_service_email_reply_to_id(service_id, reply_to_id, type_)
|
||||
elif type_ == SMS_TYPE:
|
||||
return check_service_sms_sender_id(service_id, reply_to_id, type_)
|
||||
elif type_ == LETTER_TYPE:
|
||||
return check_service_letter_contact_id(service_id, reply_to_id, type_)
|
||||
|
||||
|
||||
def check_service_email_reply_to_id(service_id, reply_to_id, notification_type):
|
||||
@@ -223,13 +219,3 @@ def check_service_sms_sender_id(service_id, sms_sender_id, notification_type):
|
||||
message = 'sms_sender_id {} does not exist in database for service id {}' \
|
||||
.format(sms_sender_id, service_id)
|
||||
raise BadRequestError(message=message)
|
||||
|
||||
|
||||
def check_service_letter_contact_id(service_id, letter_contact_id, notification_type):
|
||||
if letter_contact_id:
|
||||
try:
|
||||
return dao_get_letter_contact_by_id(service_id, letter_contact_id).contact_block
|
||||
except NoResultFound:
|
||||
message = 'letter_contact_id {} does not exist in database for service id {}' \
|
||||
.format(letter_contact_id, service_id)
|
||||
raise BadRequestError(message=message)
|
||||
|
||||
@@ -28,12 +28,6 @@ from app.dao.fact_notification_status_dao import (
|
||||
)
|
||||
from app.dao.inbound_numbers_dao import dao_allocate_number_for_service
|
||||
from app.dao.organisation_dao import dao_get_organisation_by_service_id
|
||||
from app.dao.returned_letters_dao import (
|
||||
fetch_most_recent_returned_letter,
|
||||
fetch_recent_returned_letter_count,
|
||||
fetch_returned_letter_summary,
|
||||
fetch_returned_letters,
|
||||
)
|
||||
from app.dao.service_contact_list_dao import (
|
||||
dao_archive_contact_list,
|
||||
dao_get_contact_list_by_id,
|
||||
@@ -59,13 +53,6 @@ from app.dao.service_guest_list_dao import (
|
||||
dao_fetch_service_guest_list,
|
||||
dao_remove_service_guest_list,
|
||||
)
|
||||
from app.dao.service_letter_contact_dao import (
|
||||
add_letter_contact_for_service,
|
||||
archive_letter_contact,
|
||||
dao_get_letter_contact_by_id,
|
||||
dao_get_letter_contacts_by_service_id,
|
||||
update_letter_contact,
|
||||
)
|
||||
from app.dao.service_sms_sender_dao import (
|
||||
archive_sms_sender,
|
||||
dao_add_sms_sender_for_service,
|
||||
@@ -126,17 +113,11 @@ from app.service.service_data_retention_schema import (
|
||||
)
|
||||
from app.service.service_senders_schema import (
|
||||
add_service_email_reply_to_request,
|
||||
add_service_letter_contact_block_request,
|
||||
add_service_sms_sender_request,
|
||||
)
|
||||
from app.service.utils import get_guest_list_objects
|
||||
from app.user.users_schema import post_set_permissions_schema
|
||||
from app.utils import (
|
||||
DATE_FORMAT,
|
||||
DATETIME_FORMAT_NO_TIMEZONE,
|
||||
get_prev_next_pagination_links,
|
||||
midnight_n_days_ago,
|
||||
)
|
||||
from app.utils import get_prev_next_pagination_links
|
||||
|
||||
service_blueprint = Blueprint('service', __name__)
|
||||
|
||||
@@ -477,6 +458,7 @@ def get_notification_for_service(service_id, notification_id):
|
||||
), 200
|
||||
|
||||
|
||||
# TODO: possibly unnecessary after removing letters
|
||||
@service_blueprint.route('/<uuid:service_id>/notifications/<uuid:notification_id>/cancel', methods=['POST'])
|
||||
def cancel_notification_for_service(service_id, notification_id):
|
||||
notification = notifications_dao.get_notification_by_id(notification_id, service_id)
|
||||
@@ -787,48 +769,6 @@ def delete_service_reply_to_email_address(service_id, reply_to_email_id):
|
||||
return jsonify(data=archived_reply_to.serialize()), 200
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/letter-contact', methods=["GET"])
|
||||
def get_letter_contacts(service_id):
|
||||
result = dao_get_letter_contacts_by_service_id(service_id)
|
||||
return jsonify([i.serialize() for i in result]), 200
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/letter-contact/<uuid:letter_contact_id>', methods=["GET"])
|
||||
def get_letter_contact_by_id(service_id, letter_contact_id):
|
||||
result = dao_get_letter_contact_by_id(service_id=service_id, letter_contact_id=letter_contact_id)
|
||||
return jsonify(result.serialize()), 200
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/letter-contact', methods=['POST'])
|
||||
def add_service_letter_contact(service_id):
|
||||
# validate the service exists, throws ResultNotFound exception.
|
||||
dao_fetch_service_by_id(service_id)
|
||||
form = validate(request.get_json(), add_service_letter_contact_block_request)
|
||||
new_letter_contact = add_letter_contact_for_service(service_id=service_id,
|
||||
contact_block=form['contact_block'],
|
||||
is_default=form.get('is_default', True))
|
||||
return jsonify(data=new_letter_contact.serialize()), 201
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/letter-contact/<uuid:letter_contact_id>', methods=['POST'])
|
||||
def update_service_letter_contact(service_id, letter_contact_id):
|
||||
# validate the service exists, throws ResultNotFound exception.
|
||||
dao_fetch_service_by_id(service_id)
|
||||
form = validate(request.get_json(), add_service_letter_contact_block_request)
|
||||
new_reply_to = update_letter_contact(service_id=service_id,
|
||||
letter_contact_id=letter_contact_id,
|
||||
contact_block=form['contact_block'],
|
||||
is_default=form.get('is_default', True))
|
||||
return jsonify(data=new_reply_to.serialize()), 200
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/letter-contact/<uuid:letter_contact_id>/archive', methods=['POST'])
|
||||
def delete_service_letter_contact(service_id, letter_contact_id):
|
||||
archived_letter_contact = archive_letter_contact(service_id, letter_contact_id)
|
||||
|
||||
return jsonify(data=archived_letter_contact.serialize()), 200
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/sms-sender', methods=['POST'])
|
||||
def add_service_sms_sender(service_id):
|
||||
dao_fetch_service_by_id(service_id)
|
||||
@@ -1006,71 +946,6 @@ def check_if_reply_to_address_already_in_use(service_id, email_address):
|
||||
)
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/returned-letter-statistics', methods=['GET'])
|
||||
def returned_letter_statistics(service_id):
|
||||
|
||||
most_recent = fetch_most_recent_returned_letter(service_id)
|
||||
|
||||
if not most_recent:
|
||||
return jsonify({
|
||||
'returned_letter_count': 0,
|
||||
'most_recent_report': None,
|
||||
})
|
||||
|
||||
most_recent_reported_at = datetime.combine(
|
||||
most_recent.reported_at, datetime.min.time()
|
||||
)
|
||||
|
||||
if most_recent_reported_at < midnight_n_days_ago(7):
|
||||
return jsonify({
|
||||
'returned_letter_count': 0,
|
||||
'most_recent_report': most_recent.reported_at.strftime(DATETIME_FORMAT_NO_TIMEZONE),
|
||||
})
|
||||
|
||||
count = fetch_recent_returned_letter_count(service_id)
|
||||
|
||||
return jsonify({
|
||||
'returned_letter_count': count.returned_letter_count,
|
||||
'most_recent_report': most_recent.reported_at.strftime(DATETIME_FORMAT_NO_TIMEZONE),
|
||||
})
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/returned-letter-summary', methods=['GET'])
|
||||
def returned_letter_summary(service_id):
|
||||
results = fetch_returned_letter_summary(service_id)
|
||||
|
||||
json_results = [{'returned_letter_count': x.returned_letter_count,
|
||||
'reported_at': x.reported_at.strftime(DATE_FORMAT)
|
||||
} for x in results]
|
||||
|
||||
return jsonify(json_results)
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/returned-letters', methods=['GET'])
|
||||
def get_returned_letters(service_id):
|
||||
results = fetch_returned_letters(service_id=service_id, report_date=request.args.get('reported_at'))
|
||||
|
||||
json_results = [
|
||||
{'notification_id': x.notification_id,
|
||||
# client reference can only be added on API letters
|
||||
'client_reference': x.client_reference if x.api_key_id else None,
|
||||
'reported_at': x.reported_at.strftime(DATE_FORMAT),
|
||||
'created_at': x.created_at.strftime(DATETIME_FORMAT_NO_TIMEZONE),
|
||||
# it doesn't make sense to show hidden templates
|
||||
'template_name': x.template_name if not x.hidden else None,
|
||||
'template_id': x.template_id if not x.hidden else None,
|
||||
'template_version': x.template_version if not x.hidden else None,
|
||||
'user_name': x.user_name or 'API',
|
||||
'email_address': x.email_address or 'API',
|
||||
'original_file_name': x.original_file_name,
|
||||
'job_row_number': x.job_row_number,
|
||||
# the file name for a letter uploaded via the UI
|
||||
'uploaded_letter_file_name': x.client_reference if x.hidden and not x.api_key_id else None
|
||||
} for x in results]
|
||||
|
||||
return jsonify(sorted(json_results, key=lambda i: i['created_at'], reverse=True))
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/contact-list', methods=['GET'])
|
||||
def get_contact_list(service_id):
|
||||
contact_lists = dao_get_contact_lists(service_id)
|
||||
|
||||
@@ -1,21 +1,12 @@
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app import create_random_identifier
|
||||
from app.config import QueueNames
|
||||
from app.dao.notifications_dao import _update_notification_status
|
||||
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
|
||||
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
|
||||
from app.dao.services_dao import dao_fetch_service_by_id
|
||||
from app.dao.templates_dao import dao_get_template_by_id_and_service_id
|
||||
from app.dao.users_dao import get_user_by_id
|
||||
from app.models import (
|
||||
EMAIL_TYPE,
|
||||
KEY_TYPE_NORMAL,
|
||||
LETTER_TYPE,
|
||||
NOTIFICATION_DELIVERED,
|
||||
PRIORITY,
|
||||
SMS_TYPE,
|
||||
)
|
||||
from app.models import EMAIL_TYPE, KEY_TYPE_NORMAL, PRIORITY, SMS_TYPE
|
||||
from app.notifications.process_notifications import (
|
||||
persist_notification,
|
||||
send_notification_to_queue,
|
||||
@@ -38,9 +29,8 @@ def validate_created_by(service, created_by_id):
|
||||
raise BadRequestError(message=message)
|
||||
|
||||
|
||||
# TODO: possibly unnecessary after removing letters
|
||||
def create_one_off_reference(template_type):
|
||||
if template_type == LETTER_TYPE:
|
||||
return create_random_identifier()
|
||||
return None
|
||||
|
||||
|
||||
@@ -92,17 +82,11 @@ def send_one_off_notification(service_id, post_data):
|
||||
|
||||
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
|
||||
|
||||
if template.template_type == LETTER_TYPE and service.research_mode:
|
||||
_update_notification_status(
|
||||
notification,
|
||||
NOTIFICATION_DELIVERED,
|
||||
)
|
||||
else:
|
||||
send_notification_to_queue(
|
||||
notification=notification,
|
||||
research_mode=service.research_mode,
|
||||
queue=queue_name,
|
||||
)
|
||||
send_notification_to_queue(
|
||||
notification=notification,
|
||||
research_mode=service.research_mode,
|
||||
queue=queue_name,
|
||||
)
|
||||
|
||||
return {'id': str(notification.id)}
|
||||
|
||||
|
||||
@@ -13,19 +13,6 @@ add_service_email_reply_to_request = {
|
||||
}
|
||||
|
||||
|
||||
add_service_letter_contact_block_request = {
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "POST service letter contact block",
|
||||
"type": "object",
|
||||
"title": "Add new letter contact block for service",
|
||||
"properties": {
|
||||
"contact_block": {"type": "string"},
|
||||
"is_default": {"type": "boolean"}
|
||||
},
|
||||
"required": ["contact_block", "is_default"]
|
||||
}
|
||||
|
||||
|
||||
add_service_sms_sender_request = {
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "POST add service SMS sender",
|
||||
|
||||
Reference in New Issue
Block a user