31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'app/controllers/api/v1/edi_messages_controller.rb', line 31
def purchase_orders
raw_body = request.raw_post
logger.info "Api::V1::EdiMessagesController#purchase_orders, request.raw_post length: #{raw_body&.length}"
partner = params[:partner]&.to_sym
logger.info "Api::V1::EdiMessagesController#purchase_orders, partner: #{partner}"
if Edi::BaseOrchestrator.partners.select { |k, _v| k == partner }.any?
EdiCommunicationLog.transaction do
json_hash = JSON.parse(raw_body)
data_blob = json_hash.to_json
edi_log = EdiCommunicationLog.new(partner: partner,
category: 'order_batch',
data: data_blob,
data_type: 'json',
transmit_datetime: Time.current)
if @test
logger.info 'Api::V1::EdiMessagesController#purchase_orders, test, setting to auto-archive.'
edi_log.notes = 'Test order_batch from API endpoint, auto-archiving'
edi_log.state = 'archived'
end
if edi_log.save
logger.info "saved 850/PO/order batch to edi log #{edi_log.id}"
render json: { status: 'OK', transation_id: edi_log.id, json: edi_log.data }, status: :ok
else
error = edi_log.errors.full_messages.join(', ')
logger.error "Authenticated EDI partner #{partner} COULD NOT SAVE to edi log! Error: #{error}."
logger.error data_blob
notify_edi_api_failure(partner:, error:, raw_body:, context: 'EdiCommunicationLog validation failed')
render json: {
status: 'Could not parse or successfully process JSON payload.',
message: error
}, status: :bad_request
end
rescue StandardError => e
ErrorReporting.error(e, { message: "Unable to process purchase order EDI message for partner #{partner}!", controller: 'edi_messages', action: 'purchase_orders' })
notify_edi_api_failure(partner:, error: e, raw_body:, context: 'Exception inside transaction block')
render json: {
status: 'Could not parse or successfully process JSON payload.',
message: e.message
}, status: :bad_request
end
else
render json: { status: "Could not find EDI partner '#{partner}'" }, status: :not_found
end
rescue StandardError => e
ErrorReporting.critical(e, { message: "EdiMessagesController.purchase_orders unhandled error", controller: 'edi_messages', action: 'purchase_orders' })
notify_edi_api_failure(partner: params[:partner], error: e, raw_body: (raw_body rescue nil), context: 'Unhandled exception in purchase_orders')
render json: { status: 'Internal error', message: e.message }, status: :internal_server_error unless performed?
end
|