From bf44a4d3100d55b19d058c393f4f3119fc630e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nataniel=20L=C3=B3pez?= Date: Fri, 22 May 2020 21:08:44 +0000 Subject: [PATCH 1/2] improved code and clients formatting --- lib/api-create.js | 22 +---------- lib/listener-created.js | 23 +---------- lib/model-client.js | 10 ++++- tests/api-create.js | 80 +++------------------------------------ tests/listener-created.js | 56 +-------------------------- tests/model-client.js | 2 +- 6 files changed, 20 insertions(+), 173 deletions(-) diff --git a/lib/api-create.js b/lib/api-create.js index 01b921a..fde8ea4 100644 --- a/lib/api-create.js +++ b/lib/api-create.js @@ -32,28 +32,10 @@ class ClientCreateAPI extends API { const databaseSettings = Settings.get('database')[this.clientModel.newClientsDatabaseKey]; - const clientDatabase = { - dbHost: databaseSettings.host - }; - - if(databaseSettings.protocol) - clientDatabase.dbProtocol = databaseSettings.protocol; - - if(databaseSettings.port) - clientDatabase.dbPort = databaseSettings.port; - - if(databaseSettings.user) - clientDatabase.dbUser = databaseSettings.user; - - if(databaseSettings.password) - clientDatabase.dbPassword = databaseSettings.password; - const clientsToCreate = clientCodes.map(code => { return { - code, - status: this.clientModel.constructor.statuses.active, - ...clientDatabase, - dbDatabase: `janis-${code}` + ...this.clientModel.getFormattedClient(code), + ...databaseSettings }; }); diff --git a/lib/listener-created.js b/lib/listener-created.js index cf1806b..32530ef 100644 --- a/lib/listener-created.js +++ b/lib/listener-created.js @@ -30,28 +30,9 @@ class ClientCreatedListener extends EventListener { const databaseSettings = Settings.get('database')[this.clientModel.newClientsDatabaseKey]; - const clientDatabase = { - dbHost: databaseSettings.host, - dbDatabase: `janis-${clientCode}` - }; - - if(databaseSettings.protocol) - clientDatabase.dbProtocol = databaseSettings.protocol; - - if(databaseSettings.port) - clientDatabase.dbPort = databaseSettings.port; - - if(databaseSettings.user) - clientDatabase.dbUser = databaseSettings.user; - - if(databaseSettings.password) - clientDatabase.dbPassword = databaseSettings.password; - - await this.clientModel.save({ - code: clientCode, - status: this.clientModel.constructor.statuses.active, - ...clientDatabase + ...this.clientModel.getFormattedClient(clientCode), + ...databaseSettings }); await mongoDBIndexCreator.executeForClientCode(clientCode); diff --git a/lib/model-client.js b/lib/model-client.js index 09474e5..327f5a7 100644 --- a/lib/model-client.js +++ b/lib/model-client.js @@ -33,7 +33,15 @@ class Client extends Model { } static get excludeFieldsInLog() { - return ['dbUser', 'dbPassword', 'dbHost']; + return ['user', 'password', 'host']; + } + + getFormattedClient(code) { + return { + code, + status: this.constructor.statuses.active, + dbDatabase: `janis-${code}` + }; } } diff --git a/tests/api-create.js b/tests/api-create.js index 21a98f5..553d633 100644 --- a/tests/api-create.js +++ b/tests/api-create.js @@ -39,39 +39,13 @@ describe('APIs', () => { } }; - const fakeBasicSettings = { - - ...fakeSettings, - - database: { - - core: { - host: fakeSettings.database.core.host - }, - newClients: { - host: fakeSettings.database.newClients.host - } - } - }; - const expectedClientObject = { code: 'some-client', status: ClientModel.statuses.active, - dbHost: fakeSettings.database.newClients.host, - dbProtocol: fakeSettings.database.newClients.protocol, - dbPort: fakeSettings.database.newClients.port, - dbUser: fakeSettings.database.newClients.user, - dbPassword: fakeSettings.database.newClients.password, + ...fakeSettings.database.newClients, dbDatabase: 'janis-some-client' }; - const expectedBasicClientObject = { - code: expectedClientObject.code, - status: expectedClientObject.status, - dbHost: expectedClientObject.dbHost, - dbDatabase: expectedClientObject.dbDatabase - }; - APITest(ClientCreateAPI, '/api/client', [ { @@ -93,7 +67,7 @@ describe('APIs', () => { mockRequire(fakeClientPath, ClientModel); sandbox.stub(Settings, 'get') - .callsFake(setting => fakeBasicSettings[setting]); + .callsFake(setting => fakeSettings[setting]); sandbox.stub(ClientModel.prototype, 'multiSave') .resolves(true); @@ -106,9 +80,9 @@ describe('APIs', () => { after: (res, sandbox) => { sandbox.assert.calledOnceWithExactly(ClientModel.prototype.multiSave, [ - expectedBasicClientObject, + expectedClientObject, { - ...expectedBasicClientObject, + ...expectedClientObject, code: 'other-client', dbDatabase: 'janis-other-client' } @@ -117,56 +91,12 @@ describe('APIs', () => { sandbox.assert.calledOnceWithExactly( ClientCreateAPI.prototype.postSaveHook, ['some-client', 'other-client'], - fakeBasicSettings.database.newClients + fakeSettings.database.newClients ); mockRequire.stop(fakeClientPath); } }, - { - description: 'Should save all the received new clients to clients DB with full database config', - session: true, - request: { - data: { - clients: [ - 'some-client', - 'other-client' - ] - } - }, - response: { - code: 200 - }, - before: sandbox => { - - mockRequire(fakeClientPath, ClientModel); - - sandbox.stub(Settings, 'get') - .callsFake(setting => fakeSettings[setting]); - - sandbox.stub(ClientModel.prototype, 'multiSave') - .resolves(true); - - sandbox.stub(MongoDBIndexCreator.prototype, 'executeForClientDatabases') - .resolves(); - - sandbox.spy(ClientCreateAPI.prototype, 'postSaveHook'); - }, - after: (res, sandbox) => { - - sandbox.assert.calledOnceWithExactly(ClientModel.prototype.multiSave, [ - expectedClientObject, - { - ...expectedClientObject, - code: 'other-client', - dbDatabase: 'janis-other-client' - } - ]); - - sandbox.assert.calledOnceWithExactly(ClientCreateAPI.prototype.postSaveHook, ['some-client', 'other-client'], fakeSettings.database.newClients); - mockRequire.stop(fakeClientPath); - } - }, { description: 'Should return 500 when the client model multiSave fails', session: true, diff --git a/tests/listener-created.js b/tests/listener-created.js index 23f2e2e..d0bef55 100644 --- a/tests/listener-created.js +++ b/tests/listener-created.js @@ -46,39 +46,13 @@ describe('Client Created Listener', async () => { } }; - const fakeBasicSettings = { - - ...fakeSettings, - - database: { - - core: { - host: fakeSettings.database.core.host - }, - newClients: { - host: fakeSettings.database.newClients.host - } - } - }; - const expectedClientObject = { code: 'some-client', status: ClientModel.statuses.active, - dbHost: fakeSettings.database.newClients.host, - dbProtocol: fakeSettings.database.newClients.protocol, - dbPort: fakeSettings.database.newClients.port, - dbUser: fakeSettings.database.newClients.user, - dbPassword: fakeSettings.database.newClients.password, + ...fakeSettings.database.newClients, dbDatabase: 'janis-some-client' }; - const expectedBasicClientObject = { - code: expectedClientObject.code, - status: expectedClientObject.status, - dbHost: expectedClientObject.dbHost, - dbDatabase: expectedClientObject.dbDatabase - }; - await EventListenerTest(handler, [ { @@ -147,34 +121,6 @@ describe('Client Created Listener', async () => { mockRequire(fakeClientPath, ClientModel); - sandbox.stub(ClientModel.prototype, 'save') - .resolves('5dea9fc691240d00084083f9'); - - sandbox.stub(Settings, 'get') - .callsFake(setting => fakeBasicSettings[setting]); - - sandbox.stub(MongoDBIndexCreator.prototype, 'executeForClientCode') - .resolves(); - - sandbox.spy(ClientCreatedListener.prototype, 'postSaveHook'); - }, - after: sandbox => { - - sandbox.assert.calledOnceWithExactly(ClientModel.prototype.save, expectedBasicClientObject); - sandbox.assert.calledOnceWithExactly(MongoDBIndexCreator.prototype.executeForClientCode, 'some-client'); - sandbox.assert.calledOnceWithExactly(ClientCreatedListener.prototype.postSaveHook, 'some-client', fakeBasicSettings.database.newClients); - mockRequire.stop(fakeClientPath); - }, - responseCode: 200 - }, - { - description: 'Should return 200 when client model saves the new client with full database config sucessfully', - event: validEvent, - session: true, - before: sandbox => { - - mockRequire(fakeClientPath, ClientModel); - sandbox.stub(ClientModel.prototype, 'save') .resolves('5dea9fc691240d00084083f9'); diff --git a/tests/model-client.js b/tests/model-client.js index 65351dc..5e70ef6 100644 --- a/tests/model-client.js +++ b/tests/model-client.js @@ -85,7 +85,7 @@ describe('ClientModel', () => { }); it('Should return the exclude fields for logging', async () => { - assert.deepStrictEqual(ClientModel.excludeFieldsInLog, ['dbUser', 'dbPassword', 'dbHost']); + assert.deepStrictEqual(ClientModel.excludeFieldsInLog, ['user', 'password', 'host']); }); it('Should return false when shouldCreateLogs', () => { From b9ea0a2f15b253082948aa3e53518743f278e93c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nataniel=20L=C3=B3pez?= Date: Tue, 26 May 2020 13:29:34 +0000 Subject: [PATCH 2/2] Removed unnecessary session --- CHANGELOG.md | 3 +++ lib/api-create.js | 7 +++---- lib/helper/instance-getter.js | 14 +++++++------- lib/listener-created.js | 7 +++---- tests/api-create.js | 6 ------ tests/listener-created.js | 3 --- 6 files changed, 16 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f934ecb..e1b30db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - `databaseSettings` parameter for `postSaveHook()` method - `newClientsDatabaseKey` setting for new clients database config +### Removed +- `session` in client create API and Listener + ### Changed - `Client` Model now set the new clients databaseKey from config diff --git a/lib/api-create.js b/lib/api-create.js index fde8ea4..a4b154d 100644 --- a/lib/api-create.js +++ b/lib/api-create.js @@ -11,10 +11,9 @@ class ClientCreateAPI extends API { get clientModel() { - if(!this._clientModel) { - const instanceGetter = this.session.getSessionInstance(InstanceGetter); - this._clientModel = instanceGetter.getModelInstance('client'); - } + if(!this._clientModel) + this._clientModel = InstanceGetter.getModelInstance('client'); + return this._clientModel; } diff --git a/lib/helper/instance-getter.js b/lib/helper/instance-getter.js index 581cd16..54086cc 100644 --- a/lib/helper/instance-getter.js +++ b/lib/helper/instance-getter.js @@ -12,10 +12,10 @@ class InstanceGetter { */ static getModelClass(entity) { - const modelPath = this.prototype.getModelRelativePath(entity); + const modelPath = this.getModelRelativePath(entity); try { - return this.prototype.getClass(modelPath); + return this.getClass(modelPath); } catch(e) { throw new Error(`Invalid Model ${entity}. Must be in ${modelPath}.`); } @@ -25,7 +25,7 @@ class InstanceGetter { * Returns an instance model from the service. * @param {string} entity */ - getModelInstance(entity) { + static getModelInstance(entity) { const modelPath = this.getModelRelativePath(entity); @@ -36,20 +36,20 @@ class InstanceGetter { } } - getModelRelativePath(entity) { + static getModelRelativePath(entity) { return path.join(process.cwd(), process.env.MS_PATH || '', 'models', entity); } - getClass(classPath) { + static getClass(classPath) { // eslint-disable-next-line global-require, import/no-dynamic-require return require(classPath); } - getInstance(classPath) { + static getInstance(classPath) { const TheClass = this.getClass(classPath); - return this.session.getSessionInstance(TheClass); + return new TheClass(); } } diff --git a/lib/listener-created.js b/lib/listener-created.js index 32530ef..a4bd090 100644 --- a/lib/listener-created.js +++ b/lib/listener-created.js @@ -18,10 +18,9 @@ class ClientCreatedListener extends EventListener { get clientModel() { - if(!this._clientModel) { - const instanceGetter = this.session.getSessionInstance(InstanceGetter); - this._clientModel = instanceGetter.getModelInstance('client'); - } + if(!this._clientModel) + this._clientModel = InstanceGetter.getModelInstance('client'); + return this._clientModel; } diff --git a/tests/api-create.js b/tests/api-create.js index 553d633..ee19c88 100644 --- a/tests/api-create.js +++ b/tests/api-create.js @@ -50,7 +50,6 @@ describe('APIs', () => { { description: 'Should save all the received new clients to clients DB', - session: true, request: { data: { clients: [ @@ -99,7 +98,6 @@ describe('APIs', () => { }, { description: 'Should return 500 when the client model multiSave fails', - session: true, request: { data: { clients: ['some-client'] @@ -132,7 +130,6 @@ describe('APIs', () => { }, { description: 'Should return 500 when the index creator fails', - session: true, request: { data: { clients: ['some-client'] @@ -166,7 +163,6 @@ describe('APIs', () => { }, { description: 'Should return 400 when the received request data is invalid', - session: true, request: { data: ['something'] }, @@ -176,7 +172,6 @@ describe('APIs', () => { }, { description: 'Should return 400 when the received clients are invalid', - session: true, request: { data: { clients: { some: 'object' } @@ -188,7 +183,6 @@ describe('APIs', () => { }, { description: 'Should return 400 when the client model is not in the corresponding path', - session: true, request: { data: { clients: [ diff --git a/tests/listener-created.js b/tests/listener-created.js index d0bef55..be91d55 100644 --- a/tests/listener-created.js +++ b/tests/listener-created.js @@ -66,7 +66,6 @@ describe('Client Created Listener', async () => { { description: 'Should return 500 when client model fails to save the new client', event: validEvent, - session: true, before: sandbox => { mockRequire(fakeClientPath, ClientModel); @@ -91,7 +90,6 @@ describe('Client Created Listener', async () => { { description: 'Should return 500 when the index creator fails', event: validEvent, - session: true, before: sandbox => { mockRequire(fakeClientPath, ClientModel); @@ -116,7 +114,6 @@ describe('Client Created Listener', async () => { { description: 'Should return 200 when client model saves the new client sucessfully', event: validEvent, - session: true, before: sandbox => { mockRequire(fakeClientPath, ClientModel);