Skip to content

Commit 1fe3a6a

Browse files
committed
Merge branch 'feature/nrmi-363-active-urn-list-view' of github.com:Crown-Commercial-Service/DataSubmissionServiceAPI into feature/nrmi-162-inactive-urn-data
2 parents 4694631 + eaed8f4 commit 1fe3a6a

9 files changed

Lines changed: 190 additions & 13 deletions

File tree

app/controllers/admin/urn_lists_controller.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Admin::UrnListsController < AdminController
2-
before_action :find_latest_list, only: %i[index download]
2+
before_action :find_latest_list, only: %i[index]
33

44
def index
55
@urn_lists = UrnList.order(created_at: :desc).page(params[:page])
@@ -21,11 +21,6 @@ def create
2121
redirect_to new_admin_urn_list_path, alert: 'Please choose a file to upload'
2222
end
2323

24-
def download
25-
resp = s3_client.get_object(bucket: bucket, key: @latest_urn_list.file_key)
26-
send_data resp.body.read, filename: @latest_urn_list.file_name.to_s
27-
end
28-
2924
private
3025

3126
def urn_list_params
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'csv'
2+
3+
class Admin::UrnsController < AdminController
4+
def index
5+
@search = params[:search].to_s.strip
6+
7+
@customers = Customer.where(deleted: false).order(:name).search(@search).page(params[:page])
8+
end
9+
10+
def download
11+
send_data urn_csv,
12+
type: 'text/csv',
13+
disposition: 'attachment',
14+
filename: "customer_urns_#{Time.zone.today}.csv"
15+
end
16+
17+
private
18+
19+
def urn_csv
20+
CSV.generate(headers: true) do |csv|
21+
csv << ['URN', 'CustomerName', 'PostCode', 'Sector', 'Published']
22+
23+
Customer.where(deleted: false).order(:name).find_each do |customer|
24+
csv << [customer.urn, customer.name, customer.postcode, customer.sector, customer.published]
25+
end
26+
end
27+
end
28+
end

app/views/admin/urn_lists/index.html.haml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
%ul.govuk-page-actions--actions
1010
%li.govuk-page-actions--action
1111
= link_to 'Add a new URN list', new_admin_urn_list_path
12-
- if @latest_urn_list.present?
13-
%li.govuk-page-actions--action
14-
= link_to 'Download URN list', download_admin_urn_list_path(@latest_urn_list, disposition: "attachment"), {'aria-label' => 'Download latest URN list'}
12+
%li.govuk-page-actions--action
13+
= link_to 'View Active URN list', admin_urns_path
1514
1615
.govuk-grid-row
1716
.govuk-grid-column-full
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.govuk-grid-row
2+
.govuk-grid-column-two-thirds
3+
= link_to 'Back', admin_urn_lists_path, { class: 'govuk-back-link govuk-!-margin-bottom-5', title: 'Back to URN list log' }
4+
5+
%h1.govuk-heading-xl Active URN list
6+
7+
.govuk-grid-row
8+
.govuk-grid-column-two-thirds
9+
%h2.govuk-heading-s
10+
Search
11+
= form_with url: admin_urns_path, method: :get, local: true do
12+
.ccs-search-form-group
13+
%label.govuk-label.govuk-visually-hidden{for: 'search'} Search
14+
%input#search{name: 'search', type: 'text', value: params[:search], class: ['govuk-!-width-two-thirds', 'govuk-input']}
15+
%button.govuk-button Search
16+
17+
.govuk-grid-column-one-third
18+
%nav.govuk-page-actions{"aria-labelledby" => "page-actions-title"}
19+
%h2#page-actions-title.govuk-heading-s{"aria-label" => "Page actions"} Actions
20+
%ul.govuk-page-actions--actions
21+
%li.govuk-page-actions--action
22+
= link_to 'Download Active URN list', download_admin_urns_path
23+
24+
.govuk-grid-row
25+
.govuk-grid-column-full
26+
- if @customers.any?
27+
%table.govuk-table{:class => 'govuk-!-margin-top-7'}
28+
%thead.govuk-table__head
29+
%tr.govuk-table__row
30+
%th.govuk-table__header URN
31+
%th.govuk-table__header Organisation name
32+
%th.govuk-table__header Sector
33+
%th.govuk-table__header Postcode
34+
%th.govuk-table__header Published
35+
%tbody.govuk-table__body
36+
- @customers.each do |customer|
37+
%tr.govuk-table__row
38+
%td.govuk-table__cell= customer.urn
39+
%td.govuk-table__cell= customer.name
40+
%td.govuk-table__cell= customer.sector.titleize
41+
%td.govuk-table__cell= customer.postcode
42+
%td.govuk-table__cell= customer.published? ? 'true' : 'false'
43+
%nav.pagination.ccs-pagination{"aria-label" => "Pagination", :role => "navigation"}
44+
.ccs-pagination__summary= page_entries_info @customers, entry_name: "customer"
45+
= paginate @customers
46+
47+
- else params[:search]
48+
%p
49+
No customers found for ‘#{params[:search]}’.

app/views/kaminari/_next_page.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
-# per_page: number of items to fetch per page
77
-# remote: data-remote
88
%span.next
9-
= link_to_unless current_page.last?, t('Next »').html_safe, url, rel: 'next', remote: remote
9+
= link_to_unless current_page.last?, t('Next »').html_safe, url, rel: 'next', remote: remote, class: 'ccs-pagination__link'

app/views/kaminari/_prev_page.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
-# remote: data-remote
88
%span.prev
99
%a.ccs-pagination__link
10-
= link_to_unless current_page.first?, t('« Previous').html_safe, url, rel: 'prev', remote: remote
10+
= link_to_unless current_page.first?, t('« Previous').html_safe, url, rel: 'prev', remote: remote, class: 'ccs-pagination__link'

config/routes.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,14 @@
175175
end
176176
end
177177

178-
resources :urn_lists, only: %i[index new create] do
179-
member do
178+
resources :urns, only: %i[index] do
179+
collection do
180180
get :download
181181
end
182182
end
183183

184+
resources :urn_lists, only: %i[index new create]
185+
184186
resources :downloads, only: %i[index show new]
185187

186188
resources :unfinished_tasks, only: %i[index]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'rails_helper'
2+
3+
RSpec.feature 'Admin can search for URNs' do
4+
before do
5+
@customer1 = FactoryBot.create(:customer, urn: '123', name: 'Customer One', postcode: 'AB1 2CD',
6+
sector: :central_government)
7+
@customer2 = FactoryBot.create(:customer, urn: '456', name: 'Customer Two', postcode: 'EF3 4GH',
8+
sector: :wider_public_sector)
9+
@customer3 = FactoryBot.create(:customer, urn: '789', name: 'Customer Three', postcode: 'IJ5 6KL',
10+
sector: :wider_public_sector)
11+
sign_in_as_admin
12+
end
13+
14+
scenario 'Viewing all URNs' do
15+
visit admin_urns_path
16+
expect(page).to have_content '123'
17+
expect(page).to have_content '456'
18+
expect(page).to have_content '789'
19+
end
20+
21+
scenario 'Searching by customer name' do
22+
visit admin_urns_path
23+
fill_in 'Search', with: 'One'
24+
click_button 'Search'
25+
expect(page).to have_content '123'
26+
expect(page).to_not have_content '456'
27+
expect(page).to_not have_content '789'
28+
end
29+
30+
scenario 'Searching by URN' do
31+
visit admin_urns_path
32+
fill_in 'Search', with: '456'
33+
click_button 'Search'
34+
expect(page).to_not have_content '123'
35+
expect(page).to have_content '456'
36+
expect(page).to_not have_content '789'
37+
end
38+
39+
scenario 'Searching by postcode' do
40+
visit admin_urns_path
41+
fill_in 'Search', with: 'IJ5 6KL'
42+
click_button 'Search'
43+
expect(page).to_not have_content '123'
44+
expect(page).to_not have_content '456'
45+
expect(page).to have_content '789'
46+
end
47+
end

spec/requests/admin/urns_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'rails_helper'
2+
require 'csv'
3+
4+
RSpec.describe 'Admin URNs', type: :request do
5+
include SingleSignOnHelpers
6+
7+
before do
8+
stub_govuk_bank_holidays_request
9+
mock_sso_with(email: 'admin@example.com')
10+
get '/auth/google_oauth2/callback'
11+
end
12+
13+
describe 'GET /admin/urns' do
14+
it 'renders the URN search page' do
15+
get admin_urns_path
16+
17+
expect(response).to have_http_status(:ok)
18+
expect(response.body).to include('Active URN list')
19+
expect(response.body).to include('Search')
20+
expect(response.body).to include('Download Active URN list')
21+
end
22+
end
23+
24+
describe 'GET /admin/urns/download' do
25+
let!(:active_customer) do
26+
create(:customer, urn: '123', name: 'Active Customer One', postcode: 'AB1 2CD', sector: :central_government)
27+
end
28+
let!(:deleted_customer) do
29+
create(:customer, urn: '456', name: 'Deleted Customer', postcode: 'IJ5 6KL', sector: :wider_public_sector,
30+
deleted: true)
31+
end
32+
33+
it 'returns a CSV file with active customers' do
34+
get download_admin_urns_path
35+
36+
expect(response).to have_http_status(:ok)
37+
expect(response.headers['Content-Type']).to include('text/csv')
38+
expect(response.headers['Content-Disposition']).to include("filename=\"customer_urns_#{Time.zone.today}.csv\"")
39+
40+
csv = CSV.parse(response.body, headers: true)
41+
expect(csv.headers).to eq(['URN', 'CustomerName', 'PostCode', 'Sector', 'Published'])
42+
expect(csv.length).to eq(1)
43+
44+
expect(csv[0]['URN']).to include('123')
45+
expect(csv[0]['CustomerName']).to eq(active_customer.name)
46+
expect(csv[0]['PostCode']).to eq(active_customer.postcode)
47+
expect(csv[0]['Sector']).to eq(active_customer.sector)
48+
49+
# Ensure deleted customer is not included
50+
csv.each do |row|
51+
expect(row['URN']).not_to include('456')
52+
expect(row['CustomerName']).not_to eq(deleted_customer.name)
53+
expect(row['PostCode']).not_to eq(deleted_customer.postcode)
54+
end
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)