mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
Return tuple from can_cancel_letter_job for clarity
This commit is contained in:
@@ -170,20 +170,20 @@ def dao_cancel_letter_job(job):
|
|||||||
def can_letter_job_be_cancelled(job):
|
def can_letter_job_be_cancelled(job):
|
||||||
template = dao_get_template_by_id(job.template_id)
|
template = dao_get_template_by_id(job.template_id)
|
||||||
if template.template_type != LETTER_TYPE:
|
if template.template_type != LETTER_TYPE:
|
||||||
return "Only letter jobs can be cancelled through this endpoint. This is not a letter job."
|
return False, "Only letter jobs can be cancelled through this endpoint. This is not a letter job."
|
||||||
|
|
||||||
notifications = Notification.query.filter(
|
notifications = Notification.query.filter(
|
||||||
Notification.job_id == job.id
|
Notification.job_id == job.id
|
||||||
).all()
|
).all()
|
||||||
count_notifications = len(notifications)
|
count_notifications = len(notifications)
|
||||||
if job.job_status != JOB_STATUS_FINISHED or count_notifications != job.notification_count:
|
if job.job_status != JOB_STATUS_FINISHED or count_notifications != job.notification_count:
|
||||||
return "This job is still being processed. Wait a couple of minutes and try again."
|
return False, "This job is still being processed. Wait a couple of minutes and try again."
|
||||||
count_cancellable_notifications = len([
|
count_cancellable_notifications = len([
|
||||||
n for n in notifications if n.status in CANCELLABLE_JOB_LETTER_STATUSES
|
n for n in notifications if n.status in CANCELLABLE_JOB_LETTER_STATUSES
|
||||||
])
|
])
|
||||||
if count_cancellable_notifications != job.notification_count or not letter_can_be_cancelled(
|
if count_cancellable_notifications != job.notification_count or not letter_can_be_cancelled(
|
||||||
NOTIFICATION_CREATED, job.created_at
|
NOTIFICATION_CREATED, job.created_at
|
||||||
):
|
):
|
||||||
return "Sorry, it's too late, letters have already been sent."
|
return False, "Sorry, it's too late, letters have already been sent."
|
||||||
|
|
||||||
return True
|
return True, None
|
||||||
|
|||||||
@@ -66,12 +66,12 @@ def cancel_job(service_id, job_id):
|
|||||||
@job_blueprint.route('/<job_id>/cancel-letter-job', methods=['POST'])
|
@job_blueprint.route('/<job_id>/cancel-letter-job', methods=['POST'])
|
||||||
def cancel_letter_job(service_id, job_id):
|
def cancel_letter_job(service_id, job_id):
|
||||||
job = dao_get_job_by_service_id_and_job_id(service_id, job_id)
|
job = dao_get_job_by_service_id_and_job_id(service_id, job_id)
|
||||||
can_we_cancel = can_letter_job_be_cancelled(job)
|
can_we_cancel, errors = can_letter_job_be_cancelled(job)
|
||||||
if can_we_cancel is True:
|
if can_we_cancel:
|
||||||
data = dao_cancel_letter_job(job)
|
data = dao_cancel_letter_job(job)
|
||||||
return jsonify(data), 200
|
return jsonify(data), 200
|
||||||
else:
|
else:
|
||||||
return jsonify(message=can_we_cancel), 400
|
return jsonify(message=errors), 400
|
||||||
|
|
||||||
|
|
||||||
@job_blueprint.route('/<job_id>/notifications', methods=['GET'])
|
@job_blueprint.route('/<job_id>/notifications', methods=['GET'])
|
||||||
|
|||||||
Reference in New Issue
Block a user