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]
Models::Auditable::ALWAYS_IGNORED
Instance Attribute Summary collapse
Delegated Instance Attributes
collapse
Class Method Summary
collapse
-
.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
#publish_event
Instance Attribute Details
#category ⇒ Object
48
|
# File 'app/models/edi_communication_log.rb', line 48
validates :category, :partner, presence: true
|
#partner ⇒ Object
48
|
# File 'app/models/edi_communication_log.rb', line 48
validates :category, :partner, presence: true
|
Class Method Details
.categories_for_select ⇒ Object
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] } 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)
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_select ⇒ Object
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_acknowledge: 'PR',
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_select ⇒ Object
146
147
148
|
# File 'app/models/edi_communication_log.rb', line 146
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
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_select ⇒ Object
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
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_items ⇒ ActiveRecord::Relation<CatalogItem>
55
|
# File 'app/models/edi_communication_log.rb', line 55
has_many :catalog_items, through: :edi_documents
|
#confirm_outbound_processing? ⇒ Boolean
426
427
428
|
# File 'app/models/edi_communication_log.rb', line 426
def confirm_outbound_processing?
outbound? && orchestrator.confirm_outbound_processing?
end
|
#customers ⇒ Object
Alias for Orchestrator#customers
333
|
# File 'app/models/edi_communication_log.rb', line 333
delegate :customers, to: :orchestrator
|
#data_as_hash ⇒ Object
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_json ⇒ Object
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_dup ⇒ Object
63
64
65
|
# File 'app/models/edi_communication_log.rb', line 63
def deep_dup
deep_clone(include: [:edi_documents, :uploads])
end
|
#direction ⇒ Object
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_documents ⇒ ActiveRecord::Relation<EdiDocument>
53
|
# File 'app/models/edi_communication_log.rb', line 53
has_many :edi_documents, dependent: :destroy
|
#file_info_as_json ⇒ Object
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_category ⇒ Object
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
262
263
264
|
# File 'app/models/edi_communication_log.rb', line 262
def inbound?
direction == :inbound
end
|
#invoices ⇒ ActiveRecord::Relation<Invoice>
59
|
# File 'app/models/edi_communication_log.rb', line 59
has_many :invoices, through: :orders
|
#items ⇒ ActiveRecord::Relation<Item>
56
|
# File 'app/models/edi_communication_log.rb', line 56
has_many :items, through: :edi_documents
|
#json? ⇒ Boolean
166
167
168
|
# File 'app/models/edi_communication_log.rb', line 166
def json?
data_type == 'json'
end
|
#md5_match?(filename, data) ⇒ 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_signature ⇒ Object
204
205
206
|
# File 'app/models/edi_communication_log.rb', line 204
def md5_signature
self.class.md5_signature(data)
end
|
#orchestrator ⇒ Object
329
330
331
|
# File 'app/models/edi_communication_log.rb', line 329
def orchestrator
Edi::BaseOrchestrator.build(partner)
end
|
#orders ⇒ ActiveRecord::Relation<Order>
57
|
# File 'app/models/edi_communication_log.rb', line 57
has_many :orders, through: :edi_documents
|
#orders_for_packing_slip_matchup ⇒ Object
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
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? 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
398
399
400
401
402
403
|
# File 'app/models/edi_communication_log.rb', line 398
def process_buy_box_status
catalog_items.each do |catalog_item|
orchestrator.pull_buy_box_status(catalog_item)
end
end
|
391
392
393
394
395
396
|
# File 'app/models/edi_communication_log.rb', line 391
def process_catalog_item_information
catalog_items.each do |catalog_item|
orchestrator.pull_catalog_information(catalog_item)
end
end
|
#receipts ⇒ ActiveRecord::Relation<Receipt>
58
|
# File 'app/models/edi_communication_log.rb', line 58
has_many :receipts, through: :edi_documents
|
#statistics ⇒ Object
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_data ⇒ Object
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_sent ⇒ Object
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
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}")]
catalog_items.update_all(CatalogItem.sanitize_sql_for_assignment(sql))
end
|
#update_invoices_transmission_state ⇒ Object
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_status ⇒ Object
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
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>
54
|
# File 'app/models/edi_communication_log.rb', line 54
has_many :uploads, as: :resource, dependent: :destroy
|
#validate_md5 ⇒ Object
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
162
163
164
|
# File 'app/models/edi_communication_log.rb', line 162
def xml?
data_type == 'xml'
end
|