diff --git a/app/utils.py b/app/utils.py index 98f87f5bc..b38b7665b 100644 --- a/app/utils.py +++ b/app/utils.py @@ -128,20 +128,47 @@ def get_errors_for_csv(recipients, template_type): def generate_notifications_csv(**kwargs): from app import notification_api_client - fieldnames, list_of_keys = _create_key_and_fieldnames_for_csv(kwargs) - if 'page' not in kwargs: kwargs['page'] = 1 + + if kwargs['job_id']: + fieldnames = ['Row number', 'Recipient', 'Template', 'Type', 'Job', 'Status', 'Time'] + else: + fieldnames = ['Recipient', 'Template', 'Type', 'Job', 'Status', 'Time'] + yield ','.join(fieldnames) + '\n' while kwargs['page']: + # if job_id then response looks different notifications_resp = notification_api_client.get_notifications_for_service(**kwargs) notifications = notifications_resp['notifications'] - - for notification in notifications: - values = [notification[x] for x in list_of_keys] - line = ','.join(str(i) for i in values) + '\n' - yield line + if kwargs['job_id']: + for notification in notifications: + values = [ + notification['row_number'], + notification['recipient'], + notification['template_name'], + notification['template_type'], + notification['job_name'], + notification['status'], + notification['created_at'] + ] + line = ','.join(str(i) for i in values) + '\n' + yield line + else: + # Change here + for notification in notifications: + values = [ + notification['to'], + notification['template']['name'], + notification['template']['template_type'], + notification.get('job_name', None), + notification['status'], + notification['created_at'], + notification['updated_at'] + ] + line = ','.join(str(i) for i in values) + '\n' + yield line if notifications_resp['links'].get('next'): kwargs['page'] += 1 else: @@ -149,33 +176,6 @@ def generate_notifications_csv(**kwargs): raise Exception("Should never reach here") -def _create_key_and_fieldnames_for_csv(kwargs): - # if job_id then response looks different - if kwargs['job_id']: - list_of_keys = [ - 'row_number', - 'recipient', - 'template_name', - 'template_type', - 'job_name', - 'status', - 'created_at' - ] - fieldnames = ['Row number', 'Recipient', 'Template', 'Type', 'Job', 'Status', 'Time'] - else: - list_of_keys = [ - 'recipient', - 'template_name', - 'template_type', - 'job_name', - 'status', - 'created_at', - 'updated_at' - ] - fieldnames = ['Recipient', 'Template', 'Type', 'Job', 'Status', 'Time', 'Completed at'] - return fieldnames, list_of_keys - - def get_page_from_request(): if 'page' in request.args: try: diff --git a/tests/app/test_utils.py b/tests/app/test_utils.py index bd3fdaf8d..6b9f3207b 100644 --- a/tests/app/test_utils.py +++ b/tests/app/test_utils.py @@ -43,12 +43,15 @@ def _get_notifications_csv( data = { 'notifications': [{ "row_number": row_number, + "to": recipient, "recipient": recipient, "template_name": template_name, "template_type": template_type, + "template": {"name": template_name, "template_type": template_type}, "job_name": job_name, "status": status, - "created_at": created_at + "created_at": created_at, + "updated_at": None } for i in range(rows)], 'total': rows, 'page_size': 50, @@ -136,7 +139,8 @@ def test_generate_notifications_csv_only_calls_once_if_no_next_link(_get_notific assert _get_notifications_csv_mock.call_count == 1 -def test_generate_notifications_csv_calls_twice_if_next_link(mocker): +@pytest.mark.parametrize("job_id", ["some", None]) +def test_generate_notifications_csv_calls_twice_if_next_link(mocker, job_id): service_id = '1234' response_with_links = _get_notifications_csv(service_id, rows=7, with_links=True) response_with_no_links = _get_notifications_csv(service_id, rows=3, with_links=False) @@ -149,7 +153,7 @@ def test_generate_notifications_csv_calls_twice_if_next_link(mocker): ] ) - csv_content = generate_notifications_csv(service_id=service_id, job_id=fake_uuid) + csv_content = generate_notifications_csv(service_id=service_id, job_id=fake_uuid if job_id else None) csv = DictReader(StringIO('\n'.join(csv_content))) assert len(list(csv)) == 10