Class: EdiCommunicationLog

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable, OrderQuery
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 =
%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 =
%w[xml json]

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

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::EventPublishable

#publish_event

Instance Attribute Details

#categoryObject (readonly)



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

validates :category, :partner, presence: true

#partnerObject (readonly)



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

validates :category, :partner, presence: true

Class Method Details

.categories_for_selectObject



150
151
152
# File 'app/models/edi_communication_log.rb', line 150

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



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

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



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

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



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

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

.file_extension_for_category(category, partner) ⇒ Object



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

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



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

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



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

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



208
209
210
# File 'app/models/edi_communication_log.rb', line 208

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

.partners_for_selectObject



146
147
148
# File 'app/models/edi_communication_log.rb', line 146

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:



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

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



154
155
156
# File 'app/models/edi_communication_log.rb', line 154

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)


212
213
214
# File 'app/models/edi_communication_log.rb', line 212

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

#catalog_itemsActiveRecord::Relation<CatalogItem>

Returns:

See Also:



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

has_many :catalog_items, through: :edi_documents

#confirm_outbound_processing?Boolean

Returns:

  • (Boolean)


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

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

#customersObject

Alias for Orchestrator#customers

Returns:

  • (Object)

    Orchestrator#customers

See Also:



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

delegate :customers, to: :orchestrator

#data_as_hashObject



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

def data_as_hash
  return unless json?

  JSON.parse(data)
end

#data_as_jsonObject



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

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

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

#data_as_json=(val) ⇒ Object



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

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



63
64
65
# File 'app/models/edi_communication_log.rb', line 63

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

#directionObject



225
226
227
228
229
230
# File 'app/models/edi_communication_log.rb', line 225

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:



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

has_many :edi_documents, dependent: :destroy

#file_info_as_jsonObject



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

def file_info_as_json
  return unless file_info.present?

  JSON.pretty_generate(file_info)
end

#friendly_categoryObject



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

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

#inbound?Boolean

Returns:

  • (Boolean)


262
263
264
# File 'app/models/edi_communication_log.rb', line 262

def inbound?
  direction == :inbound
end

#invoicesActiveRecord::Relation<Invoice>

Returns:

  • (ActiveRecord::Relation<Invoice>)

See Also:



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

has_many :invoices, through: :orders

#itemsActiveRecord::Relation<Item>

Returns:

  • (ActiveRecord::Relation<Item>)

See Also:



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

has_many :items, through: :edi_documents

#json?Boolean

Returns:

  • (Boolean)


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

def json?
  data_type == 'json'
end

#md5_match?(filename, data) ⇒ Boolean

Returns:

  • (Boolean)


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

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



204
205
206
# File 'app/models/edi_communication_log.rb', line 204

def md5_signature
  self.class.md5_signature(data)
end

#orchestratorObject



329
330
331
# File 'app/models/edi_communication_log.rb', line 329

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

#ordersActiveRecord::Relation<Order>

Returns:

  • (ActiveRecord::Relation<Order>)

See Also:



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

has_many :orders, through: :edi_documents

#orders_for_packing_slip_matchupObject



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

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)


258
259
260
# File 'app/models/edi_communication_log.rb', line 258

def outbound?
  direction == :outbound
end

#process(force: false) ⇒ Object



335
336
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
# File 'app/models/edi_communication_log.rb', line 335

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



398
399
400
401
402
403
# File 'app/models/edi_communication_log.rb', line 398

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



391
392
393
394
395
396
# File 'app/models/edi_communication_log.rb', line 391

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:



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

has_many :receipts, through: :edi_documents

#statisticsObject



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

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

#statistics_product_dataObject



410
411
412
413
# File 'app/models/edi_communication_log.rb', line 410

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

#update_catalog_items_ecl_sentObject



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

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}")]
  # we update all catalog items linked to this EDI Communication Log with the same Date/Time i.e. now
  catalog_items.update_all(CatalogItem.sanitize_sql_for_assignment(sql))
end

#update_invoices_transmission_stateObject



442
443
444
445
446
447
448
449
450
451
# File 'app/models/edi_communication_log.rb', line 442

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, "Error updating invoice #{invoice.id} transmission state to transmitted", { edi_communication_log_id: id, invoice_id: invoice.id })
  end
end

#update_statusObject



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

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:



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

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

#validate_md5Object

TODO : hook this in later



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

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)


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

def xml?
  data_type == 'xml'
end