Class: VANotify::NotificationEmail::SavedClaim

Inherits:
Object
  • Object
show all
Defined in:
lib/va_notify/notification_email/saved_claim.rb

Overview

general SavedClaim email notification function

an entry should be in Settings.vanotify.services - config/settings.yml

Examples:

21p_527ez: &vanotify_services_pension
  api_key: fake_secret
  email:
    confirmation:
      template_id: form527ez_confirmation_email_template_id
      flipper_id: false
    error:
      template_id: form527ez_error_email_template_id,
      flipper_id: form527ez_error_email_flipper_id
    received: null
pensions: *vanotify_services_pension

Direct Known Subclasses

Burials::NotificationEmail

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(saved_claim_id, service_name: nil) ⇒ SavedClaim

constructor

Parameters:

  • saved_claim (SavedClaim)

    the claim for which to send a notification

  • service_name (String) (defaults to: nil)

    alternative serivce name listed in Settings default will be the formatted claim.form_id @see #vanotify_service



30
31
32
33
# File 'lib/va_notify/notification_email/saved_claim.rb', line 30

def initialize(saved_claim_id, service_name: nil)
  @claim = claim_class.find(saved_claim_id)
  @vanotify_service = service_name
end

Instance Attribute Details

#claimObject (readonly, private)

Returns the value of attribute claim.



63
64
65
# File 'lib/va_notify/notification_email/saved_claim.rb', line 63

def claim
  @claim
end

#email_template_idObject (readonly, private)

Returns the value of attribute email_template_id.



63
64
65
# File 'lib/va_notify/notification_email/saved_claim.rb', line 63

def email_template_id
  @email_template_id
end

#email_typeObject (readonly, private)

Returns the value of attribute email_type.



63
64
65
# File 'lib/va_notify/notification_email/saved_claim.rb', line 63

def email_type
  @email_type
end

Instance Method Details

#claim_classObject (private)

the type of SavedClaim to be queried



66
67
68
# File 'lib/va_notify/notification_email/saved_claim.rb', line 66

def claim_class
  ::SavedClaim
end

#deliver(email_type, at: nil) ⇒ ClaimVANotification

deliver a notification for claim

Parameters:

  • email_type (Symbol)

    one of VANotify::NotificationEmail::Type and defined in Settings

  • at (String|DateTime) (defaults to: nil)

    valid date string to schedule sending of notification @see VANotify::EmailJob#perform_at

Returns:

See Also:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/va_notify/notification_email/saved_claim.rb', line 44

def deliver(email_type, at: nil)
  @email_type = email_type
  @email_template_id = valid_attempt?
  return unless email_template_id

  at ? enqueue_email(email_template_id, at) : send_email(email_template_id)

  db_record = claim.insert_notification(email_template_id)
  tags, context = monitoring
  VANotify::NotificationEmail.monitor_deliver_success(tags:, context:)

  db_record
rescue => e
  tags, context = monitoring
  VANotify::NotificationEmail.monitor_send_failure(e&.message, tags:, context:)
end

#emailObject (private)

retrieve the email from the claim

  • specific claim types should have an ‘email` function defined

  • or should inherit this class and override this function



145
146
147
# File 'lib/va_notify/notification_email/saved_claim.rb', line 145

def email
  claim.email
end

#enqueue_email(email_template_id, at) ⇒ Object (private)

schedule sending of email at future date

Parameters:

  • email_template_id (String)

    the template id to be used

  • at (String|DateTime)

    valid date string to schedule sending of notification

See Also:

  • EmailJob#perform_at


122
123
124
125
126
127
128
129
# File 'lib/va_notify/notification_email/saved_claim.rb', line 122

def enqueue_email(email_template_id, at)
  VANotify::EmailJob.perform_at(
    at,
    email,
    email_template_id,
    personalization
  )
end

#flipper_enabled?(flipper_id) ⇒ Boolean (private)

flipper exists and is enabled

Parameters:

  • flipper_id (String)

    the flipper id

Returns:

  • (Boolean)


77
78
79
# File 'lib/va_notify/notification_email/saved_claim.rb', line 77

def flipper_enabled?(flipper_id)
  !flipper_id || (flipper_id && Flipper.enabled?(:"#{flipper_id}"))
end

#monitoringObject (private)

create the tags and context for monitoring



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/va_notify/notification_email/saved_claim.rb', line 104

def monitoring
  tags = ["service_name:#{vanotify_service}",
          "form_id:#{claim.form_id}",
          "email_template_id:#{email_template_id}"]
  context = {
    form_id: claim.form_id,
    saved_claim_id: claim.id,
    service_name: vanotify_service,
    email_type:,
    email_template_id:
  }
  [tags, context]
end

#personalizationObject (private)

assemble details for personalization in the email

  • specific claim types should inherit this class and override this function



151
152
153
154
155
156
# File 'lib/va_notify/notification_email/saved_claim.rb', line 151

def personalization
  {
    'date_submitted' => claim.,
    'confirmation_number' => claim.confirmation_number
  }
end

#send_email(email_template_id) ⇒ Object (private)

send the notification email immediately

Parameters:

  • email_template_id (String)

    the template id to be used

See Also:

  • EmailJob#perform_async


134
135
136
137
138
139
140
# File 'lib/va_notify/notification_email/saved_claim.rb', line 134

def send_email(email_template_id)
  VANotify::EmailJob.perform_async(
    email,
    email_template_id,
    personalization
  )
end

#valid_attempt?Boolean (private)

check prerequisites before attempting to send the email

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/va_notify/notification_email/saved_claim.rb', line 82

def valid_attempt?
  config = Settings.vanotify.services[vanotify_service]
  raise ArgumentError, "Invalid service_name '#{vanotify_service}'" unless config

  email_config = config.email[email_type]
  raise ArgumentError, "Invalid email_type '#{email_type}'" unless email_config

  @email_template_id = email_config.template_id
  raise VANotify::NotificationEmail::FailureToSend, 'Invalid template' unless email_template_id
  raise VANotify::NotificationEmail::FailureToSend, 'Missing email' if email.blank?

  is_enabled = flipper_enabled?(email_config.flipper_id)
  already_sent = claim.va_notification?(email_config.template_id)
  if already_sent
    tags, context = monitoring
    VANotify::NotificationEmail.monitor_duplicate_attempt(tags:, context:)
  end

  email_template_id if is_enabled && !already_sent
end

#vanotify_serviceObject (private)

return or default the service_name to be used



71
72
73
# File 'lib/va_notify/notification_email/saved_claim.rb', line 71

def vanotify_service
  @vanotify_service ||= claim&.form_id&.downcase&.gsub(/-/, '_')
end