Skip to content

Commit

Permalink
Merge pull request #996 from alphagov/update-create-support-ticket
Browse files Browse the repository at this point in the history
Update create support ticket endpoint
  • Loading branch information
AgaDufrat authored Aug 8, 2024
2 parents 828e2a3 + 713afb4 commit d91eb58
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
16 changes: 12 additions & 4 deletions app/controllers/support_tickets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class SupportTicketsController < ApplicationController
def create
support_ticket = SupportTicket.new(attributes)
support_ticket = SupportTicket.new(support_ticket_attributes)

if support_ticket.valid?
GDS_ZENDESK_CLIENT.ticket.create!(support_ticket.attributes)
GDS_ZENDESK_CLIENT.ticket.create!(support_ticket.zendesk_ticket_attributes)

render json: { status: "success" }, status: :created
else
Expand All @@ -13,7 +13,15 @@ def create

private

def attributes
params.slice(:subject, :tags, :user_agent, :description)
def support_ticket_attributes
params.slice(
:subject, :description, :priority, :requester, :collaborators, :tags, :custom_fields, :ticket_form_id
).permit(
:subject, :description, :priority, :ticket_form_id,
requester: %i[locale_id email name],
collaborators: [],
tags: [],
custom_fields: %i[id value]
)
end
end
22 changes: 15 additions & 7 deletions app/models/support_ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@ class SupportTicket

def initialize(attributes)
@subject = attributes.fetch(:subject, nil)
@tags = attributes.fetch(:tags, nil)
@description = attributes.fetch(:description, nil)
@priority = attributes.fetch(:priority, nil)
@requester = attributes.fetch(:requester, nil)
@collaborators = attributes.fetch(:collaborators, nil)
@tags = attributes.fetch(:tags, nil)
@custom_fields = attributes.fetch(:custom_fields, nil)
@ticket_form_id = attributes.fetch(:ticket_form_id, nil)
end

def attributes
def zendesk_ticket_attributes
{
"subject" => subject,
"comment" => { "body" => description },
"priority" => priority,
"requester" => requester,
"collaborators" => collaborators,
"tags" => tags,
"comment" => {
"body" => description,
},
}
"custom_fields" => custom_fields,
"ticket_form_id" => ticket_form_id,
}.compact
end

private

attr_reader :subject, :tags, :description
attr_reader :subject, :description, :priority, :requester, :collaborators, :tags, :custom_fields, :ticket_form_id
end
30 changes: 22 additions & 8 deletions spec/models/support_ticket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,47 @@
end
end

describe SupportTicket, "#attributes" do
describe SupportTicket, "#zendesk_ticket_attributes" do
it "generates a hash of attributes to create a Zendesk ticket" do
support_ticket = described_class.new(
subject: "Feedback for app",
tags: %w[app_name],
description: "Ticket details go here.",
priority: "normal",
requester: { "locale_id" => 1, "email" => "[email protected]", "name" => "Some user" },
collaborators: %w[[email protected] [email protected]],
tags: %w[app_name],
custom_fields: [
{ "id" => 7_948_652_819_356, "value" => "cr_inaccuracy" },
{ "id" => 7_949_106_580_380, "value" => "cr_benefits" },
],
ticket_form_id: 123,
)

expect(support_ticket.attributes).to eq(
expect(support_ticket.zendesk_ticket_attributes).to eq(
"subject" => "Feedback for app",
"tags" => %w[app_name],
"comment" => {
"body" => "Ticket details go here.",
},
"priority" => "normal",
"requester" => { "locale_id" => 1, "email" => "[email protected]", "name" => "Some user" },
"collaborators" => %w[[email protected] [email protected]],
"tags" => %w[app_name],
"custom_fields" => [
{ "id" => 7_948_652_819_356, "value" => "cr_inaccuracy" },
{ "id" => 7_949_106_580_380, "value" => "cr_benefits" },
],
"ticket_form_id" => 123,
)
end

it "generates a hash of attributes where the body omits the optional user agent" do
it "generates a hash of attributes and omits the optional attributes if value was not provided" do
support_ticket = described_class.new(
subject: "Feedback for app",
tags: %w[app_name],
description: "Ticket details go here.",
)

expect(support_ticket.attributes).to eq(
expect(support_ticket.zendesk_ticket_attributes).to eq(
"subject" => "Feedback for app",
"tags" => %w[app_name],
"comment" => {
"body" => "Ticket details go here.",
},
Expand Down
11 changes: 10 additions & 1 deletion spec/requests/support_tickets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
subject: "Feedback for app",
tags: %w[app_name],
description: "Ticket details go here.",
priority: "normal",
requester: { locale_id: 1, email: "[email protected]", name: "Some user" },
collaborators: %w[[email protected] [email protected]],
custom_fields: [
{ id: 7_948_652_819_356, value: "cr_inaccuracy" },
{ id: 7_949_106_580_380, value: "cr_benefits" },
],
ticket_form_id: 123,
}

expect(response.code).to eq("201")
Expand All @@ -28,13 +36,14 @@
params: {
subject: "Feedback for app",
tags: %w[app_name],
requester: { locale_id: 1, email: "[email protected]", name: "Some user" },
description: "Ticket details go here.",
}

expect(zendesk_request).to have_been_made
end

it "responds unsuccessfully if the feedback isn't valid" do
it "responds unsuccessfully if the support ticket isn't valid" do
post "/support-tickets",
params: { subject: "Feedback for app" }

Expand Down

0 comments on commit d91eb58

Please sign in to comment.