From 189fbe8409590dcfca5e946b0c6c9991a5c39f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nataniel=20L=C3=B3pez?= Date: Fri, 22 May 2020 18:06:42 +0000 Subject: [PATCH] Fixed newClients databaseKey --- CHANGELOG.md | 1 + README.md | 3 ++- lib/api-create.js | 2 +- lib/listener-created.js | 2 +- lib/model-client.js | 4 ++++ tests/api-create.js | 2 +- tests/listener-created.js | 2 +- tests/model-client.js | 22 ++++++++++++++++++++++ 8 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05ba3cc..f934ecb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] ### Added - `databaseSettings` parameter for `postSaveHook()` method +- `newClientsDatabaseKey` setting for new clients database config ### Changed - `Client` Model now set the new clients databaseKey from config diff --git a/README.md b/README.md index 4d1edcc..ea86c37 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,8 @@ You should configure the database config in your service, in order to get the co } }, "clients": { - "databaseKey": "newClients", // The new clients config databaseKey, + "newClientsDatabaseKey": "newClients", // The new clients config databaseKey ("newClients" by default) + "databaseKey": "newClients", // The databaseKey where store the new clients ("core" by default) "table": "clients" // The clients table where create the clients ("clients" by default) } } diff --git a/lib/api-create.js b/lib/api-create.js index c4d3fda..01b921a 100644 --- a/lib/api-create.js +++ b/lib/api-create.js @@ -30,7 +30,7 @@ class ClientCreateAPI extends API { async process({ clients: clientCodes } = this.data) { - const databaseSettings = Settings.get('database')[this.clientModel.databaseKey]; + const databaseSettings = Settings.get('database')[this.clientModel.newClientsDatabaseKey]; const clientDatabase = { dbHost: databaseSettings.host diff --git a/lib/listener-created.js b/lib/listener-created.js index e2ec47a..cf1806b 100644 --- a/lib/listener-created.js +++ b/lib/listener-created.js @@ -28,7 +28,7 @@ class ClientCreatedListener extends EventListener { async process(clientCode = this.eventId) { - const databaseSettings = Settings.get('database')[this.clientModel.databaseKey]; + const databaseSettings = Settings.get('database')[this.clientModel.newClientsDatabaseKey]; const clientDatabase = { dbHost: databaseSettings.host, diff --git a/lib/model-client.js b/lib/model-client.js index 930e98f..09474e5 100644 --- a/lib/model-client.js +++ b/lib/model-client.js @@ -13,6 +13,10 @@ class Client extends Model { return this.constructor.settings.databaseKey || 'core'; } + get newClientsDatabaseKey() { + return this.constructor.settings.newClientsDatabaseKey || 'newClients'; + } + static get table() { return this.settings.table || 'clients'; } diff --git a/tests/api-create.js b/tests/api-create.js index 39b5ec7..21a98f5 100644 --- a/tests/api-create.js +++ b/tests/api-create.js @@ -35,7 +35,7 @@ describe('APIs', () => { } }, clients: { - databaseKey: 'newClients' + newClientsDatabaseKey: 'newClients' } }; diff --git a/tests/listener-created.js b/tests/listener-created.js index cab9283..23f2e2e 100644 --- a/tests/listener-created.js +++ b/tests/listener-created.js @@ -42,7 +42,7 @@ describe('Client Created Listener', async () => { } }, clients: { - databaseKey: 'newClients' + newClientsDatabaseKey: 'newClients' } }; diff --git a/tests/model-client.js b/tests/model-client.js index c5c5ddc..65351dc 100644 --- a/tests/model-client.js +++ b/tests/model-client.js @@ -37,6 +37,28 @@ describe('ClientModel', () => { assert.deepStrictEqual(clientModel.databaseKey, 'some-databaseKey'); }); + it('should return the newClients as databaseKey for new clients when is not set in settings', () => { + + sandbox.stub(Settings, 'get') + .returns({}); + + const clientModel = new ClientModel(); + + assert.deepStrictEqual(clientModel.newClientsDatabaseKey, 'newClients'); + }); + + it('should return the new clients databaseKey setted in settings', () => { + + sandbox.stub(Settings, 'get') + .returns({ + newClientsDatabaseKey: 'some-databaseKey' + }); + + const clientModel = new ClientModel(); + + assert.deepStrictEqual(clientModel.newClientsDatabaseKey, 'some-databaseKey'); + }); + it('should return clients as table name when is not set in settings', () => { sandbox.stub(Settings, 'get')