<commit_before>module QBIntegration
  class ReturnAuthorization < Base
    attr_reader :order, :ra

    def initialize(message = {}, config)
      super
      @ra = payload[:return_authorization]
      @order = payload[:return_authorization][:order]

      # for compatibility with sales receipt service
      payload[:order] = @order
    end

    def create
      if sales_receipt = sales_receipt_service.find_by_order_number
        credit_memo = credit_memo_service.create_from_return ra, sales_receipt
        text = "Created Quickbooks Credit Memo #{credit_memo.id} for return #{ra[:number]}"
        [200, text]
      else
        [500, "Received return for order not sync with quickbooks"]
      end
    end

    def update
      # TODO should be able to remove this first check when recreating
      # vcr cassetes
      if sales_receipt = sales_receipt_service.find_by_order_number

        if credit_memo = credit_memo_service.find_by_number(ra[:number])
          credit_memo_service.update credit_memo, ra, sales_receipt
          text = "Updated Quickbooks Credit Memo #{credit_memo.id} for return #{ra[:number]}"
          [200, text]
        else
          credit_memo = credit_memo_service.create_from_return ra, sales_receipt
          text = "Created Quickbooks Credit Memo #{credit_memo.id} for return #{ra[:number]}"
          [200, text]
        end
      else
        [500, "Received return for order not sync with quickbooks"]
      end
    end

    def sync
      if sales_receipt = sales_receipt_service.find_by_order_number
        case message_name
        when "return_authorization:new"
          credit_memo = credit_memo_service.create_from_return ra, sales_receipt
          text = "Created Quickbooks Credit Memo #{credit_memo.id} for return #{ra[:number]}"
          [200, notification(text)]
        when "return_authorization:updated"
          if credit_memo = credit_memo_service.find_by_number(ra[:number])
            credit_memo_service.update credit_memo, ra, sales_receipt
            text = "Updated Quickbooks Credit Memo #{credit_memo.id} for return #{ra[:number]}"
            [200, notification(text)]
          else
            credit_memo = credit_memo_service.create_from_return ra, sales_receipt
            text = "Created Quickbooks Credit Memo #{credit_memo.id} for return #{ra[:number]}"
            [200, notification(text)]
          end
        else
          [500, notification("Cand handle message #{message_name}", "error")]
        end
      else
        [500, notification("Received return for order not sync with quickbooks", "error")]
      end
    end

    def sales_receipt_service
      Service::SalesReceipt.new(config, payload, dependencies: false)
    end

    def notification(text, level = 'info')
      { 'message_id' => message_id,
        'notifications' => [
          {
            'level' => level,
            'subject' => text,
            'description' => text
          }
        ]
      }.with_indifferent_access
    end
  end
end<commit_msg>Drop deprecated message stuff in ReturnAuthorization<commit_after>module QBIntegration
  class ReturnAuthorization < Base
    attr_reader :order, :ra

    def initialize(message = {}, config)
      super
      @ra = payload[:return_authorization]
      @order = payload[:return_authorization][:order]

      # for compatibility with sales receipt service
      payload[:order] = @order
    end

    def create
      if sales_receipt = sales_receipt_service.find_by_order_number
        credit_memo = credit_memo_service.create_from_return ra, sales_receipt
        text = "Created Quickbooks Credit Memo #{credit_memo.id} for return #{ra[:number]}"
        [200, text]
      else
        [500, "Received return for order not sync with quickbooks"]
      end
    end

    def update
      # TODO should be able to remove this first check when recreating
      # vcr cassetes
      if sales_receipt = sales_receipt_service.find_by_order_number

        if credit_memo = credit_memo_service.find_by_number(ra[:number])
          credit_memo_service.update credit_memo, ra, sales_receipt
          text = "Updated Quickbooks Credit Memo #{credit_memo.id} for return #{ra[:number]}"
          [200, text]
        else
          credit_memo = credit_memo_service.create_from_return ra, sales_receipt
          text = "Created Quickbooks Credit Memo #{credit_memo.id} for return #{ra[:number]}"
          [200, text]
        end
      else
        [500, "Received return for order not sync with quickbooks"]
      end
    end

    def sales_receipt_service
      Service::SalesReceipt.new(config, payload, dependencies: false)
    end
  end
end
{'fraction_non_alphanumeric': 0.08083190394511149, 'fraction_numerical': 0.009648370497427101, 'mean_word_length': 2.696513470681458, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5778676', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1313, 'n_words': 363}