This repository has been archived by the owner on Dec 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
161 lines (138 loc) · 4.63 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
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
/* jshint esversion: 6 */
// dependencies
const req = require('rfr');
const express = require('express');
const logger = require('morgan');
const mongoose = require('mongoose');
// libs
const settings = req('/settings');
const utils = req('/lib/utils');
const activity = req('/lib/activity');
const ineedhelp = req('/lib/ineedhelp');
const lightfaden = req('/lib/lightfaden');
const Hybris = req('/lib/hybris');
var hybris = new Hybris(settings.cred.hybris.clientid, settings.cred.hybris.secret);
const MONGOPATH = settings.cred.mongo.path;
const MONGOOPTIONS = {
useMongoClient: true,
native_parser: true,
poolSize: 5,
keepAlive: true,
socketOptions: {
socketTimeoutMS: 0,
connectionTimeout: 0,
},
};
mongoose.connection.on('connected', function () {
console.log('> Connected to MongoDB...');
const app = express();
app.use(logger('dev'));
app.use((req, res, next) => {
if (req.query.userId) {
utils.createUser(req.query.userId, (err, data) => {
if (err) {
console.log(err);
res.status(500).json({msg: err});
}
else {
console.log(data);
next();
}
});
} else return res.status(500).json({msg: 'missing userId'});
});
app.get('/hybris/createcustomer', function(req, res) {
var q = req.query;
var name = q.name;
var mail = q.email;
var street = q.street;
var zip = q.zip;
var city = q.city;
var userId = q.userId;
if (name && mail && street && zip && city) {
hybris.createCustomer(name, mail, street, zip, city, (err, hybrisId) => {
utils.updateUser(userId, hybrisId, (err, hybrisId) => {
res.status(200).json({msg: hybrisId});
});
});
} else {
res.json({msg: 'missing parameter (name, maik, street, zip, city'});
}
});
app.get('/hybris/subscribecustomertoproduct', (req, res) => {
var q = req.query;
var userId = q.userId;
var product = q.product;
utils.findUser(userId, (err, user) => {
if (err) return res.status(500).json({msg: err});
hybris.subscribeCustomerToProduct(user.hybrisId, product, (err, id) => {
if (err) return res.status(500).json({msg: err});
return res.status(500).json({msg: id});
});
})
});
app.get('/hybris/getbillforcustomer', (req, res) => {
var q = req.query;
var userId = q.userId;
utils.findUser(userId, (err, user) => {
if (err) return res.status(500).json({msg: err});
hybris.getBillForCustomer(user.hybrisId, Date.now(), (err, bills) => {
if (err) return res.status(500).json({msg: err});
return res.status(500).json({msg: bills});
});
})
});
app.get('/activity', function(req, res) {
var q = req.query;
if (q.activity) {
activity.setActivity(q.userId, q.activity, (err, msg) => {
if (err) res.status(500).json({msg: err});
else res.status(200).json({msg: msg});
});
} else {
res.json({msg: 'missing paramater (activity, target)'});
}
});
app.get('/ineedhelp', function(req, res) {
if (req.query.element){
ineedhelp.getHelp(req.query.userId, user.query.element, (err, help) => {
if (err) res.status(500).json({msg: err});
else res.status(200).json({msg: help});
});
} else res.status(500).json({msg: 'missing parameter (element)'});
});
app.get('/lightfaden', function(req, res) {
if (req.query.route){
lightfaden.getLightfaden(req.query.userId, req.query.route, (err, lightfaeden) => {
if (err) return res.status(500).json({msg: err});
else return res.status(200).json(lightfaeden);
});
} else return res.status(500).json({msg: 'missing parameter (route)'});
});
var server = app.listen(settings.conf.api.port, function(err) {
if (err) console.log(err);
else console.log('server is running: http://localhost:' + settings.conf.api.port);
});
});
/////////////////////////////////
// MongoDB Connection Handling //
/////////////////////////////////
mongoose.connection.on('error', function (err) {
console.error('> Failed to connect to MongoDB on startup ', err);
});
mongoose.connection.on('disconnected', function () {
console.log('> Mongoose default connection to MongoDB disconnected');
});
var gracefulExit = function () {
mongoose.connection.close(function () {
console.log('> MongoDB disconnected through app termination');
process.exit(0);
});
};
process.on('SIGINT', gracefulExit).on('SIGTERM', gracefulExit);
try {
mongoose.connect(MONGOPATH, MONGOOPTIONS);
console.log('> Trying to connect to MongoDB...');
} catch (err) {
console.log('> Sever initialization failed ', err.message);
}