producteca-sdk
Advanced tools
Comparing version 0.0.21 to 0.0.22
@@ -0,0 +0,0 @@ (function() { |
(function() { | ||
var Adjustment, smartParseFloat, _; | ||
var Adjustment, smartParseFloat, smartParseInt, trimIfStr, _, | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | ||
@@ -11,3 +12,3 @@ _ = require("lodash"); | ||
} | ||
if (typeof str === 'number') { | ||
if (_.isNumber(str)) { | ||
return str; | ||
@@ -24,16 +25,99 @@ } | ||
smartParseInt = (function(_this) { | ||
return function(n) { | ||
if (_.isNumber(n)) { | ||
return n; | ||
} else { | ||
return parseInt(n); | ||
} | ||
}; | ||
})(this); | ||
trimIfStr = (function(_this) { | ||
return function(str) { | ||
if (_.isString(str)) { | ||
return str.trim(); | ||
} else { | ||
return str; | ||
} | ||
}; | ||
})(this); | ||
module.exports = Adjustment = (function() { | ||
function Adjustment(dto) { | ||
this._adaptStock = __bind(this._adaptStock, this); | ||
this._adaptPrice = __bind(this._adaptPrice, this); | ||
this.forEachStock = __bind(this.forEachStock, this); | ||
this.forEachPrice = __bind(this.forEachPrice, this); | ||
var _ref; | ||
dto = _.mapValues(dto, function(it) { | ||
if (it != null) { | ||
return it.trim(); | ||
if (_.isArray(it)) { | ||
return it.map((function(_this) { | ||
return function(val) { | ||
return _.mapValues(val, function(inner) { | ||
return trimIfStr(inner); | ||
}); | ||
}; | ||
})(this)); | ||
} else { | ||
return it; | ||
return trimIfStr(it); | ||
} | ||
}); | ||
_.extend(this, dto); | ||
this.price = smartParseFloat(dto.price); | ||
this.stock = _.max([0, parseInt(dto.stock)]); | ||
if (this.prices != null) { | ||
this.prices.forEach((function(_this) { | ||
return function(it) { | ||
return it.value = _this._adaptPrice(it.value); | ||
}; | ||
})(this)); | ||
} else { | ||
this.price = this._adaptPrice(this.price); | ||
} | ||
if (this.stocks != null) { | ||
if ((_ref = this.stocks) != null) { | ||
_ref.forEach((function(_this) { | ||
return function(it) { | ||
return it.quantity = _this._adaptStock(it.quantity); | ||
}; | ||
})(this)); | ||
} | ||
} else { | ||
this.stock = this._adaptStock(this.stock); | ||
} | ||
} | ||
Adjustment.prototype.forEachPrice = function(fn) { | ||
if (this.prices == null) { | ||
return fn(this.price); | ||
} | ||
return this.prices.forEach((function(_this) { | ||
return function(_arg) { | ||
var priceList, value; | ||
value = _arg.value, priceList = _arg.priceList; | ||
return fn(value, priceList); | ||
}; | ||
})(this)); | ||
}; | ||
Adjustment.prototype.forEachStock = function(fn) { | ||
if (this.stocks == null) { | ||
return fn(this.stock); | ||
} | ||
return this.stocks.forEach((function(_this) { | ||
return function(_arg) { | ||
var quantity, warehouse; | ||
quantity = _arg.quantity, warehouse = _arg.warehouse; | ||
return fn(quantity, warehouse); | ||
}; | ||
})(this)); | ||
}; | ||
Adjustment.prototype._adaptPrice = function(price) { | ||
return smartParseFloat(price); | ||
}; | ||
Adjustment.prototype._adaptStock = function(stock) { | ||
return _.max([0, smartParseInt(stock)]); | ||
}; | ||
return Adjustment; | ||
@@ -40,0 +124,0 @@ |
(function() { | ||
var AjusteStock; | ||
var Adjustment; | ||
AjusteStock = require("./adjustment"); | ||
Adjustment = require("./adjustment"); | ||
describe("Ajustment", function() { | ||
it("does trim to the basic properties", function() { | ||
var ajusteStock; | ||
ajusteStock = new AjusteStock({ | ||
var adjustment; | ||
adjustment = new Adjustment({ | ||
identifier: "915004085101 ", | ||
name: "COLGANTE CLEMENT 3 X E27 MÁX. 23W NEGRO TELA " | ||
}); | ||
ajusteStock.identifier.should.equal("915004085101"); | ||
return ajusteStock.name.should.equal("COLGANTE CLEMENT 3 X E27 MÁX. 23W NEGRO TELA"); | ||
adjustment.identifier.should.equal("915004085101"); | ||
return adjustment.name.should.equal("COLGANTE CLEMENT 3 X E27 MÁX. 23W NEGRO TELA"); | ||
}); | ||
it("does trim to the inner properties in arrays", function() { | ||
return new Adjustment({ | ||
stocks: [ | ||
{ | ||
warehouse: " hola" | ||
} | ||
] | ||
}).stocks[0].warehouse.should.equal("hola"); | ||
}); | ||
describe("parsing price to float...", function() { | ||
it("thousands separator: ','; decimals separator: '.'", function() { | ||
return new AjusteStock({ | ||
price: "4,160.99" | ||
}).price.should.equal(4160.99); | ||
return new Adjustment({ | ||
prices: [ | ||
{ | ||
value: "4,160.99" | ||
} | ||
] | ||
}).prices[0].value.should.equal(4160.99); | ||
}); | ||
it("thousands separator: '.'; decimals separator: ','", function() { | ||
return new AjusteStock({ | ||
price: "4.160,99" | ||
}).price.should.equal(4160.99); | ||
return new Adjustment({ | ||
prices: [ | ||
{ | ||
value: "4.160,99" | ||
} | ||
] | ||
}).prices[0].value.should.equal(4160.99); | ||
}); | ||
it("without thousands separator", function() { | ||
return new AjusteStock({ | ||
price: "4160.99" | ||
}).price.should.equal(4160.99); | ||
return new Adjustment({ | ||
prices: [ | ||
{ | ||
value: "4160.99" | ||
} | ||
] | ||
}).prices[0].value.should.equal(4160.99); | ||
}); | ||
return it("without thousands and decimals separator", function() { | ||
return new AjusteStock({ | ||
price: "4160" | ||
}).price.should.equal(4160); | ||
it("without thousands and decimals separator", function() { | ||
return new Adjustment({ | ||
prices: [ | ||
{ | ||
value: "4160" | ||
} | ||
] | ||
}).prices[0].value.should.equal(4160); | ||
}); | ||
return it("don't try to parse the price if it's a Number", function() { | ||
return new Adjustment({ | ||
prices: [ | ||
{ | ||
value: 4160 | ||
} | ||
] | ||
}).prices[0].value.should.equal(4160); | ||
}); | ||
}); | ||
it("parses the stock to int", function() { | ||
return new AjusteStock({ | ||
stock: "2.00" | ||
}).stock.should.equal(2); | ||
return describe("parsing stocks to int...", function() { | ||
it("parses the stock to int if it's a string", function() { | ||
return new Adjustment({ | ||
stocks: [ | ||
{ | ||
quantity: "2.00" | ||
} | ||
] | ||
}).stocks[0].quantity.should.equal(2); | ||
}); | ||
it("don't try to parse the stock if it's a Number", function() { | ||
return new Adjustment({ | ||
stocks: [ | ||
{ | ||
quantity: 2 | ||
} | ||
] | ||
}).stocks[0].quantity.should.equal(2); | ||
}); | ||
return it("intializes the stock in 0 when the provided is lower", function() { | ||
return new Adjustment({ | ||
stocks: [ | ||
{ | ||
quantity: "-4.00" | ||
} | ||
] | ||
}).stocks[0].quantity.should.equal(0); | ||
}); | ||
}); | ||
return it("intializes the stock in 0 when the provided is lower", function() { | ||
return new AjusteStock({ | ||
stock: "-4.00" | ||
}).stock.should.equal(0); | ||
}); | ||
}); | ||
}).call(this); |
@@ -93,4 +93,11 @@ (function() { | ||
Syncer.prototype._updatePrice = function(adjustment, product) { | ||
console.log("Updating price of ~" + adjustment.identifier + "(" + product.id + ") with value $" + adjustment.price + "..."); | ||
return this.productecaApi.updatePrice(product, this.settings.priceList, adjustment.price); | ||
return adjustment.forEachPrice((function(_this) { | ||
return function(price, priceList) { | ||
if (priceList == null) { | ||
priceList = _this.settings.priceList; | ||
} | ||
console.log("Updating price of ~" + adjustment.identifier + "(" + product.id + ") in priceList " + priceList + " with value $" + price + "..."); | ||
return _this.productecaApi.updatePrice(product, priceList, price); | ||
}; | ||
})(this)); | ||
}; | ||
@@ -101,13 +108,20 @@ | ||
variationId = this._getVariation(product, adjustment).id; | ||
console.log("Updating stock of ~" + adjustment.identifier + "(" + product.id + ", " + variationId + ") with quantity " + adjustment.stock + "..."); | ||
return this.productecaApi.updateStocks({ | ||
id: product.id, | ||
warehouse: this.settings.warehouse, | ||
stocks: [ | ||
{ | ||
variation: variationId, | ||
quantity: adjustment.stock | ||
return adjustment.forEachStock((function(_this) { | ||
return function(stock, warehouse) { | ||
if (warehouse == null) { | ||
warehouse = _this.settings.warehouse; | ||
} | ||
] | ||
}); | ||
console.log("Updating stock of ~" + adjustment.identifier + "(" + product.id + ", " + variationId + ") in warehouse " + warehouse + " with quantity " + stock + "..."); | ||
return _this.productecaApi.updateStocks({ | ||
id: product.id, | ||
warehouse: warehouse, | ||
stocks: [ | ||
{ | ||
variation: variationId, | ||
quantity: stock | ||
} | ||
] | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
@@ -114,0 +128,0 @@ |
(function() { | ||
var Product, Q, Syncer, chai, sinon, _; | ||
var Adjustment, Product, Q, Syncer, chai, sinon, _; | ||
@@ -14,2 +14,4 @@ _ = require("lodash"); | ||
Adjustment = require("./adjustment"); | ||
chai = require("chai"); | ||
@@ -101,22 +103,95 @@ | ||
}); | ||
it("se ignoran los productos cuyo sku es vacio", function() { | ||
syncer.execute([ | ||
{ | ||
identifier: "", | ||
stock: 40 | ||
} | ||
]); | ||
return client.updateStocks.called.should.be["false"]; | ||
describe("en el caso más completo (variantes - multi listaDePrecios / depósito)...", function() { | ||
beforeEach(function() { | ||
syncer.settings.identifier = "barcode"; | ||
return syncer.execute([ | ||
new Adjustment({ | ||
identifier: "CamperaRompeNocheNegra", | ||
prices: [ | ||
{ | ||
priceList: "Precios Cuidados", | ||
value: "30" | ||
}, { | ||
priceList: "Con Tarjeta de Crédito", | ||
value: "90" | ||
} | ||
], | ||
stocks: [ | ||
{ | ||
warehouse: "Villa Lugano", | ||
quantity: 20 | ||
} | ||
] | ||
}), new Adjustment({ | ||
identifier: "CamperaRompeNocheBlanca", | ||
prices: [ | ||
{ | ||
priceList: "Default", | ||
value: "99" | ||
} | ||
], | ||
stocks: [ | ||
{ | ||
warehouse: "Palermo", | ||
quantity: 38 | ||
} | ||
] | ||
}) | ||
]); | ||
}); | ||
it("actualiza los precios", function() { | ||
client.updatePrice.should.have.callCount(3); | ||
client.updatePrice.should.have.been.calledWith(camperaVariable, "Precios Cuidados", 30); | ||
client.updatePrice.should.have.been.calledWith(camperaVariable, "Con Tarjeta de Crédito", 90); | ||
return client.updatePrice.should.have.been.calledWith(camperaVariable, "Default", 99); | ||
}); | ||
return it("actualiza los stocks", function() { | ||
client.updateStocks.should.have.callCount(2); | ||
client.updateStocks.should.have.been.calledWith({ | ||
id: 1, | ||
warehouse: "Villa Lugano", | ||
stocks: [ | ||
{ | ||
variation: 2, | ||
quantity: 20 | ||
} | ||
] | ||
}); | ||
return client.updateStocks.should.have.been.calledWith({ | ||
id: 1, | ||
warehouse: "Palermo", | ||
stocks: [ | ||
{ | ||
variation: 4, | ||
quantity: 38 | ||
} | ||
] | ||
}); | ||
}); | ||
}); | ||
describe("cuando los productos no tienen variantes...", function() { | ||
var ajuste; | ||
ajuste = { | ||
ajuste = new Adjustment({ | ||
identifier: "123456", | ||
price: 25, | ||
stock: 40 | ||
}; | ||
}); | ||
it("se ignoran los productos cuyo sku es vacio", function() { | ||
syncer.execute([ | ||
{ | ||
identifier: "", | ||
stock: 40 | ||
} | ||
]); | ||
return client.updateStocks.called.should.be["false"]; | ||
}); | ||
it("_joinAdjustmentsAndProducts linkea ajustes con productos de Producteca", function() { | ||
var ajustes; | ||
var ajustes, clean; | ||
ajustes = syncer._joinAdjustmentsAndProducts([ajuste]); | ||
return ajustes.linked[0].should.eql({ | ||
clean = (function(_this) { | ||
return function(o) { | ||
return JSON.parse(JSON.stringify(o)); | ||
}; | ||
})(this); | ||
return clean(ajustes.linked[0]).should.eql(clean({ | ||
adjustment: { | ||
@@ -128,5 +203,5 @@ identifier: "123456", | ||
products: [campera, camperaVariable] | ||
}); | ||
})); | ||
}); | ||
describe("al ejecutar dispara una request a Parsimotion matcheando el id segun sku", function() { | ||
describe("al ejecutar dispara una request a Producteca matcheando el id segun sku", function() { | ||
beforeEach(function() { | ||
@@ -170,52 +245,17 @@ return syncer.execute([ajuste]); | ||
}); | ||
describe("ejecutar devuelve un objeto con el resultado de la sincronizacion:", function() { | ||
var resultadoShouldHaveProperty; | ||
resultadoShouldHaveProperty = null; | ||
beforeEach(function() { | ||
var resultado; | ||
resultado = syncer.execute([ | ||
{ | ||
identifier: "123456", | ||
stock: 28 | ||
}, { | ||
identifier: "55555", | ||
stock: 70 | ||
} | ||
]); | ||
return resultadoShouldHaveProperty = function(name, value) { | ||
return resultado.then(function(actualizados) { | ||
return actualizados[name].should.eql(value); | ||
}); | ||
}; | ||
}); | ||
it("los unlinked", function() { | ||
return resultadoShouldHaveProperty("unlinked", [ | ||
{ | ||
identifier: "55555" | ||
} | ||
]); | ||
}); | ||
return it("los linked", function() { | ||
return resultadoShouldHaveProperty("linked", [ | ||
{ | ||
identifier: "123456" | ||
} | ||
]); | ||
}); | ||
}); | ||
return describe("cuando los productos sí tienen variantes...", function() { | ||
describe("cuando los productos sí tienen variantes...", function() { | ||
it("cuando sincronizo por sku: no sincroniza las variantes", function() { | ||
var ajustes; | ||
ajustes = [ | ||
{ | ||
new Adjustment({ | ||
identifier: "CamperaRompeNocheNegra", | ||
price: 11, | ||
stock: 23 | ||
}, { | ||
}), new Adjustment({ | ||
identifier: "CamperaRompeNocheBlanca", | ||
price: 12, | ||
stock: 24 | ||
}, { | ||
}), new Adjustment({ | ||
identifier: "123456" | ||
} | ||
}) | ||
]; | ||
@@ -245,13 +285,13 @@ return syncer.execute(ajustes).then((function(_this) { | ||
ajustes = [ | ||
{ | ||
new Adjustment({ | ||
identifier: "CamperaRompeNocheNegra", | ||
price: 11, | ||
stock: 23 | ||
}, { | ||
}), new Adjustment({ | ||
identifier: "CamperaRompeNocheBlanca", | ||
price: 12, | ||
stock: 24 | ||
}, { | ||
}), new Adjustment({ | ||
identifier: "123456" | ||
} | ||
}) | ||
]; | ||
@@ -276,4 +316,39 @@ return syncer.execute(ajustes).then((function(_this) { | ||
}); | ||
return describe("resultado de la sincronización...", function() { | ||
var resultadoShouldHaveProperty; | ||
resultadoShouldHaveProperty = null; | ||
beforeEach(function() { | ||
var resultado; | ||
resultado = syncer.execute([ | ||
new Adjustment({ | ||
identifier: "123456", | ||
stock: 28 | ||
}), new Adjustment({ | ||
identifier: "55555", | ||
stock: 70 | ||
}) | ||
]); | ||
return resultadoShouldHaveProperty = function(name, value) { | ||
return resultado.then(function(actualizados) { | ||
return actualizados[name].should.eql(value); | ||
}); | ||
}; | ||
}); | ||
it("los unlinked", function() { | ||
return resultadoShouldHaveProperty("unlinked", [ | ||
{ | ||
identifier: "55555" | ||
} | ||
]); | ||
}); | ||
return it("los linked", function() { | ||
return resultadoShouldHaveProperty("linked", [ | ||
{ | ||
identifier: "123456" | ||
} | ||
]); | ||
}); | ||
}); | ||
}); | ||
}).call(this); |
@@ -8,6 +8,7 @@ { | ||
}, | ||
"version": "0.0.21", | ||
"version": "0.0.22", | ||
"main": "build/sdk.js", | ||
"dependencies": { | ||
"bluebird": "^2.3.11", | ||
"grunt-bump": "^0.3.1", | ||
"lodash": "~2.4.1", | ||
@@ -14,0 +15,0 @@ "q": "~1.0.1", |
# Producteca SDK |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
52391
883
5
+ Addedgrunt-bump@^0.3.1
+ Addedabbrev@1.1.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedargparse@1.0.10(transitive)
+ Addedarray-each@1.0.1(transitive)
+ Addedarray-slice@1.1.0(transitive)
+ Addedasync@3.2.6(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedcolors@1.1.2(transitive)
+ Addeddateformat@4.6.3(transitive)
+ Addeddetect-file@1.0.0(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedeventemitter2@0.4.14(transitive)
+ Addedexit@0.1.2(transitive)
+ Addedexpand-tilde@2.0.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedfindup-sync@4.0.05.0.0(transitive)
+ Addedfined@1.2.0(transitive)
+ Addedflagged-respawn@1.0.1(transitive)
+ Addedfor-in@1.0.2(transitive)
+ Addedfor-own@1.0.0(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedgetobject@1.0.2(transitive)
+ Addedglob@7.1.7(transitive)
+ Addedglobal-modules@1.0.0(transitive)
+ Addedglobal-prefix@1.0.2(transitive)
+ Addedgrunt@1.6.1(transitive)
+ Addedgrunt-bump@0.3.4(transitive)
+ Addedgrunt-cli@1.4.3(transitive)
+ Addedgrunt-known-options@2.0.0(transitive)
+ Addedgrunt-legacy-log@3.0.0(transitive)
+ Addedgrunt-legacy-log-utils@2.1.0(transitive)
+ Addedgrunt-legacy-util@2.0.1(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhomedir-polyfill@1.0.3(transitive)
+ Addedhooker@0.2.3(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedini@1.3.8(transitive)
+ Addedinterpret@1.1.0(transitive)
+ Addedis-absolute@1.0.0(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedis-relative@1.0.0(transitive)
+ Addedis-unc-path@1.0.0(transitive)
+ Addedis-windows@1.0.2(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedisobject@3.0.1(transitive)
+ Addedjs-yaml@3.14.1(transitive)
+ Addedkind-of@6.0.3(transitive)
+ Addedliftup@3.0.1(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedmake-iterator@1.0.1(transitive)
+ Addedmap-cache@0.2.2(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addedminimatch@3.0.8(transitive)
+ Addednopt@3.0.64.0.3(transitive)
+ Addedobject.defaults@1.1.0(transitive)
+ Addedobject.map@1.0.1(transitive)
+ Addedobject.pick@1.3.0(transitive)
+ Addedos-homedir@1.0.2(transitive)
+ Addedos-tmpdir@1.0.2(transitive)
+ Addedosenv@0.1.5(transitive)
+ Addedparse-filepath@1.0.2(transitive)
+ Addedparse-passwd@1.0.0(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpath-root@0.1.1(transitive)
+ Addedpath-root-regex@0.1.2(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedrechoir@0.7.1(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedresolve-dir@1.0.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsemver@4.3.6(transitive)
+ Addedsprintf-js@1.0.31.1.3(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
+ Addedunc-path-regex@0.1.2(transitive)
+ Addedunderscore.string@3.3.6(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedv8flags@3.2.0(transitive)
+ Addedwhich@1.3.12.0.2(transitive)
- Removedminimatch@3.1.2(transitive)