Skip to content
This repository has been archived by the owner on Mar 10, 2021. It is now read-only.

Commit

Permalink
Migrate to new node-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
emmenko committed Oct 7, 2014
1 parent ba31f45 commit e8e392d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 74 deletions.
11 changes: 6 additions & 5 deletions package.json
Expand Up @@ -27,12 +27,13 @@
"test": "grunt coverage"
},
"dependencies": {
"optimist": "0.6.1",
"bluebird": "2.3.x",
"lynx": "0.2.0",
"q": "1.0.1",
"sphere-node-client": "0.8.1",
"sphere-node-utils": "0.4.13",
"underscore": "1.6.0"
"optimist": "0.6.1",
"sphere-node-sdk": "1.0.0-rc1",
"sphere-node-utils": "0.5.1",
"underscore": "1.7.0",
"underscore-mixins": "*"
},
"devDependencies": {
"coveralls": "2.10.0",
Expand Down
8 changes: 3 additions & 5 deletions src/coffee/run.coffee
@@ -1,4 +1,4 @@
SphereClient = require 'sphere-node-client'
{SphereClient} = require 'sphere-node-sdk'
{Logger, ProjectCredentialsConfig} = require 'sphere-node-utils'
package_json = require '../package.json'
AvailabilityService = require './service/availability'
Expand Down Expand Up @@ -41,8 +41,6 @@ ProjectCredentialsConfig.create()
client_secret: argv.clientSecret
timeout: argv.timeout
user_agent: "#{package_json.name} - #{package_json.version}"
logConfig:
logger: logger
options.host = argv.sphereHost if argv.sphereHost?

client = new SphereClient options
Expand All @@ -55,11 +53,11 @@ ProjectCredentialsConfig.create()
availability.sendMetrics()
logger.info availability.getSummaryReport()
@exitCode = 0
.fail (error) =>
.catch (error) =>
logger.error error, 'Oops, something went wrong!'
@exitCode = 1
.done()
.fail (error) =>
.catch (error) =>
logger.error error, 'Problems on getting client credentials from config files.'
@exitCode = 1
.done()
67 changes: 32 additions & 35 deletions src/coffee/service/availability.coffee
@@ -1,7 +1,7 @@
Q = require 'q'
_ = require 'underscore'
_.mixin require('underscore-mixins')
Promise = require 'bluebird'
Lynx = require 'lynx'
{Qutils} = require 'sphere-node-utils'

module.exports = class

Expand Down Expand Up @@ -41,43 +41,40 @@ module.exports = class

run: ->
@logger.info "Running with inventory check flag (#{@withInventoryCheck})"
@client.productProjections.staged(false).sort('id').all().process (payload) =>
@client.productProjections.staged(false).sort('id').perPage(10).process (payload) =>
@totalProducts = payload.body.total unless @totalProducts
products = payload.body.results
@logger.debug "Processing #{_.size products} products"

Qutils.processList products, (chunk) =>
@logger.debug "Updating #{_.size chunk} products in CHUNKS"
Q.allSettled _.map chunk, (product) =>
Promise.settle _.map products, (product) =>

allVariants = [product.masterVariant].concat(product.variants or [])
allVariants = [product.masterVariant].concat(product.variants or [])

Qutils.processList allVariants, (variantsChunk) =>
@expandVariants variantsChunk
, {accumulate: true, maxParallel: 15}
.then (expandedVariants) =>
actions = @buildActions _.flatten(expandedVariants)
if _.size(actions) > 0
payload =
version: product.version
actions: actions
@summary.total++
@client.products.byId(product.id).update(payload)
else
Q()
.then (results) =>
failures = []
_.each results, (result) =>
if result.state is 'fulfilled'
@summary.synced++
else
@summary.failed++
failures.push result.reason
@logger.debug @getSummaryReport('Progress')
if _.size(failures) > 0
@logger.error errors: failures, 'Errors while syncing products'
Q()
, {accumulate: false, maxParallel: 10}
Promise.map _.batchList(allVariants, 15), (variantsChunk) =>
@expandVariants variantsChunk
, {concurrency: 1} # run 1 batch at a time
.then (expandedVariants) =>
actions = @buildActions _.flatten(expandedVariants)
if _.size(actions) > 0
payload =
version: product.version
actions: actions
@summary.total++
@client.products.byId(product.id).update(payload)
else
Promise.resolve()
.then (results) =>
failures = []
_.each results, (result) =>
if result.isFulfilled()
@summary.synced++
if result.isRejected()
@summary.failed++
failures.push result.reason()
@logger.debug @getSummaryReport('Progress')
if _.size(failures) > 0
@logger.error errors: failures, 'Errors while syncing products'
Promise.resolve()
, {accumulate: false}

expandVariants: (variants) ->
Expand All @@ -88,7 +85,7 @@ module.exports = class
ie.fetch()
.then (result) =>
@logger.debug "Found #{result.body.count} inventories (tot: #{result.body.total}) out of #{_.size variants} given variants"
Q _.map variants, (v) ->
Promise.resolve _.map variants, (v) ->
inventory = _.find result.body.results, (r) -> r.sku is v.sku
if inventory
v.availability =
Expand All @@ -97,7 +94,7 @@ module.exports = class
v.availability = undefined
v
else
Q variants
Promise.resolve variants

buildActions: (variants) ->
_.chain(variants).map (variant) =>
Expand Down
29 changes: 13 additions & 16 deletions src/spec/integration.spec.coffee
@@ -1,7 +1,7 @@
Q = require 'q'
_ = require 'underscore'
_.mixin require('sphere-node-utils')._u
SphereClient = require 'sphere-node-client'
_.mixin require('underscore-mixins')
Promise = require 'bluebird'
{SphereClient} = require 'sphere-node-sdk'
{Logger} = require 'sphere-node-utils'
AvailabilityService = require '../lib/service/availability'
package_json = require '../package.json'
Expand Down Expand Up @@ -59,10 +59,7 @@ describe 'Integration specs', ->
streams: [
{ level: 'info', stream: process.stdout }
]
@client = new SphereClient
config: Config.config
logConfig:
logger: @logger
@client = new SphereClient Config
@availability = new AvailabilityService @client, @logger,
withInventoryCheck: true
attributeName: 'isOnStock'
Expand All @@ -73,35 +70,35 @@ describe 'Integration specs', ->
expect(result.statusCode).toBe 201
@productType = result.body
done()
.fail (error) =>
.catch (error) =>
@logger.error error
done(_.prettify(error))

afterEach (done) ->
@logger.debug 'Unpublishing all products'
@client.products.sort('id').where('masterData(published = "true")').process (payload) =>
Q.all _.map payload.body.results, (product) =>
Promise.all _.map payload.body.results, (product) =>
@client.products.byId(product.id).update(updateUnpublish(product.version))
.then (results) =>
@logger.info "Unpublished #{results.length} products"
@logger.debug 'About to delete all products'
@client.products.perPage(0).fetch()
.then (payload) =>
@logger.debug "Deleting #{payload.body.total} products"
Q.all _.map payload.body.results, (product) =>
Promise.all _.map payload.body.results, (product) =>
@client.products.byId(product.id).delete(product.version)
.then (results) =>
@logger.info "Deleted #{results.length} products"
@logger.debug 'About to delete all product types'
@client.productTypes.perPage(0).fetch()
.then (payload) =>
@logger.debug "Deleting #{payload.body.total} product types"
Q.all _.map payload.body.results, (productType) =>
Promise.all _.map payload.body.results, (productType) =>
@client.productTypes.byId(productType.id).delete(productType.version)
.then (results) =>
@logger.debug "Deleted #{results.length} product types"
@client.inventoryEntries.all().process (payload) =>
Q.all _.map payload.body.results, (inventoryEntry) =>
Promise.all _.map payload.body.results, (inventoryEntry) =>
@client.inventoryEntries.byId(inventoryEntry.id).delete(inventoryEntry.version)
.then (results) =>
@logger.info "Deleted #{results.length} inventory entries"
Expand All @@ -110,7 +107,7 @@ describe 'Integration specs', ->
@availability = null
@productType = null
done()
.fail (error) =>
.catch (error) =>
@logger.error error
done(_.prettify(error))
, 60000 # 1min
Expand All @@ -125,7 +122,7 @@ describe 'Integration specs', ->

it 'should sync availability with inventory check', (done) ->
stocks = [{sku: 's1', onStock: 10}, {sku: 's2', onStock: 0}, {sku: 's3', onStock: 30}]
Q.all _.map stocks, (stock) => @client.inventoryEntries.create(newInventoryEntry(stock))
Promise.all _.map stocks, (stock) => @client.inventoryEntries.create(newInventoryEntry(stock))
.then (result) =>
@logger.debug "Created #{_.size result} inventory entries"
inventoryEntries = _.map result, (r) -> r.body
Expand All @@ -138,7 +135,7 @@ describe 'Integration specs', ->
allVariants = [result.body.masterVariant].concat(result.body.variants)
_.each allVariants, (v) -> expect(v.availability).not.toBeDefined()
@logger.debug "About to publish product with id #{product.id}"
@client.products.byId(product.id).update(updatePublish(product.version))
@client.products.byId(product.id).update(updatePublish(result.body.version))
.then =>
@logger.debug 'About to run availability sync'
@availability.run()
Expand All @@ -158,6 +155,6 @@ describe 'Integration specs', ->
else
expect(_.size(v.attributes)).toBe 0 # masterVariant case
done()
.fail (error) =>
.catch (error) =>
@logger.error error
done(_.prettify(error))
22 changes: 9 additions & 13 deletions src/spec/service/availability.spec.coffee
@@ -1,6 +1,6 @@
Q = require 'q'
_ = require 'underscore'
SphereClient = require 'sphere-node-client'
Promise = require 'bluebird'
{SphereClient} = require 'sphere-node-sdk'
{Logger} = require 'sphere-node-utils'
AvailabilityService = require '../../lib/service/availability'
package_json = require '../../package.json'
Expand Down Expand Up @@ -36,10 +36,7 @@ describe 'Availability service', ->
streams: [
{ level: 'info', stream: process.stdout }
]
client = new SphereClient
config: Config.config
logConfig:
logger: logger
client = new SphereClient Config
@availability = new AvailabilityService client, logger,
withInventoryCheck: false
attributeName: 'isOnStock'
Expand Down Expand Up @@ -79,11 +76,11 @@ describe 'Availability service', ->
.then (expanded) ->
expect(expanded).toEqual VARIANTS
done()
.fail (e) -> done(e)
.catch (e) -> done(e)

it 'should expand variants (with inventory check)', (done) ->
spyOn(@availability.client.inventoryEntries, 'fetch').andCallFake ->
Q
Promise.resolve
statusCode: 200
body:
count: _.size(INVENTORY_ENTRIES)
Expand All @@ -94,7 +91,7 @@ describe 'Availability service', ->
.then (expanded) ->
expect(expanded).toEqual EXPANDED_VARIANTS
done()
.fail (e) -> done(e)
.catch (e) -> done(e)

it 'should build actions', ->
actions = @availability.buildActions EXPANDED_VARIANTS
Expand Down Expand Up @@ -126,13 +123,13 @@ describe 'Availability service', ->
spyOn(@availability.client.productProjections, 'process').andCallFake (fn, opts) ->
fn {statusCode: 200, body: {total: 1, results: PRODUCTS}}
spyOn(@availability.client.inventoryEntries, 'fetch').andCallFake ->
Q
Promise.resolve
statusCode: 200
body:
count: _.size(INVENTORY_ENTRIES)
total: _.size(INVENTORY_ENTRIES)
results: INVENTORY_ENTRIES
spyOn(@availability.client.products, 'update').andCallFake -> Q {statusCode: 200}
spyOn(@availability.client.products, 'update').andCallFake -> Promise.resolve {statusCode: 200}
@availability.run()
.then (result) =>
expect(_.isEmpty(result)).toBe true
Expand All @@ -147,5 +144,4 @@ describe 'Availability service', ->
synced: 1
failed: 0
done()
.fail (e) -> done(e)

.catch (e) -> done(e)

0 comments on commit e8e392d

Please sign in to comment.