mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-19 05:58:53 -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()
|
||||
|
||||
|
||||
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 = [
|
||||
Job.service_id == service_id,
|
||||
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(
|
||||
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 \
|
||||
.filter(*query_filter) \
|
||||
.order_by(Job.processing_started.desc(), Job.created_at.desc()) \
|
||||
|
||||
@@ -128,10 +128,13 @@ def get_jobs_by_service(service_id):
|
||||
else:
|
||||
limit_days = None
|
||||
|
||||
statuses = [x.strip() for x in request.args.get('statuses', '').split(',')]
|
||||
|
||||
page = int(request.args.get('page', 1))
|
||||
return jsonify(**get_paginated_jobs(service_id, limit_days, statuses, page))
|
||||
return jsonify(**get_paginated_jobs(
|
||||
service_id,
|
||||
limit_days=limit_days,
|
||||
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'])
|
||||
@@ -185,13 +188,21 @@ def create_job(service_id):
|
||||
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(
|
||||
service_id,
|
||||
limit_days=limit_days,
|
||||
page=page,
|
||||
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
|
||||
for job_data in data:
|
||||
|
||||
@@ -12,7 +12,7 @@ from sqlalchemy.dialects.postgresql import (
|
||||
JSONB,
|
||||
)
|
||||
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.recipients import (
|
||||
validate_email_address,
|
||||
@@ -2138,12 +2138,27 @@ class ServiceContactList(db.Model):
|
||||
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
|
||||
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):
|
||||
created_at_in_bst = convert_utc_to_bst(self.created_at)
|
||||
contact_list = {
|
||||
"id": str(self.id),
|
||||
"original_file_name": self.original_file_name,
|
||||
"row_count": self.row_count,
|
||||
"job_count": self.get_job_count(),
|
||||
"template_type": self.template_type,
|
||||
"service_id": str(self.service_id),
|
||||
"created_by": self.created_by.name,
|
||||
|
||||
@@ -380,9 +380,13 @@ class JobSchema(BaseSchema):
|
||||
service_name = fields.Nested(
|
||||
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)
|
||||
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):
|
||||
return job.template.template_type
|
||||
|
||||
|
||||
Reference in New Issue
Block a user