<commit_before>module OrderOperations
  module ItemBuilder

    def allowed_to_update?
      # need to check if this token is present in the params
      unloggedin_token = params.try(:[], :order).try(:[], :paymentToken)
      actual_token = model.payment_token

      allowed? || unloggedin_token == actual_token
    end

    # Each Entry looks like this:
    #
    #   {"quantity"=>"1",
    #  "price"=>"45",
    #  "partnerName"=>"",
    #  "danceOrientation"=>"",
    #  "size"=>"",
    #  "lineItem"=>"123",
    #  "order"=>"",
    #  "lineItemId"=>"123",
    #  "lineItemType"=>"OrderLineItem"}
    #
    # This is just straight from ember
    #
    # @param [Array] params_for_items a list of orderLineItem param entries
    def build_items(params_for_items)
      existing_items = params_for_items.select{ |i| i[:id].present? }
      new_items = params_for_items.select{ |i| i[:id].blank? }

      update_items(existing_items)
      create_items(new_items)
    end


    def update_items(items)
      create_or_update(items) do |id, kind, data|
        item = @model.line_items.find(data[:id])
        next unless item

        item.update(
          quantity: data[:quantity],
        )
      end
    end

    def create_items(items)
      create_or_update(items) do |id, kind, data|
        item = OrderLineItem.new(
          line_item_id: id,
          line_item_type: kind,
          price: data[:price],
          quantity: data[:quantity],
          order: @model
        )

        if item.valid?
          @model.line_items << item
        else
          @model.errors.add(:base, item.errors.full_messages.to_s)
        end
      end
    end

    def create_or_update(items, &block)
      items.each do |item_data|
        id = item_data[:lineItemId]
        kind = item_data[:lineItemType]

        non_line_items = [MembershipDiscount.name, Package.name, Competition.name]
        if !non_line_items.include?(kind)
          kind = kind.include?("LineItem") ? kind : "LineItem::#{kind}"
        end

        yield(id, kind, item_data)
      end<commit_msg>added properties to save on order line items<commit_after>module OrderOperations
  module ItemBuilder

    def allowed_to_update?
      # need to check if this token is present in the params
      unloggedin_token = params.try(:[], :order).try(:[], :paymentToken)
      actual_token = model.payment_token

      allowed? || unloggedin_token == actual_token
    end

    # Each Entry looks like this:
    #
    #   {"quantity"=>"1",
    #  "price"=>"45",
    #  "partnerName"=>"",
    #  "danceOrientation"=>"",
    #  "size"=>"",
    #  "lineItem"=>"123",
    #  "order"=>"",
    #  "lineItemId"=>"123",
    #  "lineItemType"=>"OrderLineItem"}
    #
    # This is just straight from ember
    #
    # @param [Array] params_for_items a list of orderLineItem param entries
    def build_items(params_for_items)
      existing_items = params_for_items.select{ |i| i[:id].present? }
      new_items = params_for_items.select{ |i| i[:id].blank? }

      update_items(existing_items)
      create_items(new_items)
    end


    def update_items(items)
      create_or_update(items) do |id, kind, data|
        item = @model.line_items.find(data[:id])
        next unless item

        item.update(
          quantity: data[:quantity],
        )
      end
    end

    def create_items(items)
      create_or_update(items) do |id, kind, data|
        item = OrderLineItem.new(
          line_item_id: id,
          line_item_type: kind,
          price: data[:price],
          quantity: data[:quantity],
          partner_name: data[:partnerName],
          dance_orientation: data[:danceOrientation],
          size: data[:size],
          order: @model
        )

        if item.valid?
          @model.line_items << item
        else
          @model.errors.add(:base, item.errors.full_messages.to_s)
        end
      end
    end

    def create_or_update(items, &block)
      items.each do |item_data|
        id = item_data[:lineItemId]
        kind = item_data[:lineItemType]

        non_line_items = [MembershipDiscount.name, Package.name, Competition.name]
        if !non_line_items.include?(kind)
          kind = kind.include?("LineItem") ? kind : "LineItem::#{kind}"
        end

        yield(id, kind, item_data)
      end
{'fraction_non_alphanumeric': 0.12279480037140204, 'fraction_numerical': 0.004178272980501393, 'mean_word_length': 2.3982649842271293, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, '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': '4255983', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1379, 'n_words': 292}