Advertisement
Guest User

Untitled

a guest
Mar 28th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.71 KB | None | 0 0
  1. var express = require('express'),
  2. nforce = require('nforce'),
  3. nodemailer = require('nodemailer'),
  4. config = require('./config/config.' + process.env.NODE_ENV),
  5. tconfig = require('./config/topics'),
  6. exec = require('child_process').exec,
  7. http = require('http'),
  8. //pushToppicsController = require('./controller/pushtopics'),
  9. mongoConnect = require('./controller/mongodb-connect'),
  10. log = require('./controller/log');
  11. //process info
  12. log('NODE_ENV : ' + process.env.NODE_ENV);
  13. log('PID : ' + process.pid);
  14. //configure app
  15. var app = express();
  16. var transport = nodemailer.createTransport('SMTP', {
  17. host: config.email.host,
  18. auth: {
  19. user: config.email.username,
  20. pass: config.email.password,
  21. }
  22. });
  23. //salesforce initialisation
  24. var org = nforce.createConnection({
  25. clientId: config.salesforce.CLIENT_ID,
  26. clientSecret: config.salesforce.CLIENT_SECRET,
  27. redirectUri: config.salesforce.CALLBACK_URL + '/oauth/_callback',
  28. apiVersion: 'v32.0',
  29. mode: 'multi',
  30. environment: config.salesforce.ENVIRONMENT,
  31. autoRefresh: true
  32. });
  33. var globals = {
  34. config: config,
  35. mongoClient: false,
  36. affiliateDB: false,
  37. qscourseDB: false,
  38. qscourseLocalDB: false
  39. };
  40. //mongodb connection
  41. mongoConnect.init(globals, function() {
  42. //pushToppicsController.init(globals);
  43. org.authenticate({
  44. username: globals.config.salesforce.USERNAME,
  45. password: globals.config.salesforce.PASSWORD,
  46. securityToken: globals.config.salesforce.SECURITYTOKEN
  47. }, function(err, oauth) {
  48. if(err) {
  49. log('Salesforce authentication Error: ' + err.message);
  50. //throw err;
  51. } else {
  52. log(':: Connected to Salesforce ::');
  53. }
  54. org.oauth = oauth;
  55. var sapi_log_collection = globals.qscourseLocalDB.collection('sapi_log');
  56. //log connect event
  57. sapi_log_collection.insert({
  58. event: 'salesforce_connect',
  59. oauth: oauth,
  60. createdDate: new Date(),
  61. }, function(err, docs) {});
  62. //streams
  63. var streams = {};
  64. //new logic to call cli script
  65. for(topic in tconfig.pushtopics) {
  66. //create stream
  67. streams[topic] = org.stream({
  68. topic: topic,
  69. oauth: oauth
  70. });
  71. (function(ptopic){
  72. log(':: Attempting to subscribe: ' + ' :: ' + ptopic + ' :: ');
  73. streams[ptopic].on('connect', function(){
  74. log(':: Connected to pushtopic - ' + ptopic +' :: ');
  75. sapi_log_collection.insert({
  76. event: 'topic_connect',
  77. topic: ptopic,
  78. createdDate: new Date(),
  79. }, function(err, docs) {});
  80. });
  81. streams[ptopic].on('error', function(error){
  82. log(':: Error received from pushtopic :: ' + ptopic, error);
  83. sapi_log_collection.insert({
  84. event: 'topic_error',
  85. topic: ptopic,
  86. error: error,
  87. createdDate: new Date(),
  88. }, function(err, docs) {});
  89. });
  90. streams[ptopic].on('data', function(data){
  91. log('-===>', ptopic, '<===-');
  92. //save event details in database
  93. sapi_log_collection.insert({
  94. event: 'topic_data',
  95. topic: ptopic,
  96. type: data.event.type,
  97. createdDate: new Date(data.event.createdDate),
  98. sobject: data.sobject
  99. }, function(err, docs) {
  100. var apps = tconfig.pushtopics[ptopic];
  101. for(var i=0; i<apps.length; i++) {
  102. (function(app, doc) {
  103. var url = [
  104. 'http://',
  105. tconfig.apps[app].url[process.env.NODE_ENV],
  106. '/admin/',
  107. tconfig.apps[app].script, '/',
  108. doc._id
  109. ].join('');
  110. log(app, ':', url);
  111. var set = {}, key = 'response.' + app;
  112. set[key] = {
  113. url: url,
  114. date: new Date(),
  115. };
  116. //send get request
  117. http.get(url, function(res) {
  118. log("HTTP_STATUS_CODE: " + res.statusCode);
  119. set[key].statusCode = res.statusCode;
  120. var body = '';
  121. res.on('data', function (chunk) {
  122. body += chunk;
  123. });
  124. //get body
  125. res.on('end', function (chunk) {
  126. set[key].body = body;
  127. sapi_log_collection.update({
  128. _id: doc._id
  129. }, {
  130. $set: set
  131. }, function(err, result) {
  132. //error email on non 200 response
  133. if(res.statusCode != 200) {
  134. transport.sendMail({
  135. from: config.email.from,
  136. to: config.email.to,
  137. subject: app + ' : ' + process.env.NODE_ENV +' : ' + doc.topic + ' [' + res.statusCode + ']',
  138. text: url + "n" + body
  139. }, function(error, responseStatus) {
  140. if(error) {
  141. console.log('UNABLE TO SEND ERROR EMAIL.');
  142. } else {
  143. console.log('Email responseStatus:', responseStatus);
  144. }
  145. });
  146. }
  147. //log(app, ': error:', err, 'result:', result);
  148. });
  149. });
  150. }).on('error', function(error) {
  151. //incase of any error in request
  152. log("ERROR: " + error.message);
  153. set[key].error = error;
  154. sapi_log_collection.update({
  155. _id: doc._id
  156. }, {
  157. $set: set
  158. }, function(err, result){
  159. //log(app, ': error:', err, 'result:', result);
  160. });
  161. //send errors
  162. transport.sendMail({
  163. from: config.email.from,
  164. to: 'asmatullah@zeptosystems.com',
  165. subject: app + ' : ' + process.env.NODE_ENV +' : ' + doc.topic,
  166. text: url + "n" + error.message
  167. }, function(error, responseStatus) {
  168. if(error) {
  169. console.log('UNABLE TO SEND ERROR EMAIL.');
  170. } else {
  171. console.log('Email responseStatus:', responseStatus);
  172. }
  173. });
  174. });
  175. })(apps[i], docs[0]);
  176. }
  177. });
  178. });
  179. })(topic);
  180. }
  181. //old logic
  182. //TODO: move all the business logic from pushToppicsController to respective project Console modules
  183. /*for(topic in pushToppicsController.pushtopics) {
  184. //create stream
  185. streams[topic] = org.stream({
  186. topic: topic,
  187. oauth: oauth
  188. });
  189. (function(ptopic){
  190. streams[ptopic].on('connect', function(){
  191. log(':: Connected to pushtopic - ' + ptopic +' ::');
  192. });
  193. streams[ptopic].on('error', function(error){
  194. log(':: Error received from pushtopic :: ' + ptopic, error);
  195. });
  196. streams[ptopic].on('data', function(data){
  197. pushToppicsController[pushToppicsController.pushtopics[ptopic]](data);
  198. });
  199. })(topic);
  200. }*/
  201. });
  202. });
  203. //main page
  204. app.get('/', function(req, res) {
  205. log(':: HTTP REQUEST ::');
  206. var out = {
  207. authenticated: false
  208. };
  209. if(org.oauth) {
  210. out.authenticated = new Date(parseInt(org.oauth.issued_at));
  211. } else {
  212. log(':: org.oauth not athenticated ::');
  213. }
  214. res.json(out);
  215. });
  216. //fire up webserver
  217. app.listen(config.http.port, config.http.host);
  218. log('Server running at http://' + config.http.host + ':' + config.http.port + '/');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement