Skip to content

Commit

Permalink
Merge pull request #1244 from alphagov/amend-user-related-forms
Browse files Browse the repository at this point in the history
Amend user-related forms
  • Loading branch information
floehopper authored Jan 16, 2024
2 parents 6450dc7 + ac2cc28 commit 21bb8d0
Show file tree
Hide file tree
Showing 32 changed files with 688 additions and 985 deletions.
8 changes: 1 addition & 7 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
//= require select2

$(document).ready(function () {
$('input[name="support_requests_create_or_change_user_request[action]"]').change(function () {
var displayAdditionalFields = ($(this).val() !== 'change_user')
$('#support_requests_create_or_change_user_request_requested_user_attributes_job_input').toggle(displayAdditionalFields)
$('#support_requests_create_or_change_user_request_requested_user_attributes_phone_input').toggle(displayAdditionalFields)
})

$('.dropdown-toggle').dropdown()
$('.select2').select2()
$('.select2').select2({ allowClear: true, placeholder: '' })
})

This file was deleted.

37 changes: 37 additions & 0 deletions app/controllers/change_existing_user_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "gds_zendesk/users"
require "zendesk_api/error"

class ChangeExistingUserRequestsController < RequestsController
protected

def new_request
Support::Requests::ChangeExistingUserRequest.new
end

def zendesk_ticket_class
Zendesk::Ticket::ChangeExistingUserRequestTicket
end

def parse_request_from_params
Support::Requests::ChangeExistingUserRequest.new(create_or_change_user_request_params)
end

def create_or_change_user_request_params
params.require(:support_requests_change_existing_user_request).permit(
:additional_comments,
requester_attributes: %i[email name collaborator_emails],
requested_user_attributes: %i[name email],
).to_h
end

def save_to_zendesk(submitted_request)
super
create_or_update_user_in_zendesk(submitted_request.requested_user) if submitted_request.for_new_user?
end

def create_or_update_user_in_zendesk(requested_user)
GDS_ZENDESK_CLIENT.users.create_or_update_user(requested_user)
rescue ZendeskAPI::Error::ClientError => e
exception_notification_for(e)
end
end
49 changes: 49 additions & 0 deletions app/controllers/create_new_user_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "gds_zendesk/users"
require "zendesk_api/error"

class CreateNewUserRequestsController < RequestsController
include ExploreHelper

protected

def new_request
@organisations_list = support_api.organisations_list.to_a.map { |o| organisation_title(o) }
Support::Requests::CreateNewUserRequest.new
end

def zendesk_ticket_class
Zendesk::Ticket::CreateNewUserRequestTicket
end

def parse_request_from_params
Support::Requests::CreateNewUserRequest.new(create_or_change_user_request_params)
end

def create_or_change_user_request_params
params.require(:support_requests_create_new_user_request).permit(
:additional_comments,
requester_attributes: %i[email name collaborator_emails],
requested_user_attributes: %i[name email organisation],
).to_h
end

def save_to_zendesk(submitted_request)
super
create_or_update_user_in_zendesk(submitted_request.requested_user) if submitted_request.for_new_user?
end

def create_or_update_user_in_zendesk(requested_user)
GDS_ZENDESK_CLIENT.users.create_or_update_user(requested_user)
rescue ZendeskAPI::Error::ClientError => e
exception_notification_for(e)
end

private

def support_api
GdsApi::SupportApi.new(
Plek.find("support-api"),
bearer_token: ENV["SUPPORT_API_BEARER_TOKEN"],
)
end
end
27 changes: 3 additions & 24 deletions app/models/support/gds/requested_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,13 @@ module Support
module GDS
class RequestedUser
include ActiveModel::Model
attr_accessor :name, :email, :job, :phone, :other_training
attr_writer :training

TRAINING_OPTIONS = {
"Writing for GOV.UK" => "writing",
"Using Whitehall Publisher" => "using_publisher",
}.freeze
attr_accessor :name, :email, :organisation

validates :name, :email, presence: true
validates :email, format: { with: /@/ }
validates :other_training, presence: true, if: -> { training.empty? }
validate :validate_training_options

def formatted_training
training.map { |k| TRAINING_OPTIONS.key(k) }.to_sentence
end

def training
Array(@training).reject(&:empty?)
end

private

def validate_training_options
unless (training - TRAINING_OPTIONS.values).empty?
errors.add(:training, "must be one of the provided options")
end
end
def job; end
def phone; end
end
end
end
5 changes: 3 additions & 2 deletions app/models/support/navigation/section_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class SectionGroups
def initialize(current_user = nil)
@current_user = current_user
@groups = [
Support::Navigation::SectionGroup.new("User access", user_access_requests),
Support::Navigation::SectionGroup.new("Content request", content_requests),
Support::Navigation::SectionGroup.new("Technical support", technical_support_requests),
Support::Navigation::SectionGroup.new("User access", user_access_requests),
Support::Navigation::SectionGroup.new("Campaigns", campaign_requests),
Support::Navigation::SectionGroup.new("Feedback for tools in Beta", feedback_requests),
Support::Navigation::SectionGroup.new("Topic taxonomy requests", taxonomy_requests),
Expand Down Expand Up @@ -51,7 +51,8 @@ def technical_support_requests

def user_access_requests
sections_for(
Support::Requests::AccountsPermissionsAndTrainingRequest,
Support::Requests::CreateNewUserRequest,
Support::Requests::ChangeExistingUserRequest,
Support::Requests::RemoveUserRequest,
)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/support/permissions/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(user)
Support::Requests::UnpublishContentRequest,
]
end
can :create, [Support::Requests::AccountsPermissionsAndTrainingRequest, Support::Requests::RemoveUserRequest] if user.has_permission?("user_managers")
can :create, [Support::Requests::CreateNewUserRequest, Requests::ChangeExistingUserRequest, Support::Requests::RemoveUserRequest] if user.has_permission?("user_managers")
can :create, [Support::Requests::FoiRequest, Support::Requests::NamedContact] if user.has_permission?("api_users")
can :request, :global_export_request if user.has_permission?("feedex_exporters")
can :request, :review_feedback if user.has_permission?("feedex_reviewers")
Expand Down

This file was deleted.

50 changes: 50 additions & 0 deletions app/models/support/requests/change_existing_user_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Support
module Requests
class ChangeExistingUserRequest < Request
attr_accessor :requested_user,
:additional_comments

validate do |request|
if request.requested_user && !request.requested_user.valid?
errors.add :base, message: "The details of the user in question are either incomplete or invalid."
end
end
validates :requested_user, presence: true
validates :additional_comments, presence: true

def requested_user_attributes=(attr)
self.requested_user = Support::GDS::RequestedUser.new(attr)
end

def initialize(attrs = {})
self.requested_user = Support::GDS::RequestedUser.new

super
end

def action
"change_user"
end

def for_new_user?
false
end

def formatted_action
"Change an existing user's account"
end

def inside_government_related?
false
end

def self.label
"Change an existing user's account"
end

def self.description
"Request changes to an existing user's account."
end
end
end
end
Loading

0 comments on commit 21bb8d0

Please sign in to comment.