Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. // find the existing customer or create a new one
  2. Customer.findOrCreate({email: customer.email}, customer).exec(function (err, customerFound) {
  3.  
  4. if (err) {
  5. sails.log(vehicle.vrm + ': error finding or creating customer');
  6. sails.log(err);
  7. return res.serverError(500);
  8. }
  9. else {
  10.  
  11. sails.log(vehicle.vrm + ": customer ID " + customerFound.id);
  12.  
  13. // overwrite the customer object with the data in the database
  14. customer = customerFound; // TODO should only do this if customer is found in db?
  15.  
  16. var contactDetailsArgs = {
  17. id: valuation.partner_id,
  18. vrm: vehicle.vrm,
  19. name: customer.name,
  20. email: customer.email,
  21. phone: customer.phone,
  22. postcode: customer.postcode
  23. };
  24.  
  25. soap.createClient(sails.config.motorway.tcbgApiUrl, function(err, client) {
  26.  
  27. if (err) {
  28. sails.log(vehicle.vrm + ': error initialising soap client');
  29. sails.log(err);
  30. return res.serverError(500);
  31. }
  32.  
  33. client.login({
  34. username: sails.config.motorway.tcbgApiUser,
  35. password: sails.config.motorway.tcbgApiPassword
  36. }, function(err, result) {
  37.  
  38. if (err) {
  39. sails.log(vehicle.vrm + ': error logging into to tcbg api');
  40. sails.log(err);
  41. return res.serverError(500);
  42. }
  43.  
  44. client.setSecurity(new cookie(client.lastResponseHeaders));
  45.  
  46. client.contactDetails(contactDetailsArgs, function(err, result, raw) {
  47.  
  48. sails.log(vehicle.vrm + ': contactDetails: ' + client.lastRequest);
  49. sails.log(vehicle.vrm + ': contactDetails: ' + raw);
  50.  
  51. if (err) {
  52. sails.log(vehicle.vrm + ': error sending customer details');
  53. sails.log(err);
  54. return res.serverError(500);
  55. }
  56. else {
  57. if (!result) {
  58. sails.log(vehicle.vrm + ': customer already exists with tcbg');
  59. }
  60. return res.send(customer);
  61. }
  62.  
  63. });
  64.  
  65. });
  66.  
  67. });
  68.  
  69. }
  70.  
  71. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement