Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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);
- }
- });
- });
- });
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement