Class: EdiCommunicationLog
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].freeze
Models::Auditable::ALWAYS_IGNORED
Constants included
from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
Delegated Instance Attributes
collapse
Class Method Summary
collapse
-
.active_first ⇒ ActiveRecord::Relation<EdiCommunicationLog>
A relation of EdiCommunicationLogs that are active first.
-
.categories_for_select ⇒ Object
-
.cleanup_xml(xml) ⇒ Object
-
.create_outbound_file_from_data(data:, file_extension:, partner:, category:, file_name: nil, resources: nil, data_type: 'csv', file_info: {}, transmit_after: nil) ⇒ Object
-
.data_type_for_select ⇒ Object
-
.file_extension_for_category(category, partner) ⇒ Object
-
.generate_file_name_from_data(data, file_extension, partner, category) ⇒ Object
-
.get_file_prefix_from(partner, category) ⇒ Object
-
.md5_signature(data) ⇒ Object
-
.partners_for_select ⇒ Object
-
.requiring_processing ⇒ ActiveRecord::Relation<EdiCommunicationLog>
A relation of EdiCommunicationLogs that are requiring processing.
-
.states_for_select ⇒ Object
Instance Method Summary
collapse
#all_skipped_columns, #audit_reference_data, #creator, #should_not_save_version, #stamp_record, #updater
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
config
#after_commit
#publish_event
Instance Attribute Details
#category ⇒ Object
50
|
# File 'app/models/edi_communication_log.rb', line 50
validates :category, :partner, presence: true
|
#partner ⇒ Object
50
|
# File 'app/models/edi_communication_log.rb', line 50
validates :category, :partner, presence: true
|
Class Method Details
.active_first ⇒ ActiveRecord::Relation<EdiCommunicationLog>
A relation of EdiCommunicationLogs that are active first. Active Record Scope
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_select ⇒ Object
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] } 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)
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_select ⇒ Object
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_acknowledge: 'PR',
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_select ⇒ Object
148
149
150
|
# File 'app/models/edi_communication_log.rb', line 148
def self.partners_for_select
Edi::BaseOrchestrator.partners.keys.sort
end
|
.requiring_processing ⇒ ActiveRecord::Relation<EdiCommunicationLog>
A relation of EdiCommunicationLogs that are requiring processing. Active Record Scope
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_select ⇒ Object
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
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_items ⇒ ActiveRecord::Relation<CatalogItem>
57
|
# File 'app/models/edi_communication_log.rb', line 57
has_many :catalog_items, through: :edi_documents
|
#confirm_outbound_processing? ⇒ Boolean
428
429
430
|
# File 'app/models/edi_communication_log.rb', line 428
def confirm_outbound_processing?
outbound? && orchestrator.confirm_outbound_processing?
end
|
#customers ⇒ Object
Alias for Orchestrator#customers
335
|
# File 'app/models/edi_communication_log.rb', line 335
delegate :customers, to: :orchestrator
|
#data_as_hash ⇒ Object
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_json ⇒ Object
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_dup ⇒ Object
65
66
67
|
# File 'app/models/edi_communication_log.rb', line 65
def deep_dup
deep_clone(include: %i[edi_documents uploads])
end
|
#direction ⇒ Object
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_documents ⇒ ActiveRecord::Relation<EdiDocument>
55
|
# File 'app/models/edi_communication_log.rb', line 55
has_many :edi_documents, dependent: :destroy
|
#file_info_as_json ⇒ Object
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_category ⇒ Object
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
264
265
266
|
# File 'app/models/edi_communication_log.rb', line 264
def inbound?
direction == :inbound
end
|
#invoices ⇒ ActiveRecord::Relation<Invoice>
61
|
# File 'app/models/edi_communication_log.rb', line 61
has_many :invoices, through: :orders
|
#items ⇒ ActiveRecord::Relation<Item>
58
|
# File 'app/models/edi_communication_log.rb', line 58
has_many :items, through: :edi_documents
|
#json? ⇒ Boolean
168
169
170
|
# File 'app/models/edi_communication_log.rb', line 168
def json?
data_type == 'json'
end
|
#md5_match?(filename, data) ⇒ 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_signature ⇒ Object
206
207
208
|
# File 'app/models/edi_communication_log.rb', line 206
def md5_signature
self.class.md5_signature(data)
end
|
#orchestrator ⇒ Object
331
332
333
|
# File 'app/models/edi_communication_log.rb', line 331
def orchestrator
Edi::BaseOrchestrator.build(partner)
end
|
#orders ⇒ ActiveRecord::Relation<Order>
59
|
# File 'app/models/edi_communication_log.rb', line 59
has_many :orders, through: :edi_documents
|
#orders_for_packing_slip_matchup ⇒ Object
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
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? 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_status ⇒ Object
400
401
402
403
404
405
|
# File 'app/models/edi_communication_log.rb', line 400
def process_buy_box_status
catalog_items.each do |catalog_item|
orchestrator.pull_buy_box_status(catalog_item)
end
end
|
393
394
395
396
397
398
|
# File 'app/models/edi_communication_log.rb', line 393
def process_catalog_item_information
catalog_items.each do |catalog_item|
orchestrator.pull_catalog_information(catalog_item)
end
end
|
#receipts ⇒ ActiveRecord::Relation<Receipt>
60
|
# File 'app/models/edi_communication_log.rb', line 60
has_many :receipts, through: :edi_documents
|
#statistics ⇒ Object
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_data ⇒ Object
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_sent ⇒ Object
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
message_sent_json_column_name = "last_#{category}_sent_json"
return unless CatalogItem.method_defined?(message_sent_json_column_name)
sql = ["#{message_sent_json_column_name} = jsonb_set(#{message_sent_json_column_name}, '{ #{partner} }', ?)", %("#{Time.current}")]
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_state ⇒ Object
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_status ⇒ Object
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)
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
|
#uploads ⇒ ActiveRecord::Relation<Upload>
56
|
# File 'app/models/edi_communication_log.rb', line 56
has_many :uploads, as: :resource, dependent: :destroy
|
#validate_md5 ⇒ Object
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
164
165
166
|
# File 'app/models/edi_communication_log.rb', line 164
def xml?
data_type == 'xml'
end
|