mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-22 09:41:05 -04:00
remove save-letter task
This commit is contained in:
@@ -12,11 +12,8 @@ from sqlalchemy.exc import IntegrityError, SQLAlchemyError
|
||||
|
||||
from app import create_random_identifier, create_uuid, encryption, notify_celery
|
||||
from app.aws import s3
|
||||
from app.celery import provider_tasks, research_mode_tasks
|
||||
from app.celery import provider_tasks
|
||||
from app.config import QueueNames
|
||||
from app.dao.daily_sorted_letter_dao import (
|
||||
dao_create_or_update_daily_sorted_letter,
|
||||
)
|
||||
from app.dao.inbound_sms_dao import dao_get_inbound_sms_by_id
|
||||
from app.dao.jobs_dao import dao_get_job_by_id, dao_update_job
|
||||
from app.dao.notifications_dao import (
|
||||
@@ -26,15 +23,11 @@ from app.dao.notifications_dao import (
|
||||
get_notification_by_id,
|
||||
update_notification_status_by_reference,
|
||||
)
|
||||
from app.dao.provider_details_dao import (
|
||||
get_provider_details_by_notification_type,
|
||||
)
|
||||
from app.dao.returned_letters_dao import insert_or_update_returned_letters
|
||||
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
|
||||
from app.dao.service_inbound_api_dao import get_service_inbound_api_for_service
|
||||
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
from app.exceptions import DVLAException, NotificationTechnicalFailureException
|
||||
from app.models import (
|
||||
DVLA_RESPONSE_STATUS_SENT,
|
||||
EMAIL_TYPE,
|
||||
@@ -136,8 +129,7 @@ def process_row(row, template, job, service, sender_id=None):
|
||||
|
||||
send_fns = {
|
||||
SMS_TYPE: save_sms,
|
||||
EMAIL_TYPE: save_email,
|
||||
LETTER_TYPE: save_letter
|
||||
EMAIL_TYPE: save_email
|
||||
}
|
||||
|
||||
send_fn = send_fns[template_type]
|
||||
@@ -341,57 +333,6 @@ def save_api_email_or_sms(self, encrypted_notification):
|
||||
current_app.logger.error(f"Max retry failed Failed to persist notification {notification['id']}")
|
||||
|
||||
|
||||
@notify_celery.task(bind=True, name="save-letter", max_retries=5, default_retry_delay=300)
|
||||
def save_letter(
|
||||
self,
|
||||
service_id,
|
||||
notification_id,
|
||||
encrypted_notification,
|
||||
):
|
||||
notification = encryption.decrypt(encrypted_notification)
|
||||
|
||||
postal_address = PostalAddress.from_personalisation(
|
||||
InsensitiveDict(notification['personalisation'])
|
||||
)
|
||||
|
||||
service = SerialisedService.from_id(service_id)
|
||||
template = SerialisedTemplate.from_id_and_service_id(
|
||||
notification['template'],
|
||||
service_id=service.id,
|
||||
version=notification['template_version'],
|
||||
)
|
||||
|
||||
try:
|
||||
# if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up
|
||||
status = NOTIFICATION_CREATED if not service.research_mode else NOTIFICATION_SENDING
|
||||
|
||||
saved_notification = persist_notification(
|
||||
template_id=notification['template'],
|
||||
template_version=notification['template_version'],
|
||||
postage=postal_address.postage if postal_address.international else template.postage,
|
||||
recipient=postal_address.normalised,
|
||||
service=service,
|
||||
personalisation=notification['personalisation'],
|
||||
notification_type=LETTER_TYPE,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
created_at=datetime.utcnow(),
|
||||
job_id=notification['job'],
|
||||
job_row_number=notification['row_number'],
|
||||
notification_id=notification_id,
|
||||
reference=create_random_identifier(),
|
||||
client_reference=get_reference_from_personalisation(notification['personalisation']),
|
||||
reply_to_text=template.reply_to_text,
|
||||
status=status
|
||||
)
|
||||
|
||||
update_notification_status_by_reference(saved_notification.reference, 'delivered')
|
||||
|
||||
current_app.logger.debug("Letter {} created at {}".format(saved_notification.id, saved_notification.created_at))
|
||||
except SQLAlchemyError as e:
|
||||
handle_exception(self, notification, notification_id, e)
|
||||
|
||||
|
||||
def handle_exception(task, notification, notification_id, exc):
|
||||
if not get_notification_by_id(notification_id):
|
||||
retry_msg = '{task} notification for job {job} row number {row} and notification id {noti}'.format(
|
||||
|
||||
@@ -29,7 +29,6 @@ from app.celery.tasks import (
|
||||
save_api_email,
|
||||
save_api_sms,
|
||||
save_email,
|
||||
save_letter,
|
||||
save_sms,
|
||||
send_inbound_sms_to_service,
|
||||
)
|
||||
@@ -94,7 +93,6 @@ def test_should_have_decorated_tasks_functions():
|
||||
assert process_job.__wrapped__.__name__ == 'process_job'
|
||||
assert save_sms.__wrapped__.__name__ == 'save_sms'
|
||||
assert save_email.__wrapped__.__name__ == 'save_email'
|
||||
assert save_letter.__wrapped__.__name__ == 'save_letter'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -368,8 +366,6 @@ def test_should_process_all_sms_job(sample_job_with_placeholdered_template,
|
||||
(SMS_TYPE, True, 'save_sms', 'research-mode-tasks'),
|
||||
(EMAIL_TYPE, False, 'save_email', 'database-tasks'),
|
||||
(EMAIL_TYPE, True, 'save_email', 'research-mode-tasks'),
|
||||
(LETTER_TYPE, False, 'save_letter', 'database-tasks'),
|
||||
(LETTER_TYPE, True, 'save_letter', 'research-mode-tasks'),
|
||||
])
|
||||
def test_process_row_sends_letter_task(template_type, research_mode, expected_function, expected_queue, mocker):
|
||||
mocker.patch('app.celery.tasks.create_uuid', return_value='noti_uuid')
|
||||
@@ -1343,28 +1339,6 @@ def test_process_incomplete_job_email(mocker, sample_email_template):
|
||||
assert mock_email_saver.call_count == 8 # There are 10 in the file and we've added two already
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Remove mail")
|
||||
def test_process_incomplete_job_letter(mocker, sample_letter_template):
|
||||
mocker.patch('app.celery.tasks.s3.get_job_and_metadata_from_s3',
|
||||
return_value=(load_example_csv('multiple_letter'), {'sender_id': None}))
|
||||
mock_letter_saver = mocker.patch('app.celery.tasks.save_letter.apply_async')
|
||||
|
||||
job = create_job(template=sample_letter_template, notification_count=10,
|
||||
created_at=datetime.utcnow() - timedelta(hours=2),
|
||||
scheduled_for=datetime.utcnow() - timedelta(minutes=31),
|
||||
processing_started=datetime.utcnow() - timedelta(minutes=31),
|
||||
job_status=JOB_STATUS_ERROR)
|
||||
|
||||
create_notification(sample_letter_template, job, 0)
|
||||
create_notification(sample_letter_template, job, 1)
|
||||
|
||||
assert Notification.query.filter(Notification.job_id == job.id).count() == 2
|
||||
|
||||
process_incomplete_job(str(job.id))
|
||||
|
||||
assert mock_letter_saver.call_count == 8
|
||||
|
||||
|
||||
@freeze_time('2017-01-01')
|
||||
def test_process_incomplete_jobs_sets_status_to_in_progress_and_resets_processing_started_time(mocker, sample_template):
|
||||
mock_process_incomplete_job = mocker.patch('app.celery.tasks.process_incomplete_job')
|
||||
|
||||
Reference in New Issue
Block a user