diff --git a/app/mailers/api_action_mailer.rb b/app/mailers/api_action_mailer.rb index 4e13fefac..613add626 100644 --- a/app/mailers/api_action_mailer.rb +++ b/app/mailers/api_action_mailer.rb @@ -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 diff --git a/app/views/api_action_mailer/send_email.html.erb b/app/views/api_action_mailer/send_email.html.erb index 5e8e2dbbf..2849c73b9 100644 --- a/app/views/api_action_mailer/send_email.html.erb +++ b/app/views/api_action_mailer/send_email.html.erb @@ -45,6 +45,31 @@ <% end %> + <% elsif @api_action.include_api_resource_data && !@api_resource && @meta_data %> +

+ Errors: + <%= @meta_data["errors"] %> +

+

+ Api namespace: + <%=@meta_data["namespace"]&.name %> +

+

+ Properties: + <%= @meta_data["properties"] %> +

+
+ <% object_fields(@meta_data["properties"]).each do |prop| %> +
+
+ <%= "#{prop}:" %> +
+
+ <%= @meta_data["properties"][prop] %> +
+
+ <% end %> +
<% end %> diff --git a/test/fixtures/api_actions.yml b/test/fixtures/api_actions.yml index a05ec9b31..7ac66c321 100644 --- a/test/fixtures/api_actions.yml +++ b/test/fixtures/api_actions.yml @@ -81,6 +81,15 @@ error_api_action_one: email: test@restarone.com 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: test@restarone.com + api_namespace: one + error_api_action_two: type: ErrorApiAction action_type: redirect diff --git a/test/mailers/api_action_mailer_test.rb b/test/mailers/api_action_mailer_test.rb new file mode 100644 index 000000000..5420fa7a4 --- /dev/null +++ b/test/mailers/api_action_mailer_test.rb @@ -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 \ No newline at end of file