Skip to content

Commit

Permalink
Merge pull request #1170 from alphagov/zendesk-custom-fields
Browse files Browse the repository at this point in the history
Add functionality to set Zendesk custom fields and form template
  • Loading branch information
AgaDufrat authored Oct 13, 2023
2 parents ce1a15a + fdf8968 commit 62a4dbd
Show file tree
Hide file tree
Showing 22 changed files with 503 additions and 81 deletions.
16 changes: 16 additions & 0 deletions app/models/support/requests/content_change_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ def self.label
def self.description
"Request changes to GOV.UK content managed by GDS content designers"
end

def self.reason_for_change_options
Zendesk::CustomField.options_for_name("[CR] Reason for the request")
end

def reason_for_change_options
self.class.reason_for_change_options
end

def self.subject_area_options
Zendesk::CustomField.options_for_name("[CR] Subject Area")
end

def subject_area_options
self.class.subject_area_options
end
end
end
end
48 changes: 48 additions & 0 deletions app/models/zendesk/custom_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Zendesk
class CustomField
CUSTOM_FIELDS_DATA = YAML.safe_load_file(
"config/zendesk/custom_fields_data.yml",
permitted_classes: [
Zendesk::CustomFieldType::DateField,
Zendesk::CustomFieldType::DropDown,
Zendesk::CustomFieldType::Text,
],
).freeze

class << self
delegate :set, :options_for_name, to: :new
end

def set(id:, input:)
{ "id" => id, "value" => find_by_id(id).prepare_value(input) }
end

def options_for_name(name)
find_by_name(name).options.values
end

private

def find_by_id(id)
field = CUSTOM_FIELDS_DATA.select { |f| f.id == id }.first

if field.nil?
raise "Unable to find custom field ID: #{id}. " \
"Ensure it's defined in config/zendesk/custom_fields_data.yml"
end

field
end

def find_by_name(name)
field = CUSTOM_FIELDS_DATA.select { |f| f.name == name }.first

if field.nil?
raise "Unable to find custom field name: #{name}. " \
"Ensure it's defined in config/zendesk/custom_fields_data.yml"
end

field
end
end
end
12 changes: 12 additions & 0 deletions app/models/zendesk/custom_field_type/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Zendesk
module CustomFieldType
class Base
attr_reader :id, :name

def initialize(args)
@id = args[:id]
@name = args[:name]
end
end
end
end
11 changes: 11 additions & 0 deletions app/models/zendesk/custom_field_type/date_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Zendesk
module CustomFieldType
class DateField < Base
def prepare_value(input)
date = Date.parse(input)

date.strftime("%F")
end
end
end
end
30 changes: 30 additions & 0 deletions app/models/zendesk/custom_field_type/drop_down.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Zendesk
module CustomFieldType
class DropDown < Base
attr_reader :id, :name, :options

def initialize(args)
@options = args[:options]

super
end

def prepare_value(input)
name_tag = find_name_tag(input)

if name_tag.nil?
raise "Unable to find name tag for '#{input}' to populate custom fields. " \
"Ensure it's defined in config/zendesk/custom_fields_data.yml"
end

name_tag
end

private

def find_name_tag(input)
options&.key(input)
end
end
end
end
9 changes: 9 additions & 0 deletions app/models/zendesk/custom_field_type/text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Zendesk
module CustomFieldType
class Text < Base
def prepare_value(input)
input.to_s
end
end
end
end
10 changes: 9 additions & 1 deletion app/models/zendesk/ticket/content_advice_request_ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def tags
protected

def comment_snippets
[
optional_comment_snippets + [
request_label(field: :details),
request_label(
field: :urls,
Expand All @@ -28,6 +28,14 @@ def comment_snippets
]
end

def optional_comment_snippets
snippets = []
snippets << Zendesk::LabelledSnippet.new(on: self, field: :needed_by_date) if needed_by_date
snippets << Zendesk::LabelledSnippet.new(on: self, field: :time_constraint_reason, label: "Reason for time constraint") if time_constraint_reason

snippets
end

def deadline_date
if needed_by_date.present?
Date.parse(needed_by_date).strftime("%-d %b")
Expand Down
36 changes: 25 additions & 11 deletions app/models/zendesk/ticket/content_change_request_ticket.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Zendesk
module Ticket
class ContentChangeRequestTicket < Zendesk::ZendeskTicket
TICKET_FORM_ID = 7_949_329_694_108

def subject
if @request.title.present?
"#{@request.title} - Content change request"
Expand All @@ -13,20 +15,23 @@ def tags
super + %w[content_amend]
end

def custom_fields
fields = [
CustomField.set(id: 7_948_652_819_356, input: @request.reason_for_change),
CustomField.set(id: 7_949_106_580_380, input: @request.subject_area),
]
fields << CustomField.set(id: 7_949_136_091_548, input: needed_by_date) if needed_by_date
fields << CustomField.set(id: 7_949_152_975_772, input: not_before_date) if not_before_date
fields << CustomField.set(id: 8_250_061_570_844, input: needed_by_time) if needed_by_time
fields << CustomField.set(id: 8_250_075_489_052, input: not_before_time) if not_before_time

fields
end

protected

def comment_snippets
[
Zendesk::LabelledSnippet.new(
on: @request,
field: :reason_for_change,
label: "Reason for change request",
),
Zendesk::LabelledSnippet.new(
on: @request,
field: :subject_area,
label: "Subject area",
),
snippets = [
Zendesk::LabelledSnippet.new(
on: @request,
field: :url,
Expand All @@ -38,6 +43,15 @@ def comment_snippets
label: "Details of what should be added, amended or removed",
),
]
if time_constraint_reason
snippets << Zendesk::LabelledSnippet.new(
on: self,
field: :time_constraint_reason,
label: "Reason for time constraint",
)
end

snippets
end
end
end
Expand Down
1 change: 1 addition & 0 deletions app/models/zendesk/ticket/remove_user_request_ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def tags

def comment_snippets
[
Zendesk::LabelledSnippet.new(on: self, field: :not_before_date),
Zendesk::LabelledSnippet.new(on: @request, field: :user_name),
Zendesk::LabelledSnippet.new(on: @request, field: :user_email),
Zendesk::LabelledSnippet.new(on: @request, field: :reason_for_removal),
Expand Down
24 changes: 8 additions & 16 deletions app/models/zendesk/zendesk_ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(request)
def_delegators :@requester, :email, :name, :collaborator_emails

def comment
Zendesk::SnippetCollection.new(base_comment_snippets + comment_snippets).to_s
Zendesk::SnippetCollection.new(comment_snippets).to_s
end

def not_before_date
Expand All @@ -41,6 +41,12 @@ def needed_by_time
end
end

def time_constraint_reason
if value?(:time_constraint) && value?(:time_constraint_reason, @request.time_constraint)
@request.time_constraint.time_constraint_reason
end
end

def tags
%w[govt_form]
end
Expand All @@ -50,27 +56,13 @@ def inside_government_tag_if_needed
end

def to_s
Zendesk::SnippetCollection.new(base_attribute_snippets + base_comment_snippets + comment_snippets).to_s
Zendesk::SnippetCollection.new(base_attribute_snippets + comment_snippets).to_s
end

def priority
"normal"
end

def base_comment_snippets
if value?(:time_constraint)
[
Zendesk::LabelledSnippet.new(on: self, field: :needed_by_date, label: "Needed by date"),
Zendesk::LabelledSnippet.new(on: self, field: :needed_by_time, label: "Needed by time"),
Zendesk::LabelledSnippet.new(on: self, field: :not_before_date, label: "Not before date"),
Zendesk::LabelledSnippet.new(on: self, field: :not_before_time, label: "Not before time"),
Zendesk::LabelledSnippet.new(on: @request.time_constraint, field: :time_constraint_reason, label: "Reason for time constraint"),
]
else
[]
end
end

def comment_snippets
[]
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/zendesk/zendesk_tickets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def raise_ticket(ticket_to_raise)
tags: ticket_to_raise.tags,
comment: { "body" => ticket_to_raise.comment },
}
ticket_options.merge!(custom_fields: ticket_to_raise.custom_fields) if ticket_to_raise.respond_to?(:custom_fields)
ticket_options.merge!(ticket_form_id: ticket_to_raise.class::TICKET_FORM_ID) if ticket_to_raise.class.const_defined?(:TICKET_FORM_ID)

ZendeskTicketWorker.perform_async(ticket_options.stringify_keys)
end
Expand Down
32 changes: 2 additions & 30 deletions app/views/content_change_requests/_request_details.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,7 @@
</p>
<span class="form-wrapper">
<%= f.select :reason_for_change,
options_for_select([
"Factual inaccuracy",
"Content is causing problems for users",
"New information (policy, campaign, service) - for example, service start page needed",
"Missing information",
"Update - policy or process change",
"Uprating - policy or money changes at the new tax year (6 April)",
"Service Review",
"Other"
], f.object.reason_for_change),
options_for_select(f.object.reason_for_change_options, f.object.reason_for_change),
{ include_blank: true },
class: "input-md-6 form-control",
aria: {
Expand All @@ -64,26 +55,7 @@
</p>
<span class="form-wrapper">
<%= f.select :subject_area,
options_for_select([
"Benefits",
"Births, death, marriages and care",
"Business and self-employed",
"Childcare and parenting",
"Citizenship and living in the UK",
"Crime, justice and the law",
"Disabled people",
"Driving and transport",
"Education and learning",
"Employing people",
"Environment and countryside",
"Health",
"Housing and local services",
"Money and tax",
"Passports, travel and living abroad",
"Visas and immigration",
"Working, jobs and pensions",
"Other"
], f.object.subject_area),
options_for_select(f.object.subject_area_options, f.object.subject_area),
{ include_blank: true },
class: "input-md-6 form-control",
aria: {
Expand Down
47 changes: 47 additions & 0 deletions config/zendesk/custom_fields_data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
- !ruby/object:Zendesk::CustomFieldType::DropDown
name: "[CR] Reason for the request"
id: 7948652819356
options:
cr_inaccuracy: Factual inaccuracy
cr_user_problem: Content is causing problems for users
cr_new_info: New information (policy, campaign, service) - for example, service start page needed
cr_missing_info: Missing information
cr_policy_process_update: Update - policy or process change
cr_uprating: Uprating - policy or money changes at the new tax year (6 April)
cr_service_review: Service Review
cr_reason_other: Other
- !ruby/object:Zendesk::CustomFieldType::DropDown
name: "[CR] Subject Area"
id: 7949106580380
options:
cr_benefits: Benefits
cr_births_deaths: Births, death, marriages and care
cr_business_selfemployed: Business and self-employed
cr_childcare_parenting: Childcare and parenting
cr_citizenship: Citizenship and living in the UK
cr_crime_justice: Crime, justice and the law
cr_disabled_people: Disabled people
cr_driving_transport: Driving and transport
cr_education_learning: Education and learning
cr_employing_people: Employing people
cr_environment_countryside: Environment and countryside
cr_health: Health
cr_housing_local_services: Housing and local services
cr_money_tax: Money and tax
cr_passports_travel_living_abroad: Passports, travel and living abroad
cr_visas_immigration: Visas and immigration
cr_working_jobs_pensions: Working, jobs and pensions
cr_subject_other: Other
- !ruby/object:Zendesk::CustomFieldType::DateField
name: "[CR] Deadline"
id: 7949136091548
- !ruby/object:Zendesk::CustomFieldType::DateField
name: "[CR] Do not publish before"
id: 7949152975772
- !ruby/object:Zendesk::CustomFieldType::Text
name: "[CR] Deadline (time)"
id: 8250061570844
- !ruby/object:Zendesk::CustomFieldType::Text
name: "[CR] Do not publish before (time)"
id: 8250075489052
Loading

0 comments on commit 62a4dbd

Please sign in to comment.