Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien Baltardive committed Oct 17, 2020
0 parents commit 8434de7
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .env.example
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=""
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
/package-lock.json
.env
70 changes: 70 additions & 0 deletions controllers/odoo.js
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;
19 changes: 19 additions & 0 deletions package.json
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"
}
}
24 changes: 24 additions & 0 deletions server.js
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}!`))

0 comments on commit 8434de7

Please sign in to comment.