forked from jamesward/heroku-connect-phone-change
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (41 loc) · 1.78 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
var express = require('express');
var bodyParser = require('body-parser');
var pg = require('pg');
var app = express();
app.set('port', process.env.PORT || 5000);
app.use(express.static('public'));
app.use(bodyParser.json());
app.post('/update', function(req, res) {
pg.connect(process.env.DATABASE_URL, function (err, conn, done) {
// watch for any connect issues
if (err) console.log(err);
conn.query(
'UPDATE salesforce.Contact SET Phone = $1, MobilePhone = $1 WHERE LOWER(FirstName) = LOWER($2) AND LOWER(LastName) = LOWER($3) AND LOWER(Email) = LOWER($4)',
[req.body.phone.trim(), req.body.firstName.trim(), req.body.lastName.trim(), req.body.email.trim()],
function(err, result) {
if (err != null || result.rowCount == 0) {
conn.query('INSERT INTO salesforce.Contact (Phone, MobilePhone, FirstName, LastName, Email) VALUES ($1, $2, $3, $4, $5)',
[req.body.phone.trim(), req.body.phone.trim(), req.body.firstName.trim(), req.body.lastName.trim(), req.body.email.trim()],
function(err, result) {
done();
if (err) {
res.status(400).json({error: err.message});
}
else {
// this will still cause jquery to display 'Record updated!'
// eventhough it was inserted
res.json(result);
}
});
}
else {
done();
res.json(result);
}
}
);
});
});
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});