<commit_before>_ = require('underscore')._
xmlHelpers = require '../lib/xmlhelpers'
InventoryUpdater = require('sphere-node-sync').InventoryUpdater
Q = require 'q'

class StockXmlImport extends InventoryUpdater
  constructor: (options) ->
    super(options)

  elasticio: (msg, cfg, cb, snapshot) ->
    console.log 'elasticio'
    if msg.attachments
      console.log 'elasticio - attachments'
      for attachment of msg.attachments
        continue if not attachment.match /xml$/i
        content = msg.attachments[attachment].content
        continue if not content
        xmlString = new Buffer(content, 'base64').toString()
        @run xmlString, cb
    else if msg.body
      console.log 'elasticio - body'
      # TODO: As we get only one entry here, we should query for the existing one and not
      # get the whole inventory
      @initMatcher().then () =>
        console.log 'elasticio - initMatcher'
        @createOrUpdate([@createInventoryEntry(msg.body.SKU, msg.body.QUANTITY)], cb)
      .fail (msg) =>
        console.log 'elasticio - initMatcher - fail'
        @returnResult false, msg, cb
    else
      console.log 'elasticio - nothing'
      @returnResult false, 'No data found in elastic.io msg.', cb

  run: (xmlString, callback) ->
    throw new Error 'String required' unless _.isString xmlString
    throw new Error 'Callback must be a function' unless _.isFunction callback

    xmlHelpers.xmlTransform xmlHelpers.xmlFix(xmlString), (err, result) =>
      if err
        @returnResult false, 'Error on parsing XML: ' + err, callback
      else
        @ensureChannelByKey(@rest, 'expectedStock').then (channel) =>
          stocks = @mapStock result.root, channel.id
          @initMatcher().then () =>
            @createOrUpdate stocks, callback
          .fail (msg) =>
            @returnResult false, msg, callback<commit_msg>Fix elastic.io checks on passed message.<commit_after>_ = require('underscore')._
xmlHelpers = require '../lib/xmlhelpers'
InventoryUpdater = require('sphere-node-sync').InventoryUpdater
Q = require 'q'

class StockXmlImport extends InventoryUpdater
  constructor: (options) ->
    super(options)

  elasticio: (msg, cfg, cb, snapshot) ->
    console.log 'elasticio'
    if _.size(msg.attachments) > 0
      console.log 'elasticio - attachments'
      for attachment of msg.attachments
        continue if not attachment.match /xml$/i
        content = msg.attachments[attachment].content
        continue if not content
        xmlString = new Buffer(content, 'base64').toString()
        @run xmlString, cb
    else if _.size(msg.body) > 0
      console.log 'elasticio - body'
      # TODO: As we get only one entry here, we should query for the existing one and not
      # get the whole inventory
      @initMatcher().then () =>
        console.log 'elasticio - initMatcher'
        @createOrUpdate([@createInventoryEntry(msg.body.SKU, msg.body.QUANTITY)], cb)
      .fail (msg) =>
        console.log 'elasticio - initMatcher - fail'
        @returnResult false, msg, cb
    else
      console.log 'elasticio - nothing'
      @returnResult false, 'No data found in elastic.io msg.', cb

  run: (xmlString, callback) ->
    throw new Error 'String required' unless _.isString xmlString
    throw new Error 'Callback must be a function' unless _.isFunction callback

    xmlHelpers.xmlTransform xmlHelpers.xmlFix(xmlString), (err, result) =>
      if err
        @returnResult false, 'Error on parsing XML: ' + err, callback
      else
        @ensureChannelByKey(@rest, 'expectedStock').then (channel) =>
          stocks = @mapStock result.root, channel.id
          @initMatcher().then () =>
            @createOrUpdate stocks, callback
          .fail (msg) =>
            @returnResult false, msg, callback
{'fraction_non_alphanumeric': 0.10537463595446121, 'fraction_numerical': 0.0015885623510722795, 'mean_word_length': 2.816161616161616, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, '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': '906600', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1039, 'n_words': 330}