-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adrien Baltardive
committed
Oct 17, 2020
0 parents
commit 8434de7
Showing
5 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
PORT=8081 | ||
|
||
#Odoo | ||
ODOO_HOST="" | ||
ODOO_USER="" | ||
ODOO_PASS="" | ||
ODOO_DB="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/node_modules | ||
/package-lock.json | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Odoo lead creation | ||
var express = require('express'); | ||
var router = express.Router(); | ||
|
||
const bodyParser = require('body-parser'); | ||
const jsonParser = bodyParser.json(); | ||
const validator = require('validator'); | ||
|
||
var Odoo = require('odoo-xmlrpc'); | ||
|
||
const HOST = process.env.ODOO_HOST; | ||
const PORT = process.env.ODOO_PORT || 443; | ||
|
||
const USER = process.env.ODOO_USER; | ||
const PASS = process.env.ODOO_PASS; | ||
const DB = process.env.ODOO_DB; | ||
|
||
var odoo = new Odoo({ | ||
url: HOST, | ||
port: PORT, | ||
db: DB, | ||
username: USER, | ||
password: PASS | ||
}); | ||
|
||
|
||
/* | ||
* CREATE LEAD | ||
*/ | ||
|
||
router.post('/crm/lead/create', jsonParser, async (req, res) => { | ||
|
||
if(!validator.isEmail(req.body.email)){ return res.status(401).send('Validation error (email)'); } | ||
|
||
var xmlData = { | ||
active: true, | ||
name: req.body.name, | ||
contact_name: req.body.name, | ||
email_from: req.body.email, | ||
description: req.body.description, | ||
company_id: 2, //important if you use multicompany | ||
} | ||
|
||
return odoo.connect(function (err) { | ||
|
||
if (err) { | ||
console.log('ERROR', err); | ||
return res.status(500).send('ERROR CONNECTING TO SERVER'); | ||
} | ||
|
||
var inParams = []; | ||
inParams.push(xmlData) | ||
var params = []; | ||
params.push(inParams); | ||
|
||
odoo.execute_kw('crm.lead', 'create', params, function (err, value) { | ||
if (err) { | ||
console.log(err); | ||
return res.status(400).send('ERROR CREATING RECORD'); | ||
} | ||
|
||
console.log('Lead created', xmlData); | ||
return res.status(200).send('SUCCESS'); | ||
}); | ||
|
||
}); | ||
}); | ||
|
||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "odoo-lead-connector", | ||
"version": "1.0.0", | ||
"description": "Odoo lead connector", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "ACTE Solutions", | ||
"license": "ISC", | ||
"dependencies": { | ||
"dotenv": "^8.2.0", | ||
"express": "^4.17.1", | ||
"express-rate-limit": "^5.1.3", | ||
"helmet": "^4.1.1", | ||
"odoo-xmlrpc": "^1.0.8", | ||
"validator": "^13.1.17" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const express = require('express') | ||
require('dotenv').config(); | ||
|
||
const app = express() | ||
const port = process.env.PORT || 8081 | ||
const helmet = require('helmet') | ||
const rateLimit = require("express-rate-limit"); | ||
|
||
const odoo = require('./controllers/odoo'); | ||
|
||
//Enable helmet globally | ||
app.use(helmet()) | ||
|
||
// Rate limiter | ||
const odooLimiter = rateLimit({ | ||
windowMs: 60 * 60 * 1000, // 60 minutes | ||
max: 3 //max 3 request per hour | ||
}); | ||
|
||
// Controller | ||
app.post('/crm/*', odooLimiter, odoo); | ||
|
||
// Start app | ||
app.listen(port, () => console.log(`ACTE gateway running on port ${port}!`)) |