-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
89 lines (73 loc) · 2.48 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
const path = require('path');
const cors = require('cors');
const dotenv = require('dotenv');
const jsonServer = require('json-server');
const request = require('request');
dotenv.config();
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults({
static: 'client/build',
});
const PORT = process.env.PORT || 5000;
const basePrimaveraUrl = `https://my.jasminsoftware.com/api/${process.env.TENANT}/${process.env.ORGANIZATION}`;
const SalesController = require('./modules/sales');
const FinancialController = require('./modules/financial');
const ProductController = require('./modules/product');
const InventoryController = require('./modules/inventory');
const PurchasesController = require('./modules/purchases');
const SuppliersController = require('./modules/suppliers');
const AuthController = require('./modules/auth');
// eslint-disable-next-line no-underscore-dangle
const db = router.db.__wrapped__;
server.use(cors());
server.use(middlewares);
server.use(jsonServer.bodyParser);
const loginPrimavera = () => {
const options = {
method: 'POST',
url: 'https://identity.primaverabss.com/connect/token',
headers: {
'Content-Type': 'multipart/form-data',
},
formData: {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
scope: 'application',
grant_type: 'client_credentials',
},
};
request(options, function(error, response, body) {
if (error) throw new Error(error);
const jsonF = JSON.parse(response.body);
global.primaveraRequests = request.defaults({
headers: { Authorization: `Bearer ${jsonF.access_token}` },
});
});
};
loginPrimavera();
SalesController(server, db, basePrimaveraUrl);
FinancialController(server, db);
ProductController(server, db, basePrimaveraUrl);
InventoryController(server, basePrimaveraUrl);
PurchasesController(server, basePrimaveraUrl);
SuppliersController(server, basePrimaveraUrl);
AuthController(server);
server.get('/api/year', (req, res) => {
res.json({ year: parseInt(db.Header.FiscalYear, 10) });
});
// Set static folder in production
if (process.env.NODE_ENV === 'production') {
server.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
process.on('SIGINT', () => {
console.log('Bye bye!');
process.exit();
});
server.use(router);
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
module.exports = server;