mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-31 23:26:23 -05:00
Return specific error messages if job cannot be cancelled
Also move checking if job can be cancelled to the endpoint
This commit is contained in:
@@ -30,6 +30,11 @@ from app.models import (
|
||||
)
|
||||
|
||||
|
||||
CANCELLABLE_LETTER_STATUSES = [
|
||||
'created', 'cancelled', 'virus-scan-failed', 'validation-failed', 'technical-failure', 'pending-virus-check'
|
||||
]
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_notification_outcomes_for_job(service_id, job_id):
|
||||
return db.session.query(
|
||||
@@ -157,30 +162,30 @@ def dao_get_jobs_older_than_data_retention(notification_types):
|
||||
|
||||
@transactional
|
||||
def dao_cancel_letter_job(job):
|
||||
if can_cancel_letter_job(job):
|
||||
number_of_notifications_cancelled = Notification.query.filter(
|
||||
Notification.job_id == job.id
|
||||
).update({'status': NOTIFICATION_CANCELLED,
|
||||
'updated_at': datetime.utcnow(),
|
||||
'billable_units': 0})
|
||||
job.job_status = JOB_STATUS_CANCELLED
|
||||
dao_update_job(job)
|
||||
return number_of_notifications_cancelled
|
||||
else:
|
||||
return False
|
||||
number_of_notifications_cancelled = Notification.query.filter(
|
||||
Notification.job_id == job.id
|
||||
).update({'status': NOTIFICATION_CANCELLED,
|
||||
'updated_at': datetime.utcnow(),
|
||||
'billable_units': 0})
|
||||
job.job_status = JOB_STATUS_CANCELLED
|
||||
dao_update_job(job)
|
||||
return number_of_notifications_cancelled
|
||||
|
||||
|
||||
def can_cancel_letter_job(job):
|
||||
def can_letter_job_be_cancelled(job):
|
||||
template = dao_get_template_by_id(job.template_id)
|
||||
if template.template_type != LETTER_TYPE:
|
||||
return False
|
||||
return "Only letter jobs can be cancelled through this endpoint. This is not a letter job."
|
||||
if job.job_status != JOB_STATUS_FINISHED:
|
||||
return False
|
||||
# Notifications are not in pending-virus-check
|
||||
return "This job is still being processed. Wait a couple of minutes and try again."
|
||||
|
||||
count_notifications = Notification.query.filter(
|
||||
Notification.job_id == job.id,
|
||||
Notification.status.in_(['created', 'pending-virus-check', 'cancelled'])
|
||||
Notification.status.in_(CANCELLABLE_LETTER_STATUSES)
|
||||
).count()
|
||||
if count_notifications != job.notification_count:
|
||||
return False
|
||||
return letter_can_be_cancelled(NOTIFICATION_CREATED, job.created_at)
|
||||
return "This job is still being processed. Wait a couple of minutes and try again."
|
||||
if letter_can_be_cancelled(NOTIFICATION_CREATED, job.created_at):
|
||||
return True
|
||||
else:
|
||||
return "Sorry, it's too late, letters have already been sent."
|
||||
|
||||
@@ -15,7 +15,7 @@ from app.dao.jobs_dao import (
|
||||
dao_get_future_scheduled_job_by_id_and_service_id,
|
||||
dao_get_notification_outcomes_for_job,
|
||||
dao_cancel_letter_job,
|
||||
can_cancel_letter_job
|
||||
can_letter_job_be_cancelled
|
||||
)
|
||||
from app.dao.fact_notification_status_dao import fetch_notification_statuses_for_job
|
||||
from app.dao.services_dao import dao_fetch_service_by_id
|
||||
@@ -63,17 +63,15 @@ def cancel_job(service_id, job_id):
|
||||
return get_job_by_service_and_job_id(service_id, job_id)
|
||||
|
||||
|
||||
@job_blueprint.route('/<job_id>/cancel-letter-job')
|
||||
@job_blueprint.route('/<job_id>/cancel-letter-job', methods=['POST'])
|
||||
def cancel_letter_job(service_id, job_id):
|
||||
job = dao_get_job_by_service_id_and_job_id(service_id, job_id)
|
||||
if can_cancel_letter_job(job):
|
||||
dao_cancel_letter_job(job)
|
||||
return jsonify(), 201
|
||||
can_we_cancel = can_letter_job_be_cancelled(job)
|
||||
if can_we_cancel is True:
|
||||
data = dao_cancel_letter_job(job)
|
||||
return jsonify(data), 200
|
||||
else:
|
||||
# reasons the letter can't be cancelled:
|
||||
# letters are not yet saved to db --> can still cancel but later
|
||||
# it's too late
|
||||
return jsonify("CHANGE ME: some error to say the job can't be cancelled"), 400
|
||||
return jsonify(message=can_we_cancel), 400
|
||||
|
||||
|
||||
@job_blueprint.route('/<job_id>/notifications', methods=['GET'])
|
||||
|
||||
Reference in New Issue
Block a user