Skip to content

Commit

Permalink
Merge b9ea0a2 into c7b3600
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed May 26, 2020
2 parents c7b3600 + b9ea0a2 commit 8dd37d4
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 197 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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

Expand Down
29 changes: 5 additions & 24 deletions lib/api-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -32,28 +31,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
};
});

Expand Down
14 changes: 7 additions & 7 deletions lib/helper/instance-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}.`);
}
Expand All @@ -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);

Expand All @@ -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();
}
}

Expand Down
30 changes: 5 additions & 25 deletions lib/listener-created.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -30,28 +29,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);
Expand Down
10 changes: 9 additions & 1 deletion lib/model-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
};
}
}

Expand Down
86 changes: 5 additions & 81 deletions tests/api-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,44 +39,17 @@ 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', [

{
description: 'Should save all the received new clients to clients DB',
session: true,
request: {
data: {
clients: [
Expand All @@ -93,7 +66,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);
Expand All @@ -106,9 +79,9 @@ describe('APIs', () => {
after: (res, sandbox) => {

sandbox.assert.calledOnceWithExactly(ClientModel.prototype.multiSave, [
expectedBasicClientObject,
expectedClientObject,
{
...expectedBasicClientObject,
...expectedClientObject,
code: 'other-client',
dbDatabase: 'janis-other-client'
}
Expand All @@ -117,59 +90,14 @@ 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,
request: {
data: {
clients: ['some-client']
Expand Down Expand Up @@ -202,7 +130,6 @@ describe('APIs', () => {
},
{
description: 'Should return 500 when the index creator fails',
session: true,
request: {
data: {
clients: ['some-client']
Expand Down Expand Up @@ -236,7 +163,6 @@ describe('APIs', () => {
},
{
description: 'Should return 400 when the received request data is invalid',
session: true,
request: {
data: ['something']
},
Expand All @@ -246,7 +172,6 @@ describe('APIs', () => {
},
{
description: 'Should return 400 when the received clients are invalid',
session: true,
request: {
data: {
clients: { some: 'object' }
Expand All @@ -258,7 +183,6 @@ describe('APIs', () => {
},
{
description: 'Should return 400 when the client model is not in the corresponding path',
session: true,
request: {
data: {
clients: [
Expand Down
Loading

0 comments on commit 8dd37d4

Please sign in to comment.