Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions app/controllers/admin/urns_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
require 'csv'

class Admin::UrnsController < AdminController
# rubocop:disable Metrics/AbcSize
def index
@search = params[:search].to_s.strip
@active_search = params[:active_search].to_s.strip
@inactive_search = params[:inactive_search].to_s.strip

@customers = Customer.where(deleted: false).order(:name).search(@search).page(params[:page])
@customers = Customer
.where(deleted: false)
.search(@active_search)
.order(:name)
.page(params[:active_page])
@inactive_customers = InactiveCustomer
.search(@inactive_search)
.order(date_made_inactive: :desc)
.page(params[:inactive_page])

respond_to do |format|
format.html
format.js
end
end
# rubocop:enable Metrics/AbcSize

def download
send_data urn_csv,
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,24 @@ def user_auth_logs

render jsonapi: objects, class: { OpenStruct: SerializableUserAuthLog }, status: :ok
end

def deactivate
user = User.find_by!(auth_id: current_auth_id)

unless user.can_deactivate?
# rubocop:disable Layout/LineLength
return render jsonapi_errors: { user: ['Cannot be deactivated because they are the only user associated with their suppliers'] },
status: :unprocessable_entity
# rubocop:enable Layout/LineLength
end

result = DeactivateUser.new(user: user).call

if result.success?
render jsonapi: user, status: :ok
else
render jsonapi_errors: { user: ['Could not be deactivated'] },
status: :unprocessable_entity
end
end
end
15 changes: 15 additions & 0 deletions app/models/inactive_customer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
class InactiveCustomer < ApplicationRecord
validates :inactive_urn, presence: true, uniqueness: true

def self.search(query)
if query.blank?
all
else
where(
'cast(inactive_urn as text) ILIKE :query
OR inactive_customer_name ILIKE :query
OR cast(replacement_urn as text) ILIKE :query
OR replacement_customer_name ILIKE :query
OR replacement_post_code ILIKE :query',
query: "%#{query}%"
)
end
end
end
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ def multiple_suppliers?
def active?
!auth_id.nil?
end

def can_deactivate?
suppliers.all? do |supplier|
supplier.active_users.where.not(id: id).exists?
end
end
end
2 changes: 1 addition & 1 deletion app/serializable/serializable_user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class SerializableUser < JSONAPI::Serializable::Resource
type 'users'
attributes :multiple_suppliers?, :name, :email, :created_at
attributes :multiple_suppliers?, :can_deactivate?, :name, :email, :created_at
end
19 changes: 18 additions & 1 deletion app/services/deactivate_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,33 @@ def call
result = Result.new(true)

User.transaction do
lock_linked_suppliers!

unless user.can_deactivate?
result.success = false
raise ActiveRecord::Rollback
end

begin
DeleteUserInAuth0.new(user: user).call
rescue Auth0::Exception
result.success = false
Rails.logger.error("Error adding user #{user.email} to Auth0 during DeactivateUser")
raise ActiveRecord::Rollback
end
user.update(auth_id: nil)

unless user.update(auth_id: nil)
result.success = false
raise ActiveRecord::Rollback
end
end

result
end

private

def lock_linked_suppliers!
user.suppliers.lock.load
end
end
2 changes: 1 addition & 1 deletion app/views/admin/urn_lists/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
%li.govuk-page-actions--action
= link_to 'Add a new Active URN list', new_admin_urn_list_path
%li.govuk-page-actions--action
= link_to 'View Active URN list', admin_urns_path
= link_to 'View URN lists', admin_urns_path

.govuk-grid-row
.govuk-grid-column-full
Expand Down
49 changes: 49 additions & 0 deletions app/views/admin/urns/_active_customers.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.govuk-grid-row
.govuk-grid-column-two-thirds
%h3.govuk-heading-s Search

= form_with url: admin_urns_path, method: :get, local: false do
.ccs-search-form-group
%label.govuk-label.govuk-visually-hidden{for: 'active-search'}
Search active URNs
%input#active-search{
name: 'active_search',
type: 'text',
value: @active_search,
class: ['govuk-!-width-two-thirds', 'govuk-input']
}
%button#active-search-button.govuk-button Search

.govuk-grid-column-one-third
%nav.govuk-page-actions{"aria-labelledby" => "page-actions-title"}
%h2#page-actions-title.govuk-heading-s{"aria-label" => "Page actions"} Actions
%ul.govuk-page-actions--actions
%li.govuk-page-actions--action
= link_to 'Download Active URN list', download_admin_urns_path

.govuk-grid-row
.govuk-grid-column-full
- if @customers.any?
%table.govuk-table{:class => 'govuk-!-margin-top-7'}
%thead.govuk-table__head
%tr.govuk-table__row
%th.govuk-table__header URN
%th.govuk-table__header Organisation name
%th.govuk-table__header Sector
%th.govuk-table__header Postcode
%th.govuk-table__header Published
%tbody.govuk-table__body
- @customers.each do |customer|
%tr.govuk-table__row
%td.govuk-table__cell= customer.urn
%td.govuk-table__cell= customer.name
%td.govuk-table__cell= customer.sector.titleize
%td.govuk-table__cell= customer.postcode
%td.govuk-table__cell= customer.published? ? 'true' : 'false'
%nav.pagination.ccs-pagination{"aria-label" => "Active URN pagination", :role => "navigation"}
#active-customers-pagination-summary.ccs-pagination__summary= page_entries_info @customers, entry_name: "customer"
#active-customers-pagination= paginate @customers, param_name: :active_page, params: {active_search: @active_search}, remote: true

- elsif @active_search.present?
%p
No customers found for ‘#{@active_search}’.
50 changes: 50 additions & 0 deletions app/views/admin/urns/_inactive_customers.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.govuk-grid-row
.govuk-grid-column-two-thirds
%h3.govuk-heading-s Search

= form_with url: admin_urns_path, method: :get, local: false do
.ccs-search-form-group
%label.govuk-label.govuk-visually-hidden{for: 'inactive-search'}
Search inactive URNs
%input#inactive-search{
name: 'inactive_search',
type: 'text',
value: @inactive_search,
class: ['govuk-!-width-two-thirds', 'govuk-input']
}
%button#inactive-search-button.govuk-button Search

.govuk-grid-row
.govuk-grid-column-full
- if @inactive_customers.any?
%table.govuk-table{:class => 'govuk-!-margin-top-7'}
%thead.govuk-table__head
%tr.govuk-table__row
%th.govuk-table__header Inactive Customer
%th.govuk-table__header Inactive Date
%th.govuk-table__header Replacement Customer
%th.govuk-table__header Post Code
%th.govuk-table__header Replacement Status
%tbody.govuk-table__body
- @inactive_customers.each do |inactive_customer|
%tr.govuk-table__row
%td.govuk-table__cell
= inactive_customer.inactive_customer_name
%br/
%small
= inactive_customer.inactive_urn
%td.govuk-table__cell= inactive_customer.date_made_inactive
%td.govuk-table__cell
= inactive_customer.replacement_customer_name
%br/
%small
= inactive_customer.replacement_urn unless inactive_customer.replacement_urn.zero?
%td.govuk-table__cell= inactive_customer.replacement_post_code
%td.govuk-table__cell= inactive_customer.replacement_status
%nav.pagination.ccs-pagination{"aria-label" => "Inactive URN pagination", :role => "navigation"}
#inactive-customers-pagination-summary.ccs-pagination__summary= page_entries_info @inactive_customers, entry_name: "inactive customer"
#inactive-customers-pagination= paginate @inactive_customers, param_name: :inactive_page, params: {active_search: @inactive_search}, remote: true

- elsif @inactive_search.present?
%p
No inactive customers found for ‘#{@inactive_search}’.
60 changes: 19 additions & 41 deletions app/views/admin/urns/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,26 @@
.govuk-grid-column-two-thirds
= link_to 'Back', admin_urn_lists_path, { class: 'govuk-back-link govuk-!-margin-bottom-5', title: 'Back to URN list log' }

%h1.govuk-heading-xl Active URN list
%h1.govuk-heading-xl URN lists

.govuk-grid-row
.govuk-grid-column-two-thirds
%h2.govuk-heading-s
Search
= form_with url: admin_urns_path, method: :get, local: true do
.ccs-search-form-group
%label.govuk-label.govuk-visually-hidden{for: 'search'} Search
%input#search{name: 'search', type: 'text', value: params[:search], class: ['govuk-!-width-two-thirds', 'govuk-input']}
%button.govuk-button Search
.govuk-tabs{data: {module: 'govuk-tabs'}}
%h2.govuk-tabs__title
Contents

.govuk-grid-column-one-third
%nav.govuk-page-actions{"aria-labelledby" => "page-actions-title"}
%h2#page-actions-title.govuk-heading-s{"aria-label" => "Page actions"} Actions
%ul.govuk-page-actions--actions
%li.govuk-page-actions--action
= link_to 'Download Active URN list', download_admin_urns_path
%ul.govuk-tabs__list
%li.govuk-tabs__list-item.govuk-tabs__list-item--selected
%a.govuk-tabs__tab{href: '#active-urns'}
Active URNs

.govuk-grid-row
.govuk-grid-column-full
- if @customers.any?
%table.govuk-table{:class => 'govuk-!-margin-top-7'}
%thead.govuk-table__head
%tr.govuk-table__row
%th.govuk-table__header URN
%th.govuk-table__header Organisation name
%th.govuk-table__header Sector
%th.govuk-table__header Postcode
%th.govuk-table__header Published
%tbody.govuk-table__body
- @customers.each do |customer|
%tr.govuk-table__row
%td.govuk-table__cell= customer.urn
%td.govuk-table__cell= customer.name
%td.govuk-table__cell= customer.sector.titleize
%td.govuk-table__cell= customer.postcode
%td.govuk-table__cell= customer.published? ? 'true' : 'false'
%nav.pagination.ccs-pagination{"aria-label" => "Pagination", :role => "navigation"}
.ccs-pagination__summary= page_entries_info @customers, entry_name: "customer"
= paginate @customers
%li.govuk-tabs__list-item
%a.govuk-tabs__tab{href: '#inactive-urns'}
Inactive URNs

#active-urns.govuk-tabs__panel
.results{id: 'active-customers-table'}= render 'active_customers', customers: @customers


#inactive-urns.govuk-tabs__panel.govuk-tabs__panel
.results{id: 'inactive-customers-table'}= render 'inactive_customers', inactive_customers: @inactive_customers

- else params[:search]
%p
No customers found for ‘#{params[:search]}’.

8 changes: 8 additions & 0 deletions app/views/admin/urns/index.js.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-if params[:active_page] || params.key?(:active_search)
$('#active-customers-table').html("#{j (render partial: 'active_customers', locals: {customers: @customers})}")
$('#active-customers-pagination').html("#{j (paginate(@customers, :param_name => "active_page", :remote => true).to_s)}");
$('#active-customers-pagination-summary').html("#{j (page_entries_info(@customers, entry_name: "customer").to_s)}");
-if params[:inactive_page] || params.key?(:inactive_search)
$('#inactive-customers-table').html("#{j (render partial: 'inactive_customers', locals: {inactive_customers: @inactive_customers})}")
$('#inactive-customers-pagination').html("#{j (paginate(@inactive_customers, :param_name => "inactive_page", :remote => true).to_s)}");
$('#inactive-customers-pagination-summary').html("#{j (page_entries_info(@inactive_customers, entry_name: "inactive customer").to_s)}");
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
collection do
patch :update_name
patch :update_email
patch :deactivate
get :user_auth_logs
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/features/admin_can_search_urns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@

scenario 'Searching by customer name' do
visit admin_urns_path
fill_in 'Search', with: 'One'
click_button 'Search'
fill_in 'active-search', with: 'One'
click_button 'active-search-button'
expect(page).to have_content '123'
expect(page).to_not have_content '456'
expect(page).to_not have_content '789'
end

scenario 'Searching by URN' do
visit admin_urns_path
fill_in 'Search', with: '456'
click_button 'Search'
fill_in 'active-search', with: '456'
click_button 'active-search-button'
expect(page).to_not have_content '123'
expect(page).to have_content '456'
expect(page).to_not have_content '789'
end

scenario 'Searching by postcode' do
visit admin_urns_path
fill_in 'Search', with: 'IJ5 6KL'
click_button 'Search'
fill_in 'active-search', with: 'IJ5 6KL'
click_button 'active-search-button'
expect(page).to_not have_content '123'
expect(page).to_not have_content '456'
expect(page).to have_content '789'
Expand Down
Loading
Loading