effective_postmark 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3c865f268155269e90bfab82beaca405a2adfed8b20c7156214f5b95574986c3
4
- data.tar.gz: 183783ce5068b9b1abfdba98551d1a812a070f414a56f0fe787826a0855dd680
3
+ metadata.gz: c54904eee89796343bceb312989b4ac5396eaafbd300e60aac6d5f9574421494
4
+ data.tar.gz: 76956ccf6a60141ab891c5cd8f1959fb240b84de12d102d970425628dc3806b1
5
5
  SHA512:
6
- metadata.gz: d927becf5cb36887f8a3093975e6b5cf543d9fe34d73fa11900372ec9807d484a5b40ae7e7d7e5ed30fb3e01f702f765b582a038526d8151309e965c674694db
7
- data.tar.gz: bf2041254c685af6d7c2dbe5f64c2ba3ecf9ae62a5fbee7dd0fafe319992361a8842fbe2a19b2f2cf746d1691050f1b5feaaca92ec75ac1f036a9617575b7d65
6
+ metadata.gz: 9b4a3721507412cee63854db7f40b266dc74d3e8624f9f8372dfb3f6f26f76cb2413507c9bf537ba4edb244124520d381e677175fc3b477ade6ad882a103aa2a
7
+ data.tar.gz: 0bb3550864a845eac196ac9c26cb6755a4c8ba347210a1882bbb28945ffde11e5a2b615ae2b9851ef9406b74a254edd59ece41c5fa40bd8ca96f6b8291114173
@@ -9,7 +9,7 @@ module EffectivePostmarkMailer
9
9
  end
10
10
  end
11
11
 
12
- rescue_from(::StandardError, with: :effective_postmark_error) unless Rails.env.test? || Rails.env.development?
12
+ #rescue_from(::StandardError, with: :effective_postmark_error)
13
13
  rescue_from(::Postmark::InactiveRecipientError, with: :effective_postmark_inactive_recipient_error)
14
14
  end
15
15
 
@@ -59,6 +59,11 @@ module EffectivePostmarkMailer
59
59
  Rails.logger.info "\e[31m\e[1mEMAIL FAILED\e[0m\e[22m" # bold red
60
60
  Rails.logger.info "#{exception.inspect}"
61
61
 
62
+ EffectiveLogger.error(exception.message) if defined?(EffectiveLogger)
63
+ ExceptionNotifier.notify_exception(exception) if defined?(ExceptionNotifier)
64
+
65
+ raise(exception) if Rails.env.test? || Rails.env.development?
66
+
62
67
  true
63
68
  end
64
69
 
@@ -38,6 +38,15 @@ module EffectivePostmarkUser
38
38
  update_columns(email_delivery_error: 'Inactive Recipient', email_delivery_error_at: Time.zone.now)
39
39
  end
40
40
 
41
+ # Assigned by the rake task effective_postmark:assign_email_delivery_errors
42
+ def postmark_suppression!(reason:, date:)
43
+ return if email_delivery_error.present? # If we already marked invalid, don't mark again
44
+
45
+ date = (Time.zone.parse(date) rescue Time.zone.now) if date.kind_of?(String)
46
+
47
+ update_columns(email_delivery_error: reason, email_delivery_error_at: date)
48
+ end
49
+
41
50
  # Triggered by an admin to reactivate the email address
42
51
  def postmark_reactivate!
43
52
  # Make an API request to reactivate this user
@@ -9,6 +9,38 @@ module Effective
9
9
  @client = ::Postmark::ApiClient.new(api_token)
10
10
  end
11
11
 
12
+ # [ "outbound", "broadcast-stream" ]
13
+ def streams()
14
+ @streams ||= begin
15
+ client.get_message_streams
16
+ .select { |stream| ['Broadcasts', 'Transactional'].include?(stream[:message_stream_type]) }
17
+ .map { |stream| stream[:id] }
18
+ end
19
+ end
20
+
21
+ def suppressions()
22
+ streams()
23
+ .flat_map { |stream| client.dump_suppressions(stream) }
24
+ .uniq { |suppression| suppression[:email_address] }
25
+ end
26
+
27
+ # Called by rake effective_postmark:assign_email_delivery_errors
28
+ def assign_email_delivery_errors!(users)
29
+ raise('expected an effective_postmark_user klass') unless users.try(:effective_postmark_user?)
30
+
31
+ suppressions().each do |suppression|
32
+ email = suppression[:email_address].to_s.downcase.strip
33
+ next unless email.present?
34
+
35
+ user = users.find_for_database_authentication(email: email)
36
+ next unless user.present?
37
+
38
+ user.postmark_suppression!(reason: suppression[:suppression_reason], date: suppression[:created_at])
39
+ end
40
+
41
+ true
42
+ end
43
+
12
44
  # EffectivePostmark.api.reactivate(email)
13
45
  def reactivate(user)
14
46
  raise('expected an effective_postmark_user') unless user.class.try(:effective_postmark_user?)
@@ -18,8 +50,7 @@ module Effective
18
50
 
19
51
  # There are multiple streams. outbound / broadcast / inbound
20
52
  begin
21
- client.delete_suppressions(:outbound, emails)
22
- client.delete_suppressions(:broadcast, emails)
53
+ streams().each { |stream| client.delete_suppressions(stream, emails) }
23
54
  true
24
55
  rescue => e
25
56
  false
@@ -1,3 +1,3 @@
1
1
  module EffectivePostmark
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
@@ -18,6 +18,10 @@ module EffectivePostmark
18
18
  Effective::PostmarkApi.new(api_token: api_token)
19
19
  end
20
20
 
21
+ def self.api_present?
22
+ api_token.present?
23
+ end
24
+
21
25
  def self.mailer_class
22
26
  mailer&.constantize || Effective::PostmarkMailer
23
27
  end
@@ -1,2 +1,25 @@
1
1
  namespace :effective_postmark do
2
+ desc 'Assign email delivery errors to postmark users from suppressions API'
3
+ task assign_email_delivery_errors: :environment do
4
+ table = ActiveRecord::Base.connection.table_exists?(:users)
5
+ blank_tenant = defined?(Tenant) && Tenant.current.blank?
6
+
7
+ if table && !blank_tenant && EffectivePostmark.api_present?
8
+ puts "Assigning postmark email delivery errors"
9
+
10
+ klass = User.all
11
+ raise("expected an effective_postmark_user User class") unless klass.try(:effective_postmark_user?)
12
+
13
+ api = EffectivePostmark.api
14
+
15
+ begin
16
+ api.assign_email_delivery_errors!(klass)
17
+ rescue StandardError => e
18
+ ExceptionNotifier.notify_exception(e) if defined?(ExceptionNotifier)
19
+ puts "Error with effective_postmark:assign_email_delivery_errors"
20
+ end
21
+ end
22
+
23
+ puts 'All done'
24
+ end
2
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_postmark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-16 00:00:00.000000000 Z
11
+ date: 2024-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails