|
| 1 | +# == Schema Information |
| 2 | +# |
| 3 | +# Table name: email_attempts |
| 4 | +# |
| 5 | +# id :uuid not null, primary key |
| 6 | +# bcc :string |
| 7 | +# cc :string |
| 8 | +# created_by_type :string |
| 9 | +# delivered :boolean |
| 10 | +# from :string |
| 11 | +# postmark_stream :string |
| 12 | +# postmark_template_alias :string |
| 13 | +# postmark_template_model :jsonb |
| 14 | +# provider :integer |
| 15 | +# reply_to :string |
| 16 | +# response_body :jsonb |
| 17 | +# response_status :integer |
| 18 | +# to :string |
| 19 | +# created_at :datetime not null |
| 20 | +# updated_at :datetime not null |
| 21 | +# created_by_id :uuid |
| 22 | +# |
| 23 | +# Indexes |
| 24 | +# |
| 25 | +# index_email_attempts_on_created_by (created_by_type,created_by_id) |
| 26 | +# index_email_attempts_on_response_body (response_body) USING gin |
| 27 | +# |
| 28 | +class EmailAttempt < ApplicationRecord |
| 29 | + # The record which created this EmailAttempt |
| 30 | + belongs_to :created_by, polymorphic: true, optional: true |
| 31 | + |
| 32 | + # The provider which will send the email for us. |
| 33 | + # Rails cannot send emails. It only delegates them. |
| 34 | + # enum :provider, { postmark: 0, sendgrid: 1 }, scopes: true |
| 35 | + |
| 36 | + after_save :send_email |
| 37 | + |
| 38 | + private |
| 39 | + |
| 40 | + def blocker |
| 41 | + return 'no to' if to.nil? |
| 42 | + return 'no from' if from.nil? |
| 43 | + return 'no postmark_stream' if postmark_stream.nil? |
| 44 | + return 'no postmark_template_alias' if postmark_template_alias.nil? |
| 45 | + return 'no postmark_template_model' if postmark_template_model.nil? |
| 46 | + return 'model is not a hash' unless postmark_template_model.is_a? Hash |
| 47 | + |
| 48 | + # Never deliver an email twice |
| 49 | + # This also allows admins to edit and retry until delivered |
| 50 | + return 'already delivered' if delivered == true |
| 51 | + |
| 52 | + nil |
| 53 | + end |
| 54 | + |
| 55 | + def send_email |
| 56 | + log.info "✅ saved EmailAttempt #{id}" |
| 57 | + |
| 58 | + if blocker.present? |
| 59 | + log.warn "🔥 #{blocker}" |
| 60 | + return |
| 61 | + end |
| 62 | + |
| 63 | + log.info '✅ sending ...' |
| 64 | + |
| 65 | + ap from |
| 66 | + ap to |
| 67 | + ap postmark_template_alias |
| 68 | + ap postmark_template_model |
| 69 | + |
| 70 | + api = Postmark::Api.new(server_token: ENV['POSTMARK_SERVER_TOKEN']) |
| 71 | + |
| 72 | + response = api.send_email_with_template( |
| 73 | + message_stream: postmark_stream, |
| 74 | + template_alias: postmark_template_alias, |
| 75 | + template_model: postmark_template_model, |
| 76 | + from: from, |
| 77 | + to: to, |
| 78 | + cc: cc, |
| 79 | + bcc: bcc, |
| 80 | + reply_to: reply_to |
| 81 | + ) |
| 82 | + |
| 83 | + if response.status == 200 |
| 84 | + log.info '✅ success' |
| 85 | + else |
| 86 | + log.info '❌ fail' |
| 87 | + end |
| 88 | + |
| 89 | + ap response.status |
| 90 | + ap response.body |
| 91 | + |
| 92 | + self.response_status = response.status |
| 93 | + self.response_body = JSON.parse(response.body) if response.body.present? |
| 94 | + self.delivered = response.status == 200 |
| 95 | + |
| 96 | + save! |
| 97 | + |
| 98 | + log.info '✅ done' |
| 99 | + end |
| 100 | +end |
0 commit comments