Class: ReferenceEvent

Inherits:
ApplicationRecord show all
Defined in:
app/models/reference_event.rb

Defined Under Namespace

Modules: Levels, Slugs

Constant Summary collapse

DISPATCHABLE_EVENTS =

as an example for dispatchable events, use invalid employee only create event for invoices which pass to new status which don’t have events

[
  Slugs::INVALID_EMPLOYEE,
  Slugs::EMPLOYEES_WITHOUT_TIMESHEETS,
  Slugs::PAYROLL_PERIOD_WITHOUT_TRANSACTION,
  Slugs::UNREVIEWED_PAYROLL_TRANSACTION,
  Slugs::NEW_LOAD_TENDER,
  Slugs::INVOICE_SENT_VIA_EDI,
  Slugs::INVALID_INVOICE
].freeze
PROCESSABLE_EVENTS =

as an example for processable events, use invoice sent via edi

[
  Slugs::EMPLOYEE_PAYROLL_DISBURSEMENT,
  Slugs::INVOICE_SENT_VIA_EDI
].freeze
VALID_LEVELS =
[
  Levels::DEBUG,
  Levels::INFO,
  Levels::WARN,
  Levels::ERROR,
  Levels::FATAL,
  Levels::UNKNOWN
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

define_decrypted_attribute, define_decrypted_attributes, #errors_in_bullet_points, primary_connection, special_connection

Class Method Details

.errorsObject



74
75
76
# File 'app/models/reference_event.rb', line 74

def errors
  ReferenceEvent.where(level: Levels::ERROR)
end

Instance Method Details

#descriptionObject



156
157
158
# File 'app/models/reference_event.rb', line 156

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

#end_event!Object



135
136
137
138
139
# File 'app/models/reference_event.rb', line 135

def end_event!
  update!(
    end_at: Time.current
  )
end

#error?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'app/models/reference_event.rb', line 123

def error?
  [Levels::ERROR, Levels::FATAL].include?(level)
end

#event_notification_configurationObject



106
107
108
109
110
111
# File 'app/models/reference_event.rb', line 106

def event_notification_configuration
  EventNotificationConfiguration.find_by(
    event_slug: slug,
    company_id: company_id
  )
end

#event_notification_configuration_idObject



145
146
147
148
149
150
# File 'app/models/reference_event.rb', line 145

def event_notification_configuration_id
  @event_notification_configuration_id ||= EventNotificationConfiguration.find_by(
    event_slug: slug,
    company_id: company_id
  ).id
end

#event_notification_deliveries(company_id:) ⇒ Object



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

def event_notification_deliveries(company_id:)
  EventNotificationDelivery.joins(:event_notification_configuration)
                           .where(
                             event_notification_configurations: {
                               event_slug: slug,
                               company_id: company_id
                             }
                           )
end

#formatted_created_dateObject



141
142
143
# File 'app/models/reference_event.rb', line 141

def formatted_created_date
  I18n.l(created_at, format: :long_with_time)
end

#last_deliveries(limit: 10) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'app/models/reference_event.rb', line 168

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

#nameObject



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

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

#occurence_descriptionObject



160
161
162
163
164
165
166
# File 'app/models/reference_event.rb', line 160

def occurence_description
  I18n.t(
    "reference_events.#{slug.underscore}.occurence_description",
    **reference_attributes,
    created_at: formatted_created_date
  )
end

#processObject



187
188
189
# File 'app/models/reference_event.rb', line 187

def process
  processor_class.new(reference_event: self).process
end

#reference_nameObject



183
184
185
# File 'app/models/reference_event.rb', line 183

def reference_name
  reference.name
end

#reference_urlObject



179
180
181
# File 'app/models/reference_event.rb', line 179

def reference_url
  reference.url
end

#statusObject



127
128
129
# File 'app/models/reference_event.rb', line 127

def status
  end_at ? 'ended' : 'active'
end

#update_eventObject



131
132
133
# File 'app/models/reference_event.rb', line 131

def update_event
  updater_class.new(reference_event: self).updated_event
end