Sha256: cb52e7670e35a0961d79385259071d9c53d8bd3b3e1bc08e0c25dcc1d0427860

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require_relative '../operation'

module LedgerSync
  module Adaptors
    module NetSuite
      module Operation
        class Create
          include NetSuite::Operation::Mixin

          private

          def create_in_ledger
            return LedgerSync::Result.Success(response) if response.success?

            failure(
              Error::OperationError.new(
                operation: self,
                response: response
              )
            )
          end

          def find
            resource.ledger_id = ledger_id

            self.class.operations_module::Find.new(
              adaptor: adaptor,
              resource: resource
            ).perform
          end

          def ledger_id
            @ledger_id ||= response.headers['Location'].split('/').last
          end

          def operate
            create_in_ledger
              .and_then { success }
          end

          def response
            ledger_hash = ledger_serializer.to_ledger_hash
            ledger_hash.delete('entityId')

            @response ||= adaptor.post(
              body: ledger_hash,
              path: ledger_serializer.class.api_resource_path
            )
          end

          def success
            find.and_then do |find_result|
              super(
                resource: find_result.resource,
                response: response
              )
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ledger_sync-1.3.5 lib/ledger_sync/adaptors/netsuite/operation/create.rb
ledger_sync-1.3.4 lib/ledger_sync/adaptors/netsuite/operation/create.rb
ledger_sync-1.3.3 lib/ledger_sync/adaptors/netsuite/operation/create.rb
ledger_sync-1.3.2 lib/ledger_sync/adaptors/netsuite/operation/create.rb
ledger_sync-1.3.1 lib/ledger_sync/adaptors/netsuite/operation/create.rb