<commit_before>debug = require('debug')('sphere-product-sync-import')
_ = require 'underscore'
_.mixin require('underscore-mixins')
Promise = require 'bluebird'
{SphereClient, ProductSync} = require 'sphere-node-sdk'

class ProductImport

  constructor: (@logger, options = {}) ->
    @sync = new ProductSync
    @client = new SphereClient options

  _resetSummary: ->
    @_summary =
      emptySKU: 0
      created: 0
      updated: 0


  performStream: (chunk, cb) ->
    @_processBatches(chunk).then -> cb()

  _processBatches: (products) ->
    batchedList = _.batchList(products, 30) # max parallel elem to process
    Promise.map batchedList, (productsToProcess) =>
      debug 'Chunk: %j', productsToProcess
      # extract all skus from master variant and variants of all jsons in the batch

      debug 'Chunk (unique products): %j', uniqueProductsToProcessBySku

      skus = _.map uniqueProductsToProcessBySku, (s) =>
        @_summary.emptySKU++ if _.isEmpty s.sku
        # TODO: query also for channel?
        "\"#{s.sku}\""
      predicate = "sku in (#{skus.join(', ')})"
      # masterVariant(sku="M0E20000000E30L") or variants(sku="M0E20000000E30L")
      # masterVariant(sku in ("B3-717597", "B3-717487")) or variants(sku in ("B3-717597", "B3-717487"))
      @client.products
      .where(predicate)
      .fetch()
      .then (results) =>
        debug 'Fetched products: %j', results
        queriedEntries = results.body.results
        @_createOrUpdate productsToProcess, queriedEntries
      .then (results) =>
        _.each results, (r) =>
          switch r.statusCode
            when 201 then @_summary.created++
            when 200 then @_summary.updated++
        Promise.resolve()
    ,{concurrency: 1} # run 1 batch at a time


  _prepareProductFetchBySkuQueryPredicate: (skus) ->
    predicate = {}
    skuString = "sku in (\"#{skus.join('", "')}\")"
    predicate.predicateString = "masterVariant(#{skuString}) or variants(#{skuString})"
    predicate.byteSize = Buffer.byteLength(predicate.predicateString,'utf-8')
    return predicate
<commit_msg>MER-41: method  call updates.<commit_after>debug = require('debug')('sphere-product-sync-import')
_ = require 'underscore'
_.mixin require('underscore-mixins')
Promise = require 'bluebird'
{SphereClient, ProductSync} = require 'sphere-node-sdk'

class ProductImport

  constructor: (@logger, options = {}) ->
    @sync = new ProductSync
    @client = new SphereClient options

  _resetSummary: ->
    @_summary =
      emptySKU: 0
      created: 0
      updated: 0


  performStream: (chunk, cb) ->
    @_processBatches(chunk).then -> cb()

  _processBatches: (products) ->
    batchedList = _.batchList(products, 30) # max parallel elem to process
    Promise.map batchedList, (productsToProcess) =>
      # extract all skus from master variant and variants of all jsons in the batch
      skus = @_extractUniqueSkus(productsToProcess)
      predicate = @_prepareProductFetchBySkuQueryPredicate(skus)
      # Fetch products from product projections end point by list of skus.
      @client.productProjections
      .where(predicate)
      .fetch()
      .then (results) =>
        debug 'Fetched products: %j', results
        queriedEntries = results.body.results
        @_createOrUpdate productsToProcess, queriedEntries
      .then (results) =>
        _.each results, (r) =>
          switch r.statusCode
            when 201 then @_summary.created++
            when 200 then @_summary.updated++
        Promise.resolve()
    ,{concurrency: 1} # run 1 batch at a time


  _prepareProductFetchBySkuQueryPredicate: (skus) ->
    predicate = {}
    skuString = "sku in (\"#{skus.join('", "')}\")"
    predicate.predicateString = "masterVariant(#{skuString}) or variants(#{skuString})"
    predicate.byteSize = Buffer.byteLength(predicate.predicateString,'utf-8')
    return predicate

{'fraction_non_alphanumeric': 0.12467733608673207, 'fraction_numerical': 0.020650490449148167, 'mean_word_length': 3.189189189189189, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1763257', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1171, 'n_words': 296}