Skip to content

Commit

Permalink
Feature: Mobile - Handle to/from fields in ticket article creation se…
Browse files Browse the repository at this point in the history
…rvice.
  • Loading branch information
fliebe92 authored and dominikklein committed Nov 24, 2022
1 parent 806111f commit 65214e6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
52 changes: 50 additions & 2 deletions app/services/service/ticket/article/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ def execute(article_data:)
ticket_id = article_data.delete(:ticket_id)
form_id = article_data.delete(:form_id)

if Ticket.find(ticket_id).nil?
raise ActiveRecord::RecordNotFound, "Ticket #{ticket_id} for new article could not be found."
end

Ticket::Article.new(article_data).tap do |article|
article.ticket_id = ticket_id
article.attachments = attachments(article, form_id)
transform_article(article, ticket_id, form_id)

article.save!

Expand All @@ -23,6 +26,51 @@ def execute(article_data:)

private

def transform_article(article, ticket_id, form_id)
article.ticket_id = ticket_id
article.attachments = attachments(article, form_id)

transform_to_from(article)

article
end

def transform_to_from(article)
ticket = Ticket.find(article.ticket_id)

customer_display_name = display_name(ticket.customer)
group_name = ticket.group.name

if article.sender.name.eql?('Customer')
article.from = customer_display_name
article.to = group_name
else
article.to = customer_display_name
article.from = group_name
end
end

def display_name(user)
if user.fullname.present? && user.email.present?
return Mail::Address.new.tap do |addr|
addr.display_name = user.fullname
addr.address = user.email
end.format
end

return user.fullname if user.fullname.present?

display_name_fallback(user)
end

def display_name_fallback(user)
return user.email if user.email.present?
return user.phone if user.phone.present?
return user.login if user.login.present?

'???'
end

def attachments(article, form_id)
attachments_inline = []
if article.body && article.content_type&.match?(%r{text/html}i)
Expand Down
12 changes: 12 additions & 0 deletions spec/graphql/gql/mutations/ticket/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ def it_fails_to_create_ticket
it_creates_ticket(articles: 1)
expect(Ticket.last.articles.last.sender.name).to eq(Ticket::Article::Sender.first.name)
end

it 'sets correct "to" and "from" values', :aggregate_failures do
it_creates_ticket(articles: 1)
expect(Ticket.last.articles.last.from).to eq(agent.fullname)
expect(Ticket.last.articles.last.to).to eq("#{customer.fullname} <#{customer.email}>")
end
end

context 'with no type' do
Expand Down Expand Up @@ -323,6 +329,12 @@ def it_fails_to_create_ticket
it_creates_ticket(articles: 1)
expect(Ticket.last.articles.last.type.name).to eq('note')
end

it 'sets correct "to" and "from" values', :aggregate_failures do
it_creates_ticket(articles: 1)
expect(Ticket.last.articles.last.to).to eq(Ticket.last.group.name)
expect(Ticket.last.articles.last.from).to eq(customer.fullname)
end
end

context 'with an article flagged as internal' do
Expand Down

0 comments on commit 65214e6

Please sign in to comment.