Skip to content

Commit

Permalink
Merge pull request #1 from schaechinger/refactoring/1c4a-service
Browse files Browse the repository at this point in the history
Refactoring/1c4a service
  • Loading branch information
schaechinger committed Feb 25, 2018
2 parents 7bc8a01 + 7f828cf commit dcb6496
Show file tree
Hide file tree
Showing 14 changed files with 535 additions and 218 deletions.
4 changes: 2 additions & 2 deletions lib/Address/Address.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class Address {
/** @type {string} */
this._street = street;
/** @type {string} */
this._zip = zip;
/** @type {string} */
this._city = city;
/** @type {(string|null)} */
this._additional = additional;
Expand All @@ -42,6 +40,8 @@ class Address {
if ('number' === typeof zip) {
zip += '';
}
/** @type {string} */
this._zip = zip;

country = country || COUNTRY_CODES.DEFAULT;
if (!COUNTRY_CODES[country]) {
Expand Down
172 changes: 33 additions & 139 deletions lib/Internetmarke.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ const errors = require('./errors'),
Order = require('./Order'),
Partner = require('./Partner'),
User = require('./User'),
OneClickForAppService = require('./Service/Soap/OneClickForApp'),
AddressFactory = require('./Address/AddressFactory'),
layoutZoneHelper = require('./helper/LayoutZones'),
{ LAYOUT_ZONES, OUTPUT_FORMATS, WSDL } = require('./constants');

const _PARTNER = Symbol('partner'),
_SOAP = Symbol('soap'),
_USER = Symbol('user');

class Internetmarke {
Expand All @@ -31,16 +31,17 @@ class Internetmarke {
* every request.
*/
constructor(partner) {
/** @type {(Partner)} */
this[_PARTNER] = partner;
/** @type {(Client|null)} */
this[_SOAP] = null;
/** @type {(User|null)} */
this[_USER] = null;

/** @type {Gallery} */
this._gallery = new Gallery();
/** @type {Order} */
this._order = new Order();
/** @type {OneClickForAppService} */
this._1C4AService = new OneClickForAppService({ partner });
/** @type {Object} */
this._config = {
outputFormat: OUTPUT_FORMATS.PNG,
Expand All @@ -49,31 +50,17 @@ class Internetmarke {
}

/**
* Authorize an user to the api for check it's validity.
*
* @param {User} user - the user object that should be authenticated.
* @returns {Promise.<Internetmarke>}
*/
authenticateUser(user) {
this[_USER] = user;

return this._getSoapClient()
.then(client => {
return client.authenticateUserAsync(this[_USER].getCredentials())
})
.then(response => {
if (response) {
this[_USER].setToken(response.userToken)
.setBalance(response.walletBalance)
.setTerms(response.showTermAndCondition)
.setInfoMessage(response.infoMessage || null);
}

return this._1C4AService.authenticateUser(this[_USER])
.then(success => {
return this;
})
.catch(reason => {
// TODO: check error type and message
// throw error error.internetmarke.invalidUserCredentials

// OperationalError {cause: Error: ns2:Server: Unknown user or invalid passwor…, isOperational: true, root: Object, response: IncomingMessage, body: "<?xml version='1....
});
}

Expand Down Expand Up @@ -106,53 +93,17 @@ class Internetmarke {
* archive, the order id and the coucher ids.
*/
checkout({ orderId = null } = {}) {
const method = `checkoutShoppingCart${this._config.outputFormat}Async`;
let orderIdPromise = Promise.resolve(orderId);
if (!orderId) {
orderIdPromise = this._generateOrderId();
}

let checkoutOrderId = orderId;

return orderIdPromise.then(orderId => {
checkoutOrderId = orderId;

return this._getSoapClient();
})
.then(client => {
let order = this._order.checkout({ orderId: checkoutOrderId });
if (!order) {
throw new Error(errors.internetmarke.shoppingcartEmpty);
}
if (order.total > this[_USER].getBalance()) {
throw new Error(errors.internetmarke.walletEmpty);
}
let order = this._order.getCheckout({ orderId });

order = Object.assign({
userToken: this[_USER].getToken()
}, order);

return client[method](order);
})
.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);
});
if (order.total > this[_USER].getBalance()) {
throw new Error(errors.internetmarke.walletEmpty);
}

return result;
});
return this._1C4AService.checkout({
order,
user: this[_USER],
outputFormat: this._config.outputFormat
});
}

/**
Expand All @@ -166,36 +117,12 @@ class Internetmarke {
* @returns {Promise.<Object>} - The url of the preview voucher.
*/
getVoucherPreview({ productCode, imageId = null }) {
const method = `retrievePreviewVoucher${this._config.outputFormat}Async`;

return this._getSoapClient()
.then(client => {
return client[method]({
productCode,
voucherLayout
})
})
.then(response => {
return {
link: response.link
};
});
}

/**
* Specify the layout of the voucher from the LAYOUT_ZONES enum.
*
* @param {string} voucherLayout - The layout that should be used.
* @returns {boolean}
*/
setDefaultVoucherLayout(voucherLayout) {
const valid = layoutZoneHelper.validate(voucherLayout);

if (valid) {
this._config.voucherLayout = voucherLayout;
}

return valid;
return this._1C4AService.previewVoucher({
productCode,
imageId,
voucherLayout: this._config.voucherLayout,
outputFormat: this._config.outputFormat
});
}

/**
Expand All @@ -204,57 +131,24 @@ class Internetmarke {
* @returns {Promise.<boolean[]>}
*/
updateGalleries() {
return this._gallery.updateGalleries(this._getSoapClient());
return Promise.resolve(this);
//return this._gallery.updateGalleries(this._getSoapClient());
}

/**
* Helper method to retrieve the soap client for every api call.
* Specify the layout of the voucher from the LAYOUT_ZONES enum.
*
* @returns {Promise.<Client>}
* @param {string} voucherLayout - The layout that should be used.
* @returns {boolean}
*/
_getSoapClient() {
let promise = null;

if (this[_SOAP]) {
promise = Promise.resolve(this[_SOAP]);
}
else {
promise = new Promise(resolve => {
soap.createClientAsync(WSDL, {
disableCache: true
})
.then(client => {
this[_SOAP] = client;

if (this[_PARTNER]) {
client.addSoapHeader(this[_PARTNER].getSoapHeaders());
}
setDefaultVoucherLayout(voucherLayout) {
const valid = layoutZoneHelper.validate(voucherLayout);

resolve(client);
});
});
if (valid) {
this._config.voucherLayout = voucherLayout;
}

return promise;
}

/**
* Create a globally unique order id from the api.
*
* @returns {Promise.number}
*/
_generateOrderId() {
return this._getSoapClient()
.then(client => {
return client.createShopOrderIdAsync({
userToken: this[_USER].getToken()
})
})
.then(response => {
this[_USER].addOrderId(response.shopOrderId);

return response.shopOrderId;
});
return valid;
}
}

Expand Down
21 changes: 13 additions & 8 deletions lib/Order/Order.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

'use strict';

const Position = require('./Position');
const Position = require('./Position'),
errors = require('../errors');

class Order {
/**
Expand Down Expand Up @@ -55,30 +56,34 @@ class Order {
* should be generated and attached after checkout.
* @param {number} [metadata.createShippingList] - Determine whether a shipping
* list should be created: 0: no, 1: without addresses, 2: with addresses
* @returns {(Object|boolean)}
* @returns {Object}
*/
checkout({ orderId = this.orderId, createManifest = true, createShippingList = 2 } = {}) {
if (!orderId || !this._positions.length) {
return false;
getCheckout({ orderId = this.orderId, createManifest = true, createShippingList = 2 } = {}) {
if (!this._positions.length) {
throw new Error(errors.internetmarke.shoppingcartEmpty);
}

const positions = [];
let total = 0;

this._positions.forEach(pos => {
total += pos.getPrice();

positions.push(pos.getPosition());
});

return {
shopOrderId: orderId,
const order = {
//ppl: 33,
positions,
total,
createManifest,
createShippingList
};

if (orderId) {
order.shopOrderId = orderId;
}

return order;
}
}

Expand Down
Loading

0 comments on commit dcb6496

Please sign in to comment.