Class: EventNotificationConfiguration

Inherits:
ApplicationRecord show all
Includes:
TimeHelper
Defined in:
app/models/event_notification_configuration.rb

Constant Summary collapse

VALID_DELIVERY_METHODS =
[
  'email'
]
NOTIFIABLE_EVENT_SLUGS =
[
  'INVALID_EMPLOYEE',
  'UNCOMPLETED_COMPANY_SETUP',
  'EMPLOYEES_WITHOUT_TIMESHEETS',
  'PAYROLL_PERIOD_WITHOUT_TRANSACTION',
  'UNREVIEWED_PAYROLL_TRANSACTION',
  'NEW_LOAD_TENDER',
  'INVALID_INVOICE'
]

Instance Method Summary collapse

Methods included from TimeHelper

#cron_with_zone, #destructure_seconds

Methods inherited from ApplicationRecord

define_decrypted_attribute, define_decrypted_attributes, #errors_in_bullet_points, primary_connection, special_connection

Instance Method Details

#active_reference_eventsObject



148
149
150
# File 'app/models/event_notification_configuration.rb', line 148

def active_reference_events
  ReferenceEvent.where(slug: event_slug, end_at: nil)
end

#delivery_methodsObject



89
90
91
92
93
94
# File 'app/models/event_notification_configuration.rb', line 89

def delivery_methods
  [{
    label: I18n.t('auth.email'),
    value: 'email'
  }]
end

#descriptionObject



81
82
83
# File 'app/models/event_notification_configuration.rb', line 81

def description
  I18n.t("reference_events.#{event_slug.underscore}.description")
end

#dummy_email_htmlObject



114
115
116
117
118
119
120
121
122
# File 'app/models/event_notification_configuration.rb', line 114

def dummy_email_html
  template_path = Rails.root.join('app', 'views', 'notification_mailer', "#{event_slug.downcase}.html.erb")
  template = File.read(template_path)
  renderer = ERB.new(template)

  @params = dummy_params_class.new

  renderer.result(binding)
end

#dummy_params_classObject



110
111
112
# File 'app/models/event_notification_configuration.rb', line 110

def dummy_params_class
  "EmailParams::#{event_slug.underscore.camelize}EmailDummyParams".constantize
end

#last_deliveries(limit: 10) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'app/models/event_notification_configuration.rb', line 124

def last_deliveries(limit: 10)
  event_notification_deliveries
    .order(delivered_at: :desc)
    .limit(limit)
    .as_json(
      methods: [
        :recipient_email
      ]
    )
end

#limit_typeObject



96
97
98
99
100
101
# File 'app/models/event_notification_configuration.rb', line 96

def limit_type
  return 'max_times' if max_times
  return 'max_age' if max_age

  'max_times'
end

#nameObject



77
78
79
# File 'app/models/event_notification_configuration.rb', line 77

def name
  I18n.t("reference_events.#{event_slug.underscore}.name")
end

#notify_reference_events_recipientsObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/event_notification_configuration.rb', line 135

def notify_reference_events_recipients
  notification_deliveries = []
  each_active_reference_event do |reference_event|
    reference_event_notification_deliveries = notify_reference_event_recipients(
      reference_event: reference_event
    )

    notification_deliveries += reference_event_notification_deliveries
  end

  notification_deliveries
end

#reference_eventsObject



70
71
72
73
74
75
# File 'app/models/event_notification_configuration.rb', line 70

def reference_events
  ReferenceEvent.where(
    slug: event_slug,
    company_id: company_id
  )
end

#schedule_cronObject



85
86
87
# File 'app/models/event_notification_configuration.rb', line 85

def schedule_cron
  fugit_cron&.original
end

#schedule_typeObject



103
104
105
106
107
108
# File 'app/models/event_notification_configuration.rb', line 103

def schedule_type
  return 'none' unless active
  return 'once' unless recurring_schedule

  'periodic' if recurring_schedule.present?
end

#sender_params_classObject



152
153
154
155
# File 'app/models/event_notification_configuration.rb', line 152

def sender_params_class
  capitalized_delivery_method = delivery_method.capitalize
  "#{capitalized_delivery_method}Params::#{event_slug.underscore.camelize}#{capitalized_delivery_method}Params".constantize
end