UNPKG

node-landmark

Version:
78 lines (62 loc) 1.7 kB
var _ = require('lodash'); var q = require('bluebird'); var js2xmlparser = require("js2xmlparser"); var Request = require('./request'); function Landmark(config){ config = _.defaults(config || {}, { test: false }); var requiredFields = ['username', 'password', 'clientId', 'shipMethod', 'region']; _.each(requiredFields, function(field){ if(!config[field]){ throw new Error([field, 'is required'].join(' ')); } }); this.config = config; this.request = new Request(config); }; Landmark.prototype.buildLogin = function(){ var self = this; return { Login: { Username: self.config.username, Password: self.config.password }, Test: self.config.test ? 'true' : 'false', ClientID: self.config.clientId, VendorInformation: _.reduce(self.config.vendor, function(vendor, val, key){ vendor[['Vendor', _.capitalize(key)].join('')] = val; return vendor }, {}) }; }; // outbound order creation Landmark.prototype.importRequest = function(data){ var self = this; if(!('ShipmentInsuranceFreight' in data)){ throw new Error('ShipmentInsuranceFreight is required - this is the total charge amount for the order'); } data = _.merge({ ShipMethod: self.config.shipMethod, ItemsCurrency: 'USD', ProduceLabel: 'false', ShipTo: { Region: 'Landmark SoCal', } }, data); data = _.merge(self.buildLogin(), data); var xml = js2xmlparser('ImportRequest', data, { declaration: { include: true } }); return this.request.makeRequest(xml) .then(function(data){ return _.first(data.ImportResponse.Result); }); }; Landmark.prototype.shipRequest = function(){ }; Landmark.prototype.TrackRequest = function(){ }; module.exports = Landmark;