-
-
Notifications
You must be signed in to change notification settings - Fork 277
Support Attaching Files #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| module NetSuite | ||
| module Actions | ||
| class AttachFile | ||
| include Support::Requests | ||
|
|
||
| def initialize(object, file) | ||
| @object = object | ||
| @file = file | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def request(credentials = {}) | ||
| NetSuite::Configuration.connection({}, credentials).call(:attach, :message => request_body) | ||
| end | ||
|
|
||
| # <soap:Body> | ||
| # <platformMsgs:attach> | ||
| # <platformCore:attachReference xsi:type="platformCore:AttachContactReference"> | ||
| # <platformCore::attachTo internalId="176" type="customer" xsi:type="platformCore::RecordRef"> | ||
| # </platformCore:attachTo> | ||
| # <platformCore:attachRecord internalId="1467" type="file" xsi:type="platformCore:RecordRef"/> | ||
| # </platformCore:attachReference> | ||
| # </platformMsgs:attach> | ||
| # </soap:Body> | ||
|
|
||
| def request_body | ||
| { | ||
| 'platformCore:attachReference' => { | ||
| '@xsi:type' => 'platformCore:AttachBasicReference', | ||
| 'platformCore:attachTo' => { | ||
| '@internalId' => @object.internal_id, | ||
| '@type' => @object.type, | ||
| '@xsi:type' => 'platformCore:RecordRef' | ||
| }, | ||
| 'platformCore:attachedRecord' => { | ||
| '@internalId' => @file.internal_id, | ||
| '@type' => 'file', | ||
| '@xsi:type' => 'platformCore:RecordRef' | ||
| } | ||
| } | ||
| } | ||
| end | ||
|
|
||
| def success? | ||
| @success ||= response_hash[:status][:@is_success] == 'true' | ||
| end | ||
|
|
||
| def response_body | ||
| @response_body ||= response_hash[:base_ref] | ||
| end | ||
|
|
||
| def response_errors | ||
| if response_hash[:status] && response_hash[:status][:status_detail] | ||
| @response_errors ||= errors | ||
| end | ||
| end | ||
|
|
||
| def response_hash | ||
| @response_hash ||= @response.to_hash[:attach_response][:write_response] | ||
| end | ||
|
|
||
| def errors | ||
| error_obj = response_hash[:status][:status_detail] | ||
| error_obj = [error_obj] if error_obj.class == Hash | ||
| error_obj.map do |error| | ||
| NetSuite::Error.new(error) | ||
| end | ||
| end | ||
|
|
||
| module Support | ||
| def attach_file(file, credentials = {}) | ||
| response = NetSuite::Actions::AttachFile.call([self, file], credentials) | ||
|
|
||
| @errors = response.errors | ||
|
|
||
| if response.success? | ||
| @internal_id = response.body[:@internal_id] | ||
| true | ||
| else | ||
| false | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,15 @@ def to_attributes!(hash, kname, v) | |
| end | ||
|
|
||
| def record_type | ||
| "#{record_namespace}:#{self.class.to_s.split('::').last}" | ||
| "#{record_namespace}:#{record_type_without_namespace}" | ||
| end | ||
|
|
||
| def type | ||
| record_type_without_namespace.downcase | ||
| end | ||
|
|
||
| def record_type_without_namespace | ||
| "#{self.class.to_s.split('::').last}" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Yes!! I've been wanting to add this for a while. Thanks! |
||
| end | ||
|
|
||
| def refresh(credentials = {}) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe NetSuite::Actions::AttachFile do | ||
| before(:all) { savon.mock! } | ||
| after(:all) { savon.unmock! } | ||
|
|
||
| let(:invoice) { NetSuite::Records::Invoice.new(internal_id: 999) } | ||
| let(:file) { double('file', internal_id: 111) } | ||
| let(:message) do | ||
| { | ||
| 'platformCore:attachReference' => { | ||
| '@xsi:type' => 'platformCore:AttachBasicReference', | ||
| 'platformCore:attachTo' => { | ||
| '@internalId' => invoice.internal_id, | ||
| '@type' => invoice.type, | ||
| '@xsi:type' => 'platformCore:RecordRef' | ||
| }, | ||
| 'platformCore:attachedRecord' => { | ||
| '@internalId' => file.internal_id, | ||
| '@type' => 'file', | ||
| '@xsi:type' => 'platformCore:RecordRef' | ||
| } | ||
| } | ||
| } | ||
| end | ||
|
|
||
| before do | ||
| savon.expects(:attach).with(:message => message).returns(envelope) | ||
| end | ||
|
|
||
| context 'when successful' do | ||
| let(:envelope) { File.read('spec/support/fixtures/attach/attach_file_to_invoice.xml') } | ||
|
|
||
| it 'returns a valid Response object' do | ||
| response = NetSuite::Actions::AttachFile.call([invoice, file]) | ||
| expect(response).to be_kind_of(NetSuite::Response) | ||
| expect(response).to be_success | ||
| end | ||
| end | ||
|
|
||
| context 'when not successful' do | ||
| let(:envelope) { File.read('spec/support/fixtures/attach/attach_file_to_invoice_error.xml') } | ||
|
|
||
| it 'provides an error method on the object with details about the error' do | ||
| invoice.attach_file(file) | ||
| error = invoice.errors.first | ||
|
|
||
| expect(error).to be_kind_of(NetSuite::Error) | ||
| expect(error.type).to eq('ERROR') | ||
| expect(error.code).to eq('INVALID') | ||
| expect(error.message).to eq('Invalid request.') | ||
| end | ||
|
|
||
| it 'provides an error method on the response' do | ||
| response = NetSuite::Actions::AttachFile.call([invoice, file]) | ||
| expect(response.errors.first).to be_kind_of(NetSuite::Error) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe NetSuite::Records::File do | ||
| let(:file) { NetSuite::Records::File.new } | ||
|
|
||
| it 'has all the right record refs' do | ||
| [:folder, :klass].each do |record_ref| | ||
| expect(file).to have_record_ref(record_ref) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
| <soapenv:Header> | ||
| <platformMsgs:documentInfo xmlns:platformMsgs="urn:messages_2011_2.platform.webservices.netsuite.com"> | ||
| <platformMsgs:nsId>WEBSERVICES_3392464_1220201115821392011296470399_67055c545d0</platformMsgs:nsId> | ||
| </platformMsgs:documentInfo> | ||
| </soapenv:Header> | ||
| <soapenv:Body> | ||
| <attachResponse xmlns="urn:messages_2_5.platform.webservices.netsuite.com"> | ||
| <writeResponse xmlns="urn:messages_2_5.platform.webservices.netsuite.com"> | ||
| <ns1:status isSuccess="true" xmlns:ns1="urn:core_2_5.platform.webservices.netsuite.com"/> | ||
| <baseRef internalId="999" type="invoice" xsi:type="ns2:RecordRef" xmlns:ns2="urn:core_2_5.platform.webservices.netsuite.com"/> | ||
| </writeResponse> | ||
| </attachResponse> | ||
| </soapenv:Body> | ||
| </soapenv:Envelope> |
20 changes: 20 additions & 0 deletions
20
spec/support/fixtures/attach/attach_file_to_invoice_error.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
| <soapenv:Header> | ||
| <platformMsgs:documentInfo xmlns:platformMsgs="urn:messages_2011_2.platform.webservices.netsuite.com"> | ||
| <platformMsgs:nsId>WEBSERVICES_3392464_1220201115821392011296470399_67055c545d0</platformMsgs:nsId> | ||
| </platformMsgs:documentInfo> | ||
| </soapenv:Header> | ||
| <soapenv:Body> | ||
| <attachResponse xmlns="urn:messages_2_5.platform.webservices.netsuite.com"> | ||
| <writeResponse xmlns="urn:messages_2_5.platform.webservices.netsuite.com"> | ||
| <platformCore:status xmlns:platformCore="urn:core_2011_2.platform.webservices.netsuite.com" isSuccess="false"> | ||
| <platformCore:statusDetail type="ERROR"> | ||
| <platformCore:code>INVALID</platformCore:code> | ||
| <platformCore:message>Invalid request.</platformCore:message> | ||
| </platformCore:statusDetail> | ||
| </platformCore:status> | ||
| </writeResponse> | ||
| </attachResponse> | ||
| </soapenv:Body> | ||
| </soapenv:Envelope> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jdedels I'm not too familiar with the
attach_filecall, but I believe types should be lower camel case, i.e.customer,salesOrder,transferOrder, etc. Do you know ifattach_fileuses a different type case or did you just test this with single-word objects?Thanks again for your help here! Sorry about the delay in taking a look at this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, it should be lower camel case. I just tested with a sales order and got the following error:
Changing the
downcasecall tolower_camelcasefixed the issue.Separately, #483 broke this because it introduces a
typesearch-only field on invoices, so when the action callstypeto fill out the XML request body, it calls the search-only field, returningnil(unless your record instance happens to be the result of a search), rather than calling this helper.Broadly I'd guess this is a problem with any instance methods defined on a record colliding with fields on that record. Maybe
netsuite_typeis better and less chance for collision? Then mayberecord_typeshould be renamed tonetsuite_namespaced_typefor consistency and clarity?