mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-06 05:18:26 -04:00
Merge pull request #2841 from alphagov/jobs-by-contact-list
Allow jobs to be grouped by contact list
This commit is contained in:
@@ -58,7 +58,15 @@ def dao_get_job_by_service_id_and_job_id(service_id, job_id):
|
|||||||
return Job.query.filter_by(service_id=service_id, id=job_id).one()
|
return Job.query.filter_by(service_id=service_id, id=job_id).one()
|
||||||
|
|
||||||
|
|
||||||
def dao_get_jobs_by_service_id(service_id, limit_days=None, page=1, page_size=50, statuses=None):
|
def dao_get_jobs_by_service_id(
|
||||||
|
service_id,
|
||||||
|
*,
|
||||||
|
limit_days=None,
|
||||||
|
page=1,
|
||||||
|
page_size=50,
|
||||||
|
statuses=None,
|
||||||
|
contact_list_id=None,
|
||||||
|
):
|
||||||
query_filter = [
|
query_filter = [
|
||||||
Job.service_id == service_id,
|
Job.service_id == service_id,
|
||||||
Job.original_file_name != current_app.config['TEST_MESSAGE_FILENAME'],
|
Job.original_file_name != current_app.config['TEST_MESSAGE_FILENAME'],
|
||||||
@@ -70,6 +78,8 @@ def dao_get_jobs_by_service_id(service_id, limit_days=None, page=1, page_size=50
|
|||||||
query_filter.append(
|
query_filter.append(
|
||||||
Job.job_status.in_(statuses)
|
Job.job_status.in_(statuses)
|
||||||
)
|
)
|
||||||
|
if contact_list_id is not None:
|
||||||
|
query_filter.append(Job.contact_list_id == contact_list_id)
|
||||||
return Job.query \
|
return Job.query \
|
||||||
.filter(*query_filter) \
|
.filter(*query_filter) \
|
||||||
.order_by(Job.processing_started.desc(), Job.created_at.desc()) \
|
.order_by(Job.processing_started.desc(), Job.created_at.desc()) \
|
||||||
|
|||||||
@@ -128,10 +128,13 @@ def get_jobs_by_service(service_id):
|
|||||||
else:
|
else:
|
||||||
limit_days = None
|
limit_days = None
|
||||||
|
|
||||||
statuses = [x.strip() for x in request.args.get('statuses', '').split(',')]
|
return jsonify(**get_paginated_jobs(
|
||||||
|
service_id,
|
||||||
page = int(request.args.get('page', 1))
|
limit_days=limit_days,
|
||||||
return jsonify(**get_paginated_jobs(service_id, limit_days, statuses, page))
|
statuses=[x.strip() for x in request.args.get('statuses', '').split(',')],
|
||||||
|
page=int(request.args.get('page', 1)),
|
||||||
|
contact_list_id=request.args.get('contact_list_id'),
|
||||||
|
))
|
||||||
|
|
||||||
|
|
||||||
@job_blueprint.route('', methods=['POST'])
|
@job_blueprint.route('', methods=['POST'])
|
||||||
@@ -185,13 +188,21 @@ def create_job(service_id):
|
|||||||
return jsonify(data=job_json), 201
|
return jsonify(data=job_json), 201
|
||||||
|
|
||||||
|
|
||||||
def get_paginated_jobs(service_id, limit_days, statuses, page):
|
def get_paginated_jobs(
|
||||||
|
service_id,
|
||||||
|
*,
|
||||||
|
limit_days,
|
||||||
|
statuses,
|
||||||
|
page,
|
||||||
|
contact_list_id,
|
||||||
|
):
|
||||||
pagination = dao_get_jobs_by_service_id(
|
pagination = dao_get_jobs_by_service_id(
|
||||||
service_id,
|
service_id,
|
||||||
limit_days=limit_days,
|
limit_days=limit_days,
|
||||||
page=page,
|
page=page,
|
||||||
page_size=current_app.config['PAGE_SIZE'],
|
page_size=current_app.config['PAGE_SIZE'],
|
||||||
statuses=statuses
|
statuses=statuses,
|
||||||
|
contact_list_id=contact_list_id,
|
||||||
)
|
)
|
||||||
data = job_schema.dump(pagination.items, many=True).data
|
data = job_schema.dump(pagination.items, many=True).data
|
||||||
for job_data in data:
|
for job_data in data:
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from sqlalchemy.dialects.postgresql import (
|
|||||||
JSONB,
|
JSONB,
|
||||||
)
|
)
|
||||||
from sqlalchemy.orm.collections import attribute_mapped_collection
|
from sqlalchemy.orm.collections import attribute_mapped_collection
|
||||||
from sqlalchemy import UniqueConstraint, CheckConstraint, Index
|
from sqlalchemy import UniqueConstraint, CheckConstraint, Index, String, and_, func
|
||||||
from notifications_utils.columns import Columns
|
from notifications_utils.columns import Columns
|
||||||
from notifications_utils.recipients import (
|
from notifications_utils.recipients import (
|
||||||
validate_email_address,
|
validate_email_address,
|
||||||
@@ -2138,12 +2138,27 @@ class ServiceContactList(db.Model):
|
|||||||
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
|
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
|
||||||
archived = db.Column(db.Boolean, nullable=False, default=False)
|
archived = db.Column(db.Boolean, nullable=False, default=False)
|
||||||
|
|
||||||
|
def get_job_count(self):
|
||||||
|
today = datetime.datetime.utcnow().date()
|
||||||
|
return Job.query.filter(
|
||||||
|
Job.contact_list_id == self.id,
|
||||||
|
func.coalesce(
|
||||||
|
Job.processing_started, Job.created_at
|
||||||
|
) >= today - func.coalesce(ServiceDataRetention.days_of_retention, 7)
|
||||||
|
).outerjoin(
|
||||||
|
ServiceDataRetention, and_(
|
||||||
|
self.service_id == ServiceDataRetention.service_id,
|
||||||
|
func.cast(self.template_type, String) == func.cast(ServiceDataRetention.notification_type, String)
|
||||||
|
)
|
||||||
|
).count()
|
||||||
|
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
created_at_in_bst = convert_utc_to_bst(self.created_at)
|
created_at_in_bst = convert_utc_to_bst(self.created_at)
|
||||||
contact_list = {
|
contact_list = {
|
||||||
"id": str(self.id),
|
"id": str(self.id),
|
||||||
"original_file_name": self.original_file_name,
|
"original_file_name": self.original_file_name,
|
||||||
"row_count": self.row_count,
|
"row_count": self.row_count,
|
||||||
|
"job_count": self.get_job_count(),
|
||||||
"template_type": self.template_type,
|
"template_type": self.template_type,
|
||||||
"service_id": str(self.service_id),
|
"service_id": str(self.service_id),
|
||||||
"created_by": self.created_by.name,
|
"created_by": self.created_by.name,
|
||||||
|
|||||||
@@ -380,9 +380,13 @@ class JobSchema(BaseSchema):
|
|||||||
service_name = fields.Nested(
|
service_name = fields.Nested(
|
||||||
ServiceSchema, attribute="service", dump_to="service_name", only=["name"], dump_only=True)
|
ServiceSchema, attribute="service", dump_to="service_name", only=["name"], dump_only=True)
|
||||||
|
|
||||||
|
template_name = fields.Method('get_template_name', dump_only=True)
|
||||||
template_type = fields.Method('get_template_type', dump_only=True)
|
template_type = fields.Method('get_template_type', dump_only=True)
|
||||||
contact_list_id = field_for(models.Job, 'contact_list_id')
|
contact_list_id = field_for(models.Job, 'contact_list_id')
|
||||||
|
|
||||||
|
def get_template_name(self, job):
|
||||||
|
return job.template.name
|
||||||
|
|
||||||
def get_template_type(self, job):
|
def get_template_type(self, job):
|
||||||
return job.template.template_type
|
return job.template.template_type
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ from app.models import (
|
|||||||
EMAIL_TYPE, SMS_TYPE, LETTER_TYPE,
|
EMAIL_TYPE, SMS_TYPE, LETTER_TYPE,
|
||||||
JOB_STATUS_FINISHED
|
JOB_STATUS_FINISHED
|
||||||
)
|
)
|
||||||
from tests.app.db import create_job, create_service, create_template, create_notification
|
from tests.app.db import create_job, create_service, create_template, create_notification, create_service_contact_list
|
||||||
|
|
||||||
|
|
||||||
def test_should_have_decorated_notifications_dao_functions():
|
def test_should_have_decorated_notifications_dao_functions():
|
||||||
@@ -168,6 +168,26 @@ def test_get_jobs_for_service_in_processed_at_then_created_at_order(notify_db, n
|
|||||||
assert jobs[index].id == created_jobs[index].id
|
assert jobs[index].id == created_jobs[index].id
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_jobs_for_service_by_contact_list(sample_template):
|
||||||
|
contact_list = create_service_contact_list()
|
||||||
|
job_1 = create_job(sample_template)
|
||||||
|
job_2 = create_job(sample_template, contact_list_id=contact_list.id)
|
||||||
|
|
||||||
|
assert dao_get_jobs_by_service_id(
|
||||||
|
sample_template.service.id
|
||||||
|
).items == [
|
||||||
|
job_2,
|
||||||
|
job_1,
|
||||||
|
]
|
||||||
|
|
||||||
|
assert dao_get_jobs_by_service_id(
|
||||||
|
sample_template.service.id,
|
||||||
|
contact_list_id=contact_list.id,
|
||||||
|
).items == [
|
||||||
|
job_2,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def test_update_job(sample_job):
|
def test_update_job(sample_job):
|
||||||
assert sample_job.job_status == 'pending'
|
assert sample_job.job_status == 'pending'
|
||||||
|
|
||||||
|
|||||||
@@ -718,6 +718,7 @@ def test_get_jobs(admin_request, sample_template):
|
|||||||
'service_name': {'name': sample_template.service.name},
|
'service_name': {'name': sample_template.service.name},
|
||||||
'statistics': [],
|
'statistics': [],
|
||||||
'template': str(sample_template.id),
|
'template': str(sample_template.id),
|
||||||
|
'template_name': sample_template.name,
|
||||||
'template_type': 'sms',
|
'template_type': 'sms',
|
||||||
'template_version': 1,
|
'template_version': 1,
|
||||||
'updated_at': None,
|
'updated_at': None,
|
||||||
@@ -740,6 +741,20 @@ def test_get_jobs_with_limit_days(admin_request, sample_template):
|
|||||||
assert len(resp_json['data']) == 2
|
assert len(resp_json['data']) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_jobs_by_contact_list(admin_request, sample_template):
|
||||||
|
contact_list = create_service_contact_list()
|
||||||
|
create_job(template=sample_template)
|
||||||
|
create_job(template=sample_template, contact_list_id=contact_list.id)
|
||||||
|
|
||||||
|
resp_json = admin_request.get(
|
||||||
|
'job.get_jobs_by_service',
|
||||||
|
service_id=sample_template.service_id,
|
||||||
|
contact_list_id=contact_list.id,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert len(resp_json['data']) == 1
|
||||||
|
|
||||||
|
|
||||||
def test_get_jobs_should_return_statistics(admin_request, sample_template):
|
def test_get_jobs_should_return_statistics(admin_request, sample_template):
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
earlier = datetime.utcnow() - timedelta(days=1)
|
earlier = datetime.utcnow() - timedelta(days=1)
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
|
import pytest
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from app.models import ServiceContactList
|
from app.models import ServiceContactList
|
||||||
from tests.app.db import (
|
from tests.app.db import (
|
||||||
create_job,
|
create_job,
|
||||||
create_service_contact_list,
|
create_service_contact_list,
|
||||||
|
create_service_data_retention,
|
||||||
create_service,
|
create_service,
|
||||||
create_template,
|
create_template,
|
||||||
)
|
)
|
||||||
@@ -65,6 +69,48 @@ def test_get_contact_list(admin_request, notify_db_session):
|
|||||||
|
|
||||||
assert len(response) == 1
|
assert len(response) == 1
|
||||||
assert response[0] == contact_list.serialize()
|
assert response[0] == contact_list.serialize()
|
||||||
|
assert response[0]['job_count'] == 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('days_of_email_retention, expected_job_count', (
|
||||||
|
(None, 8),
|
||||||
|
(7, 8),
|
||||||
|
(3, 4),
|
||||||
|
))
|
||||||
|
def test_get_contact_list_counts_jobs(
|
||||||
|
sample_template,
|
||||||
|
admin_request,
|
||||||
|
days_of_email_retention,
|
||||||
|
expected_job_count,
|
||||||
|
):
|
||||||
|
if days_of_email_retention:
|
||||||
|
create_service_data_retention(sample_template.service, 'email', days_of_email_retention)
|
||||||
|
|
||||||
|
# This should be ignored because it’s another template type
|
||||||
|
create_service_data_retention(sample_template.service, 'sms', 1)
|
||||||
|
|
||||||
|
contact_list_1 = create_service_contact_list(service=sample_template.service)
|
||||||
|
contact_list_2 = create_service_contact_list(service=sample_template.service)
|
||||||
|
|
||||||
|
for i in range(10):
|
||||||
|
create_job(
|
||||||
|
template=sample_template,
|
||||||
|
contact_list_id=contact_list_2.id,
|
||||||
|
created_at=datetime.utcnow() - timedelta(days=i)
|
||||||
|
)
|
||||||
|
|
||||||
|
response = admin_request.get(
|
||||||
|
'service.get_contact_list',
|
||||||
|
service_id=contact_list_1.service_id
|
||||||
|
)
|
||||||
|
|
||||||
|
assert len(response) == 2
|
||||||
|
|
||||||
|
assert response[0]['id'] == str(contact_list_2.id)
|
||||||
|
assert response[0]['job_count'] == expected_job_count
|
||||||
|
|
||||||
|
assert response[1]['id'] == str(contact_list_1.id)
|
||||||
|
assert response[1]['job_count'] == 0
|
||||||
|
|
||||||
|
|
||||||
def test_get_contact_list_returns_for_service(admin_request, notify_db_session):
|
def test_get_contact_list_returns_for_service(admin_request, notify_db_session):
|
||||||
|
|||||||
Reference in New Issue
Block a user