mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
Allow jobs to be filtered by contact list
Rather than showing all jobs that have been ‘copied’ from a contact list I think it makes more sense to group them under the contact list. This way it’s easier to see what messages have been sent to a given group of contacts over time. Part of this work means the API needs to return only jobs that have been created from a given contact list, when asked.
This commit is contained in:
@@ -65,6 +65,7 @@ def dao_get_jobs_by_service_id(
|
|||||||
page=1,
|
page=1,
|
||||||
page_size=50,
|
page_size=50,
|
||||||
statuses=None,
|
statuses=None,
|
||||||
|
contact_list_id=None,
|
||||||
):
|
):
|
||||||
query_filter = [
|
query_filter = [
|
||||||
Job.service_id == service_id,
|
Job.service_id == service_id,
|
||||||
@@ -77,6 +78,8 @@ def dao_get_jobs_by_service_id(
|
|||||||
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()) \
|
||||||
|
|||||||
@@ -133,6 +133,7 @@ def get_jobs_by_service(service_id):
|
|||||||
limit_days=limit_days,
|
limit_days=limit_days,
|
||||||
statuses=[x.strip() for x in request.args.get('statuses', '').split(',')],
|
statuses=[x.strip() for x in request.args.get('statuses', '').split(',')],
|
||||||
page=int(request.args.get('page', 1)),
|
page=int(request.args.get('page', 1)),
|
||||||
|
contact_list_id=request.args.get('contact_list_id'),
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
@@ -193,13 +194,15 @@ def get_paginated_jobs(
|
|||||||
limit_days,
|
limit_days,
|
||||||
statuses,
|
statuses,
|
||||||
page,
|
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:
|
||||||
|
|||||||
@@ -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'
|
||||||
|
|
||||||
|
|||||||
@@ -740,6 +740,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)
|
||||||
|
|||||||
Reference in New Issue
Block a user