Class: EdiCommunicationLog

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/edi_communication_log.rb

Overview

== Schema Information

Table name: edi_communication_logs
Database name: primary

id :integer not null, primary key
category :string not null
data :text
data_type :string default("xml"), not null
file_info :jsonb
file_name :string
legacy_catalog_item_ids :integer default([]), is an Array
next_attempt :datetime
notes :text
partner :string
process_attempts :integer default(0), not null
response_data :text
state :string
transmit_after :datetime
transmit_datetime :datetime
created_at :datetime not null
updated_at :datetime not null
transaction_id :string

Indexes

by_st_ctry_partner (state,category,partner)
category_partner (category,partner)
idx_state_created_at (state,created_at)
index_edi_communication_logs_on_created_at (created_at)
index_edi_communication_logs_on_data_gin_trgm (data) USING gin
index_edi_communication_logs_on_next_attempt (next_attempt) USING brin
index_edi_communication_logs_on_partner (partner)
index_edi_communication_logs_on_transmit_after (transmit_after) USING brin

Constant Summary collapse

CATEGORIES =

Categories.

%w[price_advice inventory_advice inventory_match_advice invoice order_batch fba_order_batch order_acknowledge order_shipment order_confirm order_fa packing_slip remittance_advice return_notification product_data product_delete
catalog_item_information buy_box_status listing_data listing_item_information listing_feed_data listing_item_schema a_plus_content_search a_plus_content_document a_plus_content_document_full a_plus_content_document_content a_plus_content_upload_destination a_plus_content_image_upload a_plus_content_validation a_plus_content_create a_plus_content_asin_relations a_plus_content_approval_submission a_plus_content_copy_to_target a_plus_content_update a_plus_content_submit].sort.freeze
DATA_TYPES =

Recognised data types.

%w[xml json].freeze

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Has many collapse

Delegated Instance Attributes collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #creator, #should_not_save_version, #stamp_record, #updater

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#categoryObject (readonly)



50
# File 'app/models/edi_communication_log.rb', line 50

validates :category, :partner, presence: true

#partnerObject (readonly)



50
# File 'app/models/edi_communication_log.rb', line 50

validates :category, :partner, presence: true

Class Method Details

.active_firstActiveRecord::Relation<EdiCommunicationLog>

A relation of EdiCommunicationLogs that are active first. Active Record Scope

Returns:

See Also:



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

scope :active_first, -> {
  in_order_of(:state, %w[ready processing exception processed retry archived], filter: false)
    .order(updated_at: :desc, created_at: :desc)
}

.categories_for_selectObject



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

def self.categories_for_select
  CATEGORIES.sort_by { |e| e.include?('a_plus') ? "zzz#{e}" : e }.map { |e| [e.titleize, e] } # just put all those "A Plus..." ECL categories at the end
end

.cleanup_xml(xml) ⇒ Object



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

def self.cleanup_xml(xml)
  # Do a bit of xml cleanup
  cleaned_xml = xml
  if (doc = begin
    Nokogiri::XML(xml) { |cfg| cfg.default_xml.noblanks }
  rescue StandardError
    nil
  end)
    cleaned_xml = doc.to_xml(indent: 2)
  end
  cleaned_xml
end

.create_outbound_file_from_data(data:, file_extension:, partner:, category:, file_name: nil, resources: nil, data_type: 'csv', file_info: {}, transmit_after: nil) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'app/models/edi_communication_log.rb', line 308

def self.create_outbound_file_from_data(data:, file_extension:, partner:, category:, file_name: nil, resources: nil, data_type: 'csv', file_info: {}, transmit_after: nil)
  file_name ||= generate_file_name_from_data(data, file_extension, partner, category)
  ecl = nil
  EdiCommunicationLog.transaction do
    ecl = EdiCommunicationLog.create!(
      partner:,
      state: 'ready',
      file_name:,
      category:,
      data:,
      data_type:,
      transmit_after:,
      file_info:
    )
    if resources.present?
      [resources].flatten.each do |resource|
        ecl.edi_documents.create!(resource: resource)
      end
    end
  end
  ecl
end

.data_type_for_selectObject



160
161
162
# File 'app/models/edi_communication_log.rb', line 160

def self.data_type_for_select
  DATA_TYPES.sort.map { |e| [e.titleize, e] }
end

.file_extension_for_category(category, partner) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'app/models/edi_communication_log.rb', line 234

def self.file_extension_for_category(category, partner)
  if partner.index('sps_commerce').present?
    'xml'
  elsif partner.index('mft_gateway').present?
    'json'
  elsif partner.index('walmart_seller').present?
    'json'
  elsif partner.index('amazon_seller').present?
    'json'
  else
    {
      inventory_advice: 'xml',
      invoice: 'invoice',
      order_batch: 'neworders',
      order_confirm: 'confirm',
      order_fa: 'fa',
      return_notification: 'return',
      catalog_item_information: 'json',
      buy_box_status: 'json',
      listing_data: 'json',
      listing_item_information: 'json',
      listing_feed_data: 'json'
    }[category.to_sym]
  end
end

.generate_file_name_from_data(data, file_extension, partner, category) ⇒ Object



298
299
300
301
302
303
304
305
306
# File 'app/models/edi_communication_log.rb', line 298

def self.generate_file_name_from_data(data, file_extension, partner, category)
  prefix = EdiCommunicationLog.get_file_prefix_from(partner, category)
  [
    prefix,
    Time.current.to_i,
    "md5#{md5_signature(data)}",
    file_extension
  ].compact.join('.')
end

.get_file_prefix_from(partner, category) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
# File 'app/models/edi_communication_log.rb', line 285

def self.get_file_prefix_from(partner, category)
  return unless partner.to_s.index('sps_commerce')

  {
    inventory_advice: 'IB',
    # order_fa: 'PR',
    order_acknowledge: 'PR',
    # order_confirm: 'PC',
    order_shipment: 'SH',
    invoice: 'IN'
  }[category.to_sym] || 'NA'
end

.md5_signature(data) ⇒ Object



210
211
212
# File 'app/models/edi_communication_log.rb', line 210

def self.md5_signature(data)
  Digest::MD5.hexdigest(data).upcase
end

.partners_for_selectObject



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

def self.partners_for_select
  Edi::BaseOrchestrator.partners.keys.sort
end

.requiring_processingActiveRecord::Relation<EdiCommunicationLog>

A relation of EdiCommunicationLogs that are requiring processing. Active Record Scope

Returns:

See Also:



69
70
71
# File 'app/models/edi_communication_log.rb', line 69

scope :requiring_processing, -> {
  where("(edi_communication_logs.state = 'ready' OR (edi_communication_logs.state = 'retry' and edi_communication_logs.next_attempt <= ?)) and (edi_communication_logs.transmit_after IS NULL OR edi_communication_logs.transmit_after <= ?)", Time.current, Time.current).order(:created_at)
}

.states_for_selectObject



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

def self.states_for_select
  state_machines[:state].states.map { |s| [s.human_name.titleize, s.value] }
end

Instance Method Details

#can_be_matched?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'app/models/edi_communication_log.rb', line 214

def can_be_matched?
  (ready? || exception?) && category == 'packing_slip' && uploads.exists?
end

#catalog_itemsActiveRecord::Relation<CatalogItem>

Returns:

See Also:



57
# File 'app/models/edi_communication_log.rb', line 57

has_many :catalog_items, through: :edi_documents

#confirm_outbound_processing?Boolean

Returns:

  • (Boolean)


428
429
430
# File 'app/models/edi_communication_log.rb', line 428

def confirm_outbound_processing?
  outbound? && orchestrator.confirm_outbound_processing?
end

#customersObject

Alias for Orchestrator#customers

Returns:

  • (Object)

    Orchestrator#customers

See Also:



335
# File 'app/models/edi_communication_log.rb', line 335

delegate :customers, to: :orchestrator

#data_as_hashObject



185
186
187
188
189
# File 'app/models/edi_communication_log.rb', line 185

def data_as_hash
  return unless json?

  JSON.parse(data)
end

#data_as_jsonObject



172
173
174
175
176
# File 'app/models/edi_communication_log.rb', line 172

def data_as_json
  return data unless data.present? && json?

  JSON.pretty_generate(JSON.parse(data))
end

#data_as_json=(val) ⇒ Object



178
179
180
181
182
183
# File 'app/models/edi_communication_log.rb', line 178

def data_as_json=(val)
  self.data = json? ? JSON.generate(JSON.parse(val)) : val
rescue JSON::ParserError
  errors.add(:data, "is not valid JSON")
  throw :abort
end

#deep_dupObject



65
66
67
# File 'app/models/edi_communication_log.rb', line 65

def deep_dup
  deep_clone(include: %i[edi_documents uploads])
end

#directionObject



227
228
229
230
231
232
# File 'app/models/edi_communication_log.rb', line 227

def direction
  return :outbound if category.in?(%w[listing_data listing_feed_data price_advice order_fa order_acknowledge order_shipment order_confirm invoice inventory_advice product_data product_delete return_notification a_plus_content_create a_plus_content_upload_destination a_plus_content_image_upload a_plus_content_validation a_plus_content_asin_relations a_plus_content_approval_submission a_plus_content_copy_to_target
                                      a_plus_content_update a_plus_content_submit])

  :inbound
end

#edi_documentsActiveRecord::Relation<EdiDocument>

Returns:

See Also:



55
# File 'app/models/edi_communication_log.rb', line 55

has_many :edi_documents, dependent: :destroy

#file_info_as_jsonObject



191
192
193
194
195
# File 'app/models/edi_communication_log.rb', line 191

def file_info_as_json
  return if file_info.blank?

  JSON.pretty_generate(file_info)
end

#friendly_categoryObject



276
277
278
279
280
281
282
283
# File 'app/models/edi_communication_log.rb', line 276

def friendly_category
  s = category.to_s
  if notes.present?
    verb = notes[/http method: (\w+)/i, 1]
    s << " (#{verb})" if verb.present?
  end
  s
end

#inbound?Boolean

Returns:

  • (Boolean)


264
265
266
# File 'app/models/edi_communication_log.rb', line 264

def inbound?
  direction == :inbound
end

#invoicesActiveRecord::Relation<Invoice>

Returns:

  • (ActiveRecord::Relation<Invoice>)

See Also:



61
# File 'app/models/edi_communication_log.rb', line 61

has_many :invoices, through: :orders

#itemsActiveRecord::Relation<Item>

Returns:

  • (ActiveRecord::Relation<Item>)

See Also:



58
# File 'app/models/edi_communication_log.rb', line 58

has_many :items, through: :edi_documents

#json?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'app/models/edi_communication_log.rb', line 168

def json?
  data_type == 'json'
end

#md5_match?(filename, data) ⇒ Boolean

Returns:

  • (Boolean)


268
269
270
271
272
273
274
# File 'app/models/edi_communication_log.rb', line 268

def md5_match?(filename, data)
  file_md5 = filename.scan(/md5(.+)\..+\z/).flatten&.first
  data_md5 = Digest::MD5.hexdigest(data).upcase
  res = skip_md5_verification || file_md5 == data_md5
  logger.info "md5_match? data: #{data_md5}, filename: #{file_md5}, result: #{res} (skip_md5_verification: #{skip_md5_verification})"
  res
end

#md5_signatureObject



206
207
208
# File 'app/models/edi_communication_log.rb', line 206

def md5_signature
  self.class.md5_signature(data)
end

#orchestratorObject



331
332
333
# File 'app/models/edi_communication_log.rb', line 331

def orchestrator
  Edi::BaseOrchestrator.build(partner)
end

#ordersActiveRecord::Relation<Order>

Returns:

  • (ActiveRecord::Relation<Order>)

See Also:



59
# File 'app/models/edi_communication_log.rb', line 59

has_many :orders, through: :edi_documents

#orders_for_packing_slip_matchupObject



218
219
220
221
222
223
224
225
# File 'app/models/edi_communication_log.rb', line 218

def orders_for_packing_slip_matchup
  return Order.none unless can_be_matched?

  Order.sales_orders.joins(:customer)
       .merge(orchestrator.customers)
       .order('orders.created_at desc')
       .where(state: Order::RELEASABLE_STATES)
end

#outbound?Boolean

Returns:

  • (Boolean)


260
261
262
# File 'app/models/edi_communication_log.rb', line 260

def outbound?
  direction == :outbound
end

#process(force: false) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'app/models/edi_communication_log.rb', line 337

def process(force: false)
  return unless (ready? || force) && persisted?
  return unless (o = orchestrator)

  case category.to_sym
  when :order_batch
    o.order_message_processor.process(self)
  when :fba_order_batch
    o.fba_order_message_processor.process(self)
  when :order_fa
    o.fa_message_sender.process(self)
  when :order_acknowledge
    o.acknowledge_message_sender.process(self)
  when :order_shipment
    o.shipment_message_sender.process(self)
  when :order_confirm
    o.confirm_message_sender.process(self)
  when :invoice
    o.invoice_message_sender.process(self)
  when :packing_slip
    o.packing_slip_processor.process(self)
  when :inventory_advice
    o.inventory_message_sender.process(self)
  when :remittance_advice
    o.remit_message_processor.process(self)
  when :product_delete
    o.product_delete_sender.process(self)
  when :return_notification
    o.return_notification_message_sender.process(self)
  when :product_data
    o.product_data_sender.process(self)
  when :price_advice
    o.price_message_sender.process(self)
  when :catalog_item_information
    process_catalog_item_information
  when :buy_box_status
    process_buy_box_status
  when :price_advice
    o.price_message_sender.process(self)
  when :listing_data
    if data&.index('patches').present? # bit hacky but this is to annoyingly implement the REST functionality of Amazon SP-API listings
      o.listing_message_sender.process(self, http_method: 'PATCH')
    else
      o.listing_message_sender.process(self, http_method: 'PUT')
    end
  when :listing_feed_data
    o.listing_message_feed_sender.process(self)
  when :a_plus_content_search, :a_plus_content_document, :a_plus_content_document_full, :a_plus_content_document_content,
       :a_plus_content_upload_destination, :a_plus_content_image_upload, :a_plus_content_validation,
       :a_plus_content_create, :a_plus_content_asin_relations, :a_plus_content_approval_submission,
       :a_plus_content_copy_to_target
    o.a_plus_content_listing_processor.process(self)
  end
  self
end

#process_buy_box_statusObject



400
401
402
403
404
405
# File 'app/models/edi_communication_log.rb', line 400

def process_buy_box_status
  # Find catalog items to pull from
  catalog_items.each do |catalog_item|
    orchestrator.pull_buy_box_status(catalog_item)
  end
end

#process_catalog_item_informationObject



393
394
395
396
397
398
# File 'app/models/edi_communication_log.rb', line 393

def process_catalog_item_information
  # Find catalog items to pull from
  catalog_items.each do |catalog_item|
    orchestrator.pull_catalog_information(catalog_item)
  end
end

#receiptsActiveRecord::Relation<Receipt>

Returns:

  • (ActiveRecord::Relation<Receipt>)

See Also:



60
# File 'app/models/edi_communication_log.rb', line 60

has_many :receipts, through: :edi_documents

#statisticsObject



407
408
409
410
# File 'app/models/edi_communication_log.rb', line 407

def statistics
  mn = :"statistics_#{category}"
  send(mn) if respond_to?(mn)
end

#statistics_product_dataObject



412
413
414
415
# File 'app/models/edi_communication_log.rb', line 412

def statistics_product_data
  mn = :"statistics_product_data_#{partner}"
  send(mn) if respond_to?(mn)
end

#update_catalog_items_ecl_sentObject



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'app/models/edi_communication_log.rb', line 432

def update_catalog_items_ecl_sent
  # after we mark an EDI Communication Log as processed, check if it's category is one of the message types we want to track, e.g. inventory_advice, price_advice etc.
  # use our pattern to find the last_xyx_sent_json column name from the category. e.g. last_inventory_advice_sent_json or last_price_advice_sent_json
  message_sent_json_column_name = "last_#{category}_sent_json"
  return unless CatalogItem.method_defined?(message_sent_json_column_name) # if this column exists in CatalogItem, then let's update it for this EDI partner

  # see https://stackoverflow.com/questions/51657123/active-record-update-all-json-field for how we got this syntax
  sql = ["#{message_sent_json_column_name} = jsonb_set(#{message_sent_json_column_name}, '{ #{partner} }', ?)", %("#{Time.current}")]
  # Update via subquery to keep `catalog_items` from appearing twice in the
  # UPDATE's FROM list. Calling `update_all` directly on the `:through`
  # association in Rails 8.1 generates a JOIN onto `catalog_items` in
  # addition to the UPDATE target, which made the unqualified column
  # reference in the SET clause ambiguous (PG::AmbiguousColumn).
  CatalogItem.where(id: edi_documents.where.not(catalog_item_id: nil).select(:catalog_item_id))
             .update_all(CatalogItem.sanitize_sql_for_assignment(sql))
end

#update_invoices_transmission_stateObject



449
450
451
452
453
454
455
456
457
458
# File 'app/models/edi_communication_log.rb', line 449

def update_invoices_transmission_state
  return unless processed? && category == 'invoice'

  Invoice.where(id: edi_documents.select(:invoice_id)).each do |invoice|
    invoice.transmit! if invoice.can_transmit?
  rescue StandardError => e
    logger.error "Error updating invoice #{invoice.id} transmission state: #{e.message} on edi_communication_log #{id}"
    ErrorReporting.error(e, message: "Error updating invoice #{invoice.id} transmission state to transmitted", edi_communication_log_id: id, invoice_id: invoice.id)
  end
end

#update_statusObject



417
418
419
420
421
422
423
424
425
426
# File 'app/models/edi_communication_log.rb', line 417

def update_status
  return unless processing? && persisted?
  return unless (o = orchestrator)

  # handle feed (e.g. Amazon Seller) vs transaction (e.g. Amazon Vendor) status updating
  o.feed_submission_result_processor.process if o.respond_to?(:feed_submission_result_processor)
  o.transaction_message_processor.process if o.respond_to?(:transaction_message_processor)
  o.mirakl_result_processor(self) if o.respond_to?(:mirakl_result_processor)
  self
end

#uploadsActiveRecord::Relation<Upload>

Returns:

  • (ActiveRecord::Relation<Upload>)

See Also:



56
# File 'app/models/edi_communication_log.rb', line 56

has_many :uploads, as: :resource, dependent: :destroy

#validate_md5Object

TODO : hook this in later



198
199
200
201
202
203
204
# File 'app/models/edi_communication_log.rb', line 198

def validate_md5
  return if md5_match?(edi_log.file_name, edi_log.data)

  logger.error "MD5 does not match, skipping #{file_name}"
  errors!
  update_column(:notes, 'MD5 in filename does not match data')
end

#xml?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'app/models/edi_communication_log.rb', line 164

def xml?
  data_type == 'xml'
end