Skip to content

Commit

Permalink
[bug fix] API resource undefined method error (#1378)
Browse files Browse the repository at this point in the history
  • Loading branch information
donrestarone and sthajay authored Jan 21, 2023
1 parent 58bfcca commit fe18115
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/mailers/api_action_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ def send_email(api_action)
mail_to = api_action.email_evaluated
from = "#{Subdomain.current.name}@#{ENV["APP_HOST"]}"
return if mail_to.empty?

@api_action = api_action
@api_resource = api_action.api_resource

if !@api_resource && api_action.meta_data
@meta_data = api_action.meta_data["api_resource"]
@meta_data["namespace"] = ApiNamespace.find_by(id: ApiAction.find(api_action.id).meta_data["api_resource"]["api_namespace_id"])
end

custom_subject = api_action.email_subject_evaluated || if api_action.api_namespace_action then "#{api_action.type} #{api_action.api_namespace_action.api_namespace&.name.pluralize} v#{api_action.api_namespace_action.api_namespace&.version}"
else "#{api_action.type} #{@api_resource.api_namespace.name.pluralize} v#{@api_resource.api_namespace.version}" end

mail(
from: from,
to: mail_to,
subject: api_action.email_subject_evaluated || "#{api_action.type} #{@api_resource.api_namespace.name.pluralize} v#{@api_resource.api_namespace.version}"
subject: custom_subject
)
end
end
Expand Down
25 changes: 25 additions & 0 deletions app/views/api_action_mailer/send_email.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@
</div>
<% end %>
</div>
<% elsif @api_action.include_api_resource_data && !@api_resource && @meta_data %>
<p>
<b>Errors:</b>
<%= @meta_data["errors"] %>
</p>
<p>
<b>Api namespace:</b>
<%=@meta_data["namespace"]&.name %>
</p>
<p>
<b>Properties:</b>
<%= @meta_data["properties"] %>
</p>
<div class="mb-3">
<% object_fields(@meta_data["properties"]).each do |prop| %>
<div class="d-flex mb-3">
<div class="mr-4">
<%= "#{prop}:" %>
</div>
<div>
<%= @meta_data["properties"][prop] %>
</div>
</div>
<% end %>
</div>
<% end %>
</main>
</body>
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/api_actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ error_api_action_one:
email: [email protected]
api_namespace: one

error_api_action_one_with_api_resource_data:
type: ErrorApiAction
action_type: send_email
include_api_resource_data: true
payload_mapping:
redirect_url:
email: [email protected]
api_namespace: one

error_api_action_two:
type: ErrorApiAction
action_type: redirect
Expand Down
29 changes: 29 additions & 0 deletions test/mailers/api_action_mailer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "test_helper"

class ApiActionMailerTest < ActionMailer::TestCase
test 'sends email without api_resource if not included in api action' do
@api_action_1 = api_actions(:error_api_action_one)
@api_action_1.update(email_subject: "")
@api_action_2 = ErrorApiAction.create(@api_action_1.attributes.merge(custom_message: @api_action_1.custom_message.to_s, parent_id: @api_action_1.id, meta_data: { api_resource: { errors: "Error Occured!", properties: {"Null"=>"", "Array"=>"", "Number"=>"", "Object"=>{"a"=>"b", "c"=>"d"}, "String"=>"", "Boolean"=>"false"} }, namespace: { name: "Namespace1" } }).except("id", "created_at", "updated_at", "api_namespace_id"))

email = ApiActionMailer.send_email(@api_action_2)
assert_emails 1 do
email.deliver_later
end
refute_includes email.body, @api_action_2.meta_data["api_resource"]["properties"]
assert_equal "#{@api_action_2.type} #{@api_action_2.api_namespace_action&.api_namespace&.name.pluralize} v#{@api_action_2.api_namespace_action&.api_namespace&.version}", email.subject
end

test 'sends email with api_resource if included in api action' do
@api_action_1 = api_actions(:error_api_action_one_with_api_resource_data)
@api_action_1.update(email_subject: "")
@api_action_2 = ErrorApiAction.create(@api_action_1.attributes.merge(custom_message: @api_action_1.custom_message.to_s, parent_id: @api_action_1.id, meta_data: { api_resource: { errors: "Error Occured!", properties: {"Null"=>"", "Array"=>"", "Number"=>"", "Object"=>{"a"=>"b", "c"=>"d"}, "String"=>"", "Boolean"=>"false"} }, namespace: { name: "Namespace1" } }).except("id", "created_at", "updated_at", "api_namespace_id"))

email = ApiActionMailer.send_email(@api_action_2)
assert_emails 1 do
email.deliver_later
end
assert_includes CGI.unescapeHTML(email.body.to_s), @api_action_2.meta_data["api_resource"]["properties"].to_s
assert_equal "#{@api_action_2.type} #{@api_action_2.api_namespace_action&.api_namespace&.name.pluralize} v#{@api_action_2.api_namespace_action&.api_namespace&.version}", email.subject
end
end

0 comments on commit fe18115

Please sign in to comment.