Created
March 17, 2017 09:55
-
-
Save harry-jones/f1e4f178de8330ca51e22964667cc373 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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