clang
Advanced tools
Comparing version 0.1.14 to 0.2.1
111
lib/clang.js
@@ -40,3 +40,5 @@ var _ = require('lodash'); | ||
} else { | ||
throw new Error('request takes 3 arguments: methodName, args (object) and callback function'); | ||
return setImmediate(function() { | ||
callback(new Error('request takes 3 arguments: methodName, args (object) and callback function')); | ||
}); | ||
} | ||
@@ -118,2 +120,109 @@ | ||
Clang.prototype.send = function(customer, options, callback) { | ||
var me = this; | ||
var customerGetMethodName; | ||
var sendMethodName; | ||
options.lookup = options.lookup || 'externalId'; | ||
var customerArgs = {}; | ||
if (options.lookup === 'externalId') { | ||
customerGetMethodName = 'customer_getByExternalId'; | ||
customerArgs.externalId = customer.externalId; | ||
} | ||
else if (options.lookup === 'customerId') { | ||
customerGetMethodName = 'customer_getById'; | ||
customerArgs.customerId = customer.id || customer.customerId; | ||
} | ||
else if (options.lookup === 'id') { | ||
customerGetMethodName = 'customer_getByEmailAddress'; | ||
customerArgs.emailAddress = customer.email || customer.emailAddress; | ||
} | ||
else { | ||
return callback(new Error('lookup method `' + options.lookup + '` is not supported')); | ||
} | ||
options.context = options.context || 'group'; | ||
if (!options.contextId) { | ||
return callback(new Error('Unknown lookup method used')); | ||
} | ||
// only support send to group for now. Needs a backing Clang campaign listening to the group | ||
if (['group'].indexOf(options.context) === -1 ) { | ||
return callback(new Error('send to context `' + options.context + '` is not supported')); | ||
} | ||
async.waterfall([ | ||
function asyncWtfGet(callback) { | ||
me.request(customerGetMethodName, customerArgs, function(err, result) { | ||
if ( | ||
// Geen record gevonden zouden we via customer_getById als volgt weten | ||
err && err.Fault && (err.Fault.faultcode == 213 || err.Fault.faultstring.match(/not found/i)) || | ||
// via customer_getByExternalId als volgt | ||
result && !result.length | ||
) { | ||
if (!options.create) { | ||
return callback(new Error('Customer nog found and create is not allowed')); | ||
} | ||
} | ||
else if (err) { | ||
return callback(err); | ||
} | ||
if (result && result.length > 1) { | ||
return callback(new Error('Multiple customers returned after get')); | ||
} | ||
callback(null, result.length ? result[0] : null); | ||
}); | ||
}, | ||
function asyncWtfCreate(record, callback) { | ||
if (record) { | ||
setImmediate(function() { | ||
callback(null, record); | ||
}); | ||
return; | ||
} | ||
me.request('customer_insert', {customer: customer}, function(err, result) { | ||
if (!result || !result.length) { | ||
return callback(new Error('No customer returned after creation')); | ||
} | ||
if (result.length > 1) { | ||
return callback(new Error('Multiple customers returned after creation')); | ||
} | ||
callback(null, result[0]); | ||
}); | ||
}, | ||
function asyncWtfSend(record, callback) { | ||
var args = {}; | ||
if (options.context === 'group') { | ||
sendMethodName = 'group_addMember'; | ||
args.group = { | ||
id: options.contextId | ||
}; | ||
args.customer = { | ||
id: record.id | ||
}; | ||
} | ||
else if (options.context === 'email') { | ||
// UNTESTED !!!!!!!!!!!!!!!!!!!!!!!!!! | ||
sendMethodName = 'email_sendToCustomer'; | ||
args = _.clone(customer); | ||
args.emailId = options.contextId; | ||
} | ||
if (sendMethodName) { | ||
return me.request(sendMethodName, args, callback); | ||
} | ||
console.warn('No context found, just synced customer', JSON.stringify(customerArgs), ' with Clang'); | ||
setImmediate(function() { | ||
callback(null, record); | ||
}); | ||
} | ||
], callback); | ||
}; | ||
//Private stuff starts here | ||
@@ -120,0 +229,0 @@ var setMethods = { |
{ | ||
"name": "clang", | ||
"version": "0.1.14", | ||
"version": "0.2.1", | ||
"description": "Node.js api wrapper for Clang's SOAP api", | ||
@@ -5,0 +5,0 @@ "author": "Christiaan Westerbeek <chris@devotis.nl>", |
231655
6712