diff --git a/.eslintrc.json b/.eslintrc.json index 7e4d14d..8945670 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -76,10 +76,10 @@ ], "lines-around-comment": "warn", "lines-around-directive": "warn", - "lines-between-class-members": [ - "warn", - "always" - ], + // "lines-between-class-members": [ + // "warn", + // "always" + // ], "max-classes-per-file": "warn", "max-nested-callbacks": "warn", "new-parens": "warn", diff --git a/options.json b/options.json index eacc17f..e832b07 100644 --- a/options.json +++ b/options.json @@ -9,7 +9,8 @@ "http": { "port": 4000 }, - "validUserTypes": ["user", "service", "system"] + "validUserTypes": ["user", "service", "system"], + "registrationEnabled": true }, "logger": { "customTypes": ["access", "unauthorised"], diff --git a/src/server/Server.js b/src/server/Server.js index ccc765c..af91328 100644 --- a/src/server/Server.js +++ b/src/server/Server.js @@ -50,6 +50,7 @@ class Server extends EventEmitter { this.port = httpOpts.port + this._shardId; this.domain = NODE_ENV === 'development' ? `localhost:${this.port}` : options.domain; this.serveFiles = null; + this.registrationEnabled = options.registrationEnabled; if (options.serveFiles) this.serveFiles = path.resolve(options.serveFiles); this.server = null; @@ -84,9 +85,9 @@ class Server extends EventEmitter { this.app.use(helmet()); this.app.use(express.json({ limit: '10mb' })); this.app.use(express.urlencoded({ extended: true })); - this.app.use(this.logRequest.bind(this)); // Logs every request - this.app.use(this.logError.bind(this)); // Logs endpoints that error and sends a 500 - this.app.use(this.ready.bind(this)); // denies requests before the server is ready + this.app.use(this.#logRequest.bind(this)); // Logs every request + this.app.use(this.#logError.bind(this)); // Logs endpoints that error and sends a 500 + this.app.use(this.#ready.bind(this)); // denies requests before the server is ready process.on('message', this._handleMessage.bind(this)); @@ -119,19 +120,19 @@ class Server extends EventEmitter { } // eslint-disable-next-line no-unused-vars - logError (err, req, res, next) { + #logError (err, req, res, next) { this.logger.error(`Unhandled error:\n${err.stack || err}`); res.status(500).send('An internal error was encountered'); } - logRequest (req, res, next) { + #logRequest (req, res, next) { res.once('finish', () => { this.logger[[ 401, 403 ].includes(res.statusCode) ? 'unauthorised' : 'access'](`[${req.get('X-Forwarded-For') || req.socket.remoteAddress}] [STATUS: ${res.statusCode}] Request to [${req.method}] ${req.route?.path || req.path}`); }); next(); } - ready (req, res, next) { + #ready (req, res, next) { if (!this._ready) return res.status(503).send('Server not ready'); next(); } @@ -156,6 +157,7 @@ class Server extends EventEmitter { if (msg._shutdown) this.shutdown(); } + // Helper function to pass options to the logger in a unified way createLogger (comp) { return new LoggerClient({ name: comp.constructor.name, ...this._options.logger }); } diff --git a/src/server/database/MongoDB.js b/src/server/database/MongoDB.js index a35abe0..c1938b4 100644 --- a/src/server/database/MongoDB.js +++ b/src/server/database/MongoDB.js @@ -127,6 +127,23 @@ class MongoDB { } + /** + * Insert document. + * + * @param {String} db The collection in which the data is to be updated + * @param {Object} filter The filter that is used to find the data + * @param {Object} data The updated data + * @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified + * @memberof Database + */ + async insertOne (db, data) { + + this.logger.debug(`Incoming insertOne query for ${db} with parameters ${JSON.stringify(data)}`); + const result = await this.db.collection(db).insertOne(data); + return result; + + } + /** * Push data to an array *