JOE
2018-10-18 e3000e9987f375d106050199123769cbbb0a435a
add appi for accounts,modules,roles
13 files modified
3 files added
358 ■■■■ changed files
model/account.js 11 ●●●● patch | view | raw | blame | history
model/location.js 58 ●●●●● patch | view | raw | blame | history
model/module.js 11 ●●●●● patch | view | raw | blame | history
model/role.js 11 ●●●●● patch | view | raw | blame | history
model/segmentation.js 12 ●●●● patch | view | raw | blame | history
package-lock.json 5 ●●●●● patch | view | raw | blame | history
package.json 1 ●●●● patch | view | raw | blame | history
routes/accounts.js 36 ●●●● patch | view | raw | blame | history
routes/index.js 28 ●●●● patch | view | raw | blame | history
routes/modules.js 48 ●●●●● patch | view | raw | blame | history
routes/roles.js 48 ●●●●● patch | view | raw | blame | history
routes/segment.js 40 ●●●●● patch | view | raw | blame | history
routes/taxonomy.js 28 ●●●● patch | view | raw | blame | history
server.js 4 ●●●● patch | view | raw | blame | history
util/util.js 9 ●●●● patch | view | raw | blame | history
views/signup.html 8 ●●●● patch | view | raw | blame | history
model/account.js
....@@ -1,16 +1,15 @@
11 const mongoose = require('mongoose');
2
-// const Schema = mongoose.Schema;
2
+const Schema = mongoose.Schema;
33
4
-const AccountSchema = new mongoose.Schema({
4
+const AccountSchema = new Schema({
55 login_id: {type: String, required: true, index: true, unique: true },
66 name: {type: String, required: true},
77 passwd: {type: String, required: true, bcrypt: true},
8
- mobile: {type: String},
9
- organization: {type: String},
10
- position: {type: String},
8
+ mobile: String,
9
+ organization: String,
10
+ position: String,
1111 role: {type: mongoose.Schema.ObjectId, ref: 'roles'},
1212 accessibles : [{type: mongoose.Schema.ObjectId, ref: 'modules'}],
1313 });
1414
15
-// Export the model
1615 module.exports = mongoose.model('accounts', AccountSchema);
model/location.js
....@@ -0,0 +1,58 @@
1
+const mongoose = require('mongoose');
2
+const Schema = mongoose.Schema;
3
+const GeoJSON = require('mongoose-geojson-schema');
4
+
5
+const sub_segment = new Schema({
6
+ // taxonomy_id: {type: mongoose.Schema.Types.ObjectId, ref: 'taxonomy'},
7
+ extraction: Boolean,
8
+ criteria_order: Number,
9
+ operator: String
10
+});
11
+
12
+const sub_location = new Schema({
13
+ type: {type: String, required: true},
14
+ coordinates: [Number]
15
+});
16
+
17
+const sub_stats = new Schema({
18
+ statsAt: { type: Date, default: Date.now },
19
+ location: [{
20
+ // taxonomy_id: {type: mongoose.Schema.Types.ObjectId, ref: 'taxonomy'},
21
+ demographic:[{
22
+ demographic_filter: String,
23
+ users: Number
24
+ }]
25
+ }]
26
+});
27
+
28
+const sub_trend = new Schema({
29
+ type: {type: String, required: true},
30
+ coordinates: [Number]
31
+});
32
+
33
+const SegmentSchema = new Schema({
34
+ location_id: {type: String, required: true},
35
+ taxonomy_id: {type: String, required: true},
36
+ name: {type: String, required: true},
37
+ coverage: mongoose.Schema.Types.Polygon,
38
+ cell_origin: mongoose.Schema.Types.Point,
39
+ radius: Number,
40
+ mcc: Number,
41
+ mnc: Number,
42
+ lac: Number,
43
+ cell_id: Number,
44
+ rnc_id: Number,
45
+ radio_type: String,
46
+ address: {
47
+ place_id:String,
48
+ address_component:[{
49
+ type:String, value:String
50
+ }]
51
+ },
52
+ taxonomy_address: String,
53
+ verifiedAt: { type: Date, default: Date.now },
54
+ updatedAt: { type: Date, default: Date.now }
55
+});
56
+
57
+// Export the model
58
+module.exports = mongoose.model('location', SegmentSchema);
model/module.js
....@@ -0,0 +1,11 @@
1
+const mongoose = require('mongoose');
2
+const Schema = mongoose.Schema;
3
+
4
+const ModuleSchema = new Schema({
5
+ name: {type: String, required: true, index: true, unique: true },
6
+ path: {type: String, required: true, unique: true },
7
+ description: String,
8
+ parent_id: {type: mongoose.Schema.ObjectId, ref: 'modules'},
9
+});
10
+
11
+module.exports = mongoose.model('modules', ModuleSchema);
model/role.js
....@@ -1 +1,10 @@
1
-role.js
1
+const mongoose = require('mongoose');
2
+const Schema = mongoose.Schema;
3
+
4
+const RoleSchema = new Schema({
5
+ name: {type: String, required: true, index: true, unique: true },
6
+ welcome_page_modile: String,
7
+ accessibles : [{type: mongoose.Schema.ObjectId, ref: 'modules'}]
8
+});
9
+
10
+module.exports = mongoose.model('roles', RoleSchema);
model/segmentation.js
....@@ -2,7 +2,7 @@
22 const Schema = mongoose.Schema;
33
44 const sub_segment = new Schema({
5
- // taxonomy_id: {type: mongoose.Schema.Types.ObjectId, ref: 'taxonomy'},
5
+ taxonomy_id: {type: mongoose.Schema.Types.ObjectId, ref: 'taxonomy'},
66 extraction: Boolean,
77 criteria_order: Number,
88 operator: String
....@@ -16,7 +16,7 @@
1616 const sub_stats = new Schema({
1717 statsAt: { type: Date, default: Date.now },
1818 location: [{
19
- // taxonomy_id: {type: mongoose.Schema.Types.ObjectId, ref: 'taxonomy'},
19
+ taxonomy_id: {type: mongoose.Schema.Types.ObjectId, ref: 'taxonomy'},
2020 demographic:[{
2121 demographic_filter: String,
2222 users: Number
....@@ -25,8 +25,8 @@
2525 });
2626
2727 const sub_trend = new Schema({
28
- type: {type: String, required: true},
29
- coordinates: [Number]
28
+ calc_date: { type: Date, default: Date.now },
29
+ users: Number
3030 });
3131
3232 const SegmentSchema = new Schema({
....@@ -37,14 +37,14 @@
3737 segment_size: Number,
3838 extracted: Number,
3939 editedAt: { type: Date, default: Date.now },
40
- accessibles: [String],
40
+ accessibles: [{type: mongoose.Schema.Types.ObjectId, ref: 'accounts'}],
4141 demographic_filter: String,
4242 segmentedAt: { type: Date, default: Date.now },
4343 extractedAt: { type: Date, default: Date.now },
4444
4545 segment_criteria:[sub_segment],
4646 location_criteria:[sub_location],
47
- stats: [sub_location],
47
+ stats: [sub_stats],
4848 trend: [sub_trend]
4949 });
5050
package-lock.json
....@@ -3180,6 +3180,11 @@
31803180 }
31813181 }
31823182 },
3183
+ "mongoose-geojson-schema": {
3184
+ "version": "2.1.3",
3185
+ "resolved": "https://registry.npmjs.org/mongoose-geojson-schema/-/mongoose-geojson-schema-2.1.3.tgz",
3186
+ "integrity": "sha512-64wDCtBKUotEDQUr3XVobywGdcWng8SoV2JAGQuCM4BXs/5eLEOBF82Ac0fwo9O+qibSNWy5m5Q9PHH3MS9gCA=="
3187
+ },
31833188 "mongoose-legacy-pluralize": {
31843189 "version": "1.0.2",
31853190 "resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz",
package.json
....@@ -15,6 +15,7 @@
1515 "jsonwebtoken": "^8.3.0",
1616 "lodash": "^4.17.11",
1717 "mongoose": "^5.3.2",
18
+ "mongoose-geojson-schema": "^2.1.3",
1819 "morgan": "~1.9.0",
1920 "mustache-express": "^1.2.7",
2021 "node-sass-middleware": "0.11.0"
routes/accounts.js
....@@ -4,7 +4,6 @@
44 const bcrypt = require('bcrypt');
55 const mongoose = require('mongoose');
66
7
-// import custom lib
87 const config = require('../config/config.js');
98 const util = require('../util/util.js');
109
....@@ -15,20 +14,39 @@
1514 let hash = bcrypt.hashSync(data.passwd, 10);
1615 data.passwd = hash;
1716
18
- Account.create(data).then(user => {
19
- res.json(user);
17
+ Account.create(data).then(account => {
18
+ res.json(account);
2019 }).catch(err => {
21
- const error = util.errorJson(err);
22
- res.status(error.result).send(error);
20
+ util.reportError(err, res);
2321 });
2422 });
2523
2624 router.get('/', function(req, res, next) {
27
- Account.find({}).then(function (users) {
28
- res.send(users);
25
+ Account.find({}).then(function (accounts) {
26
+ res.send(accounts);
2927 }).catch(err => {
30
- const error = util.errorJson(err);
31
- res.status(error.result).send(error);
28
+ util.reportError(err, res);
29
+ });
30
+});
31
+
32
+router.get('/:account_id', function(req, res, next) {
33
+ Account.findOne({_id: req.params.account_id}).then(function (account) {
34
+ res.json(account);
35
+ }).catch(err => {
36
+ util.reportError(err, res);
37
+ });
38
+});
39
+
40
+router.put('/:account_id', function(req, res, next) {
41
+ const data = Object.assign(req.body) || {};
42
+ Account.updateMany({_id: req.params.account_id}, { $set: data}, function(err, response) {
43
+ err?util.reportError(err, res):res.json({'updated': response.n});
44
+ });
45
+});
46
+
47
+router.delete('/:account_id', function(req, res, next) {
48
+ Account.deleteMany({ _id: req.params.account_id }, function (err, response) {
49
+ err?util.reportError(err, res):res.json({'deleted': response.n});
3250 });
3351 });
3452
routes/index.js
....@@ -4,11 +4,11 @@
44 const bcrypt = require('bcrypt');
55 const cookie = require('cookie');
66
7
-// import custom lib
87 const config = require('../config/config.js');
98 const util = require('../util/util.js');
109
1110 const Account = require('../model/account');
11
+const Role = require('../model/role');
1212
1313 router.get('/', function(req, res, next) {
1414 let result = {cdnRoot: config.path.cdn};
....@@ -18,12 +18,32 @@
1818 } catch(error) {
1919 token = '';
2020 }
21
- console.log(token);
2221 if(token == '') {
2322 res.render('login', result);
2423 } else {
2524 res.render('boards/dashboard', result);
2625 }
26
+});
27
+
28
+router.get('/signup', function(req, res, next) {
29
+ let param = {cdnRoot: config.path.cdn};
30
+ Role.find({}).then(function (roles) {
31
+ param.roles = roles;
32
+ res.render('signup', param);
33
+ }).catch(err => {
34
+ util.reportError(err, res);
35
+ });
36
+});
37
+
38
+router.post('/signup', function(req, res, next) {
39
+ const data = Object.assign(req.body) || {};
40
+ let hash = bcrypt.hashSync(data.passwd, 10);
41
+ data.passwd = hash;
42
+ Account.create(data).then(account => {
43
+ res.json({'account_id': account._id});
44
+ }).catch(err => {
45
+ util.reportError(err, res);
46
+ });
2747 });
2848
2949 router.post('/login', function(req, res, next) {
....@@ -47,13 +67,11 @@
4767 maxAge: 60 * 60 * 24 * 7 // 1 week
4868 }));
4969
50
- // Redirect back after setting cookie
5170 res.setHeader('Location', '/');
5271 res.end();
5372 }
5473 }).catch(err => {
55
- const error = util.errorJson(err);
56
- res.status(error.result).send(error);
74
+ util.reportError(err, res);
5775 });
5876 });
5977
routes/modules.js
....@@ -0,0 +1,48 @@
1
+const express = require('express');
2
+const router = express.Router();
3
+const mongoose = require('mongoose');
4
+
5
+const config = require('../config/config.js');
6
+const util = require('../util/util.js');
7
+
8
+const Modules = require('../model/module');
9
+
10
+router.get('/:module_id', function(req, res, next) {
11
+ Modules.findOne({_id: req.params.module_id}).then(function (module) {
12
+ res.json(module);
13
+ }).catch(err => {
14
+ util.reportError(err, res);
15
+ });
16
+});
17
+
18
+router.get('/', function(req, res, next) {
19
+ Modules.find({}).then(function (modules) {
20
+ res.json(modules);
21
+ }).catch(err => {
22
+ util.reportError(err, res);
23
+ });
24
+});
25
+
26
+router.post('/', function(req, res, next) {
27
+ const data = Object.assign(req.body) || {};
28
+ Modules.create(data).then(module => {
29
+ res.json({'module_id': module._id});
30
+ }).catch(err => {
31
+ util.reportError(err, res);
32
+ });
33
+});
34
+
35
+router.put('/:module_id', function(req, res, next) {
36
+ const data = Object.assign(req.body) || {};
37
+ Modules.updateMany({_id: req.params.module_id}, { $set: data}, function(err, response) {
38
+ err?util.reportError(err, res):res.json({'updated': response.n});
39
+ });
40
+});
41
+
42
+router.delete('/:module_id', function(req, res, next) {
43
+ Modules.deleteMany({ _id: req.params.module_id }, function (err, response) {
44
+ err?util.reportError(err, res):res.json({'deleted': response.n});
45
+ });
46
+});
47
+
48
+module.exports = router;
routes/roles.js
....@@ -0,0 +1,48 @@
1
+const express = require('express');
2
+const router = express.Router();
3
+const mongoose = require('mongoose');
4
+
5
+const config = require('../config/config.js');
6
+const util = require('../util/util.js');
7
+
8
+const Roles = require('../model/role');
9
+
10
+router.get('/:role_id', function(req, res, next) {
11
+ Roles.findOne({_id: req.params.role_id}).then(function (role) {
12
+ res.json(role);
13
+ }).catch(err => {
14
+ util.reportError(err, res);
15
+ });
16
+});
17
+
18
+router.get('/', function(req, res, next) {
19
+ Roles.find({}).then(function (roles) {
20
+ res.json(roles);
21
+ }).catch(err => {
22
+ util.reportError(err, res);
23
+ });
24
+});
25
+
26
+router.post('/', function(req, res, next) {
27
+ const data = Object.assign(req.body) || {};
28
+ Roles.create(data).then(role => {
29
+ res.json({'role_id': role._id});
30
+ }).catch(err => {
31
+ util.reportError(err, res);
32
+ });
33
+});
34
+
35
+router.put('/:role_id', function(req, res, next) {
36
+ const data = Object.assign(req.body) || {};
37
+ Roles.updateMany({_id: req.params.role_id}, { $set: data}, function(err, response) {
38
+ err?util.reportError(err, res):res.json({'updated': response.n});
39
+ });
40
+});
41
+
42
+router.delete('/:role_id', function(req, res, next) {
43
+ Roles.deleteMany({ _id: req.params.role_id }, function (err, response) {
44
+ err?util.reportError(err, res):res.json({'deleted': response.n});
45
+ });
46
+});
47
+
48
+module.exports = router;
routes/segment.js
....@@ -8,55 +8,41 @@
88
99 const Segment = require('../model/segmentation');
1010
11
-
1211 router.get('/:segment_id', function(req, res, next) {
13
- Segment.findOne({segment_id: req.params.segment_id}).then(function (segment) {
14
- res.json(segment);
15
- }).catch(err => {
16
- const error = util.errorJson(err);
17
- res.status(error.result).send(error);
12
+ Segment.findOne({_id: req.params.segment_id}).then(function (segment) {
13
+ res.json(segment);
14
+ }).catch(err => {
15
+ util.reportError(err, res);
1816 });
1917 });
2018
2119 router.get('/', function(req, res, next) {
2220 Segment.find({}).then(function (segments) {
23
- res.json(segments);
24
- }).catch(err => {
25
- const error = util.errorJson(err);
26
- res.status(error.result).send(error);
21
+ res.json(segments);
22
+ }).catch(err => {
23
+ util.reportError(err, res);
2724 });
2825 });
2926
3027 router.post('/', function(req, res, next) {
3128 const data = Object.assign(req.body) || {};
3229 Segment.create(data).then(segment => {
33
- res.json({'segment_id': segment.segment_id});
30
+ res.json({'segment_id': segment._id});
3431 }).catch(err => {
35
- const error = util.errorJson(err);
36
- res.status(error.result).send(error);
32
+ util.reportError(err, res);
3733 });
3834 });
3935
4036 router.put('/:segment_id', function(req, res, next) {
4137 const data = Object.assign(req.body) || {};
42
- // console.log(data);
43
- Segment.updateMany({segment_id: req.params.segment_id}, { $set: data}, function(err, response) {
44
- res.json({'updated': response.n});
45
- }).catch(err => {
46
- const error = util.errorJson(err);
47
- res.status(error.result).send('aaaaaaaaaa');
38
+ Segment.updateMany({_id: req.params.segment_id}, { $set: data}, function(err, response) {
39
+ err?util.reportError(err, res):res.json({'updated': response.n});
4840 });
4941 });
5042
5143 router.delete('/:segment_id', function(req, res, next) {
52
- Segment.deleteMany({ segment_id: req.params.segment_id }, function (err, response) {
53
- if (err) {
54
- console.log(err);
55
- }
56
- res.json({'deleted': response.n});
57
- }).catch(err => {
58
- const error = util.errorJson(err);
59
- res.status(error.result).send(error);
44
+ Segment.deleteMany({_id: req.params.segment_id}, function (err, response) {
45
+ err?util.reportError(err, res):res.json({'deleted': response.n});
6046 });
6147 });
6248
routes/taxonomy.js
....@@ -2,7 +2,6 @@
22 const router = express.Router();
33 const mongoose = require('mongoose');
44
5
-// import custom lib
65 const config = require('../config/config.js');
76 const util = require('../util/util.js');
87
....@@ -13,8 +12,7 @@
1312 console.log(taxonomy);
1413 res.json(taxonomy);
1514 }).catch(err => {
16
- const error = util.errorJson(err);
17
- res.status(error.result).send(error);
15
+ util.reportError(err, res);
1816 });
1917 });
2018
....@@ -22,8 +20,7 @@
2220 Taxonomy.find({}).then(function (taxonomys) {
2321 res.json(taxonomys);
2422 }).catch(err => {
25
- const error = util.errorJson(err);
26
- res.status(error.result).send(error);
23
+ util.reportError(err, res);
2724 });
2825 });
2926
....@@ -31,8 +28,7 @@
3128 Taxonomy.find({keyname: new RegExp(req.query.keyname, 'i')}).then(function (taxonomys) {
3229 res.json(taxonomys);
3330 }).catch(err => {
34
- const error = util.errorJson(err);
35
- res.status(error.result).send(error);
31
+ util.reportError(err, res);
3632 });
3733 });
3834
....@@ -53,31 +49,21 @@
5349 Taxonomy.create(data).then(taxonomy => {
5450 res.json({'key_taxonomy_id': taxonomy._id});
5551 }).catch(err => {
56
- const error = util.errorJson(err);
57
- res.status(error.result).send(error);
52
+ util.reportError(err, res);
5853 });
5954 });
6055
6156 router.put('/:key_taxonomy_id', function(req, res, next) {
6257 const data = Object.assign(req.body) || {};
6358 Taxonomy.updateMany({_id: req.params.key_taxonomy_id}, { $set: data}, function(err, response) {
64
- res.json({'updated': response.n});
65
- }).catch(err => {
66
- const error = util.errorJson(err);
67
- res.status(error.result).send(error);
59
+ err?util.reportError(err, res):res.json({'updated': response.n});
6860 });
6961 });
7062
7163 router.delete('/:key_taxonomy_id', function(req, res, next) {
7264 Taxonomy.deleteMany({ _id: req.params.key_taxonomy_id }, function (err, response) {
73
- if (err) {
74
- console.log(err);
75
- }
76
- res.json({'deleted': response.n});
77
- }).catch(err => {
78
- const error = util.errorJson(err);
79
- res.status(error.result).send(error);
80
- });;
65
+ err?util.reportError(err, res):res.json({'deleted': response.n});
66
+ });
8167 });
8268
8369 module.exports = router;
server.js
....@@ -9,6 +9,8 @@
99 const config = require('./config/config.js');
1010 const indexRouter = require('./routes/index');
1111 const accountsRouter = require('./routes/accounts');
12
+const modulesRouter = require('./routes/modules');
13
+const rolesRouter = require('./routes/roles');
1214 const segmentRouter = require('./routes/segment');
1315 const taxonomyRouter = require('./routes/taxonomy');
1416
....@@ -49,6 +51,8 @@
4951 //add router
5052 app.use('/', indexRouter);
5153 app.use('/accounts', accountsRouter);
54
+app.use('/modules', modulesRouter);
55
+app.use('/roles', rolesRouter);
5256 app.use('/segment', segmentRouter);
5357 app.use('/taxonomy', taxonomyRouter);
5458
util/util.js
....@@ -1,8 +1,8 @@
11 const _ = require('lodash');
22 const jwt = require('jsonwebtoken');
33
4
-// import custom lib
54 const config = require('../config/config.js');
5
+
66 /****************************************
77 **************** LOGGER *****************
88 *****************************************/
....@@ -27,7 +27,7 @@
2727 function errorJson(err){
2828 let resultCode = 500;
2929 let resultMsg = 'Internal server error.';
30
-
30
+ // console.log(err);
3131 if (err.name == 'ValidationError') {
3232 resultCode = 400;
3333 resultMsg = 'Bad request. The values in request are insufficient or invalid.';
....@@ -47,6 +47,10 @@
4747 return {"result": resultCode, "result_msg": resultMsg};
4848 }
4949
50
+function reportError(err, res){
51
+ const error = errorJson(err);
52
+ return res.status(error.result).send(error);
53
+}
5054
5155 /****************************************
5256 ***************** AUTH ******************
....@@ -97,4 +101,5 @@
97101 createToken: createToken,
98102 verifyToken: verifyToken,
99103 errorJson: errorJson,
104
+ reportError: reportError
100105 };
views/signup.html
....@@ -7,12 +7,12 @@
77 <input type="text" name="name" id="name" class="form-control" placeholder="Name" required/>
88 </div>
99 <div>
10
- <label for="loginId">Email :</label>
11
- <input type="text" name="loginId" id="loginId" class="form-control" placeholder="Email" required/>
10
+ <label for="login_id">Email :</label>
11
+ <input type="text" name="login_id" id="login_id" class="form-control" placeholder="Email" required/>
1212 </div>
1313 <div>
14
- <label for="password">Password</label>
15
- <input type="password" name="password" id="password" placeholder="Password" required/>
14
+ <label for="passwd">Password</label>
15
+ <input type="password" name="passwd" id="passwd" placeholder="Password" required/>
1616 </div>
1717 <div>
1818 <label for="mobile">Mobile :</label>