This repository has been archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
152 lines (118 loc) · 3.51 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2016 Appirio, Inc. All rights reserved.
* @author Igor Androsov
* @version 1.0
*/
/**
* Main application code
*/
"use strict";
/**
* set NODE_ENV if undefined
*/
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = "development";
}
/**
* import all required libraries
*/
var config = require('config');
var http = require('http');
var express = require('express');
var passport = require('passport');
var Salesforce = require('./lib/salesforce');
var async = require('async');
var rp = require('request-promise');
var pg = require ('pg');
var Account = require('./src/database').accounts;
var SurveyRoute = require('./src/database').sroute;
/**
* load controllers
*/
//var AccountsController = require("./controllers/AccountsController");
require('./lib/setup_passport');
var app = express();
app.configure(function () {
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(express.static('public'));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
// Auth0 callback handler
app.get('/callback',
passport.authenticate('auth0'),
function(req, res) {
res.redirect("/");
});
// Start Survey endpoint
app.get('/', function (req, res) {
var ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
console.log('Request User IP: '+ip);
res.render('index', {
ip: ip,
env: process.env
});
});
// POST Chatter iF uSER authenticated to SFDC
app.post('/chatter', function (req, res) {
if (!req.user || req.user.identities[0].provider !== 'salesforce') {
return res.redirect('/');
}
var access_token = req.user.identities[0].access_token;
var urls = req.user._json.urls;
var salesforce_client = new Salesforce(access_token, urls);
salesforce_client.getGroups(function (err, groups) {
if (err) return res.send(500);
salesforce_client.postToChatter({
group: groups[0],
message: req.body.message
}, function (err) {
if (err) return res.send(500);
res.redirect('/');
});
});
});
/// PROMIS - Call APEX REST using Promis example
app.post('/contact', function (req, res) {
var ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
console.log(' User IP: '+ip);
console.log('FN:'+req.body.firstName+' LN:'+req.body.lastName+' EMAIL:'+req.body.email);
//'https://developer-week-developer-edition.na16.force.com/services/apexrest/consumer'
var rest_uri = config.REST_ENDPOINT + '/consumer';
var options = {
method: 'POST',
uri: rest_uri,
body: {
firstName: req.body.firstname,
lastName: req.body.lastname,
email: req.body.email,
phone: ''
},
json: true // Automatically stringifies the body to JSON
};
rp(options)
.then(function (parsedBody) {
// POST succeeded...
console.log('Contact Succcess: '+parsedBody);
res.send('Contact Succcess: '+parsedBody);
})
.catch(function (err) {
// POST failed...
console.log('Contact ERROR: '+err);
});
});
var port = process.env.PORT || 5000;
http.createServer(app).listen(port, function () {
console.log('listening in http://localhost:' + port);
});