Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.69 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. throw err;
  50. log(':: Error thrown nforce::' + err);
  51. //send errors
  52. transport.sendMail({
  53. from: config.email.from,
  54. to: 'asmatullah@zeptosystems.com',
  55. subject: process.env.NODE_ENV + ':: Error thrown nforce::',
  56. text: "n" + err
  57. }, function(error, responseStatus) {
  58. if(error) {
  59. console.log('UNABLE TO SEND ERROR EMAIL.');
  60. } else {
  61. console.log('Email responseStatus:', responseStatus);
  62. }
  63. });
  64. } else {
  65. log(':: Connected to Salesforce ::');
  66. }
  67. org.oauth = oauth;
  68. var sapi_log_collection = globals.qscourseLocalDB.collection('sapi_log');
  69. //log connect event
  70. sapi_log_collection.insert({
  71. event: 'salesforce_connect',
  72. oauth: oauth,
  73. createdDate: new Date(),
  74. }, function(err, docs) {});
  75. //streams
  76. var streams = {};
  77. //new logic to call cli script
  78. for(topic in tconfig.pushtopics) {
  79. //create stream
  80. streams[topic] = org.stream({
  81. topic: topic,
  82. oauth: oauth
  83. });
  84. (function(ptopic){
  85. streams[ptopic].on('connect', function(){
  86. log(':: Connected to pushtopic - ' + ptopic +' ::');
  87. sapi_log_collection.insert({
  88. event: 'topic_connect',
  89. topic: ptopic,
  90. createdDate: new Date(),
  91. }, function(err, docs) {});
  92. });
  93. streams[ptopic].on('error', function(error){
  94. log(':: Error received from pushtopic :: ' + ptopic, error);
  95. sapi_log_collection.insert({
  96. event: 'topic_error',
  97. topic: ptopic,
  98. error: error,
  99. createdDate: new Date(),
  100. }, function(err, docs) {});
  101. //send errors
  102. transport.sendMail({
  103. from: config.email.from,
  104. to: 'asmatullah@zeptosystems.com',
  105. subject: process.env.NODE_ENV + ':: Recieved error',
  106. text: "n" + error
  107. }, function(error, responseStatus) {
  108. if(error) {
  109. console.log('UNABLE TO SEND ERROR EMAIL.');
  110. } else {
  111. console.log('Email responseStatus:', responseStatus);
  112. }
  113. });
  114. });
  115. streams[ptopic].on('data', function(data){
  116. log('-===>', ptopic, '<===-');
  117. //save event details in database
  118. sapi_log_collection.insert({
  119. event: 'topic_data',
  120. topic: ptopic,
  121. type: data.event.type,
  122. createdDate: new Date(data.event.createdDate),
  123. sobject: data.sobject
  124. }, function(err, docs) {
  125. var apps = tconfig.pushtopics[ptopic];
  126. for(var i=0; i<apps.length; i++) {
  127. (function(app, doc) {
  128. var url = [
  129. 'http://',
  130. tconfig.apps[app].url[process.env.NODE_ENV],
  131. '/admin/',
  132. tconfig.apps[app].script, '/',
  133. doc._id
  134. ].join('');
  135. log(app, ':', url);
  136. var set = {}, key = 'response.' + app;
  137. set[key] = {
  138. url: url,
  139. date: new Date(),
  140. };
  141. //send get request
  142. http.get(url, function(res) {
  143. log("HTTP_STATUS_CODE: " + res.statusCode);
  144. set[key].statusCode = res.statusCode;
  145. var body = '';
  146. res.on('data', function (chunk) {
  147. body += chunk;
  148. });
  149. //get body
  150. res.on('end', function (chunk) {
  151. set[key].body = body;
  152. sapi_log_collection.update({
  153. _id: doc._id
  154. }, {
  155. $set: set
  156. }, function(err, result) {
  157. //error email on non 200 response
  158. if(res.statusCode != 200) {
  159. transport.sendMail({
  160. from: config.email.from,
  161. to: config.email.to,
  162. subject: app + ' : ' + process.env.NODE_ENV +' : ' + doc.topic + ' [' + res.statusCode + ']',
  163. text: url + "n" + body
  164. }, function(error, responseStatus) {
  165. if(error) {
  166. console.log('UNABLE TO SEND ERROR EMAIL.');
  167. } else {
  168. console.log('Email responseStatus:', responseStatus);
  169. }
  170. });
  171. }
  172. //log(app, ': error:', err, 'result:', result);
  173. });
  174. });
  175. }).on('error', function(error) {
  176. //incase of any error in request
  177. log("ERROR: " + error.message);
  178. set[key].error = error;
  179. sapi_log_collection.update({
  180. _id: doc._id
  181. }, {
  182. $set: set
  183. }, function(err, result){
  184. //log(app, ': error:', err, 'result:', result);
  185. });
  186. //send errors
  187. transport.sendMail({
  188. from: config.email.from,
  189. to: 'asmatullah@zeptosystems.com',
  190. subject: app + ' : ' + process.env.NODE_ENV +' : ' + doc.topic,
  191. text: url + "n" + error.message
  192. }, function(error, responseStatus) {
  193. if(error) {
  194. console.log('UNABLE TO SEND ERROR EMAIL.');
  195. } else {
  196. console.log('Email responseStatus:', responseStatus);
  197. }
  198. });
  199. });
  200. })(apps[i], docs[0]);
  201. }
  202. });
  203. });
  204. })(topic);
  205. }
  206. //old logic
  207. //TODO: move all the business logic from pushToppicsController to respective project Console modules
  208. /*for(topic in pushToppicsController.pushtopics) {
  209. //create stream
  210. streams[topic] = org.stream({
  211. topic: topic,
  212. oauth: oauth
  213. });
  214. (function(ptopic){
  215. streams[ptopic].on('connect', function(){
  216. log(':: Connected to pushtopic - ' + ptopic +' ::');
  217. });
  218. streams[ptopic].on('error', function(error){
  219. log(':: Error received from pushtopic :: ' + ptopic, error);
  220. });
  221. streams[ptopic].on('data', function(data){
  222. pushToppicsController[pushToppicsController.pushtopics[ptopic]](data);
  223. });
  224. })(topic);
  225. }*/
  226. });
  227. });
  228. //main page
  229. app.get('/', function(req, res) {
  230. log(':: HTTP REQUEST ::');
  231. var out = {
  232. authenticated: false
  233. };
  234. if(org.oauth) {
  235. out.authenticated = new Date(parseInt(org.oauth.issued_at));
  236. }
  237. res.json(out);
  238. });
  239. //fire up webserver
  240. app.listen(config.http.port, config.http.host);
  241. log('Server running at http://' + config.http.host + ':' + config.http.port + '/');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement