Skip to content

Instantly share code, notes, and snippets.

@alexanmtz
Last active August 25, 2022 19:44
Show Gist options
  • Save alexanmtz/b909dcc332697c74f8822bb03228cef5 to your computer and use it in GitHub Desktop.
Save alexanmtz/b909dcc332697c74f8822bb03228cef5 to your computer and use it in GitHub Desktop.
Erro ao chamar send_email: *** ActiveRecord::RecordInvalid Exception: A validação falhou: User é obrigatório(a)
class Payment::PostProcessing::CreditCard
def initialize(transaction)
@transaction = transaction
end
def after_payment(order)
@order = order
register_amount_paid
@order.pay!
save_credit_card
send_email
end
private
def register_amount_paid
@order.update_column('paid_centavos', @transaction.paid_amount)
end
def save_credit_card
card = {}
card['brand'] = @transaction.card.brand
card['first_digits'] = @transaction.card.first_digits
card['last_digits'] = @transaction.card.last_digits
card['holder_name'] = @transaction.card.holder_name
card['country'] = @transaction.card.country
@order.card_info = card
@order.save!
end
def send_email
letter = Email::Letter.create!(
context: 'order_completed',
object: @order,
user_email: @order.shipping_email,
email_template_id: Email::Template.find_by!(name: 'order_completed').id
)
begin
letter.send_email
rescue SparkPostRails::DeliveryException => e
"We can't send you email because #{e.service_description}"
end
end
end
class Email::Letter < ApplicationRecord
belongs_to :object, polymorphic: true, inverse_of: :email_letters
belongs_to :user
belongs_to :email_template, class_name: 'Email::Template'
validates :user_email, presence: true, unless: :user_id?
validates :context, presence: true, inclusion: { in: %w(admin_updated_store admin_deleted_store user_signed_up
user_created_store user_created_campaign product_was_sold order_completed order_canceled first_sale boleto_paid boleto_waiting
payment_request_user payment_request_admin payment_request_confirmed order_completed_vendor) }
validates :status, presence: true, inclusion: { in: %w(init sending sent has_error) }
validates :subject, presence: true, length: { minimum: 5, maximum: 60 }, unless: :email_template_id?
validates :body, presence: true, length: { minimum: 30 }, unless: :email_template_id?
include AASM
aasm column: :status do
state :init, initial: true
state :sending
state :sent
state :has_error
event :start_sending do
transitions from: :init, to: :sending
end
event :sending_was_successful do
transitions from: :sending, to: :sent
end
event :sending_was_unsuccessful do
transitions from: :sending, to: :has_error
end
end
def send_email
self.start_sending!
response = NotifyUserMailer.letter(self).deliver!
if response['total_accepted_recipients'] >= 1
self.update_attribute(:sparkpost_id, response['id'])
self.sending_was_successful!
else
self.sending_was_unsuccessful!
end
end
def replace_substitutions(text)
Rails.application.routes.default_url_options[:host] = ENV['SITE_HOST']
case context
when 'admin_updated_store', 'admin_deleted_store', 'user_created_store'
text.gsub!('%store.title%', object.title)
text.gsub!('%store.name%', object.title)
text.gsub!('%store.url%', Rails.application.routes.url_helpers.store_url(object.slug))
text.gsub!('%store.dashboard_url%', Rails.application.routes.url_helpers.edit_dashboard_store_url(uuid: object.uuid))
when 'user_created_campaign'
text.gsub!('%campaign.title%', object.title)
text.gsub!('%campaign.name%', object.title)
text.gsub!('%campaign.url%', Rails.application.routes.url_helpers.campaign_url(id: object.slug))
text.gsub!('%campaign.dashboard_url%', Rails.application.routes.url_helpers.edit_dashboard_campaign_url(uuid: object.uuid))
when 'order_completed'
text.gsub!('%order.list_of_products%', '<ul>' + object.order_products.map { |p| "<li>(#{p.quantity}x) #{p.product.campaign.title}</li>" }.join + '</ul>')
text.gsub!('%order.subtotal%', ActionController::Base.helpers.humanized_money_with_symbol(object.common_price))
text.gsub!('%order.shipping_price%', ActionController::Base.helpers.humanized_money_with_symbol(object.shipping_price))
text.gsub!('%order.total_price%', ActionController::Base.helpers.humanized_money_with_symbol(object.total_price))
when 'boleto_waiting'
text.gsub!('%order.boleto_url%', object.boleto_url)
text.gsub!('%order.list_of_products%', '<ul>' + object.order_products.map { |p| "<li>(#{p.quantity}x) #{p.product.campaign.title}</li>" }.join + '</ul>')
text.gsub!('%order.subtotal%', ActionController::Base.helpers.humanized_money_with_symbol(object.common_price))
text.gsub!('%order.shipping_price%', ActionController::Base.helpers.humanized_money_with_symbol(object.shipping_price))
text.gsub!('%order.total_price%', ActionController::Base.helpers.humanized_money_with_symbol(object.total_price))
when 'payment_request_user'
text.gsub!('%request.amount%', ActionController::Base.helpers.humanized_money_with_symbol((object.amount_centavos)/100.0))
when 'payment_request_admin'
text.gsub!('%request.amount%', ActionController::Base.helpers.humanized_money_with_symbol(object.amount_centavos))
when 'order_completed_vendor'
order_products = object.order_products.joins(product: :campaign).where(campaigns: { user_id: user.id })
text.gsub!('%order.list_of_products%', '<ul>' + order_products.map { |p| "<li>#{p.product.campaign.title}</li>" }.join + '</ul>')
text.gsub!('%order.total_price%', ActionController::Base.helpers.humanized_money_with_symbol(order_products.sum(:price_centavos).to_f/100))
end
if context != 'order_completed' &&
context != 'boleto_waiting' &&
context != 'boleto_paid' &&
context != 'payment_request_user' &&
context != 'payment_request_admin' &&
context != 'payment_request_confirmed'
text.gsub!('%user.name%', user.name)
text.gsub!('%user.full_name%', user.name)
text.gsub!('%user.first_name%', user.first_name.to_s)
text.gsub!('%user.last_name%', user.last_name.to_s)
text.gsub!('%user.email%', user.email)
text.gsub!('%user.full_email%', user.full_email)
end
text
end
end
# admin_updated_store store store.user
# admin_deleted_store store store.user
# user_signed_up user user
# user_created_store store store.user
# user_created_campaign campaign campaign.user
# product_was_sold product product.user
# order_completed order email
# order_canceled order email
# first_sale product product.user
@alexanmtz
Copy link
Author

O @order não tem user_id, e mesmo colocando um na mão no console, o erro persiste.

Este model do Email::Letter tem um belongs_to :user

@alexanmtz
Copy link
Author

validates :user_email, presence: true, unless: :user_id?

Tem essa validação e não vi campo user_email neste modo e não sei o motivo do user_id não está sendo atribuido

@alexanmtz
Copy link
Author

Exemplo (o erro acontece mesmo salvando um user_id):

letter = Email::Letter.create!(context: 'order_completed',object:Order.last,user_email: Order.last.shipping_email,email_template_id: Email::Template.find_by!(name: 'order_completed').id)
  Order Load (0.3ms)  SELECT  "orders".* FROM "orders" ORDER BY "orders"."id" DESC LIMIT $1  [["LIMIT", 1]]
  Order Load (0.3ms)  SELECT  "orders".* FROM "orders" ORDER BY "orders"."id" DESC LIMIT $1  [["LIMIT", 1]]
  Email::Template Load (0.3ms)  SELECT  "email_templates".* FROM "email_templates" WHERE "email_templates"."name" = $1 ORDER BY "email_templates"."title" ASC LIMIT $2  [["name", "order_completed"], ["LIMIT", 1]]
   (0.1ms)  BEGIN
  Email::Template Load (0.2ms)  SELECT  "email_templates".* FROM "email_templates" WHERE "email_templates"."id" = $1 ORDER BY "email_templates"."title" ASC LIMIT $2  [["id", 6], ["LIMIT", 1]]
   (0.1ms)  ROLLBACK
ActiveRecord::RecordInvalid: A validação falhou: User é obrigatório(a)
from /Users/alexandremagno/.rvm/gems/ruby-2.3.1@usinademarcas/gems/activerecord-5.0.6/lib/active_record/validations.rb:78:in `raise_validation_error'

@pedroMirandaVitat
Copy link

Mesmo problema aqui

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment