mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
Adding new method to cancel a letter job.
This commit is contained in:
committed by
Pea Tyczynska
parent
5093b68f94
commit
6565c19a9b
@@ -2,14 +2,17 @@ import uuid
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from flask import current_app
|
||||
from notifications_utils.letter_timings import letter_can_be_cancelled
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from sqlalchemy import (
|
||||
asc,
|
||||
desc,
|
||||
func,
|
||||
)
|
||||
from sqlalchemy.testing import not_in_
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.utils import midnight_n_days_ago
|
||||
from app.models import (
|
||||
Job,
|
||||
@@ -18,7 +21,11 @@ from app.models import (
|
||||
LETTER_TYPE,
|
||||
Notification,
|
||||
Template,
|
||||
ServiceDataRetention
|
||||
ServiceDataRetention,
|
||||
NOTIFICATION_SENDING,
|
||||
NOTIFICATION_CREATED,
|
||||
NOTIFICATION_CANCELLED,
|
||||
JOB_STATUS_CANCELLED
|
||||
)
|
||||
from app.variables import LETTER_TEST_API_FILENAME
|
||||
|
||||
@@ -148,16 +155,37 @@ def dao_get_jobs_older_than_data_retention(notification_types):
|
||||
return jobs
|
||||
|
||||
|
||||
def dao_get_all_letter_jobs():
|
||||
return db.session.query(
|
||||
Job
|
||||
).join(
|
||||
Job.template
|
||||
).filter(
|
||||
Template.template_type == LETTER_TYPE,
|
||||
# test letter jobs (or from research mode services) are created with a different filename,
|
||||
# exclude them so we don't see them on the send to CSV
|
||||
Job.original_file_name != LETTER_TEST_API_FILENAME
|
||||
).order_by(
|
||||
desc(Job.created_at)
|
||||
).all()
|
||||
@transactional
|
||||
def dao_cancel_letter_job(service_id, job_id):
|
||||
if can_cancel_letter_job(job_id, service_id):
|
||||
number_of_notifications_cancelled = Notification.query.filter(
|
||||
Notification.job_id == job_id
|
||||
).update({'status': NOTIFICATION_CANCELLED,
|
||||
'updated_at': datetime.utcnow(),
|
||||
'billable_units': 0})
|
||||
# calling this 2x - pass job into can_cancel_letter_job
|
||||
job = dao_get_job_by_service_id_and_job_id(service_id, job_id)
|
||||
job.job_status = JOB_STATUS_CANCELLED
|
||||
dao_update_job(job)
|
||||
return number_of_notifications_cancelled
|
||||
|
||||
|
||||
def can_cancel_letter_job(job_id, service_id):
|
||||
job = dao_get_job_by_service_id_and_job_id(service_id, job_id)
|
||||
# assert is a letter job
|
||||
# assert job status == finished???
|
||||
# Notifications are not in pending-virus-check
|
||||
count_notifications_in_sending = Notification.query.filter(
|
||||
Notification.job_id == job_id,
|
||||
Notification.status.not_in_('created', 'pending-virus-check', 'cancelled')
|
||||
).count()
|
||||
if count_notifications_in_sending > 0:
|
||||
return False
|
||||
if not letter_can_be_cancelled(NOTIFICATION_CREATED, job.created_at):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user