Commit 49559b6cd2e616bf1a870bd09d3cd5342735756f
1 parent
58b47ee8
Exists in
master
and in
1 other branch
merge
Showing
8 changed files
with
313 additions
and
91 deletions
Show diff stats
| @@ -0,0 +1,34 @@ | @@ -0,0 +1,34 @@ | ||
| 1 | +# Logs | ||
| 2 | +logs | ||
| 3 | +*.log | ||
| 4 | +*.detail | ||
| 5 | +*.stat | ||
| 6 | +*.summary | ||
| 7 | + | ||
| 8 | +# Runtime data | ||
| 9 | +pids | ||
| 10 | +*.pid | ||
| 11 | +*.seed | ||
| 12 | + | ||
| 13 | +# Directory for instrumented libs generated by jscoverage/JSCover | ||
| 14 | +lib-cov | ||
| 15 | + | ||
| 16 | +# Coverage directory used by tools like istanbul | ||
| 17 | +coverage | ||
| 18 | + | ||
| 19 | +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
| 20 | +.grunt | ||
| 21 | + | ||
| 22 | +# node-waf configuration | ||
| 23 | +.lock-wscript | ||
| 24 | + | ||
| 25 | +# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
| 26 | +build/Release | ||
| 27 | + | ||
| 28 | +# Dependency directory | ||
| 29 | +# https://docs.npmjs.com/cli/shrinkwrap#caveats | ||
| 30 | +node_modules | ||
| 31 | + | ||
| 32 | +# Debug log from npm | ||
| 33 | +npm-debug.log | ||
| 34 | +stat.db |
app.config.json
| 1 | { | 1 | { |
| 2 | "default": { | 2 | "default": { |
| 3 | + "appName": "Hostpital service", | ||
| 3 | "appPort": 3000, | 4 | "appPort": 3000, |
| 4 | - "apiUrlPrefix": "/api/partner", | 5 | + "apiUrlPrefix": "/api", |
| 5 | "requestTimeout": 10, | 6 | "requestTimeout": 10, |
| 6 | - "redis" : { | ||
| 7 | - "host": "10.1.2.155", | ||
| 8 | - "port": "6379" | ||
| 9 | - }, | ||
| 10 | - "passport": { | ||
| 11 | - "strategy": "local", | ||
| 12 | - "configStrategy": { | ||
| 13 | - "local": { | ||
| 14 | - "usernameField": "username", | ||
| 15 | - "passwordField": "password" | ||
| 16 | - }, | ||
| 17 | - "saml": { | ||
| 18 | - "path": "/login/callback", | ||
| 19 | - "entryPoint": "https://10.252.160.223:9443/samlsso", | ||
| 20 | - "issuer": "smaf-partner" | ||
| 21 | - } | ||
| 22 | - }, | ||
| 23 | - "routes": { | ||
| 24 | - "local": { | ||
| 25 | - "loginPageUrl": "http://localhost:4200/#/login", | ||
| 26 | - "logoutRedirectUrl": "http://localhost:4200" | ||
| 27 | - }, | ||
| 28 | - "saml": { | ||
| 29 | - "loginPageUrl": "/login", | ||
| 30 | - "logoutRedirectUrl": "http://localhost:4200" | ||
| 31 | - } | ||
| 32 | - } | ||
| 33 | - }, | ||
| 34 | - "sdfHost": "http://10.1.2.222:5000", | ||
| 35 | - "sdfAPI": { | ||
| 36 | - "saleArea": "/api/abc/def" | ||
| 37 | - } | 7 | + "mongoDB" : { |
| 8 | + "hopitalIp" : "10.1.2.155:27017", | ||
| 9 | + "hopitalName" : "spw" | ||
| 10 | + } | ||
| 11 | + | ||
| 12 | + | ||
| 13 | + | ||
| 38 | }, | 14 | }, |
| 39 | "dev": { | 15 | "dev": { |
| 40 | - "redis" : { | ||
| 41 | - "host": "11.1.2.155", | ||
| 42 | - "port": "6379" | ||
| 43 | - }, | ||
| 44 | - "sdfHost": "http://10.1.2.222:5000" | ||
| 45 | - }, | ||
| 46 | - "staging": { | ||
| 47 | - "appPort": 4800, | ||
| 48 | - "redis" : { | ||
| 49 | - "host": "12.1.2.155", | ||
| 50 | - "port": "6379" | ||
| 51 | - }, | ||
| 52 | - "sdfHost": "http://10.1.2.222:5000" | ||
| 53 | - }, | ||
| 54 | - "production": { | ||
| 55 | - "appPort": 4800, | ||
| 56 | - "redis" : { | ||
| 57 | - "host": "12.1.2.155", | ||
| 58 | - "port": "6379" | ||
| 59 | - }, | ||
| 60 | - "sdfHost": "http://10.1.2.222:5000" | 16 | + |
| 61 | } | 17 | } |
| 62 | } | 18 | } |
| 63 | 19 |
app.js
| 1 | require('app-module-path').addPath(__dirname); | 1 | require('app-module-path').addPath(__dirname); |
| 2 | const express = require('express'); | 2 | const express = require('express'); |
| 3 | const app = express(); | 3 | const app = express(); |
| 4 | - | ||
| 5 | const conf = require('utils/config'); | 4 | const conf = require('utils/config'); |
| 5 | +const utils = require('utils/utils'); | ||
| 6 | 6 | ||
| 7 | -console.log(conf.get('redis.host')); | 7 | +// console.log(conf.get('redis.host')); |
| 8 | 8 | ||
| 9 | // setup generate request-id middleware | 9 | // setup generate request-id middleware |
| 10 | const addRequestId = require('express-request-id')(); | 10 | const addRequestId = require('express-request-id')(); |
| @@ -12,23 +12,23 @@ app.use(addRequestId); | @@ -12,23 +12,23 @@ app.use(addRequestId); | ||
| 12 | 12 | ||
| 13 | // setup express-session with redis store | 13 | // setup express-session with redis store |
| 14 | const session = require('express-session'); | 14 | const session = require('express-session'); |
| 15 | -const RedisStore = require('connect-redis')(session); | 15 | +// const RedisStore = require('connect-redis')(session); |
| 16 | const uuidV4 = require('uuid/v4'); | 16 | const uuidV4 = require('uuid/v4'); |
| 17 | 17 | ||
| 18 | -app.use(session({ | ||
| 19 | - genid: function(req) { | ||
| 20 | - return uuidV4(); // generates session id using UUID | ||
| 21 | - }, | ||
| 22 | - store: new RedisStore({ | ||
| 23 | - host: conf.get('redis.host'), | ||
| 24 | - port: conf.get('redis.port'), | ||
| 25 | - ttl: 180 | ||
| 26 | - }), | ||
| 27 | - secret: 'dbc33e678f', | ||
| 28 | - saveUninitialized: true, | ||
| 29 | - resave: false, | ||
| 30 | - cookie: { maxAge: 3600000 } | ||
| 31 | -})); | 18 | +// app.use(session({ |
| 19 | +// genid: function(req) { | ||
| 20 | +// return uuidV4(); // generates session id using UUID | ||
| 21 | +// }, | ||
| 22 | +// store: new RedisStore({ | ||
| 23 | +// host: conf.get('redis.host'), | ||
| 24 | +// port: conf.get('redis.port'), | ||
| 25 | +// ttl: 180 | ||
| 26 | +// }), | ||
| 27 | +// secret: 'dbc33e678f', | ||
| 28 | +// saveUninitialized: true, | ||
| 29 | +// resave: false, | ||
| 30 | +// cookie: { maxAge: 3600000 } | ||
| 31 | +// })); | ||
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | // setup json body parser | 34 | // setup json body parser |
| @@ -67,11 +67,11 @@ log4js.configure('./config/log4js_config.json'); | @@ -67,11 +67,11 @@ log4js.configure('./config/log4js_config.json'); | ||
| 67 | 67 | ||
| 68 | 68 | ||
| 69 | // setup passport | 69 | // setup passport |
| 70 | -const passport = require('utils/passport-func')(); | ||
| 71 | -const flash = require('connect-flash'); | ||
| 72 | -app.use(flash()); | ||
| 73 | -app.use(passport.initialize()); | ||
| 74 | -app.use(passport.session()); | 70 | +// const passport = require('utils/passport-func')(); |
| 71 | +// const flash = require('connect-flash'); | ||
| 72 | +// app.use(flash()); | ||
| 73 | +// app.use(passport.initialize()); | ||
| 74 | +// app.use(passport.session()); | ||
| 75 | 75 | ||
| 76 | 76 | ||
| 77 | 77 | ||
| @@ -88,11 +88,14 @@ app.use(function (req, res, next) { | @@ -88,11 +88,14 @@ app.use(function (req, res, next) { | ||
| 88 | }); | 88 | }); |
| 89 | 89 | ||
| 90 | 90 | ||
| 91 | -const utils = require('utils/utils'); | ||
| 92 | app.use( function( req, res, next ) { | 91 | app.use( function( req, res, next ) { |
| 92 | + | ||
| 93 | req = utils.findQueryString(req); | 93 | req = utils.findQueryString(req); |
| 94 | + | ||
| 95 | + | ||
| 94 | next(); | 96 | next(); |
| 95 | -}); | 97 | + }); |
| 98 | + | ||
| 96 | 99 | ||
| 97 | 100 | ||
| 98 | // get BE api configs | 101 | // get BE api configs |
| @@ -103,11 +106,14 @@ const appPort = conf.get('appPort'); | @@ -103,11 +106,14 @@ const appPort = conf.get('appPort'); | ||
| 103 | let mongoQuery = require('./controllers/mongo-db/index'); | 106 | let mongoQuery = require('./controllers/mongo-db/index'); |
| 104 | app.use('/mongo', mongoQuery); | 107 | app.use('/mongo', mongoQuery); |
| 105 | 108 | ||
| 106 | -let apiSaleArea = require('./controllers/sale-area/index'); | ||
| 107 | -app.use(apiUrlPrefix + '/sale-area', apiSaleArea); | 109 | +let preference = require('./controllers/preference/index'); |
| 110 | +app.use(apiUrlPrefix+'/preference', preference); | ||
| 111 | + | ||
| 112 | +// let apiSaleArea = require('./controllers/sale-area/index'); | ||
| 113 | +// app.use(apiUrlPrefix + '/sale-area', apiSaleArea); | ||
| 108 | 114 | ||
| 109 | -let apiLogin = require('./controllers/login/login')(passport); | ||
| 110 | -app.use('/', apiLogin); | 115 | +// let apiLogin = require('./controllers/login/login')(passport); |
| 116 | +// app.use('/', apiLogin); | ||
| 111 | 117 | ||
| 112 | 118 | ||
| 113 | 119 | ||
| @@ -136,5 +142,5 @@ app.all('*', function(req, res) { | @@ -136,5 +142,5 @@ app.all('*', function(req, res) { | ||
| 136 | // })); | 142 | // })); |
| 137 | 143 | ||
| 138 | app.listen(appPort, function () { | 144 | app.listen(appPort, function () { |
| 139 | - console.log('SMAF-Partner Backend app listening on port ' + appPort + '!'); | 145 | + console.log(conf.get('appName')+' listening on port ' + appPort + '!'); |
| 140 | }); | 146 | }); |
| @@ -0,0 +1,33 @@ | @@ -0,0 +1,33 @@ | ||
| 1 | +let express = require('express'); | ||
| 2 | +let router = express.Router(); | ||
| 3 | +let connection = require("../../utils/mongoDB") | ||
| 4 | + | ||
| 5 | + | ||
| 6 | +router.get('/:collection',async function (req, res) { | ||
| 7 | + var query = Object.assign(req.params, req.query); | ||
| 8 | + console.log(query); | ||
| 9 | + var result = await connection.reqMongo(req,req.method,query,req.params.collection) | ||
| 10 | + res.status(200).json(result); | ||
| 11 | + | ||
| 12 | + | ||
| 13 | +}); | ||
| 14 | + | ||
| 15 | +router.post('/:collection',async function (req, res) { | ||
| 16 | + var query = Object.assign(req.params, req.query); | ||
| 17 | + var result = await connection.reqMongo(req, query) | ||
| 18 | + res.status(200).json(result); | ||
| 19 | +}); | ||
| 20 | + | ||
| 21 | +router.put('/:collection',async function (req, res) { | ||
| 22 | + var query = Object.assign(req.params, req.query); | ||
| 23 | + var result = await connection.reqMongo(req, query) | ||
| 24 | + res.status(200).json(result); | ||
| 25 | +}); | ||
| 26 | + | ||
| 27 | +router.delete('/:collection',async function (req, res) { | ||
| 28 | + var query = Object.assign(req.params, req.query); | ||
| 29 | + var result = await connection.reqMongo(req, query) | ||
| 30 | + res.status(200).json(result); | ||
| 31 | +}); | ||
| 32 | + | ||
| 33 | +module.exports = router; | ||
| 0 | \ No newline at end of file | 34 | \ No newline at end of file |
package-lock.json
| 1 | { | 1 | { |
| 2 | - "name": "express-project", | 2 | + "name": "hospital", |
| 3 | "version": "1.0.0", | 3 | "version": "1.0.0", |
| 4 | "lockfileVersion": 1, | 4 | "lockfileVersion": 1, |
| 5 | "requires": true, | 5 | "requires": true, |
| @@ -471,6 +471,11 @@ | @@ -471,6 +471,11 @@ | ||
| 471 | "kuler": "1.0.1" | 471 | "kuler": "1.0.1" |
| 472 | } | 472 | } |
| 473 | }, | 473 | }, |
| 474 | + "diff": { | ||
| 475 | + "version": "3.5.0", | ||
| 476 | + "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", | ||
| 477 | + "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==" | ||
| 478 | + }, | ||
| 474 | "double-ended-queue": { | 479 | "double-ended-queue": { |
| 475 | "version": "2.1.0-0", | 480 | "version": "2.1.0-0", |
| 476 | "resolved": "https://registry.npmjs.org/double-ended-queue/-/double-ended-queue-2.1.0-0.tgz", | 481 | "resolved": "https://registry.npmjs.org/double-ended-queue/-/double-ended-queue-2.1.0-0.tgz", |
| @@ -708,6 +713,11 @@ | @@ -708,6 +713,11 @@ | ||
| 708 | "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", | 713 | "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", |
| 709 | "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" | 714 | "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" |
| 710 | }, | 715 | }, |
| 716 | + "fs.realpath": { | ||
| 717 | + "version": "1.0.0", | ||
| 718 | + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", | ||
| 719 | + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" | ||
| 720 | + }, | ||
| 711 | "generate-function": { | 721 | "generate-function": { |
| 712 | "version": "2.3.1", | 722 | "version": "2.3.1", |
| 713 | "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", | 723 | "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", |
| @@ -740,11 +750,11 @@ | @@ -740,11 +750,11 @@ | ||
| 740 | } | 750 | } |
| 741 | }, | 751 | }, |
| 742 | "glob": { | 752 | "glob": { |
| 743 | - "version": "6.0.4", | ||
| 744 | - "resolved": "https://registry.npmjs.org/glob/-/glob-6.0.4.tgz", | ||
| 745 | - "integrity": "sha1-DwiGD2oVUSey+t1PnOJLGqtuTSI=", | ||
| 746 | - "optional": true, | 753 | + "version": "7.1.2", |
| 754 | + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", | ||
| 755 | + "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", | ||
| 747 | "requires": { | 756 | "requires": { |
| 757 | + "fs.realpath": "1.0.0", | ||
| 748 | "inflight": "1.0.6", | 758 | "inflight": "1.0.6", |
| 749 | "inherits": "2.0.3", | 759 | "inherits": "2.0.3", |
| 750 | "minimatch": "3.0.4", | 760 | "minimatch": "3.0.4", |
| @@ -771,6 +781,11 @@ | @@ -771,6 +781,11 @@ | ||
| 771 | "ansi-regex": "2.1.1" | 781 | "ansi-regex": "2.1.1" |
| 772 | } | 782 | } |
| 773 | }, | 783 | }, |
| 784 | + "has-flag": { | ||
| 785 | + "version": "3.0.0", | ||
| 786 | + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", | ||
| 787 | + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" | ||
| 788 | + }, | ||
| 774 | "hawk": { | 789 | "hawk": { |
| 775 | "version": "3.1.3", | 790 | "version": "3.1.3", |
| 776 | "resolved": "https://registry.npmjs.org/hawk/-/hawk-3.1.3.tgz", | 791 | "resolved": "https://registry.npmjs.org/hawk/-/hawk-3.1.3.tgz", |
| @@ -790,6 +805,11 @@ | @@ -790,6 +805,11 @@ | ||
| 790 | "lodash": "4.17.11" | 805 | "lodash": "4.17.11" |
| 791 | } | 806 | } |
| 792 | }, | 807 | }, |
| 808 | + "he": { | ||
| 809 | + "version": "1.1.1", | ||
| 810 | + "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz", | ||
| 811 | + "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=" | ||
| 812 | + }, | ||
| 793 | "hoek": { | 813 | "hoek": { |
| 794 | "version": "2.16.3", | 814 | "version": "2.16.3", |
| 795 | "resolved": "https://registry.npmjs.org/hoek/-/hoek-2.16.3.tgz", | 815 | "resolved": "https://registry.npmjs.org/hoek/-/hoek-2.16.3.tgz", |
| @@ -1137,6 +1157,47 @@ | @@ -1137,6 +1157,47 @@ | ||
| 1137 | "minimist": "0.0.8" | 1157 | "minimist": "0.0.8" |
| 1138 | } | 1158 | } |
| 1139 | }, | 1159 | }, |
| 1160 | + "mocha": { | ||
| 1161 | + "version": "5.2.0", | ||
| 1162 | + "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.2.0.tgz", | ||
| 1163 | + "integrity": "sha512-2IUgKDhc3J7Uug+FxMXuqIyYzH7gJjXECKe/w43IGgQHTSj3InJi+yAA7T24L9bQMRKiUEHxEX37G5JpVUGLcQ==", | ||
| 1164 | + "requires": { | ||
| 1165 | + "browser-stdout": "1.3.1", | ||
| 1166 | + "commander": "2.15.1", | ||
| 1167 | + "debug": "3.1.0", | ||
| 1168 | + "diff": "3.5.0", | ||
| 1169 | + "escape-string-regexp": "1.0.5", | ||
| 1170 | + "glob": "7.1.2", | ||
| 1171 | + "growl": "1.10.5", | ||
| 1172 | + "he": "1.1.1", | ||
| 1173 | + "minimatch": "3.0.4", | ||
| 1174 | + "mkdirp": "0.5.1", | ||
| 1175 | + "supports-color": "5.4.0" | ||
| 1176 | + }, | ||
| 1177 | + "dependencies": { | ||
| 1178 | + "commander": { | ||
| 1179 | + "version": "2.15.1", | ||
| 1180 | + "resolved": "http://registry.npmjs.org/commander/-/commander-2.15.1.tgz", | ||
| 1181 | + "integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==" | ||
| 1182 | + }, | ||
| 1183 | + "debug": { | ||
| 1184 | + "version": "3.1.0", | ||
| 1185 | + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", | ||
| 1186 | + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", | ||
| 1187 | + "requires": { | ||
| 1188 | + "ms": "2.0.0" | ||
| 1189 | + } | ||
| 1190 | + }, | ||
| 1191 | + "supports-color": { | ||
| 1192 | + "version": "5.4.0", | ||
| 1193 | + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz", | ||
| 1194 | + "integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==", | ||
| 1195 | + "requires": { | ||
| 1196 | + "has-flag": "3.0.0" | ||
| 1197 | + } | ||
| 1198 | + } | ||
| 1199 | + } | ||
| 1200 | + }, | ||
| 1140 | "moment": { | 1201 | "moment": { |
| 1141 | "version": "2.22.2", | 1202 | "version": "2.22.2", |
| 1142 | "resolved": "https://registry.npmjs.org/moment/-/moment-2.22.2.tgz", | 1203 | "resolved": "https://registry.npmjs.org/moment/-/moment-2.22.2.tgz", |
package.json
| 1 | { | 1 | { |
| 2 | - "name": "express-project", | 2 | + "name": "hospital", |
| 3 | "version": "1.0.0", | 3 | "version": "1.0.0", |
| 4 | "description": "", | 4 | "description": "", |
| 5 | "main": "app.js", | 5 | "main": "app.js", |
| 6 | "scripts": { | 6 | "scripts": { |
| 7 | - "test": "echo \"Error: no test specified\" && exit 1" | 7 | + "test" : "mocha unitTest", |
| 8 | + "start" : "npm run test && node app.js" | ||
| 8 | }, | 9 | }, |
| 9 | - "author": "", | 10 | + "author": "Source Code", |
| 10 | "license": "ISC", | 11 | "license": "ISC", |
| 11 | "dependencies": { | 12 | "dependencies": { |
| 12 | "app-module-path": "^2.2.0", | 13 | "app-module-path": "^2.2.0", |
| @@ -20,6 +21,7 @@ | @@ -20,6 +21,7 @@ | ||
| 20 | "indicative": "^3.0.6", | 21 | "indicative": "^3.0.6", |
| 21 | "ldapjs": "^1.0.2", | 22 | "ldapjs": "^1.0.2", |
| 22 | "log4js": "^3.0.6", | 23 | "log4js": "^3.0.6", |
| 24 | + "mocha": "^5.2.0", | ||
| 23 | "moment": "^2.22.2", | 25 | "moment": "^2.22.2", |
| 24 | "mongodb": "^3.1.8", | 26 | "mongodb": "^3.1.8", |
| 25 | "node-json-config": "0.0.4", | 27 | "node-json-config": "0.0.4", |
| @@ -0,0 +1,18 @@ | @@ -0,0 +1,18 @@ | ||
| 1 | +describe('test app', function() { | ||
| 2 | + | ||
| 3 | + | ||
| 4 | + | ||
| 5 | + it('cal one down (calDown)' , function(done) { | ||
| 6 | + | ||
| 7 | + | ||
| 8 | + if ('1'=='1') { | ||
| 9 | + done(); | ||
| 10 | + } else { | ||
| 11 | + done(new Error("fail")); | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + }); | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + }); | ||
| 0 | \ No newline at end of file | 19 | \ No newline at end of file |
| @@ -0,0 +1,112 @@ | @@ -0,0 +1,112 @@ | ||
| 1 | +const logger = require('./request-logger'); | ||
| 2 | +var MongoClient = require('mongodb').MongoClient; | ||
| 3 | +var config = require('./config'); | ||
| 4 | + | ||
| 5 | +var connection = {}; | ||
| 6 | +connection.reqMongo = async function (req,method, query, collection) { | ||
| 7 | + | ||
| 8 | + // console.log(req.id) | ||
| 9 | + var mongoData = config.get('mongoDB'); | ||
| 10 | + var url = "mongodb://"+mongoData.hopitalIp; | ||
| 11 | + var database = mongoData.hopitalName; | ||
| 12 | + var result = await mongoReq(req,url,database,method, query, collection); | ||
| 13 | + | ||
| 14 | + return result; | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | + | ||
| 18 | +function mongoReq(req,url,database,method, query, collection){ | ||
| 19 | + | ||
| 20 | + logger.logSqlQuery({id : "session", reqId : req.id}, query); | ||
| 21 | + | ||
| 22 | + return new Promise((resolve, reject) => { | ||
| 23 | + MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { | ||
| 24 | + if (err) { | ||
| 25 | + console.log(err) | ||
| 26 | + logger.logSqlResult({id : "session", reqId : req.id}, err.errmsg); | ||
| 27 | + resolve({ | ||
| 28 | + resultCode : "50001", | ||
| 29 | + message : err.errmsg | ||
| 30 | + }); | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + switch (method) { | ||
| 34 | + case "GET": | ||
| 35 | + db.db(database).collection(collection).find(query, { projection: { _id: 0 } }).toArray(function(err, result) { | ||
| 36 | + db.close(); | ||
| 37 | + | ||
| 38 | + if (err) { | ||
| 39 | + console.log(err) | ||
| 40 | + resolve(checkResponse(req,{err:err})); | ||
| 41 | + }else | ||
| 42 | + resolve(checkResponse(req,result)); | ||
| 43 | + }); | ||
| 44 | + break; | ||
| 45 | + case "POST": | ||
| 46 | + db.db(database).collection(collection).find(query, { projection: { _id: 0 } }).toArray(function(err, result) { | ||
| 47 | + db.close(); | ||
| 48 | + | ||
| 49 | + if (err) { | ||
| 50 | + console.log(err) | ||
| 51 | + resolve(checkResponse(req,{err:err})); | ||
| 52 | + }else | ||
| 53 | + resolve(checkResponse(req,result)); | ||
| 54 | + }); | ||
| 55 | + break; | ||
| 56 | + case "PUT": | ||
| 57 | + db.db(database).collection(collection).find(query, { projection: { _id: 0 } }).toArray(function(err, result) { | ||
| 58 | + db.close(); | ||
| 59 | + | ||
| 60 | + if (err) { | ||
| 61 | + console.log(err) | ||
| 62 | + resolve(checkResponse(req,{err:err})); | ||
| 63 | + }else | ||
| 64 | + resolve(checkResponse(req,result)); | ||
| 65 | + }); | ||
| 66 | + break; | ||
| 67 | + case "DELETE": | ||
| 68 | + db.db(database).collection(collection).find(query, { projection: { _id: 0 } }).toArray(function(err, result) { | ||
| 69 | + db.close(); | ||
| 70 | + | ||
| 71 | + if (err) { | ||
| 72 | + console.log(err) | ||
| 73 | + resolve(checkResponse(req,{err:err})); | ||
| 74 | + }else | ||
| 75 | + resolve(checkResponse(req,result)); | ||
| 76 | + }); | ||
| 77 | + break; | ||
| 78 | + | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + | ||
| 82 | + }); | ||
| 83 | + }); | ||
| 84 | + | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +function checkResponse(req,response) | ||
| 88 | +{ | ||
| 89 | + | ||
| 90 | + | ||
| 91 | + if(response.err) | ||
| 92 | + { | ||
| 93 | + logger.logSqlResult({id : "session", reqId : req.id}, err.errmsg); | ||
| 94 | + var returnData = { | ||
| 95 | + resultCode : "50001", | ||
| 96 | + message : response.err.errmsg | ||
| 97 | + }; | ||
| 98 | + }else | ||
| 99 | + { | ||
| 100 | + logger.logSqlResult({id : "session", reqId : req.id}, response); | ||
| 101 | + var returnData = { | ||
| 102 | + resultCode : "20000", | ||
| 103 | + resultDescription : "Success", | ||
| 104 | + resultData : response | ||
| 105 | + }; | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + return returnData | ||
| 109 | +} | ||
| 110 | + | ||
| 111 | + | ||
| 112 | +module.exports = connection; | ||
| 0 | \ No newline at end of file | 113 | \ No newline at end of file |