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
return_batch 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 Models::Schedulable

Models::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 Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#categoryObject (readonly)



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

validates :category, :partner, presence: true

#partnerObject (readonly)



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

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:



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

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



162
163
164
# File 'app/models/edi_communication_log.rb', line 162

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



145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/edi_communication_log.rb', line 145

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



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'app/models/edi_communication_log.rb', line 318

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



170
171
172
# File 'app/models/edi_communication_log.rb', line 170

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

.file_extension_for_category(category, partner) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'app/models/edi_communication_log.rb', line 244

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



308
309
310
311
312
313
314
315
316
# File 'app/models/edi_communication_log.rb', line 308

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



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

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



220
221
222
# File 'app/models/edi_communication_log.rb', line 220

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

.partners_for_selectObject



158
159
160
# File 'app/models/edi_communication_log.rb', line 158

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:



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

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



166
167
168
# File 'app/models/edi_communication_log.rb', line 166

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)


224
225
226
# File 'app/models/edi_communication_log.rb', line 224

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

#catalog_itemsActiveRecord::Relation<CatalogItem>

Returns:

See Also:



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

has_many :catalog_items, through: :edi_documents

#confirm_outbound_processing?Boolean

Returns:

  • (Boolean)


440
441
442
# File 'app/models/edi_communication_log.rb', line 440

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

#customersObject

Alias for Orchestrator#customers

Returns:

  • (Object)

    Orchestrator#customers

See Also:



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

delegate :customers, to: :orchestrator

#data_as_hashObject



195
196
197
198
199
# File 'app/models/edi_communication_log.rb', line 195

def data_as_hash
  return unless json?

  JSON.parse(data)
end

#data_as_jsonObject



182
183
184
185
186
# File 'app/models/edi_communication_log.rb', line 182

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

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

#data_as_json=(val) ⇒ Object



188
189
190
191
192
193
# File 'app/models/edi_communication_log.rb', line 188

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



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

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

#directionObject



237
238
239
240
241
242
# File 'app/models/edi_communication_log.rb', line 237

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:



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

has_many :edi_documents, dependent: :destroy

#file_info_as_jsonObject



201
202
203
204
205
# File 'app/models/edi_communication_log.rb', line 201

def file_info_as_json
  return if file_info.blank?

  JSON.pretty_generate(file_info)
end

#friendly_categoryObject



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

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)


274
275
276
# File 'app/models/edi_communication_log.rb', line 274

def inbound?
  direction == :inbound
end

#invoicesActiveRecord::Relation<Invoice>

Returns:

  • (ActiveRecord::Relation<Invoice>)

See Also:



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

has_many :invoices, through: :orders

#itemsActiveRecord::Relation<Item>

Returns:

  • (ActiveRecord::Relation<Item>)

See Also:



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

has_many :items, through: :edi_documents

#json?Boolean

Returns:

  • (Boolean)


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

def json?
  data_type == 'json'
end

#md5_match?(filename, data) ⇒ Boolean

Returns:

  • (Boolean)


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

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



216
217
218
# File 'app/models/edi_communication_log.rb', line 216

def md5_signature
  self.class.md5_signature(data)
end

#orchestratorObject



341
342
343
# File 'app/models/edi_communication_log.rb', line 341

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

#ordersActiveRecord::Relation<Order>

Returns:

  • (ActiveRecord::Relation<Order>)

See Also:



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

has_many :orders, through: :edi_documents

#orders_for_packing_slip_matchupObject



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

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)


270
271
272
# File 'app/models/edi_communication_log.rb', line 270

def outbound?
  direction == :outbound
end

#process(force: false) ⇒ Object



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
392
393
394
395
396
397
398
399
400
401
402
403
# File 'app/models/edi_communication_log.rb', line 347

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 :return_batch
    o.returns_report_processor.process(self) if o.respond_to?(:returns_report_processor)
  when :product_data
    o.product_data_sender.call(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



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

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



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

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:



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

has_many :receipts, through: :edi_documents

#statisticsObject



419
420
421
422
# File 'app/models/edi_communication_log.rb', line 419

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

#statistics_product_dataObject



424
425
426
427
# File 'app/models/edi_communication_log.rb', line 424

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

#update_catalog_items_ecl_sentObject



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'app/models/edi_communication_log.rb', line 444

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



461
462
463
464
465
466
467
468
469
470
# File 'app/models/edi_communication_log.rb', line 461

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



429
430
431
432
433
434
435
436
437
438
# File 'app/models/edi_communication_log.rb', line 429

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:



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

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

#validate_md5Object

TODO : hook this in later



208
209
210
211
212
213
214
# File 'app/models/edi_communication_log.rb', line 208

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)


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

def xml?
  data_type == 'xml'
end