Skip to content

Commit

Permalink
Merge 76cef7e into 9ed3abf
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonpereyra committed May 29, 2020
2 parents 9ed3abf + 76cef7e commit 3d7344b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- New Clients settings can use `{{code}}` to have dinamic settings

## [2.0.1] - 2020-05-28
### Fixed
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ You should configure the database config in your service, in order to get the co
},
"newClients": { // DB config that the clients will use
"type": "mongodb",
"host": "clients-host"
"host": "clients-host",
"database": "janis-{{code}}" // necesary to add dinamic database names
// ...
}
},
Expand Down
5 changes: 1 addition & 4 deletions lib/api-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ class ClientCreateAPI extends API {
const databaseSettings = Settings.get('database')[this.clientModel.newClientsDatabaseKey];

const clientsToCreate = clientCodes.map(code => {
return {
...databaseSettings,
...this.clientModel.getFormattedClient(code)
};
return this.clientModel.getFormattedClient(code, databaseSettings);
});

await this.clientModel.multiSave(clientsToCreate);
Expand Down
5 changes: 1 addition & 4 deletions lib/listener-created.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ class ClientCreatedListener extends EventListener {

const databaseSettings = Settings.get('database')[this.clientModel.newClientsDatabaseKey];

await this.clientModel.save({
...databaseSettings,
...this.clientModel.getFormattedClient(clientCode)
});
await this.clientModel.save(this.clientModel.getFormattedClient(clientCode, databaseSettings));

await mongoDBIndexCreator.executeForClientCode(clientCode);
return this.postSaveHook(clientCode, databaseSettings);
Expand Down
12 changes: 10 additions & 2 deletions lib/model-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ class Client extends Model {
return ['user', 'password', 'host', 'protocol', 'port', 'database'];
}

getFormattedClient(code) {
getFormattedClient(code, databaseSettings = {}) {

const settings = Object.entries(databaseSettings).reduce((acum, [key, value]) => {
return {
...acum,
[key]: typeof value === 'string' ? value.replace('{{code}}', code) : value
};
}, {});

return {
code,
status: this.constructor.statuses.active,
database: `janis-${code}`
...settings
};
}
}
Expand Down
67 changes: 65 additions & 2 deletions tests/api-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ describe('APIs', () => {
protocol: 'some-protocol://',
port: 27017,
user: 'some-user',
password: 'some-password'
password: 'some-password',
database: 'janis-{{code}}'
}
},
clients: {
Expand All @@ -46,7 +47,6 @@ describe('APIs', () => {
};

APITest(ClientCreateAPI, '/api/client', [

{
description: 'Should save all the received new clients to clients DB',
request: {
Expand Down Expand Up @@ -95,6 +95,69 @@ describe('APIs', () => {
mockRequire.stop(fakeClientPath);
}
},
{
description: 'Should save all the received new clients without database settings to clients DB without newClients Settings',
request: {
data: {
clients: [
'some-client',
'other-client'
]
}
},
response: {
code: 200
},
before: sandbox => {

const fakeSettings2 = {

database: {
core: {
host: 'core-database-host',
protocol: 'core-protocol://',
port: 27017,
user: 'core-user',
password: 'core-password'
}
}
};

mockRequire(fakeClientPath, ClientModel);

sandbox.stub(Settings, 'get')
.callsFake(setting => fakeSettings2[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, [
{
code: 'some-client',
status: ClientModel.statuses.active
},
{
code: 'other-client',
status: ClientModel.statuses.active
}
]);

sandbox.assert.calledOnceWithExactly(
ClientCreateAPI.prototype.postSaveHook,
['some-client', 'other-client'],
undefined
);

mockRequire.stop(fakeClientPath);
}
},
{
description: 'Should return 500 when the client model multiSave fails',
request: {
Expand Down
3 changes: 2 additions & 1 deletion tests/listener-created.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ describe('Client Created Listener', async () => {
protocol: 'some-protocol://',
port: 27017,
user: 'some-user',
password: 'some-password'
password: 'some-password',
database: 'janis-{{code}}'
}
},
clients: {
Expand Down

0 comments on commit 3d7344b

Please sign in to comment.