Skip to content

Instantly share code, notes, and snippets.

@harry-jones
Created March 17, 2017 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harry-jones/f1e4f178de8330ca51e22964667cc373 to your computer and use it in GitHub Desktop.
Save harry-jones/f1e4f178de8330ca51e22964667cc373 to your computer and use it in GitHub Desktop.
// find the existing customer or create a new one
Customer.findOrCreate({email: customer.email}, customer).exec(function (err, customerFound) {
if (err) {
sails.log(vehicle.vrm + ': error finding or creating customer');
sails.log(err);
return res.serverError(500);
}
else {
sails.log(vehicle.vrm + ": customer ID " + customerFound.id);
// overwrite the customer object with the data in the database
customer = customerFound; // TODO should only do this if customer is found in db?
var contactDetailsArgs = {
id: valuation.partner_id,
vrm: vehicle.vrm,
name: customer.name,
email: customer.email,
phone: customer.phone,
postcode: customer.postcode
};
soap.createClient(sails.config.motorway.tcbgApiUrl, function(err, client) {
if (err) {
sails.log(vehicle.vrm + ': error initialising soap client');
sails.log(err);
return res.serverError(500);
}
client.login({
username: sails.config.motorway.tcbgApiUser,
password: sails.config.motorway.tcbgApiPassword
}, function(err, result) {
if (err) {
sails.log(vehicle.vrm + ': error logging into to tcbg api');
sails.log(err);
return res.serverError(500);
}
client.setSecurity(new cookie(client.lastResponseHeaders));
client.contactDetails(contactDetailsArgs, function(err, result, raw) {
sails.log(vehicle.vrm + ': contactDetails: ' + client.lastRequest);
sails.log(vehicle.vrm + ': contactDetails: ' + raw);
if (err) {
sails.log(vehicle.vrm + ': error sending customer details');
sails.log(err);
return res.serverError(500);
}
else {
if (!result) {
sails.log(vehicle.vrm + ': customer already exists with tcbg');
}
return res.send(customer);
}
});
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment