forked from asahu/node-force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
180 lines (154 loc) · 5.52 KB
/
index.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var cool = require('cool-ascii-faces');
var express = require('express');
var jsforce = require('jsforce');
var pg = require('pg');
var app = express();
var conn = new jsforce.Connection();
var bodyParser = require('body-parser');
var responseaccesstoken;
var responseinstanceUrl;
// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(request, response) {
//response.render('pages/index');
response.sendFile( __dirname + "/views/pages/" + "index.htm" );
});
app.get('/login', function(request, response) {
//response.render('pages/index');
response.sendFile( __dirname + "/views/pages/" + "login.htm" );
});
app.get('/cool', function(request, response) {
response.send(cool());
});
app.post('/process_get',urlencodedParser, function(req, res) {
//Login to Salesforce using the credentials
conn.login(req.body.user_name, req.body.pwd, function(err, userInfo) {
if (err) { return console.error(err); }
// Now you can get the access token and instance URL information.
// Save them to establish connection next time.
//console.log(conn.accessToken);
//console.log(conn.instanceUrl);
// logged in user property
// console.log("User ID: " + userInfo.id);
// console.log("Org ID: " + userInfo.organizationId);
responseaccesstoken=conn.accessToken;
responseinstanceUrl=conn.instanceUrl;
// ...
res.redirect('/accounts');
});
// Prepare output in JSON format
//response = {
// user_name:req.body.user_name,
// pwd:req.body.pwd
//};
//console.log(res);
//res.end(JSON.stringify(response));
});
app.get('/accounts', function(req, res) {
// if auth has not been set, redirect to index
//console.log(responseaccesstoken);
if (responseaccesstoken=='' && responseinstanceUrl=='') { res.redirect('/'); }
var query = 'SELECT id, name,Phone FROM account LIMIT 20';
// open connection with client's stored OAuth details
var conn = new jsforce.Connection({
accessToken: responseaccesstoken,
instanceUrl: responseinstanceUrl
});
conn.query(query, function(err, result) {
if (err) {
console.error(err);
res.redirect('/');
}
//console.log(result);
//res.end(JSON.stringify(result.records));
res.render('pages/accounts',{results: result.records});
});
});
app.get('/contacts', function(req, res) {
// if auth has not been set, redirect to index
//console.log(responseaccesstoken);
if (responseaccesstoken=='' && responseinstanceUrl=='') { res.redirect('/'); }
var query = 'SELECT id, name,Phone FROM contact LIMIT 20';
// open connection with client's stored OAuth details
var conn = new jsforce.Connection({
accessToken: responseaccesstoken,
instanceUrl: responseinstanceUrl
});
conn.query(query, function(err, result) {
if (err) {
console.error(err);
res.redirect('/');
}
//console.log(result);
//res.end(JSON.stringify(result.records));
res.render('pages/contacts',{results: result.records});
});
});
app.get('/db', function (request, response) {
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
client.query('SELECT * FROM test_table', function(err, result) {
done();
if (err)
{ console.error(err); response.send("Error " + err); }
else
{ response.render('pages/db', {results: result.rows} ); }
});
});
});
app.get('/sforce', function (request, response) {
conn.login(username, password, function(err, userInfo) {
if (err) { return console.error(err); }else{
// Now you can get the access token and instance URL information.
// Save them to establish connection next time.
console.log(conn.accessToken);
console.log(conn.instanceUrl);
// logged in user property
//console.log("User ID: " + userInfo.id);
//console.log("Org ID: " + userInfo.organizationId);
response.render('pages/home', {results: userInfo} );
}
});
});
app.get('/createaccountpage', function(request, response) {
//response.render('pages/index');
response.render('pages/createaccountpage');
});
app.get('/createcontactpage', function(request, response) {
//response.render('pages/index');
response.render('pages/createcontactpage');
});
app.post('/createaccount',urlencodedParser, function (request, response) {
console.log("createaccount called");
// Single record creation
conn.sobject("Account").create({ Name : request.body.accountname , Phone : request.body.accountphone }, function(err, ret) {
if (err || !ret.success) {
return console.error(err, ret);
}
console.log("Created record id : " + ret.id);
response.redirect('/accounts');
});
});
app.post('/createcontact',urlencodedParser, function (request, response) {
console.log("createcontact called");
// Single record creation
conn.sobject("Contact").create({ lastname : request.body.accountname , Phone : request.body.accountphone }, function(err, ret) {
if (err || !ret.success) {
return console.error(err, ret);
}
console.log("Created record id : " + ret.id);
response.redirect('/contacts');
});
});
app.get('/logout', function (request, response) {
console.log("logout called");
conn.logout();
response.redirect('/');
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});