diff --git a/README.md b/README.md index 4bed6c3..1c7fb45 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,17 @@ internetmarke.checkout() }); ``` +#### Retrieve older orders + +Every order can be re-downloaded with their order id. + +```javascript +internetmarke.retrieveOrder({ orderId: 1234 }) + .then(shoppingcart => { + // same structure as checkout + }); +``` + ### Add addresses to a voucher diff --git a/lib/Internetmarke.js b/lib/Internetmarke.js index 710ab4d..3603520 100644 --- a/lib/Internetmarke.js +++ b/lib/Internetmarke.js @@ -101,7 +101,6 @@ class Internetmarke { return this._1C4AService.checkout({ order, - user: this[_USER], outputFormat: outputFormat || this._config.outputFormat, }); } @@ -115,12 +114,9 @@ class Internetmarke { * archive, the order id and the voucher ids. */ retrieveOrder({ orderId = null } = {}) { - let order = { shopOrderId: orderId }; + const order = { shopOrderId: orderId }; - return this._1C4AService.retrieveOrder({ - order, - user: this[_USER], - }); + return this._1C4AService.retrieveOrder({ order }); } /** diff --git a/lib/Service/Soap/OneClickForApp.js b/lib/Service/Soap/OneClickForApp.js index 6d6b816..30a970f 100644 --- a/lib/Service/Soap/OneClickForApp.js +++ b/lib/Service/Soap/OneClickForApp.js @@ -101,21 +101,7 @@ class OneClickForAppService extends SoapService { .then(response => { this._user.setBalance(response.walletBallance || response.walletBalance); - const result = { - orderId: response.shoppingCart.shopOrderId, - link: response.link, - vouchers: [] - }; - - response.shoppingCart.voucherList.voucher.forEach(voucher => { - const data = { id: voucher.voucherId }; - if (voucher.trackId) { - data.trackingCode = voucher.trackId; - } - result.vouchers.push(data); - }); - - return result; + return this._processShoppingCart(response); }); } @@ -135,22 +121,7 @@ class OneClickForAppService extends SoapService { }, order); return client.retrieveOrderAsync(order); }) - .then(response => { - const result = { - orderId: response.shoppingCart.shopOrderId, - link: response.link, - vouchers: [] - }; - - response.shoppingCart.voucherList.voucher.forEach(voucher => { - const data = { id: voucher.voucherId }; - if (voucher.trackId) { - data.trackingCode = voucher.trackId; - } - result.vouchers.push(data); - }); - return result; - }); + .then(this._processShoppingCart); } /** @@ -172,6 +143,30 @@ class OneClickForAppService extends SoapService { }); } + /** + * Processes the data from the soap checkout method. + * + * @param {Object} response - The raw response from the checkout or + * retrieveOrder method of the soap service. + * @returns {Object} + */ + _processShoppingCart(response) { + const result = { + orderId: response.shoppingCart.shopOrderId, + link: response.link, + vouchers: [] + }; + + response.shoppingCart.voucherList.voucher.forEach(voucher => { + const data = { id: voucher.voucherId }; + if (voucher.trackId) { + data.trackingCode = voucher.trackId; + } + result.vouchers.push(data); + }); + return result; + } + /** * Initialize the soap client with the partner information headers. * diff --git a/test/Service/Soap/OneClickForApp.test.js b/test/Service/Soap/OneClickForApp.test.js index 5e3957e..b606828 100644 --- a/test/Service/Soap/OneClickForApp.test.js +++ b/test/Service/Soap/OneClickForApp.test.js @@ -86,7 +86,46 @@ describe('1C4A Service', () => { .then(result => { const response = CLIENT_STUB.response.checkoutShoppingCartPNGAsync; USER_STUB.user._balance.should.equal(response.walletBallance); - result.should.have.properties([ 'orderId', 'link', 'vouchers' ]); + + done(); + }); + }); + + it('should process shopping cart data', () => { + const result = service._processShoppingCart(CLIENT_STUB.response.checkoutShoppingCartPNGAsync); + + result.should.have.properties(['orderId', 'link', 'vouchers']); + + result.vouchers.forEach(voucher => { + voucher.should.have.property('id'); + }); + }); + }); + + describe('retrieveOrder', () => { + const params = { + order: { ORDER_FAKE_STUB: true } + }; + + it('should call the retrieve order method', done => { + service.retrieveOrder(params) + .then(result => { + CLIENT_STUB.client.retrieveOrderAsync.calledOnce.should.be.true(); + CLIENT_STUB.client.retrieveOrderAsync.args[0][0] + .should.containDeep(params.order); + + done(); + }); + }); + + it('should process the data', done => { + const processMethod = service._processShoppingCart; + service._processShoppingCart = sinon.stub().returns({}); + + service.retrieveOrder(params) + .then(result => { + service._processShoppingCart.calledOnce.should.be.true(); + service._processShoppingCart = processMethod; done(); }); diff --git a/test/stub/soapClient/index.js b/test/stub/soapClient/index.js index 4607c41..7777294 100644 --- a/test/stub/soapClient/index.js +++ b/test/stub/soapClient/index.js @@ -27,6 +27,23 @@ const response = { }, retrievePreviewVoucherPNGAsync: { link: 'https://schaechinger.com' + }, + retrieveOrderAsync: { + link: 'https://schaechinger.com', + shoppingCart: { + shopOrderId: 1234, + voucherList: { + voucher: [ + { + voucherId: 'A1235' + }, + { + voucherId: 'B1235', + trackId: 'PQ-1234567890-DE' + } + ] + } + } } }; @@ -41,6 +58,9 @@ const client = { ), retrievePreviewVoucherPNGAsync: sinon.stub().returns( Promise.resolve(response.retrievePreviewVoucherPNGAsync) + ), + retrieveOrderAsync: sinon.stub().returns( + Promise.resolve(response.retrieveOrderAsync) ) };